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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
|
package bjc.dicelang.neodice;
import java.util.*;
import java.util.function.*;
import java.util.stream.*;
/**
* Represents a pool of dice.
*
* @author Ben Culkin
*
*/
@FunctionalInterface
public interface DiePool<SideType> {
/**
* Roll each die in the pool, and return the results.
*
* Note that this list is not guaranteed to be the same size every time it
* is rolled, because there are some pool types that could add/remove dice.
*
* @param rng The source for random numbers
*
* @return The result of rolling each die in the pool.
*/
public Stream<SideType> roll(Random rng);
/**
* Gets the dice contained in this pool.
*
* Note that the length of this list may not be the same as the length of
* the list returned by roll, because certain pool types may add additional
* dice.
*
* Also note that this list (and the Die instances contained in it) should
* not be modified. That may work for certain pool types, but it isn't
* guaranteed to work, and can lead to unintuitive behavior. For instances,
* certain pool types may return an list where multiple elements of it refer
* to the same Die instance.
*
* The default implementation throws an UnsupportedOperationException.
*
* @return The dice contained in this pool.
*
* @throws UnsupportedOperationException If the composite dice can't be retrieved.
*/
default List<Die<SideType>> contained() {
throw new UnsupportedOperationException("Can't get composite dice");
}
/*
* These die pool operations transform this pool in some way.
*/
/**
* Returns a version of this die pool which returns its results in sorted
* order.
*
* @param isDescending True to sort in descending order, false to sort in ascending order.
*
* @return The die pool, which returns its results in sorted order.
*/
default DiePool<SideType> sorted(
Comparator<SideType> comparer,
boolean isDescending) {
return new TransformDiePool<>(this,
(pool) -> pool.sorted(
isDescending
? comparer.reversed()
: comparer));
}
/**
* Return a die pool which rolls this one, then filters out any results that
* don't match the provided predicate.
*
* @param matcher The predicate that determines
*
* @return A die pool which contains only entries that pass the predicate.
*/
default DiePool<SideType> filtered(Predicate<SideType> matcher) {
return new TransformDiePool<>(this,
(pool) -> pool.filter(matcher));
}
/**
* Return a die pool which rolls this one, then drops a number of the first values.
*
* @param number The number of first values to drop.
*
* @return A die pool which has the first entries dropped.
*/
default DiePool<SideType> dropFirst(int number) {
return new TransformDiePool<>(this,
(pool) -> pool.skip(number));
}
/**
* Return a die pool which rolls this one, then drops a number of the last values.
*
* @param number The number of last values to drop.
*
* @return A die pool which has the last entries dropped.
*/
default DiePool<SideType> dropLast(int number) {
return new TransformDiePool<>(this, (pool) -> {
Deque<SideType> temp = new ArrayDeque<>();
pool.forEachOrdered((die) -> temp.add(die));
for (int i = 0; i < number; i++) temp.pollLast();
return temp.stream();
});
}
/**
* Return a die pool which rolls this one, then keeps a number of the first values.
*
* @param number The number of first values to keep.
*
* @return A die pool which has the first entries kept.
*/
default DiePool<SideType> keepFirst(int number) {
return new TransformDiePool<>(this,
(pool) -> pool.limit(number));
}
/**
* Return a die pool which rolls this one, then keeps a number of the last values.
*
* @param number The number of last values to keep.
*
* @return A die pool which has the last entries kept.
*/
default DiePool<SideType> keepLast(int number) {
return new TransformDiePool<>(this, (pool) -> {
Deque<SideType> temp = new ArrayDeque<>();
pool.forEachOrdered((die) -> temp.add(die));
while (temp.size() > number) temp.pollFirst();
return temp.stream();
});
}
/*
* These die-pool operations are formed exclusively through other die pool
* operations.
*/
/**
* Return a die pool which rolls this one, then drops a number of the lowest values.
*
* @param number The number of lowest values to drop.
*
* @return A die pool which has the lowest entries dropped.
*/
default DiePool<SideType> dropLowest(Comparator<SideType> comparer, int number) {
return this.sorted(comparer, false).dropFirst(number);
}
/**
* Return a die pool which rolls this one, then drops a number of the lowest values.
*
* @param number The number of lowest values to drop.
*
* @return A die pool which has the lowest entries dropped.
*/
default DiePool<SideType> dropHighest(Comparator<SideType> comparer,int number) {
return this.sorted(comparer, false).dropLast(number);
}
/**
* Return a die pool which rolls this one, then keeps a number of the lowest values.
*
* @param number The number of lowest values to keep.
*
* @return A die pool which has the lowest entries kept.
*/
default DiePool<SideType> keepLowest(Comparator<SideType> comparer,int number) {
return this.sorted(comparer, false).keepFirst(number);
}
/**
* Return a die pool which rolls this one, then keeps a number of the highest values.
*
* @param number The number of highest values to keep.
*
* @return A die pool which has the highest entries kept.
*/
default DiePool<SideType> keepHighest(Comparator<SideType> comparer,int number) {
return this.sorted(comparer, false).keepLast(number);
}
/* These are misc. operations that don't form new dice pools. */
/**
* Get an iterator which iterates over a single roll of this die pool.
*
* @param rng The source of random numbers.
*
* @return An iterator over a single roll of this die pool.
*/
default Iterator<SideType> iterator(Random rng) {
return this.roll(rng).iterator();
}
/**
* Create a die pool containing the provided dice.
*
* @param <Side> The type of the sides.
*
* @param dice The dice to put into the pool.
*
* @return A pool which contains the provided dice.
*/
@SafeVarargs
static <Side> DiePool<Side> containing(Die<Side>... dice) {
return new FixedDiePool<>(dice);
}
/**
* Create an expanding die pool
*
* @param <Side> The type of the sides.
*
* @param contained The contained die.
* @param expander The expanding function.
*
* @return A die pool that expands the result given the provided function.
*/
static <Side> DiePool<Side> expanding(Die<Side> contained,
BiFunction<Die<Side>, Random, Stream<Side>> expander)
{
return new ExpandDiePool<>(contained, expander);
}
}
/**
* A die pool that can expand dice.
* @author Ben Culkin
*
* @param <SideType> The type the die uses.
*/
class ExpandDiePool<SideType> implements DiePool<SideType> {
private final Die<SideType> contained;
private final BiFunction<Die<SideType>, Random, Stream<SideType>> expander;
/**
* Create a new expanding die pool.
*
* @param contained The die to expand.
* @param expander The function to use for expanding.
*/
public ExpandDiePool(Die<SideType> contained,
BiFunction<Die<SideType>, Random, Stream<SideType>> expander) {
this.contained = contained;
this.expander = expander;
}
@Override
public Stream<SideType> roll(Random rng) {
return expander.apply(contained, rng);
}
@Override
public List<Die<SideType>> contained()
{
return Arrays.asList(contained);
}
}
/**
* A die pool that has a fixed size.
*
* @author Ben Culkin
*
* @param <SideType> The type of the sides of the dice.
*/
class FixedDiePool<SideType> implements DiePool<SideType> {
private final List<Die<SideType>> dice;
/**
* Create a new fixed dice pool.
* @param dice The dice to put into the pool.
*/
public FixedDiePool(List<Die<SideType>> dice) {
this.dice = dice;
}
/**
* Create a new fixed dice pool from an array of dice.
* @param dice The dice to put into the pool.
*/
@SafeVarargs
public FixedDiePool(Die<SideType>...dice) {
this.dice = new ArrayList<>(dice.length);
for (Die<SideType> die : dice) {
this.dice.add(die);
}
}
@Override
public Stream<SideType> roll(Random rng) {
return dice.stream().map((die) -> die.roll(rng));
}
@Override
public List<Die<SideType>> contained() {
return dice;
}
@Override
public String toString() {
return dice.stream()
.map(Die<SideType>::toString)
.collect(Collectors.joining(", "));
}
@Override
public int hashCode() {
return Objects.hash(dice);
}
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null) return false;
if (getClass() != obj.getClass()) return false;
FixedDiePool<?> other = (FixedDiePool<?>) obj;
return Objects.equals(dice, other.dice);
}
}
class TimesDiePool<SideType> implements DiePool<SideType> {
private final Die<SideType> contained;
private final int numDice;
public TimesDiePool(Die<SideType> contained, int numDice) {
this.contained = contained;
this.numDice = numDice;
}
@Override
public Stream<SideType> roll(Random rng) {
return Stream.generate(() -> contained.roll(rng))
.limit(numDice);
}
@Override
public List<Die<SideType>> contained() {
List<Die<SideType>> results = new ArrayList<>(numDice);
for (int index = 0; index < numDice; index++) {
results.add(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;
}
}
class TransformDiePool<SideType> implements DiePool<SideType> {
private final DiePool<SideType> contained;
private UnaryOperator<Stream<SideType>> transform;
public TransformDiePool(DiePool<SideType> contained,
UnaryOperator<Stream<SideType>> transform) {
super();
this.contained = contained;
this.transform = transform;
}
@Override
public Stream<SideType> roll(Random rng) {
return transform.apply(contained.roll(rng));
}
@Override
public List<Die<SideType>> contained() {
return contained.contained();
}
@Override
public int hashCode() {
return Objects.hash(contained, transform);
}
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null) return false;
if (getClass() != obj.getClass()) return false;
TransformDiePool<?> other = (TransformDiePool<?>) obj;
return Objects.equals(contained, other.contained)
&& Objects.equals(transform, other.transform);
}
@Override
public String toString() {
return contained.toString() + "(transformed)";
}
}
|