Pre-lab homework. Turn in a flowchart OR pseudocode for the following complete program (parts 1 thru 4; you can save part 5 analysis for the lab itself). Your flowchart/pseudocode is to focus on the conceptually difficult parts of the programís control flow logic (it does NOT have to contain every detail such as all variable declarations). Reminder: All pre-lab homework is due at the beginning of the lab period, and late pre-lab homework will earn a grade of 0.
Executive Summary: You will build a
version of the dice game "craps". A detailed explanation of the game
comes later. First, however, a note on randomness and
computer programs. A key element of any game of chance is the random
nature of somthing like rolling the dice. How do we
introduce "randomness" into a computer program, which is structured
specifically to be "non-random"? Well, the standard library
cstdlib has a function
rand which generates
"random" numbers. The
rand function returns an integer between 0 and RAND_MAX (a
number defined in
cstdlib that is at least 32,767). The number
returned will at least appear to have been randomly chosen in the range (more
on that later).
Part 1 : "Let's Roll"
Write a program that simulates the roll of two six-sided die. The program should output the results of 5 rolls of the two die in the following format:
Player rolled 6 + 5 = 11
Player rolled 2 + 6 = 8
Player rolled 3 + 4 = 7
Player rolled 4 + 3 = 7
Player rolled 1 + 5 = 6
To use the rand function, you must include the
cstdlib library. The
How to do you transform a random number between 0 and RAND_MAX into a random number from 1 through 6? Use the modulus operator (%) : you figure out the rest!
Part 2 : "Why am I going to Jail?
Run your program from Part 1 three times
in succession. What do you note about the output? Nothing random about it! You
will be brought up on charges by the Gaming Commission if you implement a
"craps" game using this random number generator. The reason for this
repeating sequence is not accidental. In order to debug a program that uses a
RNG, it may be important to repeat the same sequence. Once the program is
debugged, we can "seed" our RNG to create a changing sequence. We use
srand to "seed"
rand. The idea is that
the particular sequence of seemingly random numbers
rand produces will
actually depend on the seed value - different seed values will produce wildly
Add to your code from Part 1 the following code:
cout << "Enter a seed value: ";
cin >> seed;
so that it appears as the first thing in "main". Execute your program three times using different seed values. Your results will be different for each new seed.
Execute your program three times using the same seed value. You will observe the same sequence! Here comes the Gaming Commission again! We do not want to have the user generate the "seed" because output can be predicted based on the seed value.
How can we seed our RNG without input from
the user and have unpredictable seed values? Let's use a function from the
ctime library. The
0) returns an integer that is the number of seconds ellapsed since 00:00 hours, Jan 1, 1970. This is a
continuously changing value that does not repeat, so it makes a great seed
value. modify your Part 1 solution by including the
ctime function and
adding the line
after "main". Run your program several times. You should no longer
notice any predictable patterns in the results. Note: Only seed the
random number generator once, typically at the beginning of a program. If you
seed it before every call to
rand, you'll be resetting the sequence constantly, and it
won't look very random.
Part 3 : Craps.
The rules for your game of craps are as follows: If the player rolls a 7 or 11 then the player wins. If the player rolls a 2, 3, or 12 the house wins. The player will continue to roll until either the player or the house wins. Your main program must use a function named "rollDice" (which you'll create) with the following prototype:
The function rollDice
will simulate a roll of two dice, compute the sum of the two dice, and return
that sum. Based on the result of rollDice,
main will do one of
three things: 1) Output "Player Wins!" 2) Output "House
Wins!" 3) output "No Winner : Roll
again". The program will continue to execute until either the player or
the house wins. Sample output:
Part 4 : "You're Fired!"
The owner of the casino, runs analysis on your craps program and sees the odds are stacked in favor of the player. He orders you to implement a version that favors the house. After showing your game to a math professor, the following rules are added to your game.
1. On the first roll, you win on 7 or 11, and you lose with 2, 3, or 12. Game over.
2. If the first roll is 4, 5, 6, 8, 9,or 10 then the number rolled becomes your setpoint. You then repeatedly roll the die until you either roll your setpoint for a win or you roll a 2, 3, 7, or 12 for a loss (7 is a loser for the player unless rolled on the first try).
Part 5 : "Show me the money"
Your program must make "the house" some money, so modify your program of part 4 to add the following functionality.
1. Ask the player if he wants to play again, and continue to play until the player wants to quit.
2. The default bet is $5. Track the number of wins for the player and the number of wins for the house. When the player quits, invoke a function (which you create) with the following prototype:
void showMoney(int pWins, int hWins);
The function accepts two integers (number of wins for player and house). It computes the amount of won or amount owed based on $5 per game and outputs a message telling the user "You won $XX! " or "Pay $XX to the cashier or your legs will be broken!"
GOING BEYOND: If you finish the above portions (requred for lab turn-in) before lab period is finished, attempt the following: