Question

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, otherwise known as ‘crapping out’. If the shooter rolls a 4, 5, 6, 8, 9 or 10 on the opening roll, then they must roll the same number before rolling a 7 to win the game.

Example:

If the shooter rolls a 6 on the come out roll, a 10 on the second roll and a 7 on the third roll, the shooter loses since he rolled a 7 before rolling another 6. If, however, he rolled a 6 on the third roll, he wins the game.


  • Your program should first ask the user if they want to play craps.

    1. If they respond with ‘Y’ or ‘y’ then go to step 2.

    2. If they respond with ‘N’ or ‘n’ and they played one or more games then display the number of wins, losses and the winning percentage (xx.x%). If they didn’t play any games then the program should terminate.

    3. If they responded with something besides ‘y’, ‘Y’, ‘n’, or ‘N’ then your program should display a message letting them know that their input was an invalid response and re-prompt them if they want to play craps and the program should go back to step 1.

  • You must use cin to a char variable to get their response.

  • Randomly generate two numbers from 1 to 6 that will be used represent each die.

  • Display each die’s value and the sum of the two dice in the following format (Your format must match the format below):

X : Y = SUM

          Where X is the first die, Y is the second die and SUM is the sum of two dice.

  • After each “roll of the dice”, if the shooter wins or losses, then your program must display an appropriate message. Otherwise, your program should display “Please roll again”. The user should press enter to roll again.  Your program must use the getline function to read the input (enter key) from the user.

  • After the game is over, your program should prompt the user if they want to play another game of craps.

  • All non constant variables must have the minimum scope required.

  • You must use the following in your program:

 

  1. At least one do while OR while

  2. At least one switch

  3. At least one if elseif

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

#include <iostream>

#include <iomanip>

using namespace std;


int main()

{

  //setting srand so that random numbers generated are not same each time

  srand(time(NULL));

  char c;

  //n to store number or games played

  int n=0;

  //w to store number of games won by player

  int w=0;

  while(true){

    //asking if user wants to play

    cout<<"Do you want to play craps [Y/N]:";

    cin>>c;

    //if user enters n then breaking out of loop

    if(c=='N'||c=='n')

      break;

    //using switch to check input

    switch(c){

      case 'Y':

      case 'y':

        {

          int s=0;

          //incrementing number of game played by user

          n++;

          do{

            //generating 2 random number

            int a=rand() % 6 + 1;

            int b=rand() % 6 + 1;

            int sum=a+b;

            cout<<a<<" : "<<b<<" = "<<sum<<endl;

            //checking if in first roll sum equals to 7 or 11

            if(s==0&&(sum==7||sum==11)){

              w++;

              cout<<"You Win!!"<<endl;

              break;

            }

            //checking if in first roll sum equals to 2, 3 or 11

            else if(s==0&&(sum==2||sum==3||sum==12)){

              cout<<"You Loose!!"<<endl;

              break;

            }

            else{

              string str;

              //storing the sum of opening roll

              if(s==0){

                getline(cin,str);

                s=sum;

              }

              //if opening rolled sum is rolled again

              //setting s==-1

              else if(s==sum)

                s=-1;

              else{

                //if 7 appears before the opening roll sum is rolled again

                if(sum==7&&s==-1){

                  w++;

                  cout<<"You Win!!"<<endl;

                  break;

                }

                else if(sum==7){

                  cout<<"You Loose!!"<<endl;

                  break;

                }

              }

              cout<<"Please roll again [press Enter]";

              getline(cin,str);

            }

          }while(true);

          break;

        }

      default:

        cout<<"Wrong Answer!!"<<endl;

    }

  }

  //if user played then printing output

  if(n!=0){

    cout<<"\n**************************************"<<endl;

    cout<<"Total number of games played:"<<n<<endl;

    cout<<"Total Number of wins:"<<w<<endl;

    cout<<"Winning percentage:";

    //printing percentage with one decimal value

    cout<<fixed<<setprecision(1)<<w*100.0/n<<"%"<<endl;

    cout<<"**************************************"<<endl;

  }

  return 0;

}


answered by: codegates
Add a comment
Know the answer?
Add Answer to:
Craps
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Similar Homework Help Questions
  • 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...

  • 9. In the casino dice game Craps, players make wagers on a sequence of rolls of...

    9. In the casino dice game Craps, players make wagers on a sequence of rolls of a pair of dice. A sequence of rolls starts with the "shooter" making an initial roll of two dice called the "come-out” roll. If the sum of the dice on the initial roll is 7 or 11 then a player with a bet on the Pass Line wins. If the initial roll results in a sum of 2, 3, or 12 ("craps") then a...

  • 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...

  • 2. "Craps" is a game played by rolling two fair dice. To play one round of...

    2. "Craps" is a game played by rolling two fair dice. To play one round of this game, the player rolls the dice and the outcome is determined by the following rules: If the total number of dots is 7 or 11 (a "natural"), then the player wins. If the total number of dots is 2, 3, or 12 C'craps"), then the player loses. If the total number of dots is 4, 5, 6,8,9, or 10, then this number is...

  • 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...

  • In craps, the "shooter" (the person rolling the dice) wins on the first roll (the "come-out"...

    In craps, the "shooter" (the person rolling the dice) wins on the first roll (the "come-out" roll) if they roll a 7 or 11 and loses if they roll a 2, 3, or 12, in which case the round ends. Otherwise, the first roll becomes the "point", and the round continues. What is the probability the shooter wins on the come-out roll? What is the probability the shooter loses on the come-out roll? What is the probability the point is...

  • using python [6] Craps is a dice-based game played in many casinos. Like blackjack, a player...

    using python [6] Craps is a dice-based game played in many casinos. Like blackjack, a player plays against the house. The game starts with the player throwing a pair of standard, six-sided dice. If the player rolls a total of 7 or 11, the player wins. If the player rolls a total of 2,3, or 12, the player loses. For all other roll values, the player will repeatedly roll the pair of dice until either she rolls the initial value...

  • 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,...

  • III. Overview & Requirements: The following description has been adopted from Deitel & Deitel. One of...

    III. Overview & Requirements: The following description has been adopted from Deitel & Deitel. One of the most popular games of chance is a dice game called "craps," which is played in casinos and back alleys throughout the world. The rules of the game are straightforward: A player rolls two dice. Each die has six faces. These faces contain 1, 2, 3, 4, 5, and 6 spots. After the dice have come to rest, the sum of the spots on...

  • 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...

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