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

import java.util.function.LongPredicate;

/**
 * Create a die pool that will count successes/failures.
 * @author Ben Culkin
 *
 */
public class CountDieMod extends Die {
	/**
	 * The pool of dice that will be rolled.
	 */
	public final Die[] dice;

	/**
	 * The predicate for counting successes.
	 */
	public final LongPredicate success;

	/**
	 * The predicate for counting failures.
	 */
	public LongPredicate failure;

	/**
	 * Create a new counted die mod with a specified success criteria.
	 * 
	 * @param success The predicate for determining a success.
	 * @param dice The pool of dice to roll.
	 */
	public CountDieMod(LongPredicate success, Die... dice) {
		this(success, null, dice);
	}

	/**
	 * Create a new counted die mod with a specified success criteria.
	 * 
	 * @param success The predicate for determining a success.
	 * @param failure The predicate for determining a failure.
	 * @param dice The pool of dice to roll.
	 */
	public CountDieMod(LongPredicate success, LongPredicate failure, Die... dice) {
		super();

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

		this.dice    = dice;
	}

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

	@Override
	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 */

	@Override
	public boolean canOptimize() {
		return false;
	}

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