Write a program that runs 1000 games of craps and answers the following questions:
In craps, a player rolls a pair of dice. If the sum of the digits rolled on the two dice is 7 or 11, the player wins. If the sum of the digits rolled on the two dice is 2, 3, or 12, the player loses. If any other sum comes up on a roll of the dice, this sum is called the player's point. The player must then continue rolling the dice until he matches his point, in which case he wins, or until he rolls a 7, in which case he loses.
Implementation & Design Restriction
Your program must include the following four functions:
PlayGame
PRE: int numRolls – reference parameter provided by calling function to receive the value for the number of rolls of the dice it took to complete the game.
POST: A game of craps has been played. numRolls contains the number of rolls of the dice that it took to complete the game.
bool return value – if true the player has won the game, if false the player has lost the game.
RollDice
PRE: None
POST: The dice have been rolled. The function returns the sum of two randomly-generated numbers, each in the range 1 to 6
PrintResults.
PRE: int GamesWon[] – array containing the count of games won. Element at index 2 contains the number of games won in games that took 2 rolls to complete, index 3 contains the number of games won in games that took 3 rolls to complete,etc. index 21 contains the number of games won in games that took more than 20 rolls to complete.
int GamesLost[] – array containing the count of games lost. Element at index 2 contains the number of games lost in games that took 2 rolls to complete, index 3 contains the number of games lost in games that took 3 rolls to complete, etc. index 21 contains the number of games won in games that took more than 20 rolls to complete.
POST: This function displays a table with three columns
Rolls Wins Losses 1 223 104 2 72 112 3 62 68 4 46 52 5 27 42 6 23 31 7 13 25 8 10 21 9 7 16 10 9 5 11 3 7 12 3 6 13 2 2 14 2 1 15 0 1 16 0 0 17 0 1 18 0 1 19 1 1 20 0 1 20+ 0 0The number in the Rolls column is the number of rolls it to complete the game. The number in the number in the Wins column contains the number of games of that length that were won. The Losses column contains the number of games of that length that were lost.
CalculateAverage
PRE: int games[] – array containing the count of games won or lost. Element at index 2 contains the number of games won or lost in games that took 2 rolls to complete, index 3 contains the number of games won or lost in games that took 3 rolls to complete,etc. CalculateAverage ignores any information in the array in any position greater than 20.
POST: int return value contains the average length of a game in the games array.
You are invited to design and implement other functions if you wish. Every function must be documented with Pre- and Post-conditions (see documentation standard).
To help you experience the joys of reusable code, a PlayGame function is being provided for you to modify. This PlayGame function was used in another program and does most of what you need it to do but does not do everything you need it to do. It is intended to be a starting point for you. You will have to modify it to do the exact function described in this homework assignment.
/* PlayGame
* PRE: none
* POST: A game of craps has been played.
* bool return value – if true the player has won the game
* if false the player has lost the
game.
*/
bool PlayGame()
{
int sum; // holds the sum of the two digits rolled on two dice
int point; // match this value to win if the game is longer than one roll.
// Roll the dice. If the sum is
// - 7 or 11 They win!
// - 2 or 3 or 12 They lose!
// - other. This is the player’s point. Roll the dice again until:
// - they match the point. If so, They win!
// - they roll a 7. They lose!sum = RollDice();
switch (sum) {
case 7: case 11:
return true;
case 2: case 3: case 12:
return false;
default:
point = sum;
while (true) {
sum = RollDice();
if (sum == 7)
return false;
if (sum == point)
return true;
}
} // end switch
}
You should use a random number generator to simulate the roll of the dice. Use the following code as an example of how to adjust the value returned by the rand function so that it ends up being a value between 1 and 6
int die1 = 1 + rand() % 6;
Don’t forget to initialize the random number generator using the srand function before calling the rand function. The code to do this is the same as the code we used in homework 2 and is shown below. If you skip this step you will discover that rand() returns the same sequence of random numbers for every execution of the program – good when you’re debugging the program, bad when you want to incorporate an element of surprise in the playing of the game.
// Seed the random-number generator with current
time so that
// the first number will be different every time we start this program.
srand( (unsigned)time( NULL ) );
This program ran 1000 games of craps. Here are the results:
Rolls |
Wins |
Losses |
1 |
223 |
104 |
2 |
72 |
112 |
3
|
62
|
68
|
4 |
46 |
52 |
5 |
27 |
42 |
6 |
23 |
31 |
7 |
13 |
25 |
8 |
10 |
21 |
9 |
7 |
16 |
10 |
9 |
5 |
11 |
3 |
7 |
12 |
3 |
6 |
13 |
2 |
2 |
14 |
2 |
1 |
15 |
0 |
1 |
16 |
0 |
0 |
17 |
0 |
1 |
18 |
0 |
1 |
19 |
1 |
1 |
20 |
0 |
1 |
20+ |
0 |
0 |
The average length of a game was 2 rolls.
NOTE: your output must be formatted exactly as displayed in this sample execution.
iostream
allows you to use cin, cout, endlcstdlib
allows you to use srand and randctime
allows you to use timeiomanip
allows you to use setw
This program is more complex than homework1 and homework2. Remember to Think First, Code Second! Spend some time designing your solution before trying to code it. Also, make sure you allow yourself enough time for this assignment
Start simply. Code functions as stubs and test the main program, then implement each function one at a time and test it.
While developing the program, use cout statements to print out the numbers rolled on the dice so that you can debug it. Comment out the cout statements after debugging is complete.
You must turn in your source code, a script file showing successful compilation and a sample execution of your program, and your documentation file called README. The command to use to turn in your files for this assignment is
/cs/bin/turnin submit cs1005 hw4 hw4.cxx hw4.script README
This assignment is due at 11:59pm on Tuesday Feb 25th, 2003.
The grade for this assignment will be based on 100 points. Point values will be awarded as follows:
Documentation (30 points)
Internal documentation
introductory comment, meaningful identifiers, proper use of indentation and blank lines, comments for variables, algorithm comments. Don’t forget that every function must be documented with pre-conditions and post-conditions.
External documentation
completion and submission of the CS department standard documentation file, (see Documentation Guidelines ). Name your documentation file README.
Robustness (10 points)
Design the program so that it would be easy to make the following changes:
- Change the number of games of craps you use to calculate the statistics from 1000 to 10000.
- Determine how many games are won or lost on the first roll, second roll...thirtieth roll and after the thirtieth roll?
You don’t have to make these changes but we want to see a design that would allow each of these changes to be made by changing only one line of code for each change and recompiling.
Testing (10 points)
The sample execution(s) in your script file should demonstrate the program’s features and should be formatted exactly as described in the sample execution.
Functionality (30 points)
The program should behave as described. Your script file should show successful compilation. Programs must compile in order to receive points for Functionality.
Design (20 points)
We will be looking for a sound, modular design, and the proper use of parameters and local variables. The program must contain the four functions defined above.
For extra points, add code to your program to answer the following questions:
An extra 25 points will be awarded to anyone who implements these additional features successfully.
NOTE: These extra credit points will be added to the Homework portion of the final grade for the course.