Question

Write a c++ program that simulates a million of games in craps. I am having a...

Write a c++ program that simulates a million of games in craps. I am having a trouble getting to loop a million of times. I am trying to use a const for a million. This is the code so far.

#include <iostream>
#include <cstdlib>// contains prototypes for functions srand and rand
#include <ctime>// contains prototype for function time
#include <iomanip>
using namespace std;

int rollDice(); // rolls dice, calculates and displays sum
void printstats();
int totroll = 0, games, point, roll;
int won[11] = { }, lost[11]= { };
int numberofRolls;
enum Status { CONTINUE, WON, LOST }; // all caps in constants

int main()
{
cout << "Histogram showing the likelihood of winning for a given number of rolls.\n\n";
cout << "Rolls\n";
int myPoint; // point if no win or loss on first roll
Status gameStatus; // can contain CONTINUE, WON or LOST

// randomize random number generator using current time
srand(time(0));

int sumOfDice = rollDice(); // first roll of the dice
numberofRolls = 0;
// determine game status and point (if needed) based on first roll
switch (sumOfDice)
{
case 7: // win with 7 on first roll
case 11: // win with 11 on first roll         
    gameStatus = WON;
    won[NULL]++;
    break;
case 2: // lose with 2 on first roll
case 3: // lose with 3 on first roll
case 12: // lose with 12 on first roll           
    gameStatus = LOST;
    lost[NULL]++;
   // break;
default: // did not win or lose, so remember point
    gameStatus = CONTINUE; // game is not over
    myPoint = sumOfDice; // remember the point
  
} // end switch

// while game is not complete
while (gameStatus == CONTINUE) // not WON or LOST
{
    sumOfDice = rollDice(); // roll dice again
    numberofRolls++;
    // determine game status
    if (sumOfDice == myPoint){ // win by making point
      gameStatus = WON;
    won[numberofRolls < 11 ? numberofRolls: 10]++;}
    else
      if (sumOfDice == 7) // lose by rolling 7 before point
        gameStatus = LOST;
    lost[numberofRolls < 11 ? numberofRolls: 10 ]++;
} // end while

} // end main

// roll dice, calculate sum and display results
int rollDice()
{
// pick random die values
int die1 = 1 + rand() % 6; // first die roll
int die2 = 1 + rand() % 6; // second die roll

int sum = die1 + die2; // compute sum of die values

return sum;
} // end function rollDice

the output i need to get

Histogram showing the likelihood of winning for a given number of rolls.

Rolls

    1    wins: ********************************************

       losses: **********************

    2    wins: ***************

       losses: **********************

    3    wins: ***********

       losses: ****************

    4    wins: ********

       losses: ***********

    5    wins: ******

       losses: ********

    6    wins: ****

       losses: ******

    7    wins: ***

       losses: ****

    8    wins: **

       losses: ***

    9    wins: *

       losses: **

   10    wins: *

       losses: **

more    wins: ***

       losses: ****

Percentage of games that are wins: 49%

Average number of rolls in a craps game: 3.4

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#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!


answered by: ANURANJAN SARSAM
Add a comment
Know the answer?
Add Answer to:
Write a c++ program that simulates a million of games in craps. I am having a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • This is for C++ #include <random> #include <iostream> #include <ctime> using namespace std; /* In the...

    This is for C++ #include <random> #include <iostream> #include <ctime> using namespace std; /* In the game of craps, a shooter rolls 2 dice and adds the dots on the upper most faces of the dice. 7 or 11 on the first roll wins, 2, 3, or 12 on the first roll loses, andthing else is call the point and the player rolls again The following program fragment uses 1-way if statements simulate the 1st roll of the dice. Replace...

  • C# Code: Write the code that simulates the gambling game of craps. To play the game,...

    C# Code: Write the code that simulates the gambling game of craps. To play the game, a player rolls a pair of dice (2 die). After the dice come to rest, the sum of the faces of the 2 die is calculated. If the sum is 7 or 11 on the first throw, the player wins and the game is over. If the sum is 2, 3, or 12 on the first throw, the player loses and the game is...

  • Please i need helpe with this JAVA code. Write a Java program simulates the dice game...

    Please i need helpe with this JAVA code. Write a Java program simulates the dice game called GAMECrap. For this project, assume that the game is being played between two players, and that the rules are as follows: Problem Statement 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...

  • Java programming Write a simulation of the Craps dice game. Craps is a dice game that...

    Java programming Write a simulation of the Craps dice game. Craps is a dice game that revolves around rolling two six-sided dice in an attempt to roll a particular number. Wins and losses are determined by rolling the dice. This assignment gives practice for: printing, loops, variables, if-statements or switch statements, generating random numbers, methods, and classes. Craps game rules: First roll: The first roll (“come-out roll”) wins if the total is a 7 or 11. The first roll loses...

  • hey, struggling with this assignment, wondering if if I could get some help. Here is the...

    hey, struggling with this assignment, wondering if if I could get some help. Here is the project details The following lists a Dice class that simulates rolling a die with a different number of sides. The default is a standard die with six sides. The rollTwoDice function simulates rolling two dice objects and returns the sum of their values. The srand function requires including cstdlib. my class ------------------------------------ class Dice { public: Dice(); DIce(int numSides); virtual int rollDice()const; protected: int...

  • Craps

    Write a C++ game that plays Craps. Craps is a game played with a pair of dice. The shooter (the player with the dice) rolls a pair of dice and the number of spots showing on the two upward faces are added up. If the opening roll (called the ‘come out roll’) is a 7 or 11, the shooter wins the game. If the opening roll results in a 2 (snake eyes), 3 or 12 (box cars), the shooter loses,...

  • Problem 9 A single game of craps (a dice game) consists of at most two rolls...

    Problem 9 A single game of craps (a dice game) consists of at most two rolls of a pair of six sided dice. The ways to win are as follows: Win-the first roll of the pair of dice sums to either 7 or 1 (you win, game over, no second roll Win the first roll of the pair of dice does NOT sum to either 7 or 1 but the sum of the second roll is equal to the sum...

  • I'm trying to make a game of Craps in C++. This is how the teacher wants...

    I'm trying to make a game of Craps in C++. This is how the teacher wants the program to run. Player rolled: 6 + 4 = 10 The point is 10 Player rolled: 3 + 3 = 6 Player rolled: 6 + 3 = 9 Player rolled: 3 + 1 = 4 Player rolled: 3 + 4 = 7 You seven'd out and lost! Player rolled: 2 + 5 = 7 You won! Player rolled: 3 + 1 = 4...

  • in C++ This assignment is to use your knowledge of Chap 1-5 to write a simulation...

    in C++ This assignment is to use your knowledge of Chap 1-5 to write a simulation program to estimate the winning chance of the Craps game. Craps is a popular dice game played in casinos. Here is how to play: Roll two dice. Each die has 6 faces representing values 1,2,3,4,5,and 6, respectively. Check the sum of 2 dice. If the sum is 2, 3, or 12 (called craps), you lose; if the sum is 7 or 11 (called natural),...

  • Hello, Could you please help me code this program in Java? It is important that all...

    Hello, Could you please help me code this program in Java? It is important that all rules are carefully followed. Using the PairOfDice class from PP 4.9, design and implement a class to play a game called Pig. In this game, the user competes against the computer. On each turn, the current player rolls a pair of dice and accumulates points. The goal is to reach 100 points before your opponent does. If, on any turn, the player rolls a...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT