summaryrefslogtreecommitdiff
path: root/dice/src/main/java/bjc/dicelang/dicev2/CountDieMod.java
blob: 0b5ec5be88416157473851c5211c4cf2b321e157 (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
package bjc.dicelang.dicev2;

import java.util.function.LongPredicate;

public class CountDieMod extends Die {
	public final Die[] dice;

	public final LongPredicate success;

	public LongPredicate failure;

	public CountDieMod(LongPredicate success, Die... dice) {
		this(success, null, dice);
	}

	public CountDieMod(LongPredicate success, LongPredicate failure, Die... dice) {
		super();

		this.success = success;
		this.failure = failure;

		this.dice    = dice;
	}

	public long[] roll() {
		return new long[] { rollSingle() };
	}

	public long rollSingle() {
		long count = 0;

		for(Die die : dice) {
			for(long val : die.roll()) {
				if(success.test(val)) count += 1;

				if(failure != null && failure.test(val)) count -= 1;
			}
		}

		return count;
	}

	/* :UnoptimizableDice */

	public boolean canOptimize() {
		return false;
	}

	public long optimize() {
		throw new UnsupportedOperationException("Counted dice can't be optimized");
	}
}