In: Computer Science
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()
int rollOneDice()
int rollTwoDice()
Write a program that implements craps according to the above rules. Additionally:
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