In: Computer Science
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.
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.
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 Program Logic
Seed the random number generator with a value of 22. 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 22.
Next, roll the dice by generating two random numbers between 1 and 6. The two numbers should be added together and then displayed along with the sum.
If the sum of the dice is equal to 7 or 11, the game is over and the player has won. Display a congratulatory message.
If the sum of the dice is equal to 2, 3, or 12, the game is over and the player has lost. Display a message indicating the player has lost because they rolled craps.
For any other sum, the sum is now the point and the game should continue until the user rolls the point again or rolls a 7. To do this:
Save the sum (the point) in a variable so it can be used for a later comparison
Display 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:
roll the dice and display the two values along with the sum
if the sum of the dice is the same as the point, display a congratulatory message indicating the player has made their point and they won the game. Also change the boolean variable that controls the loop to false to indicate the game should no longer continue.
otherwise, if the sum of the dice is 7, display a message that the player has lost the game and change the variable that controls the loop to false to indicate the game should no longer continue.
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
Include line documentation. There is no need to document every single line, but logical "chunks" of code should be preceded by a line or two that describes what the "chunk" of code does. This will be a part of every program that is submitted during the semester and this will be the last reminder in the program write-ups.
Make sure to actually use the symbolic constants that are created.
Be sure to #include
Make sure that the copy of the program that is handed in uses srand(22); 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(11); on Windows PC
Roll: 3 + 1 = 4 The point is 4 Roll: 5 + 4 = 9 Roll: 6 + 6 = 12
Roll: 1 + 2 = 3 Roll: 3 + 2 = 5 Roll: 3 + 2 = 5 Roll: 1 + 1 = 2
Roll: 6 + 1 = 7 Seven'd out! You lost!
Run 2 using srand(14); on Windows PC
Roll: 1 + 6 = 7 You won! Congratulations!
Run 3 using srand(22); on Windows PC
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! You won!
Run 4 using srand(1); on Windows PC
Roll: 6 + 6 = 12 Craps! You lost!
Run 1 using srand(11); on Mac
Roll: 6 + 5 = 11 You won! Congratulations!
Run 2 using srand(22); on Mac
Roll: 5 + 3 = 8 The point is 8 Roll: 5 + 5 = 10 Roll: 6 + 5 = 11
Roll: 6 + 1 = 7 Seven'd out! You lost!
Run 3 using srand(1); on Mac
Roll: 2 + 2 = 4 The point is 4 Roll: 6 + 3 = 9 Roll: 5 + 3 = 8
Roll: 1 + 3 = 4 The point was made! You won!
Run 4 using srand(5); on Mac
Roll: 6 + 6 = 12 Craps! You lost!
Extra Credit 1
For up to 5 points of extra credit, add code that will allow the user to wager that the game will be won.
Before the dice are rolled, the user should be prompted for how much they would like to wager on the game. This initial wager is known as the pass line bet. It's a wager that the shooter will win the game (ie. the initial roll is 7 or 11, or the shooter makes their point) (Note: the game of craps also allows the user to wager that the shooter will lose, but we'll leave that out of this implementation.)
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.
Like a casino, this implementation of craps will have a minimum wager. Use a value of $5. Make sure to check the user's wager amount and to prompt them for a new value if they enter an amount less than the minimum. This should continue until the user wagers a value greater than or equal to the minimum.
Also like a casino, the wager amount must not contain cents. So a wager of $5.25 should not be allowed. If the user adds cents to their wager amount, "return" 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 "returned" and the wager amount adjusted to $5. There are a number of ways to check for digits after the decimal point (cents). One way is to take the original wager amount and subtract the integer portion of the wager amount.
Note about extra credit: the points will ONLY be awarded if the required portions of the assignment work correctly. In other words, don't take short cuts in the rest of the program because it is assumed that 5 extra points will be awarded.
Extra Credit 1 Output 1 using srand(11); on Windows PC
How much would you like to wager (no cents allowed) (minimum:
5.00)? 10.00 Roll: 3 + 1 = 4 The point is 4 Roll: 5 + 4 = 9 Roll: 6
+ 6 = 12 Roll: 1 + 2 = 3 Roll: 3 + 2 = 5 Roll: 3 + 2 = 5 Roll: 1 +
1 = 2 Roll: 6 + 1 = 7 Seven'd out! You lost! You lost $10.00
Extra Credit 1 Output 2 using srand(22); on Windows PC
How much would you like to wager (no cents allowed) (minimum:
5.00)? 10.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! You won! You won $20.00
Extra Credit 1 Output 3 using srand(15); on Windows PC
How much would you like to wager (no cents allowed) (minimum:
5.00)? 2.50 You can't bet $2.50. The minimum bet is 5.00. Please
try again: 1.00 You can't bet $1.00. The minimum bet is 5.00.
Please try again: 5.36 You can have 0.36 back. The wager cannot
have cents. Your wager is now 5.00 Roll: 4 + 6 = 10 The point is 10
Roll: 3 + 5 = 8 Roll: 6 + 3 = 9 Roll: 1 + 1 = 2 Roll: 3 + 4 = 7
Seven'd out! You lost! You lost $5.00
Extra Credit 1 Output 1 using srand(1); on Mac
How much would you like to wager (no cents allowed) (minimum:
5.00)? 5.00 Roll: 2 + 2 = 4 The point is 4 Roll: 6 + 3 = 9 Roll: 5
+ 3 = 8 Roll: 1 + 3 = 4 The point was made! You won! You won
$10.00
Extra Credit 1 Output 2 using srand(5); on Mac
How much would you like to wager (no cents allowed) (minimum:
5.00)? 5 Roll: 6 + 6 = 12 Craps! You lost! You lost $5.00
Extra Credit 1 Output 3 using srand(22); on Mac
How much would you like to wager (no cents allowed) (minimum:
5.00)? 5.00 Roll: 5 + 3 = 8 The point is 8 Roll: 5 + 5 = 10 Roll: 6
+ 5 = 11 Roll: 6 + 1 = 7 Seven'd out! You lost! You lost $5.00
Extra Credit 2
For up to an additional 5 points of extra credit, add code that will allow the user to wager on odds for the pass line wager. This is an additional wager that if a point has been established, the shooter will make the point.
After a point has been established but before the dice are rolled to try to make the point, the user should be prompted for how much they would like to wager on the odds that the shooter will make the point.
Like extra credit 1, the minimum wager on odds is $5. Make sure to check the user's wager amount and to prompt them for a new value if they enter an amount less than the minimum. This should continue until the user wagers a value greater than or equal to the minimum.
Also make sure that the wager amount does not contain cents. If it does, "return" the cents and adjust the wager amount to remove the cents.
The payouts for wagering on the odds is based upon the point value. Points of 4 and 10 pay 2/1 (if $1 is bet, then the player wins $2 if the 4 or 10 is rolled). Points 5 and 9 pay 3/2 (if $2 is bet, then the player wins $3 if the 5 or 9 is rolled). Points 6 and 8 pay 6/5 (if $5 is bet, then the player wins $6 if the 6 or 8 is rolled).
If the point is made, the payout for wagering on the odds is added to the payout for wagering on the pass line (the value from extra credit 1).
Use the following formula to calculate the payout for wagering on odds:
wager amount + ( wager amount * odds )
For example, if the point is 8 and user wagered $15 on the odds, the user ends up with $33 for wagering on the odds.
wager amount + ( wager amount * odds ) = 15 + ( 15 * 6/5 ) = 15 + ( 90 / 5 ) = 15 + ( 18 ) = 33
If the user wagered $5 on the pass line, then the total payout is $43. $5 from pass line wager plus $5 for making the point plus $15 from wagering on the odds plus $18 for making the point with the odds wager.
Note about extra credit 2: the points will ONLY be awarded if the required portions of the assignment work correctly AND extra credit 1 works correctly.
Extra Credit 2 Output 1 using srand(22); on Windows PC
How much would you like to wager (no cents allowed) (minimum:
5.00)? 10.00 Roll: 3 + 1 = 4 The point is 4 How much would you like
to wager on the odds? 30.00 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! You won! Pass Line Win: $20.00 Odds Payout:
$90.00 You won $110.00
Extra Credit 2 Output 2 using srand(4); on Windows PC
How much would you like to wager (no cents allowed) (minimum:
5.00)? 5.00 Roll: 4 + 6 = 10 The point is 10 How much would you
like to wager on the odds? 1.25 You can't bet $1.25. The minimum
bet is 5.00. Please try again: 10.36 You can have 0.36 back. The
wager cannot have cents. Your wager is now 10.00 Roll: 4 + 3 = 7
Seven'd out! You lost! You lost $15.00
Extra Credit 2 Output 1 using srand(1); on Mac
How much would you like to wager (no cents allowed) (minimum:
5.00)? 5.00 Roll: 2 + 2 = 4 The point is 4 How much would you like
to wager on the odds? 10.00 Roll: 6 + 3 = 9 Roll: 5 + 3 = 8 Roll: 1
+ 3 = 4 The point was made! You won! Pass Line Win: $10.00 Odds
Payout: $30.00 You won $40.00
#include <iostream>
#include <cstdlib>
#include <iomanip>
using namespace std;
const int CRAPS1 = 2;
const int CRAPS2 = 3;
const int CRAPS3 = 12;
int main()
{
bool play = true;
int roll_1, roll_2, sum, point;
float wager, wager_odd;
float line_pass, odds;
srand(22);
cout << "How much would you like to wager (no cents
allowed) (minimum: 5.00)? ";
cin >> wager;
while(1)
{
if (wager < 5)
{
cout << " You can't bet $" << wager << ". The
minimum bet is 5.00. Please try again:";
cin >> wager;
}
else
{
int x = wager;
if ( wager - x != 0)
cout << " You can have "<< wager - x << " back.
The wager cannot have cents. Your wager is now " << x
<<".00";
wager = x;
break;
}
}
roll_1 = (rand() % 6) + 1;
roll_2 = (rand() % 6) + 1;
sum = roll_1 + roll_2;
cout << " Roll: " << roll_1 << " + " <<
roll_2 << " = " << sum;
if(sum == CRAPS1 || sum == CRAPS2 ||sum == CRAPS3)
{
cout << " Craps! You lost!";
cout << "You lost $" << setprecision(2) << fixed
<< wager;
return 0;
}
else if(sum == 7 || sum == 11)
{
cout << " You won! Congratulations! You won $" <<
setprecision(2) << fixed << 2 * wager;
return 0;
}
point = sum;
cout << " The point is " << point;
cout << " How much would you like to wager on the odds?
";
cin >> wager_odd;
while(1)
{
if (wager_odd < 5)
{
cout << " You can't bet $" << wager_odd << ". The
minimum bet is 5.00. Please try again:";
cin >> wager_odd;
}
else
{
int x = wager_odd;
if ( wager_odd - x != 0)
cout << " You can have "<< wager_odd - x << "
back. The wager cannot have cents. Your wager is now " << x
<<".00";
wager_odd = x;
break;
}
}
while(play)
{
roll_1 = (rand() % 6) + 1;
roll_2 = (rand() % 6) + 1;
sum = roll_1 + roll_2;
cout << " Roll: " << roll_1 << " + " << roll_2 << " = " << sum;
if (sum == point)
{
if ( point == 4 || point == 10)
odds = wager_odd + (wager_odd * 2) / 1;
else if ( point == 5 || point == 9)
odds = wager_odd + (wager_odd * 3) / 2;
else if ( point == 6 || point == 8)
odds = wager_odd + (wager_odd * 6) / 5;
line_pass = 2 * wager;
cout << " The point was made! You won!";
cout << " Pass Line Win: $" << setprecision(2) <<
fixed << line_pass;
cout << " Odds Payout: $" << setprecision(2) <<
fixed << odds;
cout << " You won $" << setprecision(2) << fixed
<< line_pass + odds;
play = false;
}
else if (sum == 7)
{
cout << " Seven'd out! You lost! You lost $" <<
setprecision(2) << fixed << wager + wager_odd;
play = false;
}
}
return 0;
}