Question

In: Computer Science

Write a C++ program to play the dice game "Craps". Craps is one of the most...

Write a C++ program to play the dice game "Craps". Craps is one of the most popular games of chance and 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 the two upward faces is calculated. If the sum is 7 or 11 on the first throw, the player wins. If the sum is 2, 3, or 12 on the first throw (called "craps"), the player loses (i.e. the "house" wins). If the sum is 4, 5, 6, 8, 9, or 10 on the first throw, then the sum becomes the player's "point." To win, you must continue rolling the dice until you "make your point." The player loses by rolling a 7 before making the point.

At a minimum, your solution must contain the functions below.

  • void displayGameRules()
    • Open rules.txt file and read the rules from the file.
    • Display the rules to the screen.
    • Close the file
    • NOTE: Using Notepad, create a rules.txt file within your project folder and copy and paste the rules inside the file.
  • int rollOneDice()
    • NOTE: This function simulates rolling one dice
    • Generate random integer number from 1 to 6
    • return the random number
  • int rollTwoDice()
    • NOTE: This function simulates rolling two dice
    • Calls rollOneDice() function twice to get two random values
    • Print to the console "Sum: 6 (Die1: 4 Die2: 2)" where 4 is the value of the first dice and 2 is the value of the second dice and 6 is the sum of both dice.
    • return the sum.

Write a program that implements craps according to the above rules. Additionally:

  • When the program first begins, show the user the rules of Craps.
  • Before each initial throw of the dice, prompt the user to ask him/her to keep playing or quit. Gameplay ends when the user chooses to quit or runs out of money.
  • Your program should allow for wagering before each roll. This means that you need to prompt the user for an initial bank balance from which wagers will be added or subtracted.
    • Validate the initial bank balance entered by the user greater than zero. If the initial bank balance is not greater than zero, then inform the user and re-prompt.
  • Before each roll prompt the user for a wager. This includes rolls when the user is trying to make their point. Wagers must be less than or equal to the available bank balance.
    • The available bank balance is based on the accumulation of the previous wagers this game.
  • Once a game is lost or won, the bank balance should be adjusted.
  • Additional wagering details include:
    • Require the user to enter a non-zero wager for the first roll. Allow the user to enter a wager of 0 for subsequent “point” rolls.
    • Validate that the user’s wager is within the limits of the player's available balance. If the wager exceeds the player's available balance, then inform the user and re-prompt.

Solutions

Expert Solution

Screenshot

---------------------------------------------------------------------------------------------------------

Program

//Header files
#include <iostream>
#include<string>
#include<fstream>
using namespace std;

//Function prototypes
void displayGameRules();
int rollOneDice();
int rollTwoDice();
bool playAgain();
bool makePoint(int&,int&);
bool play(int&,const int);
int main()
{
   srand(0);
   int money,point;
    //Call function to display game rules
   displayGameRules();
   //Ask user if they want to play
   bool ch = playAgain();
   //If yes then play
   if (ch) {
       //Function play first round to generate meke point
       if(makePoint(money,point)) {
           //Play again question
           ch = playAgain();
           //If yes
           while (ch && money>0) {
               //Next round of play
               bool playGame = play(money, point);
               //If not loss
               if (playGame) {
                   ch = playAgain();
               }
               //Otherwise exit
               else {
                   break;
               }
           }
       }
   }
   cout << "\nGood Bye!!!" << endl;
}
//Function to display the rules of the game
void displayGameRules() {
   string line;
   ifstream in("rules.txt");
   if (!in) {
       cout << "File not found!!" << endl;
       exit(0);
   }
   cout << "Rule of the game :-" << endl;
   while (!in.eof()) {
       getline(in, line);
       cout << line << endl;;
   }
   cout << endl;
   in.close();
}
//Function to roll a dice and get face of the dice
int rollOneDice() {
   return rand() % 6 + 1;
}
//Function to roll 2 dice and return sum
int rollTwoDice() {
   return rollOneDice() + rollOneDice();
}
//Function to prompt for play repetition
bool playAgain() {
   char ch;
   //Prompt for play repetition
   cout << "Do you want to play or quit(y/n): ";
   cin >> ch;
   while (toupper(ch) != 'Y' && toupper(ch) != 'N') {
       cout << "Error!!Choice should be y/n" << endl;
       cout << "Do you want to play or quit(y/n): ";
       cin >> ch;
   }
   if (toupper(ch) == 'Y') {
       return true;
   }
   return false;
}
//Function to make player point
bool makePoint(int& money,int& point) {
   int wager;
   cout << "Enter your initial balance: ";
   cin >> money;
   while (money <= 0) {
       cout << "Initial balance must be positive!!" << endl;
       cout << "Enter your initial balance: ";
       cin >> money;
   }
   cout << "Enter wager amount: ";
   cin >> wager;
   while (wager == 0 || wager > money) {
       cout << "Wager cannot be 0 and greater than money" << endl;;
       cout << "Enter wager amount: ";
       cin >> wager;
   }
   int sum = rollTwoDice();
   cout << "Dice rolled = " << sum << endl;
   if (sum == 7 || sum == 11) {
       money += wager;
       cout << "You Win!!! and earn money "<<money << endl;
   }
   else if (sum == 2 || sum == 3 or sum == 12) {
       money -= wager;
       cout << "You Loss!!! and lose your wager amount, so money = "<<money << endl;
   }
   else {
       point = sum;
       return true;
   }
   return false;
}
//Function to paly game until win or money reach 0
bool play(int& money,const int point) {
   int wager;
   cout << "Enter wager amount: ";
   cin >> wager;
   while (wager > money) {
       cout << "Wager should not be greater than money your actual balance is "<<money << endl;;
       cout << "Enter wager amount: ";
       cin >> wager;
   }
   int sum = rollTwoDice();
   cout << "Dice rolled = " << sum << endl;
   if (sum == point) {
       money += wager;
       cout << "You Win!!! and earn money " << money << endl;
   }
   else if (sum == 7 ) {
       money -= wager;
       cout << "You Loss!!! and lose your wager amount, so money = " << money << endl;
   }
   else {
       money -= wager;
       return true;
   }
   return false;
}

