summaryrefslogtreecommitdiff
path: root/BJC-Utils2/src/main/java/bjc/utils/dice/CompoundDiceExpression.java
blob: 4dd69265bef619372166615effce808920da8326 (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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package bjc.utils.dice;

/**
 * Implements a class for combining two dice with an operator
 * 
 * @author ben
 *
 */
public class CompoundDiceExpression implements IDiceExpression {
	/**
	 * The operator to use for combining the dice
	 */
	private DiceExpressionType	det;

	/**
	 * The dice on the left side of the expression
	 */
	private IDiceExpression		left;

	/**
	 * The dice on the right side of the expression
	 */
	private IDiceExpression		right;

	/**
	 * Create a new compound expression using the specified parameters
	 * 
	 * @param right
	 *            The die on the right side of the expression
	 * @param left
	 *            The die on the left side of the expression
	 * @param det
	 *            The operator to use for combining the dices
	 */
	public CompoundDiceExpression(IDiceExpression right,
			IDiceExpression left, DiceExpressionType det) {
		this.right = right;
		this.left = left;
		this.det = det;
	}

	@Override
	public int roll() {
		/*
		 * Handle each operator
		 */
		switch (det) {
			case ADD:
				return right.roll() + left.roll();
			case SUBTRACT:
				return right.roll() - left.roll();
			case MULTIPLY:
				return right.roll() * left.roll();
			case DIVIDE:
				/*
				 * Round to keep results as integers.
				 * We don't really have any need for floating-point dice
				 */
				return Math.round(right.roll() / left.roll());
			default:
				throw new IllegalStateException(
						"Got passed  a invalid ScalarExpressionType "
								+ det + ". WAT");

		}
	}
}