IN C++ LANGUAGE. Objectives: This assignment provides more practice on using fun
ID: 3777243 • Letter: I
Question
IN C++ LANGUAGE. Objectives: This assignment provides more practice on using functions to solve problems. Specific objectives include: call-by-value, call-by-reference. Note that no array is needed in this assignment. You are encouraged to use array if you know how to use it. We expect the program to consist of several small functions. The functions should be called in the main function to simulate a game. Description: Craps is a popular gambling game. The rules of the game are as follows: The player rolls 2 dice the first time (the “come out roll”). If the 2 dice sum to 7 or 11 it is a “natural” and the player wins. If the sum of the 2 dice is 2, 3 or 12 then it is “craps” and the player loses. Any other sum on the first roll 4, 5, 6, 8, 9, or 10 becomes the player’s “point”. The player then continues to roll the dice until either the player rolls the point again or until 7 is rolled. If the player rolls the point again then the player wins. If the player rolls 7 before rolling the point again then the player loses. Here are some example outcomes of games. Game 1: 7 (win) Game 2: 11 (win) Game 3: 2 (lose) Game 4: 3 (lose) Game 5: 12 (lose) Game 6: 8, 2, 6, 11, 8 (win) Game 7: 9, 12, 6, 7 (lose) The player starts off with an initial account of ($1000). The player can bet some amount of money (less than or equal to his current account) in a game. If the player wins he gets double the amount of what he bet. If he loses, the money he bet will be reduced from its current account. For example, the current account of a player is $500. He bets $100 on a game. If he wins, his account will be $ 600 (500 + 100). If he loses, his account will become $ 400 (500 -100). Your Program allows the user to play more than one games. It should give the player four options to choose: 1. Play another game 2. Print the summary of all the games played 3. Help 4. Quit. Here are some suggested functions. It is your job to define the input and output parameters. You are free to insert input parameters/arguments in the functions. You can also change the return type of functions if you need to. char FirstRoll() This function should simulate the first roll of a game and returns a char. If the player wins it returns ‘W’, loses it returns ‘L’ and ‘C’ if player neither wins nor loses. void OtherRolls() This function should simulate the game after the first roll. This function is called only when the player doesn’t win/lose in the first roll. void playOneGameOfCraps() This function should simulate one complete game of craps until the player wins or loses. You will call the previous two functions within this function. int rollOneDice() The purpose for this function is to generate a random number between 1 and 6 (including 1 and 6). It is equivalent to rolling a dice. Calling the function two times will be equivalent to rolling two dice. void printMenu() This function will be used to input an option Input: All input are done interactively. Output: A sample execution of the program is given below. Only the bolded letters are user’s input.
Game of Craps
P –Play one Game of Craps
R-print a summary of all the games played
H-help
Q-Quit
P
Current account: $1000
How much you want to bet: $100
Game 1: 7 <win>
Current account: $ 1100
------------------------------------------
Game of Craps
P –Play one Game of Craps
R-print a summary of all the games played
H-help
Q-Quit
P
Current account: $1100
How much you want to bet: $500
Game 2: 9, 12, 6, 7 <lose>
Current amount: $600
------------------------------------------
Game of Craps
P –Play one Game of Craps
R-print a summary of all the games played
H-help
Q-Quit
P Current account: $600
How much you want to bet: $50
Game 3: 4, 12, 6, 4 <win>
Current amount:
$650 ------------------------------------------
Game of Craps
P –Play one Game of Craps
R-print a summary of all the games played
H-help
Q-Quit
R
------------------------------------------
Total number of Games, wins, lost, average rolls
3, 2, 1, 2.67
Explanation / Answer
#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;
int account_bal=1000,betAmt,game_count=0,win_count=0,lose_count=0,roll_count=0;
double average_rolls=0.0;
void playOneGameOfCraps();
int rollOneDice();
char FirstRoll();
char OtherRolls(int point);
void printMenu();
int main()
{
char choice;
do
{
printMenu();
cin>>choice;
switch(choice)
{
case 'P':{
cout<<"Current account: $"<<account_bal<<endl;
cout<<"How much you want to bet: $";
cin>>betAmt;
cout<<"Game "<<++game_count<<":";
playOneGameOfCraps();
continue;
}
case 'R':{
cout<<"Total number of Games :"<<game_count<<endl;
cout<<"No of Games Won :"<<win_count<<endl;
cout<<"No of Games Lost :"<<lose_count<<endl;
average_rolls=(double)roll_count/game_count;
cout<<"Average Rolls :"<<average_rolls<<endl;
continue;
}
case 'H':{
continue;
}
case 'Q':{
break;
}
}
}while(choice!='Q');
return 0;
}
void printMenu()
{
cout<<" Game of Craps"<<endl;
cout<<"P-Play one Game of Craps"<<endl;
cout<<"R-print a summary of all the games played"<<endl;
cout<<"H-help"<<endl;
cout<<"Q-Quit"<<endl;
}
void playOneGameOfCraps()
{
int seedVal=0;
//t is a 'time_t' type variable
time_t t;
seedVal = (unsigned) time (&t);
srand(seedVal);
char ch=FirstRoll();
cout<<ch;
if(ch=='W')
{
account_bal+=betAmt;
win_count++;
cout<<" Current account :$"<<account_bal<<endl;
}
else if(ch=='L')
{
account_bal-=betAmt;
lose_count++;
cout<<" Current account :$"<<account_bal<<endl;
}
}
char FirstRoll()
{
int sum=0;
char ch;
int point;
sum=rollOneDice();
cout<<sum<<" ";
if(sum==7 || sum==11)
{
ch='w';
return ch;
}
else if(sum==2 || sum==3 || sum==12)
{
ch='L';
return ch;
}
else if(sum==4 || sum==5 || sum==6 || sum==8|| sum==9)
{
point=sum;
ch=OtherRolls(point);
}
return ch;
}
char OtherRolls(int point)
{
int sum=0;
char ch;
while(true)
{
sum=rollOneDice();
cout<<sum<<" ";
if(sum==point)
{
ch='W';
break;
}
else if(sum==7)
{
ch='L';
break;
}
else
continue;
}
return ch;
}
int rollOneDice()
{
int sum=0;
for(int i=0;i<2;i++)
{
sum+=rand() % 5+1;
}
roll_count++;
return sum;
}