----------------------------------------------

Output

Rule of the game :-
* 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 the two upward faces is calculated.
* If the sum is 7 or 11 on the first throw, the player wins.
* If the sum is 2, 3, or 12 on the first throw (called "craps"), the player loses
* If the sum is 4, 5, 6, 8, 9, or 10 on the first throw, then the sum becomes the player's "point"
* To win, you must continue rolling the dice until you "make your point."
* The player loses by rolling a 7 before making the point

Do you want to play or quit(y/n): y
Enter your initial balance: 100
Enter wager amount: 25
Dice rolled = 7
You Win!!! and earn money 125

Good Bye!!!

----------------------------------------------------

rules.txt

* 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 the two upward faces is calculated.
* If the sum is 7 or 11 on the first throw, the player wins.
* If the sum is 2, 3, or 12 on the first throw (called "craps"), the player loses
* If the sum is 4, 5, 6, 8, 9, or 10 on the first throw, then the sum becomes the player's "point"
* To win, you must continue rolling the dice until you "make your point."
* The player loses by rolling a 7 before making the point


Related Solutions

For this assignment, write a program that uses functions to simulate a game of Craps. Craps...
For this assignment, write a program that uses functions to simulate a game of Craps. Craps is a game of chance where a player (the shooter) will roll 2 six-sided dice. The sum of the dice will determine whether the player (and anyone that has placed a bet) wins immediately, loses immediately, or if the game continues. If the sum of the first roll of the dice (known as the come-out roll) is equal to 7 or 11, the player...
Write a MATLAB program to simulate the FIFTY dice game that can automatically play the FIFTY...
Write a MATLAB program to simulate the FIFTY dice game that can automatically play the FIFTY dice game for any amount of players, keep scores for all the players, clearly show the game progress for every round, every player, and every roll in the command window, automatically ends the game when the first player accumulates 50 points or more game score, and automatically select the game winner and the winning game score. i have coded most of the game, i...
Craps is a dice game in which the players make wagers on the outcome of the...
Craps is a dice game in which the players make wagers on the outcome of the roll, or a series of rolls, of a pair of dice. Most outcomes depend on the sum of the up faces of two, fair, six-sided dice. 1) What is the lower class boundary of the "6" class? 2) Find the mean. 3) Find the standard deviation. 4) Find the z-score for snake eyes.
This problem concerns the dice game craps. On the first roll of two dice, you win...
This problem concerns the dice game craps. On the first roll of two dice, you win instantly with a sum of 7 or 11 and lose instantly with a roll of 2,3, or 12. If you roll another sum, say 5, then you continue to roll until you either roll a 5 again (win) or roll a 7 (lose). How do you solve for the probability of winning?
Overview For this assignment, write a program that uses functions to simulate a game of Craps....
Overview For this assignment, write a program that uses functions to simulate a game of Craps. Craps is a game of chance where a player (the shooter) will roll 2 six-sided dice. The sum of the dice will determine whether the player (and anyone that has placed a bet) wins immediately, loses immediately, or if the game continues. If the sum of the first roll of the dice (known as the come-out roll) is equal to 7 or 11, the...
Using Python, Assignment Write a program that plays a game of craps. The program should allow...
Using Python, Assignment Write a program that plays a game of craps. The program should allow the player to make a wager before each “turn”. Before each turn, allow the user to either place a bet or exit the game. After each turn display the player’s current balance. Specifics Each player will start with $500.00. Initially, and after each turn give the user the option of betting or leaving the program. Implement this any way you wish, but make it...
Objective Make a function to serve as a Craps dice game simulator. If you are not...
Objective Make a function to serve as a Craps dice game simulator. If you are not familiar Craps is a game that involves rolling two six-sided dice and adding up the total. We are not implementing the full rules of the game, just a function that returns the value of BOTH dice and their total. Details In the function you create: Make a function that uses pointers and pass by reference to return THREE separate outputs (arrays are not allowed)....
IN C++ Write a program to play the Card Guessing Game. Your program must give the...
IN C++ Write a program to play the Card Guessing Game. Your program must give the user the following choices: - Guess only the face value of the card. -Guess only the suit of the card. -Guess both the face value and suit of the card. Before the start of the game, create a deck of cards. Before each guess, use the function random_shuffle to randomly shuffle the deck.
Write a program in Python to simulate a Craps game: 1. When you bet on the...
Write a program in Python to simulate a Craps game: 1. When you bet on the Pass Line, you win (double your money) if the FIRST roll (a pair of dice) is a 7 or 11, you lose if it is ”craps” (2, 3, or 12). Otherwise, if it is x ∈ {4, 5, 6, 8, 9, 10}, then your point x is established, and you win when that number is rolled again before ’7’ comes up. The game is...
In the game where A, B and C play together with dice. A rolls first., then...
In the game where A, B and C play together with dice. A rolls first., then B, then C and again A, B,... What is the probability that A is the first person that flips 6 first? A. 0.4727 B. 0.2379 C. 0.3956 D. 0.5
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT