Have a look at my code please

Hi, I posted here with my last programming project and got some good feedback, so I'd like the same again thanks! The task is from the Programming Projects in Walt Savitch's Absolute C++ book, chapter 4. It hasn't covered arrays, structures, classes, etc, 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*In the game of craps, a “Pass Line” bet proceeds as follows. The first roll of the
two, six-sided dice in a craps round is called the “come out roll.” The bet immediately
wins when the come out roll is 7 or 11, and loses when the come out roll
is 2, 3, or 12. If 4, 5, 6, 8, 9, or 10 is rolled on the come out roll, that number
becomes “the point.” The player keeps rolling the dice until either 7 or the point
is rolled. If the point is rolled first, then the player wins the bet. If the player rolls
a 7 first, then the player loses.

Write a program that plays craps using those rules so that it simulates a game
without human input. Instead of asking for a wager, the program should
calculate whether the player would win or lose. Create a function that simulates
rolling the two dice and returns the sum. Add a loop so that the program plays
10,000 games. Add counters that count how many times the player wins, and
how many times the player loses. At the end of the 10,000 games, compute the
probability of winning, as Wins / (Wins + Losses), and output this value. Over
the long run, who is going to win more games of craps, you or the house?*/

#include <iostream>
#include <cstdlib>

using std::cout;
using std::endl;
using std::cin;
using std::ios;

void setPrecision();
void setRandomSeed();
int diceRoll();

int main()
{
	int playerWin = 0, houseWin = 0, thePoint = 0, secondRoll = 0, dice = 0;
	double probabilityOfWinning = 0;
	
	setPrecision(); //fix decimal outputs to 2 places
	setRandomSeed(); //change seed number for rand() to allow testing with different outputs
	
	for(int a = 0; a <= 10000; a++)
	{
		thePoint = 0;
		dice = diceRoll();
		
		if(dice == 7 || dice == 11)
		{
			playerWin++;
			continue;
		}
		else if(dice == 2 || dice == 3 || dice == 12)
		{
			houseWin++;
			continue;
		}
		else
		{
			thePoint == dice;
			
			do
			{
				secondRoll = diceRoll(); //roll dice again
				
				if(secondRoll == thePoint)
				{
					playerWin++;
					break;
				}
				else if(secondRoll == 7)
				{
					houseWin++;
					break;
				}
			}
			while(secondRoll != thePoint || secondRoll != 7); //keep rolling until the Point or a 7 is rolled
		}
	}
	
	cout << "Player wins " << playerWin << " times." << endl;
	cout << "House wins " << houseWin << " times." << endl;
	
	probabilityOfWinning = (static_cast<double>(playerWin) / (playerWin + houseWin)) * 100; //cast needed to get round integer division
	
	cout << "Probability of winning is " << probabilityOfWinning << "%" << endl;
		 
	return 0;
}

//ensure all floating point number are rounded to 2 decimal places
void setPrecision()
{
	cout.setf(ios::fixed);
	cout.setf(ios::showpoint);
	cout.precision(2);
}

//set a different seed for rand() to allow testing of program with different outputs
void setRandomSeed()
{
	int randSeed;
	
	cout << "Enter a seed: ";
	cin >> randSeed;
	srand(randSeed);
}

//simulate rolling of two dice
int diceRoll()
{
	int firstDice = 0, secondDice = 0;
	
	do
	{
		firstDice = rand() % 7; //modulo 7 so that maximum value is 6
	}
	while(firstDice == 0); //dice roll can't be a 0
	
	do
	{
		secondDice = rand() % 7; //modulo 7 so that maximum value is 6
	}
	while(secondDice == 0); //dice roll can't be a 0
	
	return firstDice + secondDice;
}
Last edited on
This is more efficient:

1
2
3
4
5
6
7
8
9
//simulate rolling of two dice
int diceRoll()
{
	int firstDice = (rand() % 6) + 1; // more efficient
	
	int secondDice = (rand() % 6) + 1;
	
	return firstDice + secondDice;
}


could reduce to:

1
2
3
4
5
//simulate rolling of two dice
int diceRoll()
{
	return (rand() % 6) + (rand() % 6) + 2;
}

Last edited on
Thanks.

To make sure I understand, it's more efficient, I assume, because it doesn't involve looping over & over?

And I'm guessing each line assigns a random number between 0 and 5 to a variable, and adds 1 to give the range 1-6?

Thanks again
yes, right on both counts!
Thanks. I must be learning something if that was the only issue with my code!
@gers1978

did you get that code template from a tutorial and if so where?

What code template?
gers1978 wrote:
Thanks. I must be learning something if that was the only issue with my code!


No problem. Its certainly quite a crap program you have there... ;o)

Joking!

One other thing you might consider. Your dice rolls are integers with a fixed number of outcomes. This could be dealt with using a switch() statement. That should be slightly more efficient than the if()else{} statements. But not by a great deal. The if()s are fine too.
Topic archived. No new replies allowed.