Pre-lab homework. Turn in a flowchart (either neatly handdrawn or using the RAPTOR tool) for the following complete program (parts 1 thru 4; you can save part 5 analysis for the lab itself). Your flowchart is to focus on the conceptually difficult parts of the program’s control flow logic. 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 librarycstdlib has a function called
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).
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 prototype for rand is:
int rand();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!
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 different sequences.
Add to your code from Part 1 the following code:
int seed; cout << "Enter a seed value: "; cin >> seed; srand(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 thectime library.
The function call time(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
srand(time(0));... immediately 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.
int rollDice();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:
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: