summaryrefslogtreecommitdiff
path: root/dice/src/main/java/bjc/dicelang/neodice/Die.java
blob: a986ca2fae83d57c59f78ece7ca16b860f499b97 (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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package bjc.dicelang.neodice;

import java.util.*;
import java.util.function.*;
import java.util.stream.*;

import bjc.esodata.*;

/**
 * Represents a single polyhedral die.
 * @author Ben Culkin
 *
 */
@FunctionalInterface
public interface Die {
	/**
	 * Rolls this die.
	 * 
	 * @param rng The source for random numbers
	 * 
	 * @return The result of rolling the die.
	 */
	public int roll(Random rng);
	
	/**
	 * Returns a die pool which rolls this die the specified number of times.
	 * 
	 * @param numTimes The number of times to roll this die.
	 * 
	 * @return A die pool that rolls this die the specified number of times.
	 */
	default DiePool times(int numTimes) {
		return new TimesDiePool(this, numTimes);
	};
	
	/**
	 * Returns a die which will reroll this die as long as the provided condition is true.
	 * 
	 * @param condition The condition to reroll the die on.
	 * 
	 * @return A die that rerolls when the given condition is met.
	 */
	default Die reroll(IntPredicate condition) {
		return new RerollDie(this, condition, (lst) -> lst.get(lst.size()));
	}
	
	/**
	 * Returns a die which will reroll this die up to a specified number of times,
	 * as long as the provided condition is true.
	 * 
	 * @param condition The condition to reroll the die on.
	 * @param limit The maximum number of times to reroll the die.
	 * 
	 * @return A die that rerolls when the given condition is met.
	 */
	default Die reroll(IntPredicate condition, int limit) {
		return new RerollDie(this, condition, (lst) -> lst.get(lst.size()), limit);
	}
	
	/**
	 * Create an iterator which gives rolls of this dice.
	 * 
	 * @param rng The source for random numbers.
	 * 
	 * @return An iterator which gives rolls of this dice.
	 */
	default Iterator<Integer> iterator(Random rng) {
		return IntStream.generate(() -> this.roll(rng)).iterator();
	}
}

final class TimesDiePool implements DiePool {
	private final Die contained;
	private final int numDice;

	public TimesDiePool(Die contained, int numDice) {
		this.contained = contained;
		this.numDice = numDice;
	}

	@Override
	public int[] roll(Random rng) {
		int[] results = new int[numDice];
		
		for (int index = 0; index < numDice; index++) {
			results[index] = contained.roll(rng);
		}
		
		return results;
	}
	
	@Override
	public Die[] contained() {
		Die[] results = new Die[numDice];
		
		for (int index = 0; index < numDice; index++) {
			results[index] = contained;
		}
		
		return results;
	}

	@Override
	public String toString() {
		return String.format("%d%s", numDice, contained);
	}
	
	@Override
	public int hashCode() {
		return Objects.hash(contained, numDice);
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)                  return true;
		if (obj == null)                  return false;
		if (getClass() != obj.getClass()) return false;
		
		TimesDiePool other = (TimesDiePool) obj;
	
		return Objects.equals(contained, other.contained) && numDice == other.numDice;
	}
}

final class RerollDie implements Die {
	private final Die          contained;
	
	private final IntPredicate                           condition;
	private final Function<MinMaxList<Integer>, Integer> chooser;
	
	private int limit = Integer.MAX_VALUE;
	
	
	public RerollDie(Die contained, IntPredicate condition,
			Function<MinMaxList<Integer>, Integer> chooser) {
		this.contained = contained;
		
		this.condition = condition;
		this.chooser   = chooser;
	}
	
	public RerollDie(Die contained, IntPredicate condition,
			Function<MinMaxList<Integer>, Integer> chooser, int limit) {
		this.contained = contained;
		
		this.condition = condition;
		this.chooser   = chooser;
		
		this.limit = limit;
	}
	
	@Override
	public int roll(Random rng) {
		int roll = contained.roll(rng);
		
		MinMaxList<Integer> newRolls = new MinMaxList<Integer>(
				Comparator.naturalOrder(), roll);
		
		int rerollCount = 0;
		while (condition.test(roll) && rerollCount < limit) {
			roll = contained.roll(rng);
			newRolls.add(roll);
			
			rerollCount += 1;
		}
		
		return chooser.apply(newRolls);
	}

	// No toString, because IntPredicate can't be converted to a string
}