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?

As far as I can tell this should be the basic logic for the program

main()
  int balance
  int wager
  bool oneMoreGame

  while(balance above zero and oneMoreGame)
    enter wager
    check if wager less than balance and redo wager until it is
    gameResult = runGame()
    if gameResult is lost       
      deduct wager from balance
    else 
      add wager to balance

    ask to play another game
      if no then cash out

From what I can tell looking at your code you need to readjust the loops to get there.

Yeah sorry, its a bit long winded but thanks for that... def a good idea to boil it down to basics, just trying to get my head around it :)

First post -- CODE Tags and formatted! Such a wonderful thing to see!
One tip - change your IDE to replace TABs with 4 SPACEs instead. Notice hoe the end of your code gets lost in line-wraps. It's also easier to read in general.

Your program as written should exit on 7, 11, 2, 3, and 12 as an initial roll. You have no loop to roll again as far as I can see. Lerner provided you with a good algorithm for fixing main()

Edited 5 Years Ago by WaltP: n/a

"One tip - change your IDE to replace TABs with 4 SPACEs instead." - Cool, I'll do that.

Have a bit of reconfiguring to do but i think i'm on the right track... thanks guys

This article has been dead for over six months. Start a new discussion instead.