So here is the program statement
(1) One of the players goes first. That player announces the size of the bet, and rolls the dice. If the
player rolls a
• 7 or 11, it is called a natural. The player who rolled the dice wins.
• 2, 3 or 12, it is called craps. The player who rolled the dice loses.
• If the player's first roll is any other number (4, 5, 6, 8, 9, or 10) the bet is not immediately
decided. Instead, the number that the player rolled becomes the point and he continues to roll
the dice until either he rolls the point a 2nd time, in which case he wins, or he rolls a 7, in which
case he loses. For example, suppose a player's 1st roll is a 6.Then 6 becomes the point. He must
roll again. If his next roll were a 3, he would have to roll a 3rd time. The point remains 6. He
continues to roll the dice until he either gets a 6 (a win) or a 7 (a loss).
(2) When a player wins, he collects the bet, and gets to keep the dice and take another turn. Then the
play begins again with rule (1).
(3) When a player loses, his opponent collects the money that was bet and it becomes the opponent's
turn to roll the dice. The opponent starts play again with rule (1)
And when player 2 places a wager if his balance is greater than what it originally was he will wager 150 and if it is less he will wager 50. while player 1 always wagers 100.
My question, I have set it up thus far to go through as if one person is playing, how do i write the function for the two player set up and the function for placing the wager?
//A quick demo of a two player game with bets.
srand(time(0)); //Set the seed the random number generator to the current time.
int p1bet = 0, p2bet = 0, p1cash = 50, p2cash = 50, guess, x; //Initialize some variables. Name them sensibly.
bool turn = 1; //1 = Player 1's turn. 0 = Player 2's turn.
while(true) //Main game loop
cout << "Player 1 cash: " << p1cash << " : " << p2cash << " Player 2 cash\n\n"; //Display current cash amounts (scores).
if(turn == 1) //Get player 1's bet if it is player 1's turn.
p1bet = 0;
while(p1bet < 1 || p1bet > p1cash) //While the bet is not an allowed number.
cout << "Player 1, enter your bet: "; //Request input from the user.
cin >> p1bet;
p1cash = p1cash - p1bet;
else //Otherwise, it is player 2 to make a bet.
p2bet = 0;
while(p2bet < 1 || p2bet > p2cash)
cout << "Player 2, enter your bet: ";
cin >> p2bet;
p2cash = p2cash - p2bet;
cout << "Enter your guess: 1- 10\n";
cin >> guess;
}while(guess < 1 || guess > 10);
x = rand() % 10 + 1; //This is the random number players are trying to guess.
cout << "Guess was: " << guess << "\n";
cout << "Number was: " << x << "\n";
if(guess == x) //If player won the bet.
if(turn == 1)
p1cash = p1cash + (p1bet * 8); //The appropriate player wins 8 * amount bet.
p2cash = p2cash + (p2bet * 8); //The appropriate player wins 8 * amount bet.
else //If player lost the bet.
cout << "You lose!\n\n\n";
if(p1cash == 0 || p2cash > 2000)
cout << "Player 2 wins!\n"; //If either player wins, we break out of the main loop.
elseif(p2cash == 0 || p1cash > 2000)
cout << "Player 1 wins!\n"; //If either player wins, we break out of the main loop.
//If nobody lost the game yet...
if(turn == 0) //Swap who has the turn.
turn = 1;
turn = 0;
This could be made more tidy using functions (although I don't tend to use functions for something <100 lines long), which I assume you are already doing?
Tierney, i believe we have the same teacher lol. Thank you Mats for your help i certainly was able to get something working, however I am running into trouble with my program hitting a speed bump. It gets to a certain point and wont continue. I'm not sure exactly how to fix this.