summaryrefslogtreecommitdiff
path: root/dice/src/main/java/bjc/dicelang/neodice/Die.java
blob: 56e4327830849a847329a2690e313cf54f9e8e5a (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
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
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
 * 
 * @param <SideType> The type of value represented by this die.
 *
 */
@FunctionalInterface
public interface Die<SideType> {
	/**
	 * Rolls this die.
	 * 
	 * @param rng The source for random numbers
	 * 
	 * @return The result of rolling the die.
	 */
	public SideType 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<SideType> times(int numTimes) {
		return new ExpandDiePool<>(this, (die, rng) -> {
			return Stream.generate(() -> die.roll(rng))
					.limit(numTimes);
		});
	};
	
	/**
	 * Returns a die which will reroll this die as long as the provided condition is true.
	 * 
     * @param comparer The thing to use to compare die values.
	 * @param condition The condition to reroll the die on.
	 * 
	 * @return A die that rerolls when the given condition is met.
	 */
	default Die<SideType> reroll(
			Comparator<SideType> comparer,
			Predicate<SideType> condition) {
		return RerollDie.create(comparer, this, condition,
				(list) -> list.get(list.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 comparer The thing to use to compare die values.
	 * @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<SideType> reroll(
			Comparator<SideType> comparer,
			Predicate<SideType> condition,
			int limit) {
		return RerollDie.create(comparer, this, condition,
				(list) -> list.get(list.size()), limit);
	}
	
	/**
	 * Create an stream which gives rolls of this dice.
	 * 
	 * @param rng The source for random numbers.
	 * 
	 * @return An iterator which gives rolls of this dice.
	 */
	default Stream<SideType> stream(Random rng) {
		return Stream.generate(() -> this.roll(rng));
	}
	
	/**
	 * Create a new transforming die.
	 * 
	 * @param <NewType> The new type of the die.
	 * 
	 * @param mapper The function to use for mapping.
	 * 
	 * @return A die that transforms with the given mapping.
	 */
	default <NewType> Die<NewType> transform(Function<SideType, NewType> mapper) {
		return (rng) -> mapper.apply(this.roll(rng));
	}
	
	/**
	 * Create a simple polyhedral die with a fixed number of sides.
	 * 
	 * @param sides The number of sides for the die.
	 * 
	 * @return A die which returns a result from 1 to sides.
	 */
	static Die<Integer> polyhedral(int sides) {
		return new PolyhedralDie(sides);
	}
}

class PolyhedralDie implements Die<Integer> {
    private final int sides;

    public PolyhedralDie(int sides) {
        this.sides = sides;
    }

    @Override
    public Integer roll(Random rng) {
        // Dice are one-based, not zero-based.
        return rng.nextInt(sides) + 1;
    }

    @Override
    public String toString() {
        return String.format("d%d", sides);
    }
    
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + sides;        
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)                  return true;
        if (obj == null)                  return false;
        if (getClass() != obj.getClass()) return false;
        
        PolyhedralDie other = (PolyhedralDie) obj;
        
        if (sides != other.sides) return false;
        else                      return true;
    }
}

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<>(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<>(comparer, contained, condition, chooser, limit);
    }
}