In: Computer Science
*****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 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:
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:
/*************************************************************** 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 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
Run 2 using srand(6); on Windows PC
What's your wager (no cents allowed) (minimum: 5.00)? 15 Roll: 5 + 2 = 7 Winner! Winner! Congratulations! You won $30.00
Run 3 using srand(21); on Windows PC
What's your wager (no cents allowed) (minimum: 5.00)? 1.23 You can't bet $1.23. The minimum bet is 5.00. Please try again: 4.56 You can't bet $4.56. The minimum bet is 5.00. Please try again: 7.89 You can have $0.89 back. The wager cannot have cents. Your wager is now 7.00 Roll: 6 + 5 = 11 Winner! Winner! Congratulations! You won $14.00
Run 4 using srand(36); on Windows PC
What's your wager (no cents allowed) (minimum: 5.00)? 10.00 Roll: 1 + 1 = 2 Craps! You lose! You lost $10.00
Run 5 using srand(5); on Windows PC
What's your wager (no cents allowed) (minimum: 5.00)? 25 Roll: 1 + 2 = 3 Craps! You lose! You lost $25.00
Run 6 using srand(1); on Windows PC
What's your wager (no cents allowed) (minimum: 5.00)? 20 Roll: 6 + 6 = 12 Craps! You lose! You lost $20.00
Run 7 using srand(22); on Windows PC
What's your wager (no cents allowed) (minimum: 5.00)? 75.00 Roll: 3 + 1 = 4 The point is 4 Roll: 5 + 1 = 6 Roll: 4 + 4 = 8 Roll: 6 + 6 = 12 Roll: 4 + 4 = 8 Roll: 2 + 1 = 3 Roll: 5 + 1 = 6 Roll: 2 + 4 = 6 Roll: 3 + 5 = 8 Roll: 5 + 3 = 8 Roll: 2 + 1 = 3 Roll: 2 + 2 = 4 The point was made. Winner! You won $150.00
#include <iostream>
#include <bits/stdc++.h>
#include<stdlib.h>
using namespace std;
/*Function: double getWager()
Description: This function gets the player's wager amount and makes sure that it's a valid amount.
Arguments: No arguments passed
Returns: A double value representing the wager amount*/
double getWager()
{
double wager,d;
/*The loop continues until and unless the user gives a valid wager amount*/
do
{
cout<<"What's your wager (no cents allowed) (minimum: 5.00) ? ";
cin>>wager;
d=wager-(int)wager ;
if(d > 0 && wager>5)
{
wager=wager-d;
cout<<d<<" cents is given back."<<endl<<" Your final wager amount is : "<<wager<<endl;
}
} while(wager<5);
return wager;
}
/*Function: int rollTheDice()
Description: This function simulates the rolling of the two dice.
Arguments: No arguments passed
Returns: An integer value representing the sum of two dice rolls*/
int rollTheDice()
{
int n1,n2,roll;
n1=1 + rand()%6;
n2=1 + rand()%6;
roll=n1+n2;
cout<<" Roll :\t"<<n1<<" + "<<n2<<" = "<<roll<<endl;
return roll;
}
/*Function: bool winner(int roll)
Description: This function determines if the game of craps was immediately won on the come-out roll.
Arguments: point - an integer that represents the number that needs to
be rolled by the player to win the game
Returns: A boolean value : true if the game was immediately won or false if the game was not immediately won.*/
bool winner(int roll)
{
if(roll==7 || roll==11)
return true;
else
return false;
}
/*Function: bool craps(int roll)
Description: This function determines if the game of craps was immediately lost on the come-out roll.
Arguments: point - an integer that represents the number that needs to
be rolled by the player to win the game
Returns: A boolean value : true if the game was immediately lost or false if the game was not immediately lost.*/
bool craps(int roll)
{
if(roll==2 || roll==3 || roll==12)
return true;
else
return false;
}
/*Function: void rollMore( int point, double wager )
Description: 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*/
void rollMore(int point, double wager)
{
int sum;
cout<<" The point is : "<<point<<endl;
bool flag=true;
/*The loop continues until the sum is equal to point or sum is equal to 7*/
do
{
sum=rollTheDice();
if(sum==point)
{
cout<<"The point was made. Winner!"<<endl<<"You won $"<<2*wager;
flag=false;
}
else if(sum==7)
{
cout<<"Seven'd out! You lose!"<<endl<<"You lost $"<<wager;
flag=false;
}
}while(flag==true);
}
int main()
{
double wager;
int roll;
bool result;
wager=getWager(); //Calling the getWager() function
srand(time(0));
roll=rollTheDice(); //Calling the rollTheDice() function
result=winner(roll); //Calling the winner() function
if(result==true)
{
cout<<"Winner! Winner! Congratulations!"<<endl<<"You've won $"<<2*wager<<endl;
return 0;
}
result=craps(roll); //Calling the craps function
if(result==true)
{
cout<<"Craps! You lose!"<<endl<<"You lost $"<<wager<<endl;
return 0;
}
rollMore(roll,wager); //Calling the rollMore() function
return 0;
}
The ouput when srand(34):
PLEASE LIKE THE ANSWER IF YOU FIND IT HELPFUL OR YOU CAN COMMENT IF YOU NEED CLARITY / EXPLANATION ON ANY POINT.