summaryrefslogtreecommitdiff
path: root/BJC-Utils2/src/main/java/bjc/utils/dice/Dice.java
blob: 84c2d0a2a8a2748577044145b40cf8d58fa35f72 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package bjc.utils.dice;

public class Dice implements DiceExpression {
	private Die	die;
	private int	nDice;

	public Dice(int nDce, Die de) {
		nDice = nDce;
		die = de;
	}

	public Dice(int nDce, int nSides) {
		this(nDce, new Die(nSides));
	}

	public int roll() {
		int res = 0;

		for (int i = 0; i < nDice; i++) {
			res += die.roll();
		}

		return res;
	}

	public static Dice fromString(String dice) {
		String[] strangs = dice.split("d");

		try {
			return new Dice(Integer.parseInt(strangs[0]),
					Integer.parseInt(strangs[1]));
		} catch (NumberFormatException nfex) {
			throw new IllegalStateException(
					"Attempted to create a dice using something that's not"
							+ " an integer: " + strangs[0] + " and "
							+ strangs[1] + " are likely culprits.s");
		}
	}
}