CS1101C Lab 3 (Odd Week)
Craps
The deadline for this lab question is Thursday 16 March 2006,
23:59:59 hours.
The name of your C program file must be called craps.c,
files with any other name will not be marked.
If you have not yet downloaded and compiled / run / understood the
programs from random.zip in the IVLE Workbin -> Lecture notes, do
so first. Else you will not be able to proceed with this lab.
Background
We are finally going to get a casino. Putting aside the ethical
arguments (this is a programming course; not an ethics course), we will
do a very simple simulation of the dice gambling game called
“craps”. Craps is played by rolling two fair six-sided dice
and looking at the sum of the two dice.
Suppose the player starts off with $1000. We assume that the
amount the player will bet for each round is always $1. At the end
of each round, the player can either win (he wins $1) or
lose (he loses $1).
One Round
At the beginning of each round, the player rolls the two dice.
- If the outcome is 7 or 11, the player immediately
wins and this round is over.
- If the outcome is 2, 3, or 12, the player
immediately loses and this round is over.
- If the outcome is any other number (4, 5, 6, 8, 9, 10), that
number becomes the point. The player then continues to roll the
two dice until he wins by rolling the point again, or
until he loses by rolling a 7. For example, if the player
initially rolls a 6, he keeps rolling until he wins by getting a
6 (the point), or he loses by rolling a 7. He continues to
roll as long as he rolls anything that is not 6 (the point), and
not 7, i.e. 2, 3, 4, 5, 8, 9, 10, 11, or 12. Once he wins or
loses, the round is over.
Simulation
We would like to see whether we would win or lose in the long term.
Suppose the original or starting amount is $1000, we will
simulate 1000 rounds. If the original amount is $n, we
will always simulate n rounds. The number of rounds is always the
same as the original amount of money. The bet amount is always
$1. At the end of the n rounds, we will display the amount
of money remaining, and the final amount of money as a percentage of the
starting amount of money. Then you can intelligently decide whether to
head for the casino when it finally opens right here in Singapore.
Implementation
You will be provided with an input file consisting of different
simulation scenarios. Each line in the input file gives the parameters
for a single simulation. Each simulation typically contains many
rounds (1000, 5000, or possibly more). A typical line from the
input file consist of two integers separated by blank spaces as shown
below:
24761 1000
The explanation for each number is given as follows:
24761 | Random number seed
|
1000 | Number of rounds / Original amount
|
This says that for this particular simulation, the random number seed is
24761 and the number of rounds is 1000 (the starting
amount of money is $1000). The random number
seed should be initialized to the value shown (by using the srand
function) at the beginning of each simulation.
Input File
Assume that the text input file is always called craps1.txt. This sample input file
contains data for seven simulations:
24761 1000
24762 5000
24763 10000
24764 50000
24765 100000
24766 500000
24767 1000000
We will test your program with other simulation data, so your program
must work for the general case, and not just only for the sample input
file given.
Sample Run
The following shows a sample run for the input file craps1.txt. Actual values for the
simulation results have been replaced with x’s (the number of
x’s does not signify the number of digits). Display the percentage
to three decimal places. To print a % sign in a printf statement, use
%%.
You are reminded to follow the sample outcome exactly, else marks will
be deducted.
Seed: 24761, starting amount: $1000
final amount: $xxxx, percentage: xxx.xxx%
Seed: 24762, starting amount: $5000
final amount: $xxxx, percentage: xxx.xxx%
Seed: 24763, starting amount: $10000
final amount: $xxxx, percentage: xxx.xxx%
Seed: 24764, starting amount: $50000
final amount: $xxxx, percentage: xxx.xxx%
Seed: 24765, starting amount: $100000
final amount: $xxxx, percentage: xxx.xxx%
Seed: 24766, starting amount: $500000
final amount: $xxxx, percentage: xxx.xxx%
Seed: 24767, starting amount: $1000000
final amount: $xxxx, percentage: xxx.xxx%
Take Note
- Assume that the text input file always exists and contains only
valid integers.
- Assume that all the numbers in the input file will never be greater
than 1,000,000 or smaller than 1.
- Do not use any arrays in your program.
Tips
- To print a % sign in a printf statement, use %%.
- This may be the most complicated program you have ever designed. Go
to the drawing board and plan your program. Think about the individual
modules you want using the divide-and-conquer strategy. Write and test
each module / function individually. Do not ever write the complete
program, and then start compiling / running / testing it. You will end
up with numerous compilation and logic errors which will take a long
time to fix. Remember how I (Raymond) showed in lecture that you should
practice incremental coding by incrementally coding parts of the
program, then testing each part out before moving on to other parts of
the program.
- You should probably not use the input file given when testing
your program for the first time; you should create your own input file
(by using the Unix command “vim craps1.txt”) with
manageable values and small number of simulations. When you are
confident that your program is working for small number of simulations,
increase it.
- You should use many printf statements to give more
information of what your program is doing exactly. For example, you
should probably show the result of each dice throw, the result from each
round, the amount of money you have after each round, etc. In summary,
you should print out as much debugging information as you need; then
before submission, comment out the debugging information.
Ponder
Do you win or lose money in the long run? How much do you expect to gain
/ lose?
This document, index.html, has been accessed 20 times since 25-Jun-24 11:57:13 +08.
This is the 1st time it has been accessed today.
A total of 12 different hosts have accessed this document in the
last 445 days; your host, nsrp-source.comp.nus.edu.sg, has accessed it 7 times.
If you're interested, complete statistics for
this document are also available, including breakdowns by top-level
domain, host name, and date.