Question

In: Computer Science

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 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

  1. 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

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

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

  4. 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.

  5. 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

Solutions

Expert Solution

#include <iostream>
/* Writ statement to include file stream header */
#include <cstdlib>
#include <ctime>

using std::cout;
using std::cerr;
using std::cin;
using std::ios;
using std::endl;
/* Write appropriate using statement(s) */

void playCraps();
void reviewStatistics();
int rollDice();

int main()
{
   int choice;
   
   // continue game unless user chooses to quit
   do {
   
      // offer game options
      cout << "Choose an option" << endl
           << "1. Play a game of craps" << endl
           << "2. Review cumulative craps statistics" << endl
           << "3. Quit program" << endl;
           
      cin >> choice;
        
      if ( choice == 1 )
         playCraps();
      else if ( choice == 2 )
         reviewStatistics();

   } while ( choice != 3 );
   
   return 0;

} // end main

// review cumulative craps statistics
void reviewStatistics()
{
   /* Write a body for reviewStatistics which displays
      the total number of wins, losses and die rolls recorded 
      in craps.dat */ 

} // end function reviewStatistics
     
// play game
void playCraps()
{
   enum Status { CONTINUE, WON, LOST };
   int sum; 
   int myPoint;
   int rollCount = 0;
   Status gameStatus;
   
   /* Write statement to create an output file stream */
   
   // seed random number generator and roll dice
   srand( time( 0 ) );
   sum = rollDice();
   rollCount++;
   
   // check game conditions
   switch( sum ) {
      case 7:
      case 11:
         gameStatus = WON;
         break;
      case 2:
      case 3:
      case 12:
         gameStatus = LOST;
         break;
      default:
         gameStatus = CONTINUE;
         myPoint = sum;
         cout << "Point is " << myPoint << endl;
         break;

   } // end switch
   
   // keep rolling until player matches point or loses
   while ( gameStatus == CONTINUE ) { 
      sum = rollDice();
      rollCount++;
      
      if ( sum == myPoint )
         gameStatus = WON;

      else
         if ( sum == 7 )
            gameStatus = LOST;

   } // end while
   
   //  display status message and write results to file
   if ( gameStatus == WON ) {
      cout << "Player wins\n" << endl;
      /* Write player WIN status and the total number of die 
         rolls to a file */                  

   } // end if
   else {
      cout << "Player loses\n" << endl;
      /* Write player LOSE status and the total number of die 
         rolls to a file */ 

   } // end else

} // end function playCraps

// dice rolling function
int rollDice()
{
   int die1;
   int die2;
   int workSum;
   
   // roll two dice
   die1 = 1 + rand() % 6;
   die2 = 1 + rand() % 6;
   
   // total and print results
   workSum = die1 + die2;
   cout << "Player rolled " << die1 << " + " << die2
        << " = " << workSum << endl;
        
   return workSum;

} // end function rollDice

public class Craps { 

   public static void main(String[] args) {
      int dice;  // initial roll
      dice = (int)(6.0*Math.random() + 1.0) +
             (int)(6.0*Math.random() + 1.0);

      if (dice == 2 || dice == 3 || dice == 12) {
         System.out.println("Immediate loss with: " + dice);
      }
      else if (dice == 7 || dice == 11) {
         System.out.println("Immediate win with: " + dice);
      }
      else { 
         int point = dice; // point: 4, 5, 6, 8, 9, or 10
         System.out.println("Point: " + point);
         while (true) {  // keep rolling
            dice = (int)(6.0*Math.random() + 1.0) +
                   (int)(6.0*Math.random() + 1.0);
            System.out.println("\nNew roll: " + dice);
            if (dice == point) {
               System.out.println("Made point, won");
               break;  // break out of loop, a win
            }
            if (dice == 7) {
               System.out.println("Lost with 7");
               break;  // break out of loop, a loss
            }
            else System.out.println("No help");
         }
      }
   }

Related Solutions

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...
*****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...
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 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...
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...
Overview For this assignment, write a program that will calculate the quiz average for a student...
Overview For this assignment, write a program that will calculate the quiz average for a student in the CSCI 240 course. The student's quiz information will be needed for later processing, so it will be stored in an array. For the assignment, declare one array that will hold a maximum of 12 integer elements (ie. the quiz scores). It is recommended that this program be written in two versions. The first version of the program will read a set of...
Overview For this assignment, write a program that will calculate the quiz average for a student...
Overview For this assignment, write a program that will calculate the quiz average for a student in the CSCI course. The student's quiz information will be needed for later processing, so it will be stored in an array. For the assignment, declare one array that will hold a maximum of 12 integer elements (ie. the quiz scores). It is recommended that this program be written in two versions. The first version of the program will read a set of quiz...
Overview For this assignment, write a program that will calculate the quiz average for a student...
Overview For this assignment, write a program that will calculate the quiz average for a student in the CSCI 240 course. The student's quiz information will be needed for later processing, so it will be stored in an array. For the assignment, declare one array that will hold a maximum of 12 integer elements (ie. the quiz scores). It is recommended that this program be written in two versions. The first version of the program will read a set of...
Overview For this assignment, write a program that will calculate the quiz average for a student...
Overview For this assignment, write a program that will calculate the quiz average for a student in the CSCI 240 course. The student's quiz information will be needed for later processing, so it will be stored in an array. For the assignment, declare one array that will hold a maximum of 12 integer elements (ie. the quiz scores). It is recommended that this program be written in two versions. The first version of the program will read a set of...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT