In: Computer Science
Code to copy along with screenshots of code and output are provided.
Please refer to screenshots to understand
indentation of code
If you have any doubts or issues. Feel free to ask in
comments
Please give this answer a like, or upvote. This will be very
helpful for me.
================================================================
Screenshots of Code :
Screenshots of Output :
Code to copy:
#include <iostream>
#include <cstdlib>
using namespace std;
// defining symbolic constants
#define SEVEN 7
#define ELEVEN 11
#define TWO 2
#define THREE 3
#define TWELVE 12
// declaring functions
int rollTheDice();
bool winner(int roll);
bool craps(int roll);
void rollMore(int point, double wager);
double getWager();
int main()
{
// seeding with 34
srand(34);
// calling the get wager
double wager = getWager();
// rolling dice for first time
int roll = rollTheDice();
// check if won immediately
if(winner(roll) == true)
{
cout<<"\nCongratulations, You won the
game."<<endl;
cout<<"You won $"<<2*wager<<endl;
}
// check if lost immediately
else if(craps(roll) == true)
{
cout<<"\nOops! You lost. "<<endl;
cout<<"You lost $"<<wager<<endl;
}
// else roll more
else
{
rollMore(roll, wager);
}
return 0;
}
/***************************************************************
Function: int rollTheDice()
Use: This function is used to roll the two dice for the game
Arguments: None
Returns: return the sum of result of two dice rolled
Note: It also prints the sum and individual result after rolling
the dice
***************************************************************/
int rollTheDice()
{
// seeding with number 34
// generating random values between 1 and 6 using rand()
int dice1 = rand()%6 + 1;
int dice2 = rand()&6 + 1;
// displaying values along with sum
cout<<"Roll: "<<dice1<<" +
"<<dice2<<" = "<<dice1+dice2<<endl;
//returning the sum
return dice1+dice2;
}
/***************************************************************
Function: bool winner(int roll)
Use: This function checks if roll is equal to seven or eleven
Arguments: roll - an integer that represents the number rolled by the player
Returns: True if roll is equal to 7 or 11 otherwise return false
Note: None
***************************************************************/
bool winner(int roll)
{
if(roll == SEVEN || roll == ELEVEN)
{
// return true if roll is equal to 7 or 11
return true;
}
else
{
// otherwise return false
return false;
}
}
/***************************************************************
Function: bool craps(int roll)
Use: This function checks if roll is equal to two,three or twelve
Arguments: roll - an integer that represents the number rolled by the player
Returns: True if roll is equal to 2,3 or 12 otherwise return false
Note: None
***************************************************************/
bool craps(int roll)
{
if(roll == TWO || roll == THREE || roll == TWELVE)
{
// return true if roll is equal to 2,3 or 11
return true;
}
else
{
// otherwise return false
return false;
}
}
/***************************************************************
Function: void rollMore( int point, double wager )
Use: This function continues the craps game after the come-out roll
Arguments: point - an integer that represents the number that
needs to
be rolled by the player to win the game
wager - a double that represents the amount the player
wagered on the game
Returns: Nothing
Note: None
***************************************************************/
void rollMore(int point, double wager)
{
// displaying point
cout<<"\nThe point is
"<<point<<"\n"<<endl;
// declaring boolean to control the loop
bool continu = true;
// declaring integer variable to store the roll
int roll;
int i = 0;
// loop until continu is not false
while(continu == true)
{
// roll the dice
roll = rollTheDice();
if(roll == 7)
{
// if 7 , then game is lost
cout<<"\nOops you rolled a 7. You LOST!"<<endl;
cout<<"You lost $"<<wager<<endl;
// continu = false to end the loop
continu = false;
}
else if(roll == point)
{
// if rolled the point, then won the game
cout<<"\nCongratulations, you rolled the point. You
WON!"<<endl;
cout<<"You won $"<<2*wager<<endl;
// continu = false to end the loop
continu = false;
}
}
}
/***************************************************************
Function: double getWager()
Use: This function is used to take a valid user wager input
Arguments: none
Returns: return valid wager amount
Note: This function keeps asking for valid input using while
loop until valid input is entered.
And it also prints the cents value to be returned back entered by
user, keeping only integeral part in wager.
***************************************************************/
double getWager()
{
double wager;
// asking for wager input
cout<<"Whats your wager (no cents allowed) (minimum: 5.00)? :
";
cin>>wager;
// boolean to control the loop
bool continu = true;
// checking the input
if(wager>=5)
{
continu = false;
}
else
{
continu = true;
}
// continue asking until user enters valid wager amount
while(continu == true)
{
cout<<"Invalid Amount. Plesse enter valid wager amount again:
";
cin>>wager;
// if wager is greater than or equal to 5, then stop
asking
if(wager>=5)
{
// set continu = false to end the loop
continu = false;
}
}
// declaring variable difference to store the cent amount
double difference;
int wager_integer = wager;
difference = wager - wager_integer;
// if difference is greater then zero then display amount give
back
if(difference > 0 )
{
cout<<"\nAmount given back:
"<<difference<<"\n"<<endl;
}
// new wager after removing the cent part
wager = wager_integer;
// displaying wager
cout<<"\nWAGER = "<<wager<<"\n"<<endl;
// returning wager
return wager;
}
==============================================================