## Wednesday, July 15, 2015

### RPG

Problem:

 TopCoder Problem Statement - RPG Single Round Match 171 Round 1 - Division II, Level One

Overview:

Determine the minimum, maximum, and average for a set of dice rolls.

Java Source:
```     1 public class RPG {
2
3  public int[] dieRolls(String[] dice) {
4
5         int min = 0;
6         int max = 0;
7
8         for (int d = 0; d < dice.length; d++)  {
9
10             /*
11             * Splits the string into the parts before and after the 'd'
12             */
13             String[] s = dice[d].split("d");
14
15             int rolls = Integer.parseInt(s[0]);
16             int die = Integer.parseInt(s[1]);
17
18             min += rolls;
19             max += (rolls * die);
20         }
21
22         int average = ((max - min) / 2) + min;
23
24         return new int[] {min, max, average};
25     }
26
27
28 }
```
Notes:

The first task is to parse each string in the array to get the size of the die and the number of rolls. This is easy enough using String.split(). By splitting on the "d" character, we'll get the number of rolls in position 0, and the size of the die in position 1 or the resulting array.

To get the minimum value, we just assume that the die always comes up one. So, minimum is just equal to the number of die rolls.

To get the maximum value, we assume that die always comes up with it's largest possible value. Here, we just multiple the number of rolls by the size of the die.

Then, we compute the average using the formula on line 22.

We these three values calculate, we just create an int[] to hold them, and return it.