Have one more question! (simulate the casino game "craps")

So I need to wright a program that does this (But I have run into some issues any pointers, helps or suggestions would be greatly appreciated):

Write a program to simulate the casino game "craps".
It should play 1,000,000 games so that you can compute the
probability(%) of the "player" winning and the "house" winning.

The rules are:
Player rolls two dice.
When the sum is 7 or 11 on first throw, player wins.
When the sum is 2, 3, or 12 on first throw, "house" wins.
When the sum is 4,5,6,8,9, or 10 on first throw,
that sum becomes the player's "point".
Now, to win the player must continue rolling the dice until he
makes "point"; however should he roll a 7 then the "house" wins.
That concludes the game.

~ also how would I make it when the dice roles a "point" number to tell it to role the dice agin?

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
#include <iostream>
#include <ctime>
#include <cstdlib>

using namespace std;

int dice1;
int dice2;
int sum_of_dice;
int point;
int number_of_times_played;

int main()
{
    sum_of_dice = dice1 + dice2;
    srand(time(0));
    
    do
    {
        dice1 = rand() % 6 + 1;
        dice2 = rand() % 6 + 1;
        
        if (sum_of_dice == 7 || sum_of_dice == 11)
        {
            cout << "Congratulations you have won!" << endl;
        }
        else if (sum_of_dice == 2 || sum_of_dice == 3 || sum_of_dice == 12)
        {
           cout << "Better luck next time the house has one!" << endl;
        }
        else (sum_of_dice == 4 || sum_of_dice == 5 || sum_of_dice == 6 || sum_of_dice == 8 || sum_of_dice == 9 || sum_of_dice == 10);
        {
            cout << "Your point is now: " << sum_of_dice << endl;
            // What should I put here to make it roll the dice agin and start over?
        }
        
    } while (number_of_times_played != 1);
    
        return 0;
}//End Code! 
Last edited on
Line 15: This is out of place. You're adding the die together, but you haven't rolled them yet. This belongs after lne 21.

Line 37: number_of_times_played is not initialized. You're going to be comparing garbage here. You never increment this variable to count the number of games. Instructions say to do 1,000,000 iterations. Your while condition is faulty.

Line 34: You need to roll the dice again. You need to do this in a loop, until the point is rolled (win) or a 7 is rolled (lose).

Line 38: You need to calculate and display the percentages for the house and the player.

