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:-
#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?