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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
|
package bjc.dicelang.v2;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;
import java.util.function.Predicate;
import java.util.regex.Pattern;
import static bjc.dicelang.v2.Errors.ErrorKey.*;
public class DiceBox {
private static final Random rng = new Random();
public interface Die {
boolean canOptimize();
long optimize();
long roll();
long rollSingle();
}
public interface DieList {
boolean canOptimize();
long[] optimize();
long[] roll();
}
public static class DieExpression {
public final boolean isList;
public Die scalar;
public DieList list;
public DieExpression(Die scal) {
isList = false;
scalar = scal;
}
public DieExpression(DieList lst) {
isList = true;
list = lst;
}
public String toString() {
if(isList) return list.toString();
else return scalar.toString();
}
public String value() {
if(isList) return Arrays.toString(list.roll());
else return Long.toString(scalar.roll());
}
}
public static class ScalarDie implements Die {
private long val;
public ScalarDie(long vl) {
val = vl;
}
public boolean canOptimize() {
return true;
}
public long optimize() {
return val;
}
public long roll() {
return val;
}
public long rollSingle() {
return val;
}
public String toString() {
return Long.toString(val);
}
}
public static class SimpleDie implements Die {
private Die numDice;
private Die diceSize;
public SimpleDie(long nDice, long size) {
numDice = new ScalarDie(nDice);
diceSize = new ScalarDie(size);
}
public SimpleDie(Die nDice, long size) {
numDice = nDice;
diceSize = new ScalarDie(size);
}
public SimpleDie(long nDice, Die size) {
numDice = new ScalarDie(nDice);
diceSize = size;
}
public SimpleDie(Die nDice, Die size) {
numDice = nDice;
diceSize = size;
}
public boolean canOptimize() {
if(diceSize.canOptimize() && diceSize.optimize() == 1) return numDice.canOptimize();
else return false;
}
public long optimize() {
return numDice.optimize();
}
public long roll() {
long total = 0;
long nDice = numDice.roll();
long dSize = diceSize.roll();
for(int i = 0; i < nDice; i++) {
total += (Math.abs(rng.nextLong()) % dSize) + 1;
}
return total;
}
public long rollSingle() {
return (Math.abs(rng.nextLong()) % diceSize.roll()) + 1;
}
public String toString() {
return numDice + "d" + diceSize;
}
}
public static class FudgeDie implements Die {
private Die numDice;
public FudgeDie(long nDice) {
numDice = new ScalarDie(nDice);
}
public boolean canOptimize() {
return false;
}
public long optimize() {
return 0;
}
public long roll() {
long nDice = numDice.roll();
long res = 0;
for(int i = 0; i < nDice; i++) {
res += rollSingle();
}
return res;
}
public long rollSingle() {
switch(rng.nextInt(3)) {
case 0:
return -1;
case 1:
return 0;
case 2:
return 1;
default:
return 0;
}
}
public String toString() {
return numDice + "dF";
}
}
public static class CompoundDie implements Die {
private Die left;
private Die right;
public CompoundDie(Die lft, Die rght) {
left = lft;
right = rght;
}
public boolean canOptimize() {
return left.canOptimize() && right.canOptimize();
}
public long optimize() {
return Long.parseLong(left.optimize() + "" + right.optimize());
}
public long roll() {
return Long.parseLong(left.roll() + "" + right.roll());
}
public long rollSingle() {
return roll();
}
public String toString() {
return left.toString() + "c" + right.toString();
}
}
public static class CompoundingDie implements Die {
private Die source;
private Predicate<Long> compoundOn;
private String compoundPattern;
public CompoundingDie(Die src, Predicate<Long> compound) {
this(src, compound, null);
}
public CompoundingDie(Die src, Predicate<Long> compound, String patt) {
source = src;
compoundOn = compound;
compoundPattern = patt;
}
public boolean canOptimize() {
return false;
}
public long optimize() {
return 0;
}
public long roll() {
long res = source.roll();
long oldRes = res;
while(compoundOn.test(oldRes)) {
oldRes = source.rollSingle();
res += oldRes;
}
return res;
}
public long rollSingle() {
long res = source.rollSingle();
long oldRes = res;
while(compoundOn.test(oldRes)) {
oldRes = source.rollSingle();
res += oldRes;
}
return res;
}
public String toString() {
if(compoundPattern == null) {
return source + "!!";
} else {
return source + "!!" + compoundPattern;
}
}
}
public static class SimpleDieList implements DieList {
private Die numDice;
private Die size;
public SimpleDieList(Die nDice, Die sze) {
numDice = nDice;
size = sze;
}
public boolean canOptimize() {
if(size.canOptimize() && size.optimize() == 1) {
return numDice.canOptimize();
} else {
return false;
}
}
public long[] optimize() {
long[] ret = new long[(int)numDice.optimize()];
long optSize = size.optimize();
for(int i = 0; i < optSize; i++) {
ret[i] = 1;
}
return ret;
}
public long[] roll() {
int num = (int)numDice.roll();
long[] ret = new long[num];
for(int i = 0; i < num; i++) {
ret[i] = size.roll();
}
return ret;
}
public String toString() {
return numDice.toString() + "dl" + size.toString();
}
}
public static class ExplodingDice implements DieList {
private Die source;
private Predicate<Long> explodeOn;
private String explodePattern;
private boolean explodePenetrates;
public ExplodingDice(Die src, Predicate<Long> explode) {
this(src, explode, null, false);
}
public ExplodingDice(Die src, Predicate<Long> explode, boolean penetrate) {
this(src, explode, null, penetrate);
}
public ExplodingDice(Die src, Predicate<Long> explode, String patt, boolean penetrate) {
source = src;
explodeOn = explode;
explodePattern = patt;
explodePenetrates = penetrate;
}
public boolean canOptimize() {
return false;
}
public long[] optimize() {
return new long[0];
}
public long[] roll() {
long res = source.roll();
List<Long> resList = new LinkedList<>();
long oldRes = res;
while(explodeOn.test(oldRes)) {
oldRes = source.rollSingle();
if(explodePenetrates) oldRes -= 1;
resList.add(oldRes);
}
long[] newRes = new long[resList.size()+1];
newRes[0] = res;
int i = 1;
for(long rll : resList) {
newRes[i] = rll;
i += 1;
}
return newRes;
}
public String toString() {
if(explodePattern == null) {
return source + (explodePenetrates ? "p" : "") + "!";
} else {
return source + (explodePenetrates ? "p" : "") + "!" + explodePattern;
}
}
}
public static DieExpression parseExpression(String exp) {
if(!isValidExpression(exp)) return null;
if(scalarDiePattern.matcher(exp).matches()) {
return new DieExpression(new ScalarDie(Long.parseLong(exp.substring(0, exp.indexOf('s')))));
} else if(simpleDiePattern.matcher(exp).matches()) {
String[] dieParts = exp.split("d");
if(dieParts[0].equals("")) {
return new DieExpression(new SimpleDie(1, Long.parseLong(dieParts[1])));
} else {
return new DieExpression(new SimpleDie(Long.parseLong(dieParts[0]), Long.parseLong(dieParts[1])));
}
} else if(fudgeDiePattern.matcher(exp).matches()) {
String nDice = exp.substring(0, exp.indexOf('d'));
return new DieExpression(new FudgeDie(Long.parseLong(nDice)));
} else if(compoundDiePattern.matcher(exp).matches()) {
String[] dieParts = exp.split("c");
DieExpression left = parseExpression(dieParts[0]);
DieExpression right = parseExpression(dieParts[1]);
return new DieExpression(new CompoundDie(left.scalar, right.scalar));
} else if(compoundingDiePattern.matcher(exp).matches()) {
String[] dieParts = exp.split("!!");
DieExpression left = parseExpression(dieParts[0]);
Predicate<Long> right = deriveCond(dieParts[1]);
return new DieExpression(new CompoundingDie(left.scalar, right, dieParts[1]));
} else if(explodingDiePattern.matcher(exp).matches()) {
String[] dieParts = exp.split("!");
DieExpression left = parseExpression(dieParts[0]);
Predicate<Long> right = deriveCond(dieParts[1]);
return new DieExpression(new ExplodingDice(left.scalar, right, dieParts[1], false));
} else if(penetratingDiePattern.matcher(exp).matches()) {
String[] dieParts = exp.split("p!");
DieExpression left = parseExpression(dieParts[0]);
Predicate<Long> right = deriveCond(dieParts[1]);
return new DieExpression(new ExplodingDice(left.scalar, right, dieParts[1], true));
} else if(diceListPattern.matcher(exp).matches()) {
String[] dieParts = exp.split("dl");
DieExpression left = parseExpression(dieParts[0]);
DieExpression right = parseExpression(dieParts[1]);
return new DieExpression(new SimpleDieList(left.scalar, right.scalar));
}
// @TODO give a specific error message
return null;
}
private static final String comparePoint = "[<>=]\\d+";
private static final String scalarDie = "[\\+\\-]?\\d+sd";
private static final Pattern scalarDiePattern = Pattern.compile("\\A" + scalarDie + "\\Z");
private static final String simpleDie = "(?:\\d+)?d\\d+";
private static final Pattern simpleDiePattern = Pattern.compile("\\A" + simpleDie + "\\Z");
private static final String fudgeDie = "(?:\\d+)?dF";
private static final Pattern fudgeDiePattern = Pattern.compile("\\A" + fudgeDie + "\\Z");
private static final String compoundDie = simpleDie + "c(?:(?:" + simpleDie + ")|(?:\\d+))";
private static final Pattern compoundDiePattern = Pattern.compile("\\A" + compoundDie + "\\Z");
private static final String compoundGroup = "(?:(?:" + scalarDie + ")|(?:" + simpleDie + ")|(?:"
+ compoundDie + ")|(?:" + fudgeDie +"))";
private static final String compoundingDie = compoundGroup + "!!" + comparePoint;
private static final Pattern compoundingDiePattern = Pattern.compile("\\A" + compoundingDie + "\\Z");
private static final String explodingDie = compoundGroup + "!" + comparePoint;
private static final Pattern explodingDiePattern = Pattern.compile("\\A" + explodingDie + "\\Z");
private static final String penetratingDie = compoundGroup + "!" + comparePoint;
private static final Pattern penetratingDiePattern = Pattern.compile("\\A" + penetratingDie + "\\Z");
private static final String diceList = compoundGroup + "dl" + compoundGroup;
private static final Pattern diceListPattern = Pattern.compile("\\A" + diceList + "\\Z");
public static boolean isValidExpression(String exp) {
if(scalarDiePattern.matcher(exp).matches()) {
return true;
} else if(simpleDiePattern.matcher(exp).matches()) {
return true;
} else if(fudgeDiePattern.matcher(exp).matches()) {
return true;
} else if(compoundDiePattern.matcher(exp).matches()) {
return true;
} else if(compoundingDiePattern.matcher(exp).matches()) {
return true;
} else if(explodingDiePattern.matcher(exp).matches()) {
return true;
} else if(penetratingDiePattern.matcher(exp).matches()) {
return true;
} else if (diceListPattern.matcher(exp).matches()) {
return true;
} else {
return false;
}
}
private static Predicate<Long> deriveCond(String patt) {
long num = Long.parseLong(patt.substring(1));
switch(patt.charAt(0)) {
case '<':
return (roll) -> roll < num;
case '=':
return (roll) -> roll == num;
case '>':
return (roll) -> roll > num;
default:
return (roll) -> false;
}
}
}
|