From eda9a86d8d48758e9982cfffd470c3b38a0a4b0b Mon Sep 17 00:00:00 2001 From: Ben Culkin Date: Sat, 21 Nov 2020 23:11:43 -0500 Subject: Make dice generic Convert dice from dealing exclusively with ints, to deal with objects of arbitrary types --- .../bjc/dicelang/neodice/diepool/TimesDiePool.java | 26 +++++++++------------- 1 file changed, 11 insertions(+), 15 deletions(-) (limited to 'dice/src/main/java/bjc/dicelang/neodice/diepool/TimesDiePool.java') diff --git a/dice/src/main/java/bjc/dicelang/neodice/diepool/TimesDiePool.java b/dice/src/main/java/bjc/dicelang/neodice/diepool/TimesDiePool.java index 1b34247..56fe0e5 100644 --- a/dice/src/main/java/bjc/dicelang/neodice/diepool/TimesDiePool.java +++ b/dice/src/main/java/bjc/dicelang/neodice/diepool/TimesDiePool.java @@ -1,35 +1,31 @@ package bjc.dicelang.neodice.diepool; import java.util.*; +import java.util.stream.*; import bjc.dicelang.neodice.*; -public class TimesDiePool implements DiePool { - private final Die contained; +public class TimesDiePool implements IDiePool { + private final IDie contained; private final int numDice; - public TimesDiePool(Die contained, int numDice) { + public TimesDiePool(IDie contained, int numDice) { this.contained = contained; this.numDice = numDice; } @Override - public int[] roll(Random rng) { - int[] results = new int[numDice]; - - for (int index = 0; index < numDice; index++) { - results[index] = contained.roll(rng); - } - - return results; + public Stream roll(Random rng) { + return Stream.generate(() -> contained.roll(rng)) + .limit(numDice); } @Override - public Die[] contained() { - Die[] results = new Die[numDice]; + public List> contained() { + List> results = new ArrayList<>(numDice); for (int index = 0; index < numDice; index++) { - results[index] = contained; + results.add(contained); } return results; @@ -51,7 +47,7 @@ public class TimesDiePool implements DiePool { if (obj == null) return false; if (getClass() != obj.getClass()) return false; - TimesDiePool other = (TimesDiePool) obj; + TimesDiePool other = (TimesDiePool) obj; return Objects.equals(contained, other.contained) && numDice == other.numDice; } -- cgit v1.2.3