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
|
package bjc.dicelang.neodice;
import java.util.*;
import java.util.function.*;
/**
* Represents a pool of dice.
*
* @author Ben Culkin
*
*/
@FunctionalInterface
public interface DiePool {
/**
* Roll each die in the pool, and return the results.
*
* Note that this array 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 int[] roll(Random rng);
/**
* Gets the dice contained in this pool.
*
* Note that the length of this array may not be the same as the length of
* the array returned by roll, because certain pool types may add additional
* dice.
*
* Also note that this array (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 array 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 Die[] contained() {
throw new UnsupportedOperationException("Can't get composite dice");
}
/**
* Returns a version of this die pool which returns its results in sorted
* order.
*
* At the moment, sorting in descending order is somewhat less efficent than
* sorting in ascending order, because Java doesn't provide a built-in
* descending sort for primitive arrays.
*
* @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 sorted(boolean isDescending) {
return new SortedDiePool(this, isDescending);
}
/**
* 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 filtered(IntPredicate matcher) {
return new FilteredDiePool(this, matcher);
}
/**
* 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<Integer> iterator(Random rng) {
return Arrays.stream(this.roll(rng)).iterator();
}
}
final class SortedDiePool implements DiePool {
private final boolean isDescending;
private final DiePool pool;
public SortedDiePool(DiePool pool, boolean isDescending) {
this.pool = pool;
this.isDescending = isDescending;
}
@Override
public int[] roll(Random rng) {
int[] rolls = pool.roll(rng);
Arrays.sort(rolls);
if (isDescending) {
int[] newRolls = new int[rolls.length];
int newIndex = newRolls.length;
for (int index = 0; index < rolls.length; index++) {
newRolls[newIndex--] = rolls[index];
}
return newRolls;
} else {
return rolls;
}
}
@Override
public String toString() {
return String.format("%s (sorted %s)", pool,
isDescending ? " descending" : "ascending");
}
@Override
public int hashCode() {
return Objects.hash(isDescending, pool);
}
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null) return false;
if (getClass() != obj.getClass()) return false;
SortedDiePool other = (SortedDiePool) obj;
return isDescending == other.isDescending
&& Objects.equals(pool, other.pool);
}
}
final class FilteredDiePool implements DiePool {
private final DiePool pool;
private final IntPredicate filter;
public FilteredDiePool(DiePool pool, IntPredicate filter) {
this.pool = pool;
this.filter = filter;
}
@Override
public int[] roll(Random rng) {
int[] rolls = pool.roll(rng);
return Arrays.stream(rolls).filter(filter).toArray();
}
// No toString, since there isn't any sensible to output the filter
@Override
public int hashCode() {
return Objects.hash(filter, pool);
}
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null) return false;
if (getClass() != obj.getClass()) return false;
FilteredDiePool other = (FilteredDiePool) obj;
return Objects.equals(filter, other.filter)
&& Objects.equals(pool, other.pool);
}
}
|