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
|
package bjc.dicelang.neodice.die;
import java.util.*;
import java.util.function.*;
import bjc.dicelang.neodice.*;
import bjc.esodata.*;
public class RerollDie<SideType> implements Die<SideType> {
private final Die<SideType> contained;
private final Predicate<SideType> condition;
private final Function<MinMaxList<SideType>, SideType> chooser;
private final Comparator<SideType> comparer;
private int limit = Integer.MAX_VALUE;
private RerollDie(
Comparator<SideType> comparer,
Die<SideType> contained,
Predicate<SideType> condition,
Function<MinMaxList<SideType>, SideType> chooser) {
this.comparer = comparer;
this.contained = contained;
this.condition = condition;
this.chooser = chooser;
}
private RerollDie(
Comparator<SideType> comparer,
Die<SideType> contained,
Predicate<SideType> condition,
Function<MinMaxList<SideType>, SideType> chooser,
int limit) {
this(comparer, contained, condition, chooser);
this.limit = limit;
}
@Override
public SideType roll(Random rng) {
SideType roll = contained.roll(rng);
MinMaxList<SideType> newRolls = new MinMaxList<>(comparer, roll);
int rerollCount = 0;
while (condition.test(roll) && rerollCount < limit) {
roll = contained.roll(rng);
newRolls.add(roll);
rerollCount += 1;
}
return chooser.apply(newRolls);
}
public static <Side extends Comparable<Side>> Die<Side> create(
Die<Side> contained,
Predicate<Side> condition,
Function<MinMaxList<Side>, Side> chooser) {
return new RerollDie<>(Comparator.naturalOrder(), contained, condition, chooser);
}
public static <Side extends Comparable<Side>> Die<Side> create(
Die<Side> contained,
Predicate<Side> condition,
Function<MinMaxList<Side>, Side> chooser,
int limit) {
return new RerollDie<>(Comparator.naturalOrder(), contained, condition, chooser, limit);
}
public static <Side> Die<Side> create(
Comparator<Side> comparer,
Die<Side> contained,
Predicate<Side> condition,
Function<MinMaxList<Side>, Side> chooser) {
return new RerollDie<Side>(comparer, contained, condition, chooser);
}
public static <Side> Die<Side> create(
Comparator<Side> comparer,
Die<Side> contained,
Predicate<Side> condition,
Function<MinMaxList<Side>, Side> chooser,
int limit) {
return new RerollDie<Side>(comparer, contained, condition, chooser, limit);
}
}
|