summaryrefslogtreecommitdiff
path: root/base/src/bjc/dicelang/dice/MathDie.java
blob: e4f295352ba0c6808c079a6c439cbf71842fd43f (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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package bjc.dicelang.dice;

/**
 * A die that represents two dice with an applied math operator.
 *
 * @author EVE
 *
 */
public class MathDie implements Die {
	/* 
	 * @TODO 10/08/17 Ben Culkin :MathGeneralize
	 * 	Why do we have the operator types hardcoded, instead of just
	 * 	having a general thing for applying a binary operator to dice?
	 * 	Fix this by changing it to the more general form.
	 */
	/**
	 * The types of a math operator.
	 *
	 * @author EVE
	 *
	 */
	public static enum MathOp {
		/** Add two dice. */
		ADD,
		/** Subtract two dice. */
		SUBTRACT,
		/** Multiply two dice. */
		MULTIPLY;

		@Override
		public String toString() {
			switch (this) {
			case ADD:
				return "+";

			case SUBTRACT:
				return "-";

			case MULTIPLY:
				return "*";

			default:
				return this.name();
			}
		}
	}

	private final MathDie.MathOp type;

	private final Die       left;
	private final Die       right;

	/**
	 * Create a new math die.
	 *
	 * @param op
	 *                The operator to apply.
	 *
	 * @param lft
	 *                The left operand.
	 *
	 * @param rght
	 *                The right operand.
	 */
	public MathDie(final MathDie.MathOp op, final Die lft, final Die rght) {
		type = op;

		left = lft;
		right = rght;
	}

	@Override
	public boolean canOptimize() {
		return left.canOptimize() && right.canOptimize();
	}

	private long performOp(final long lft, final long rght) {
		switch (type) {
		case ADD:
			return lft + rght;

		case SUBTRACT:
			return lft - rght;

		case MULTIPLY:
			return lft * rght;

		default:
			return 0;
		}
	}

	@Override
	public long optimize() {
		final long lft = left.optimize();
		final long rght = right.optimize();

		return performOp(lft, rght);
	}

	@Override
	public long roll() {
		final long lft = left.roll();
		final long rght = right.roll();

		return performOp(lft, rght);
	}

	@Override
	public long rollSingle() {
		final long lft = left.rollSingle();
		final long rght = right.rollSingle();

		return performOp(lft, rght);
	}

	@Override
	public String toString() {
		return left.toString() + " " + type.toString() + " " + right.toString();
	}
}