You need to count the number of wins and losses.
Last edited on
Ohhhh I see! (Line 15's issue)

Line 37: But how would you go about "incrementing" number_of_times_played. I just don't see how to link them so to speak.

Line 34: I am still confused how to nest those loops can you give me an idea?

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
#include <iostream>
#include <ctime>
#include <cstdlib>

using namespace std;

int dice1;
int dice2;
int sum_of_dice;
int point;
int number_of_wins;
int number_of_lose;
int probability_of_winning;
int probability_of_losing;
int number_of_times_played;

int main()
{
    
    srand(time(0));
    
    do
    {
        dice1 = rand() % 6 + 1;
        dice2 = rand() % 6 + 1;
        
        sum_of_dice = dice1 + dice2;
        
        if (sum_of_dice == 7 || sum_of_dice == 11)
        {
            cout << "Congratulations you have won!" << endl;
        }
        else if (sum_of_dice == 2 || sum_of_dice == 3 || sum_of_dice == 12)
        {
           cout << "Better luck next time the house has one!" << endl;
        }
        else (sum_of_dice == 4 || sum_of_dice == 5 || sum_of_dice == 6 || sum_of_dice == 8 || sum_of_dice == 9 || sum_of_dice == 10);
        {
            cout << "Your point is now: " << sum_of_dice << endl;
            // What should I put here to make it roll the dice agin and start over?
        }
        
    } while (number_of_times_played != 1000000);
    
    number_of_wins =        // How do I make the the compiler understand this?
    number_of_lose =        // How do I make the the compiler understand this?
    probability_of_winning = number_of_wins / 10000;
    probability_of_losing = number_of_lose / 10000;
    
    cout << "Out of 1 million games you won: " << number_of_wins << endl;
    cout << "Out of 1 million games you lose: " << number_of_lose << endl;
    cout << "The probability for the Player to win is: " << probability_of_winning << "%" << endl;
    cout << "the probability for House to win is: " << probability_of_losing << "%" << endl;
    
        return 0;
}//End Code! 
1
2
3
4
5
6
7
8
9
10
11
12
13
// What should I put here to make it roll the dice agin and start over?
do {
    dice1 = rand() % 6 + 1;
    dice2 = rand() % 6 + 1;
    int sum2 = dice1 + dice2;
    if( sum2 == sum_of_dice ) {
        cout << "Congratulations you have won!" << endl;
        break;
    } else if( sum == 7 ) {
        cout << "Better luck next time the house has one!" << endl;
        break;
    }
} while(true); //<-- infinite loop, must be broken with a "break" statement 



What are you trying to do here?
1
2
number_of_wins =        // How do I make the the compiler understand this?
number_of_lose =        // How do I make the the compiler understand this? 
number_of_wins should be incremented (starting at 0) each time a game is won. number_of_loes == number_of_times_played - number_of_win.


1
2
3
do {
    ...
} while (++number_of_times_played != 1000000); //<-- You should increment this variable 
Last edited on
@mathhead200:
Do not make sum2 a double.

doubles are approximations. Attempting to compare a double to an int may not match because of the approximation of the double.

@OP
Line 15: number_of_times_played is still uninitialized.
 
int number_of_times_played = 0;


Line 45:
 
int number_of_wins = 0;  // Need to initialize this 


After line 31 (and Mathhead200's line 8):
1
2
 
    number_of_wins++;  // increment win counter 


Line 45 & line 46: You don't need these since you increment the corresponding counters within the loop.


Last edited on
@AbstractionAnon Good catch. I don't know why I did that. (My previous post has been edited.)
Thanks so much! That makes seance I have learned a lot from you all! But I have one last question. After it goes goes through and plays it's 1 million games. It proceeds to tell me that I have won all 1 million games(as you can see below) which obviously is not true. Because the outputted code is telling me that I lose quite a bit.


Out of 1 million games you won: 1000000
Out of 1 million games you lose: 0
The probability for the Player to win is: 100%
the probability for House to win is: 0%

Here is my code what do you think is going on?!?!


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
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <iomanip>

using namespace std;

int dice1;
int dice2;
int sum_of_dice;
int point;
int number_of_wins;
int number_of_lose;
double probability_of_winning;
double probability_of_losing;
int number_of_times_played;

int main()
{
    
    srand(time(0));
    
    do
    {
        dice1 = rand() % 6 + 1;
        dice2 = rand() % 6 + 1;
        
        sum_of_dice = dice1 + dice2;
        
        if (sum_of_dice == 7 || sum_of_dice == 11)
        {
            cout << "Congratulations you have won!" << endl;
        }
        else if (sum_of_dice == 2 || sum_of_dice == 3 || sum_of_dice == 12)
        {
           cout << "Better luck next time the house has one!" << endl;
        }
        else (sum_of_dice == 4 || sum_of_dice == 5 || sum_of_dice == 6 || sum_of_dice == 8 || sum_of_dice == 9 || sum_of_dice == 10);
        {
            do {
                dice1 = rand() % 6 + 1;
                dice2 = rand() % 6 + 1;
                int sum2 = dice1 + dice2;
                if( sum2 == sum_of_dice )
                {
                    cout << "Congratulations you have won!" << endl;
                    break;
                } else if( sum2 == 7 )
                {
                    cout << "Better luck next time the house has one!" << endl;
                    break;
                }
            } while(true);
            

        }
        
    } while (++number_of_times_played != 1000000);
    
    number_of_wins = number_of_times_played - number_of_lose;
    number_of_lose = number_of_times_played - number_of_wins;      // It is now telling me that I have never lost.
    probability_of_winning = number_of_wins / 10000;
    probability_of_losing = number_of_lose / 10000;
    
    cout << "Out of 1 million games you won: " << number_of_wins << endl;
    cout << "Out of 1 million games you lose: " << number_of_lose << endl;
    cout << "The probability for the Player to win is: " << probability_of_winning << setw(2) << "%" << endl;
    cout << "the probability for House to win is: " << probability_of_losing << setw(2) << "%" << endl;
    
        return 0;
}//End Code! 
Last edited on
Line 38 is wrong. It should just be else. No conditions, no semicolon. Just else.
I figured it out thanks for all your help!
Topic archived. No new replies allowed.