"Craps" Dice Roll Program issue

First I'll begin by stating the objective of the program.
Write a program to simulate the casino game "craps".
It should execute 100000000 times 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.


I attempted using a switch statement for the numbers 2, 3, 7, 11, and 12; however I failed (no errors, but no output) and then went to using if statements to see if that'd work but it didn't change anything. Here's my current program, I apologize for any rookie mistakes before hand, my professor isn't too clear so I'm trying to follow most of the tutorials in my book.

Please feel free to critique me, I learn best through experience, so any tips you can give me would be much appreciated .

The commented out switch statement is what I attempted to use beforehand.
He doesn't want us to use arrays quite yet.

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

using namespace std;
int main()
{
	bool done(false);
	int dice1(0), dice2(0), dicesum(0), point(0);
	double win(0), percent(0);

	unsigned seed = time(0);
	srand(seed);

	for (int i = 1; i <= 100000000; i++)
	{
		dice1 = rand() % 6;
		dice2 = rand() % 6;

		dicesum = dice1 + dice2;

		//First throw
		if (dicesum == 4 || 5 || 6 || 8 || 9 || 10)
		{
			point = dicesum;
			done = true;
		}
		else if (dicesum == 7 || 11)
		{
			++win;
			done = true;
		}
		/*switch (dicesum)
		{
			case 4: point = dicesum; break;
			case 5: point = dicesum; break;
			case 6: point = dicesum; break;
			case 7: ++win; break;
			case 8: point = dicesum; break;
			case 9: point = dicesum; break;
			case 10: point = dicesum; break;
			case 11: ++win; break;
		*/
		while (!done)
		{
			dice1 = rand() % 6;
			dice2 = rand() % 6;

			dicesum = dice1 + dice2;

			if (dicesum == point)
			{
				++win; done = true;
			}
			else if (dicesum == 7)
			{
				done = true;
			}
		}
		done = false;

	}
	percent = win / 100000000;
	cout << "Win Rate: " << percent << "%" << endl;
}
Last edited on
You're not properly constructing your compound conditions in the "if" statements. Consider the following:

if (dicesum == 4 || 5 || 6 || 8 || 9 || 10)

This evaluates to true if any of the following evaluate to true:

dicesum == 4
5
6
8
9
10

Since 5, 6, 8, 9 and 10 are all non-zero, they all evaluate to true, which means that the entire condition always evaluates to true.

What you actually want is:

1
2
3
4
5
6
if (dicesum == 4 ||
    dicesum == 5 || 
    dicesum == 6 || 
    dicesum == 8 || 
    dicesum == 9 || 
    dicesum == 10)



Here is a skeleton that should work for you:

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

using namespace std;
int main()
{
	bool done(false);
	int dice1(0), dice2(0), dicesum(0), point(0);
	double win(0), percent(0);

	unsigned seed = time(0);
	srand(seed);

	for (int i = 1; i <= 100000000; i++)
	// so far, so good:

//NOW:
	{
		initialize done to false, point to zero

	      	//start another loop here to repeat throwing the dice until you win or lose on the throw 
		
		while not done

		{
			roll and sum the dice

			if the point is zero (first time through this loop):
			{
				decide whether you win (7 or 11) ,lose (2,3 or 12), if neither, set the point to the sum of the dice.
					if you win or lose, <done> becomes true, win is incremented if throw wins
			}
			else //second and subsequent passes througn this loop until <done> is made true by win or loss
			{
				decide whether you win (by matching the point) or lose (throw a seven)
					if you win or lose, <done> becomes true, win is incremented if throw matches point
			}
		} //end of "not done"
	} // end of iterations (int i = 1; i <= 100000000; i++) loop
	percent = win / 100000000; // probably need to multiply this by 100 to get percentage
	cout << "Win Rate: " << percent << "%" << endl;
}
Last edited on
Thank you all, both replies helped me with my understanding, this problem has been resolved as a result.
I would be interested in seeing how your final version came out, if you would be kind enough to post the code. . .
Topic archived. No new replies allowed.