Question: Write a C++ program that simulates the casino game of craps. These are the rules of the game: If a player throws a 7 or
Write a C++ program that simulates the casino game of craps. These are the rules of the game: If a player throws a 7 or 11 (sum of two dice) the player wins the game. If a player throws a 2, 3 or 12 (sum of two dice) the player loses the game. If a player throws a 4, 5, 6, 8, 9 or 10 (sum of two dice) on the first roll, s(he) neither wins nor loses but creates a point. If this is the case, the player keeps rolling the dice until the point (4, 5, 6, 8, 9 or 10) is being rolled again, and the player wins the game. However, if the player throws a 7 (sum of two dice) before the point is thrown, the player loses the game. Create trials of 10000 times and what is the percentage (in decimals) of you winning. I already asked this question but got an incomplete answer. I tried to do it myself but got an error.
#include
int main()
{
const int TRIALS = 10000;
int dice1, dice2, sum, point, wins = 0, lose = 0;
srand(time(0));
for (int i = 1; i <= TRIALS; i++)
{
dice1 = rand() % 6 + 1;
dice2 = rand() % 6 + 1;
if ((dice1 + dice2) == 7 || (dice1 + dice2) == 11) {
wins++;
}
else if ((dice1 + dice2) == 2 || (dice1 + dice2) == 3 || (dice1 + dice2) == 12) {
lose++;
}
else if (dice1 + dice2 == 4 || dice1 + dice2 == 5 || dice1 + dice2 == 6 || dice1 + dice2 == 8 || dice1 + dice2 == 9 || dice1 + dice2 == 10)
{
dice1 = rand() % 6 + 1;
dice2 = rand() % 6 + 1;
if (dice1 + dice2)
{
wins++;
}
else if (dice1 + dice2 == 7)
{
lose++;
}
}
cout << "The number of times you have played is" << TRIALS << "times, and you won" << wins << "times, and you lost" << lose << "times. The percentage of winning is" << (wins * 1.0 / TRIALS) << endl;
return 0;
}
After debugging it should be something like: The number of times you have played is 10000 times, and you won 49## times, and you lost 50## times. //The total number of win&lose times should add up to 10000 The percentage of winning is 0.491673
Press any key to continue...
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
