2 player craps game with no inputs

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?


I just wrote this to help you out:
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
//A quick demo of a two player game with bets. 

#include <iostream>
#include <cstdlib>
#include <ctime>

using std::cout;
using std::cin;

int main()
{
    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;
        }
        do
        {
            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.
            }
            else
            {
                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.
                break;
            }
            else if(p2cash == 0 || p1cash > 2000)
            {
                cout << "Player 1 wins!\n"; //If either player wins, we break out of the main loop.
                break;
            }
        }
        //If nobody lost the game yet...
        if(turn == 0) //Swap who has the turn.
        {
            turn = 1;
        }
        else
        {
            turn = 0;
        }
    }
    return 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?
Last edited on
We have to use functions unfortunately but this seems like it will help thanks.
It is not unfortunate to have to use functions. Functions make projects readable. Functions make projects easier. Functions are sensible. Good luck!
Last edited on
Hi guys,
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.


Where exactly does the program stop? Code snippit?
Topic archived. No new replies allowed.