Question

In: Computer Science

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 wins immediately.

If the sum of the first roll of the dice is equal to 2, 3, or 12, the player has rolled "craps" and loses immediately.

If the sum of the first roll of the dice is equal to 4, 5, 6, 8, 9, or 10, the game will continue with the sum becoming the "point." The object of the game is now for the player to continue rolling the dice until they either roll a sum that equals the point or they roll a 7. If the player "makes their point" (ie. rolls a sum that equals the point), they win. If they roll a 7, they lose.

There are many wagers that can be made of a game of craps. For this program, we'll focus on a wager known as the pass line bet. It's a wager that the player will win the game (ie. the initial roll is 7 or 11, or the player makes their point).

This wager pays 1/1 or even money. This means that if the user wagers $1, they'll win $1 if the game is won. In other words, if the game is won, the user will win the amount that they wagered plus their original wager. So if the wager amount was $10 and the game is won, the user will win $10 plus get their original wager amount for a total of $20.

Random Number Generation

The random number generator will be used to "roll" the dice.

If a reminder is needed about how to use the random number generator and how to limit the values that are produced, refer back to program 4:

Link to Program 4

Basic Logic for int main()

Seed the random number generator with a value of 34. Note: other seed values may be used to produce different results. However, the version that is handed in for grading MUST use a seed value of 34.

Call the getWager() function that is described below to get the amount that the player wants to wager on the game. Make sure to save the value that is returned from the getWager() function in a double variable.

Call the rollTheDice() function that is described below for the come-out roll (the first roll of the dice). Make sure to save the value that is returned from the rollTheDice() function in an integer variable.

If the game is immediately won, the game is over. Display a congratulatory message and how much money the player has won. Call the winner() function that is described below to determine if the game is immediately won. Make sure to pass the integer value that was returned from the rollTheDice() function to the winner() function.

If the game is immediately lost, the game is over. Display a message indicating the player has lost because they rolled craps and how much money the player has lost. Call the craps() function that is described below to determine if the game is immediately lost. Make sure to pass the integer value that was returned from the rollTheDice() function to the craps() function.

Otherwise, the game continues. Call the rollMore() function that is described below. Make sure to pass the integer value that was returned from the rollTheDice() function and the double value that was returned from the getWager() function to the rollMore() function.

The Functions

Write and use the following 5 functions in the program.

int rollTheDice()

This function simulates the rolling of the two dice. It takes no argument. It returns an integer: the sum of the two dice rolls.

This function should roll the dice by generating two random numbers between 1 and 6. The random numbers should be added together and then displayed along with the sum. Finally, return the sum of the dice.

bool winner( int roll )

This function determines if the game of craps was immediately won on the come-out roll. It takes one integer argument: the come-out roll. It returns a boolean value: true if the game was immediately won or false if the game was not immediately won.

If the come-out roll is equal to 7 or 11, return true because the game has been won. Otherwise, return false because the game has not been won.

bool craps( int roll )

This function determines if the game of craps was immediately lost on the come-out roll. It takes one integer argument: the come-out roll. It returns a boolean value: true if the game was immediately lost or false if the game was not immediately lost.

If the come-out roll is equal to 2, 3, or 12, return true because the game has been lost. Otherwise, return false because the game has not been lost.

void rollMore( int point, double wager )

This function continues the game of craps until it is won or lost. It takes two arguments: an integer that represents the point and a double that represents the amount that the user has wagered. It returns nothing.

The function should start by displaying the point.

Create a boolean variable and initialize it to a value of true to indicate that the game should continue.

In a loop that executes as long as the game should continue:

call the rollTheDice() function to roll the dice

if the dice roll is the same as the point, display a congratulatory message indicating the player has made their point and won the game. Calculate and display how much money the player has won. Finally, change the boolean variable that controls the loop to false to indicate the game should no longer continue.

otherwise, if the dice roll is 7, display a message that the player has lost the game. Display how much money the player has lost. Finally, change the variable that controls the loop to false to indicate the game should no longer continue.

double getWager()

This function gets the player's wager amount and makes sure that it's a valid amount. It takes no arguments. It returns a double: a valid wager amount.

The function should start by prompting the player to enter their wager amount. Like at a casino, there is a minimum wager ($5). Use a loop to check the player's wager amount and get a new amount if needed.

Also like at a casino, the wager amount cannot contain cents. So a wager amount of $5.25 should not be allowed. If the user adds cents to their wager amount, "give back" the cents to the user and use the remaining amount as the wager amount. So if the user tries to wager $5.25, the $0.25 should be "given back" and the wager amount adjusted to $5.

There are a number of ways to check for cents. One way is to take the original wager amount and subtract the integer portion of the wager amount. If the difference is greater than 0.00, the player added cents to the wager amount. To "give back" the cents, display the cents that are given back and update the wager amount so it's equal to just the integer portion of the original wager amount.

Finish the function by returning the wager amount.

Symbolic Constants

The program MUST use at least three symbolic constants. Some options are:

