- 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.

- There are two possible points (victories) for one battle.
- The attacker rolls 3D6 (3 six-sided dice) and the defender rolls 2D6.
- 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 defender rolls.
- 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.

The probability of any outcome for any similar problem is the number of times the outcome occurs divided by the total number of possible results.

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.

count = 0; for (d = [1:6]) if (d==2 | d==5) count = count + 1; end end 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 (6^{2} = 36). For example, here's a
program that calculates the probability of a roll on 2D6 adding up
to 7:

count = 0; for (d1 = [1:6]) for (d2 = [1:6]) if (d1+d2 == 7) count = count + 1; end end end 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 6
^{5}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 vectors.
- 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 has happened. - 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 Outcome 2.
- 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.

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:

- Purpose

Describe the purpose of your program. - Approach

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?

- Issues

Describe any significant problems you encountered and how you solved them. - Results

Include the pie graph(s) in your results.