Made the interactive D8 entropy reader more forgiving for a better UX

This commit is contained in:
B.J. Dweck 2021-03-14 10:19:25 +02:00
parent 4fd4bb407b
commit 6c781accb0

View File

@ -12,15 +12,31 @@ public class InteractiveDice8EntropyGenerator extends InteractiveEntropyGenerato
@Override @Override
protected String readNextEventSetString() { protected String readNextEventSetString() {
String eventSetString = ""; StringBuilder eventSetBuffer = new StringBuilder();
Pattern eventSetPattern = Pattern.compile(String.format("[1-8]{%d}", eventsPerEventSet)); int remainingEvents = eventsPerEventSet;
Pattern eventSetPattern = Pattern.compile("[1-8]+");
while (!eventSetPattern.matcher(eventSetString).matches()) { while (remainingEvents > 0) {
System.out.print("Input 11 x 8-sided dice rolls [1-8]: ");
eventSetString = inputScanner.next(); System.out.printf("Input %d x 8-sided dice rolls [1-8]: ", remainingEvents);
String nextInput = inputScanner.next();
if (!eventSetPattern.matcher(nextInput).matches()) {
System.out.printf(
"Invalid input! Enter %d-digit sequence of digits from 1 to 8 (for example, 8765375812)" + System.lineSeparator(),
remainingEvents);
continue;
} }
return eventSetString; eventSetBuffer.append(nextInput);
remainingEvents -= nextInput.length();
}
if (remainingEvents < 0)
System.out.printf("NOTE: Discarding the %d extra dice roll(s) entered" + System.lineSeparator(), -remainingEvents);
return eventSetBuffer.substring(0, eventsPerEventSet);
} }
@Override @Override