an integer for each of the values (2, 3, and 12) that represents craps on the first roll of the die
an integer that represents the value 7
an integer that represents the value 11
Program Requirements

As with the previous assignments and the assignments until the end of the semester, complete program documentation is required. For this assignment, that means that line documentation AND function documentation boxes are needed. Make sure that main() and any function that you write contains line documentation

Each function must have a documentation box detailing:

its name
its use or purpose: that is, what does it do? What service does it provide to the code that calls it?
a list of its arguments briefly describing the meaning and use of each
the value returned (if any) or none
notes on any unusual features, assumptions, techniques, etc.
/***************************************************************
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
***************************************************************/
See the documentation standards on the course webpage for more examples or if further clarification is needed. A program will not get full credit (even if it works correctly) if these standards are not followed

Make sure to actually use the symbolic constants that are created.

Be sure to #include <cstdlib> since the random number generator is being used in the program.

Make sure that the copy of the program that is handed in uses srand(34); to set the seed value for the random number generator.

Hand in a copy of the source code (CPP file) using Blackboard.

Output

Some runs of the program follow. Each one is marked with the srand value that produced the result.

Run 1 using srand(34); on Windows PC

What's your wager (no cents allowed) (minimum: 5.00)? 45.00

Roll: 6 + 4 = 10

The point is 10

Roll: 5 + 4 = 9
Roll: 5 + 3 = 8
Roll: 3 + 1 = 4
Roll: 5 + 1 = 6
Roll: 3 + 1 = 4
Roll: 3 + 5 = 8
Roll: 6 + 5 = 11
Roll: 1 + 2 = 3
Roll: 2 + 1 = 3
Roll: 6 + 6 = 12
Roll: 6 + 3 = 9
Roll: 4 + 2 = 6
Roll: 5 + 1 = 6
Roll: 3 + 5 = 8
Roll: 1 + 2 = 3
Roll: 6 + 3 = 9
Roll: 2 + 6 = 8
Roll: 6 + 1 = 7

Seven'd out! You lose!
You lost $45.00

Solutions

Expert Solution

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;

}


==============================================================


Related Solutions

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...
*****For C++ Program***** Overview For this assignment, write a program that uses functions to simulate a...
*****For C++ Program***** 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...
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...
Programming Language: C++ Overview For this assignment, write a program that will simulate a single game...
Programming Language: C++ Overview For this assignment, write a program that will simulate a single 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 is equal to 7 or 11, the player wins immediately....
Write a modular program to simulate the Game of Life and investigate the patterns produced by...
Write a modular program to simulate the Game of Life and investigate the patterns produced by various initial configurations. Some configurations die off rather rapidly; others repeat after a certain number of generations; others change shape and size and may move across the array; and still others may produce ‘gliders’ that detach themselves from the society and sail off into space! Since the game requires an array of cells that continually expands/shrinks, you would want to use multi-dimensional arrays and...
Write a MATLAB program to simulate the Stuck in the Mud game, The following link contains...
Write a MATLAB program to simulate the Stuck in the Mud game, The following link contains the detail game description: https://www.activityvillage.co.uk/stuck-in-the-mud , with additional features that can: • Use five (5) 6-sided dice to automatically play the Stuck in the Mud game against a player. • Greet the player when the game starts. • Let the player to choose the number of rounds to play. Take care of the user input to ensure the program will not crash with inputs...
Write a program with at least 2 functions that play the game of “guess the number”...
Write a program with at least 2 functions that play the game of “guess the number” as follows: Your program chooses the number to be guessed by selecting an integer at random in the range 1 to 1000. The program then displays the following: I have a number between 1 and 1000. Can you guess my number? Please type in your first guess. The player then types the first guess. The program then responds with one of the following: 1.      ...
c++ Write a program that displays the status of an order. a) Program uses 2 functions...
c++ Write a program that displays the status of an order. a) Program uses 2 functions (in addition to main ()). b) The first function asks the user for the data below and stores the input values in reference parameters. c) Input data from user: # of spools ordered, # of spools in stock, any special shipping & handling charges over and above the $10 rate. d) The second function receives as arguments any values needed to compute and display...
Task: Craps is a popular game played in casinos. Design a program using Raptor Flowcharts to...
Task: Craps is a popular game played in casinos. Design a program using Raptor Flowcharts to play a variation of the game, as follows: Roll two dice. Each dice has six faces representing values 1, 2, 3, 4, 5, and 6, respectively. Check the sum of the two dice. If the sum is 2, 3, or 12(called craps), you loose; if the sum is 7 or 11(called natural), you win; if the sum is another value(i.e., 4, 5, 6, 8,...
Ice Cream Program Assignment Write a program that uses a function to ask the user to...
Ice Cream Program Assignment Write a program that uses a function to ask the user to choose an ice cream flavor from a menu (see output below.) You must validate the users input for the flavor of ice cream accounting for both upper and lower-case letters. You must give them an appropriate error message and allow them to try again.   Once you have a valid flavor, your function will return the flavor back to the main() function.    Your main()...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT