Hi guys, i'm doing a 7 week C++ course at the min and am struggling to keep up.
Any help on the following is appreciated.
Background Info:
The game of craps: A player rolls two dice. Each die has six faces. These
faces contain 1, 2, 3, 4, 5 and 6 spots. After the dice have come to rest, the
sum of the spots on the two upward faces is calculated. If the sum is 7 or 11 on
the rst roll, the player wins. If the sum is 2, 3 or 12 on the rst roll (called
\craps"), the player loses (i.e. the \house wins"). If the sum is 4, 5, 6, 8, 9
or 10 on the rst roll, then that sum becomes the player's \point". To win, you
must continue rolling the dice until you \make your point". The player loses by
rolling a 7 before making the point.
Create a program to simulate a game of craps. Provide a function RollDice. The program should allow wagering, as follows:
Package the portion of the program that runs one game of craps into
a function.
Initialize variable bankBalance to 1000 euro. Prompt the player to
enter a wager.
Check that wager is less than bankBalance and if not prompt the
user until a valid wager is obtained.
Run a game of craps. If the player wins, increase the bankBalance
by wager and print out the new bankBalance. If the player loses,
decrease bankBalance by wager and print the new bankBalance.
As the game progresses, print some messages to create \chatter"
such as Oh, you're going for broke now?, Ah c'mon, take a chance!
or You're up big. Now's the time to cash in your chips!
Continue playing until the player is bust or the player opts to cash
in.
My code is as follows:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
|
#include <iostream>
#include <iomanip>
#include <cstdlib> // needed for functions srand() and rand()
#include <ctime> // needed for function time()
using namespace std;
int rollDice(int diceVals[], int numberToRoll = 2);
void gameCraps(int sum, int bankBalance, int wager);
enum Status {CONTINUE, WON, LOST};
int main()
{
int sum;
int diceVals[2];
int bankBalance = 1000;
int wager;
srand(time(0));
sum = rollDice(diceVals);
cout << "Your bank balance is: " << "€" << bankBalance << "\n";
cout << "Please enter a wager - ";
cin >> wager;
if (wager > 1000)
{
cout << "Not valid" << endl;
cout << "Please enter a wager - " << endl;
cin >> wager;
}
cout << "\n" << "Your wager: " << "€" << wager << "\n\n";
cout << "Player rolled: " << diceVals[0] << " + " << diceVals[1] << endl;
gameCraps(sum, bankBalance, wager);
return 0;
}
int rollDice(int diceVals[], int numberToRoll)
{
int dicevalues = 0;
for(int i = 0; i < numberToRoll; i++)
{
diceVals[i] = 1 + rand()%6;
}
for(int i = 0; i < numberToRoll; i++)
{
dicevalues = dicevalues + diceVals[i];
}
return dicevalues;
}
void gameCraps(int sum, int bankBalance, int wager)
{
int myPoint;
int diceVals[2];
char userchoice;
Status gameStatus;
switch(sum)
{
case 7:
gameStatus = WON;
case 11:
gameStatus = WON;
break;
case 2:
gameStatus = LOST;
case 3:
gameStatus = LOST;
case 12:
gameStatus = LOST;
break;
default:
gameStatus = CONTINUE;
myPoint = sum;
cout << "Point is: " << myPoint << endl;
break;
}
while (gameStatus == CONTINUE)
{
sum = rollDice(diceVals);
cout << "Player rolled: " << diceVals[0] << " + " << diceVals[1] << endl;
if (sum == myPoint)
{
gameStatus = WON;
}
else if (sum == 7)
{
gameStatus = LOST;
}
if (gameStatus == WON)
{
cout << "\n\t***Player wins***\n" << endl;
bankBalance = bankBalance + wager;
cout << "Your bank balance is now: " << "€" << bankBalance << "\n";
cout << "Would you like to cash-in or continue? (Select 'Y', then return to continue... "
"or 'N' and return to cash-in!)\n";
cin >> userchoice;
if (userchoice == 'y')
{
cout << "You're up big. Now's the time to cash in your chips!\n";
gameStatus = CONTINUE;
}
while (gameStatus == CONTINUE)
{
sum = rollDice(diceVals);
cout << "Your bank balance is: " << "€" << bankBalance << "\n";
cout << "Please enter a wager - ";
cin >> wager;
cout << "\n" << "Your wager: " << "€" << wager << "\n\n";
cout << "Player rolled: " << diceVals[0] << " + " << diceVals[1] << endl;
gameCraps(sum, bankBalance, wager);
}
if (userchoice == 'n')
{
cout << "Ah c'mon, take a chance! You go away with a bank balance of " << "€" << bankBalance << "\n";
exit(0);
}
if (userchoice !='n' || userchoice !='y')
{
cout << "Please enter a valid choice ('Y' or 'N')\n";
exit(-1);
}
}
else
{
cout << "\n\t***Player loses***\n" << endl;
bankBalance = bankBalance - wager;
if (bankBalance <= 0)
{
cout << "Sorry, you don't have enough cash to continue! Please try again.\n";
cout << "Your bank balance is now " << "€" << bankBalance << "\n";
exit(-1);
}
if (bankBalance > 0 )
{
cout << "Would you like to cash-in or continue? (Select 'Y', then return to continue... "
"or 'N' and return to cash-in!)\n";
cin >> userchoice;
if (userchoice == 'y')
{
cout << "Oh, you're going for broke now?";
gameStatus = CONTINUE;
}
while (gameStatus == CONTINUE)
{
sum = rollDice(diceVals);
cout << "Your bank balance is: " << "€" << bankBalance << "\n";
cout << "Please enter a wager - ";
cin >> wager;
cout << "\n" << "Your wager: " << "€" << wager << "\n\n";
cout << "Player rolled: " << diceVals[0] << " + " << diceVals[1] << endl;
gameCraps(sum, bankBalance, wager);
}
if (userchoice == 'n')
{
cout << "Thanks for playing! You go away with a bank balance of " << "€" << bankBalance;
exit(0);
}
if (userchoice !='y' || userchoice !='n')
{
cout << "Please enter a valid choice to continue\n";
exit(-1);
}
}
}
}
}
|
So i've managed to mash something together, however program doesn't run as planned. When a player roles 7, Program quits... Player should win and game continue.
I can only iterate through 2 games at a time.
Any thoughts/comments?