CS1101C Lab 2 (Odd Week)
Let's Play Risk™!
The deadline for this lab question is Wednesday 02 March 2005,
23:59:59 hours.
The name of your C program file must be called risk.c,
files with any other name will not be marked.
Background
The game of Risk™ pits players against one another fighting
to conquer the world with their armies. When two armies attack each
other, they roll dice to determine the outcome.
Outcomes
For simplicity, we will assume that the attacker will always
roll three dice and the defender will always roll two
dice. All the dice are fair. There are three possible outcomes:
- Attacker scores 2, defender scores 0. Attacker wins.
- Attacker scores 1, defender scores 1. Draw result.
- Attacker scores 0, defender scores 2. Defender wins.
The outcome is determined by the following simple rules:
- Attacker rolls three dice. Only the two highest dice rolled are
taken into account.
- Defender rolls two dice. Both dice are taken into account.
- If the highest die rolled for the attacker is strictly greater than
the highest die rolled for the defender, the attacker scores 1, else the
defender scores 1. Note that the defender scores 1 if the two highest
dice show the same number.
- If the second highest die rolled for the attacker is strictly
greater than the second highest die rolled for the defender, the
attacker scores 1, else the defender scores 1. Note that the defender
scores 1 if the two second highest dice show the same number.
- Add up the scores for attacker and defender according to rules 3 and
4 above. Determine who wins according to the three possible outcomes
shown above.
Examples
Here are some examples:
- Attacker rolls 1 6 5, defender rolls 4 5. Attacker
wins both since 6 > 5 and 5 > 4 (attacker scores 2).
- Attacker rolls 5 2 4, defender rolls 6 5. Defender
wins both since 5 <= 6 and 4 <= 5 (defender scores
2).
- Attacker rolls 5 2 4, defender rolls 3 6. It's a
draw result since 5 <= 6 (defender scores 1) and 4 >
3 (attacker scores 1).
- Attacker rolls 6 1 5, defender rolls 5 6. Defender
wins both since 6 <= 6 and 5 <= 5 (defender scores
2).
- Attacker rolls 6 1 5, defender rolls 5 5. It's a
draw result since 6 > 5 (attacker scores 1) and 5 <=
5 (defender scores 1).
Text File
The text file risk1.txt contains an
unknown number of lines. Each line contains a random number seed
(unsigned integer) followed by the number of simulations to carry out
(integer). A sample risk1.txt file is given below (we will test
your program with other text files):
3565 1000
25392 5000
30894 10000
3737 50000
31413 100000
You may copy the file risk1.txt into your current directory by
typing the following cp (copy) command at the Unix prompt.
Highlight the entire line, right-click on the mouse and choose
“Copy”, then switching to your Secure Shell Window,
right-click on the mouse and choose “Paste”. Press Enter to
execute the command.
cp ~cs1101cl/www/lab2/oddweek/risk1.txt .
In the text file shown above, the first line indicates that the random
number seed is 3565 and after seeding with this random number
seed, we should carry out 1,000 simulations. The second line in
the text file indicates that the random number seed is 25392 and
after seeding with this random number seed, we should carry out
5,000 simulations. You may assume that the random number seed
will not exceed 32767 and the number of simulations will not exceed
1,000,000 (one million).
Simulation
Each simulation consists of tossing a fair die five times (three time
for the attacker and two times for the defender), then checking if the
outcome is a win for the attacker, a win for the defender, or a draw.
For each seed and number of simulations, count the total number of wins
for the attacker, the total number of wins for the defender, and the
total number of draws. Show also the percentage of each outcome.
Sample Run
The following shows a sample run, where $ indicates the Unix prompt. It
shows that for the first seed 3565 with 1000 simulations,
there were 351 successful attacks which occurred 35.100%
of the time, 302 successful defences which occurred 30.200%
of the time, and 347 draws which occurred 34.700% of the
time. Note that 351 + 302 + 347 = 1000. Print the percentages to
three decimal places.
You are reminded to follow the sample outcome exactly, else marks will
be deducted.
Hint: To print a % sign, use %% in your format string.
$ gcc -Wall risk.c -o risk
$ ./risk
Seed: 3565, number of simulations: 1000
Successful attacks: 351, 35.100%
Successful defences: 302, 30.200%
Draws: 347, 34.700%
Seed: 25392, number of simulations: 5000
Successful attacks: 1856, 37.120%
Successful defences: 1477, 29.540%
Draws: 1667, 33.340%
Seed: 30894, number of simulations: 10000
Successful attacks: 3657, 36.570%
Successful defences: 3010, 30.100%
Draws: 3333, 33.330%
Seed: 3737, number of simulations: 50000
Successful attacks: 18670, 37.340%
Successful defences: 14462, 28.924%
Draws: 16868, 33.736%
Seed: 31413, number of simulations: 100000
Successful attacks: 37158, 37.158%
Successful defences: 29237, 29.237%
Draws: 33605, 33.605%
$
Ponder
Do you think Risk™ is a fair game?
This document, index.html, has been accessed 18 times since 25-Jun-24 11:57:13 +08.
This is the 1st time it has been accessed today.
A total of 14 different hosts have accessed this document in the
last 445 days; your host, nsrp-source.comp.nus.edu.sg, has accessed it 3 times.
If you're interested, complete statistics for
this document are also available, including breakdowns by top-level
domain, host name, and date.