Project 4: Risk Dice
20 Points Total
- Calculate the probabilities of each possible outcome for
a "Risk"-style dice battle and graph the outcomes with a pie chart.
- Practice loops, conditionals, and graphs.
When you play the board game "Risk", you move armies around a map
of the world and conquer new territories in battles against
the other players. The battles are resolved by both sides rolling dice.
Depending on the situation, the outcome may be weighted towards one side
or the other, but due to the luck of the dice nothing is certain.
For this project we want to determine the probabilities of success,
failure, or somewhere in-between for a particular Risk battle,
then graph those probabilities on a pie chart.
Identify and Explore The Problem
Let us consider the typical case in Risk: one player is attacking with
three units, while the other player is defending with two units. Here
are the rules for that battle and the possible outcomes:
- There are two possible points (victories) for one battle.
- The attacker rolls 3D6 (3 six-sided dice) and the defender
- The highest die the attacker rolls is paired with the highest
die the defender rolls, and the next-highest die the attacker rolls
is paired with the second-highest die (the only one remaining) the
- For each pair of dice, if the attacker's die roll is higher than
the defender's die roll, the attacker scores a point. Otherwise,
the defender's die is higher than or equal to the attacker's and the
defender scores a point.
Write a MATLAB program that calculates the probabilities of the following
outcomes the the scenario described above:
- Outcome 1: Attacker wins both victories.
- Outcome 2: Attacker and defender each win one victory.
- Outcome 3: Defender wins both victories.
The results should be displayed in a pie chart that shows the
probability percentage of each outcome.
Create a Mathematical Model
Consider: the number of possible results of rolling a single D6
is 6. The probability of rolling a 1 through 6 on a D6 is 6/6, or
1.0 (100%). The probability of rolling a 1 or a 5 is 2/6, or 0.33
The probability of any outcome for any similar problem is the number
of times the outcome occurs divided by the total number of possible
Instead of using statistics formulas or random numbers to calculate
the answer for the Risk dice, we can use the power of the computer to
iterate through each possible result that can be rolled on 5D6, see which
of the three outcomes that particular result falls into, and then divide
each result by the total number of possible results.
Identify and Implement a Computational Method for Solving the Model
A single loop is sufficient to obtain each possible result of
rolling a single die. For example, here is a MATLAB program that will
calculate the probability of a 2 or a 5 being rolled on a D6:
count = 0;
for (d = [1:6])
if (d==2 | d==5)
count = count + 1;
probability = count / 6
Two loops would be required to try every possible combination
of rolling two dice. For each value 1..6 that the first die could
roll, the second die could roll 1..6 as well, giving 36 possible
combinations (62 = 36). For example, here's a
program that calculates the probability of a roll on 2D6 adding up
count = 0;
for (d1 = [1:6])
for (d2 = [1:6])
if (d1+d2 == 7)
count = count + 1;
probability = count / 36
We want to look at every possible combination of 5 dice being rolled.
Here's an algorithm that will work:
- Look at every possible combination of 3D6 attack dice with
2D6 defense dice. Hint: make 5 nested loops, each looping through
values 1..6. There should be loops for "a1", "a2", and "a3" as well
as "d1" and "d2". Inside the innermost loop, those five values will
represent one of the 65 combinations of 5D6.
- For the current combination, sort the die values for the 3D6 into
order and the die values for the 2D6 into order so that we can pick out
the highest two of each. Hint: make a vector out of the values you want
to sort and use the "sort" function; e.g. "attacker = sort([a1 a2 a3]);"
would set "attacker" to be a vector containing the values of a1, a2, and
a3 arranged in order from lowest to highest.
- Pick out the first and second highest for both the attacker
and the defender. These will be the values at the ends of the sorted
- If the first highest attack die is greater than the first highest
defense die and the second highest attack die is greater than the
second highest defense die, increase the count of how many times Outcome 1
- If both of the attacker's highest dice are less than or equal to the
defender's corresponding dice, add one to the number of
times Outcome 3 has happened.
- If both the previous conditions are false, add one to the count of
- Display the number of outcomes 1, 2, and 3 in a pie chart.
Give it an appropriate title and a legend that identifies
each pie slice as either "Attacker Wins 2", "Defender Wins 2",
or "Both Win 1".
Implement this algorithm as an M-File.
Extra Credit (up to +8 points)
For extra credit, calculate the information for and display the following
additional graphs. The highest dice on each side are paired up each time,
and the number of possible victories is limited by the side with the fewest dice.
Expand your original program to calculate data for all six graphs (the
original scenario plus the five below). Have the graphs display on a
single figure - use the subplot command to make three rows of two columns
of results. Give each graph an appropriate label and legend.
- Three attack dice versus one defense die (two possible outcomes: attacker
wins 1 or defender wins 1).
- Two attack dice versus two defense dice (three possible outcomes, as in the original scenario above).
- Two attack dice versus one defense die (two possible outcomes).
- One attack die versus two defense die (two possible outcomes).
- One attack die versus one defense die (two possible outcomes).
Create a report consisting of:
- a cover page,
- a project overview (with results),
- and printouts of all the M-files.
The project overview should contain the following information:
Finally, include a printout of your M-file.
Describe the purpose of your program.
Describe how you handled the different challenges that arose. Cover the following in particular - your answers can be brief, but be thorough.
- How many loops did you have? They were nested? Why?
- What did you do with all the loop counter values in the innermost loop?
- Did you have some outcome counter variables? How many?
- How did you come up with the values you used to convert your outcome counters into probabilities?
Describe any significant problems you encountered and how you solved them.
Include the pie graph(s) in your results.