74 lines
2.5 KiB
Java
74 lines
2.5 KiB
Java
package io.rudefox.burrow;
|
|
|
|
import java.util.Scanner;
|
|
import java.util.regex.Pattern;
|
|
|
|
public class InteractiveBinaryEntropyGenerator extends InteractiveEntropyGenerator {
|
|
|
|
public InteractiveBinaryEntropyGenerator(int targetBitsOfEntropy, Scanner inputScanner, boolean ansiColorOutput) {
|
|
super(inputScanner, ansiColorOutput, targetBitsOfEntropy, 1);
|
|
}
|
|
|
|
@Override
|
|
protected String readNextEventSetString() {
|
|
|
|
StringBuilder eventSetBuffer = new StringBuilder();
|
|
int remainingEvents = eventsPerEventSet;
|
|
Pattern eventSetPattern = Pattern.compile("[01]+");
|
|
|
|
while (remainingEvents > 0) {
|
|
|
|
System.out.printf("Input %d coin tosses [0-1]: ", remainingEvents);
|
|
|
|
String nextInput = inputScanner.next();
|
|
|
|
if (!eventSetPattern.matcher(nextInput).matches()) {
|
|
System.out.printf(
|
|
"Invalid input! Enter %d-digit sequence of 1's and 0's (for example, 1011101010)" + System.lineSeparator(),
|
|
remainingEvents);
|
|
continue;
|
|
}
|
|
|
|
eventSetBuffer.append(nextInput);
|
|
remainingEvents -= nextInput.length();
|
|
}
|
|
|
|
if (remainingEvents < 0)
|
|
System.out.printf("NOTE: Discarding the %d extra coin toss(es) entered" + System.lineSeparator(), -remainingEvents);
|
|
|
|
return eventSetBuffer.substring(0, eventsPerEventSet);
|
|
}
|
|
|
|
@Override
|
|
protected String getEventValueFormatString(int eventIndexWithinSet) {
|
|
return "";
|
|
}
|
|
|
|
@Override
|
|
protected int toZeroBasedInteger(int eventDecimalValue) {
|
|
return eventDecimalValue;
|
|
}
|
|
|
|
@Override
|
|
protected void outputEventValuesLine(StringBuilder eventValuesLine) { }
|
|
|
|
@Override
|
|
protected StringBuilder getInitialBitsLine() {
|
|
return new StringBuilder("| ");
|
|
}
|
|
|
|
@Override
|
|
protected void padBitsLine(StringBuilder eventSetBitsLine, int bitIndexWithinEventSet, int bitIndexWithinEntropy) {
|
|
if (bitIndexWithinEventSet == BITS_PER_EVENT_SET - 1)
|
|
eventSetBitsLine.append(" |");
|
|
else if (bitIndexWithinEventSet % BITS_PER_WORD == 0 || bitIndexWithinEventSet % BITS_PER_WORD == 6)
|
|
eventSetBitsLine.append(styleBit(" ", bitIndexWithinEntropy, bitIndexWithinEventSet));
|
|
else if (bitIndexWithinEventSet % BITS_PER_WORD == BITS_PER_WORD - 1)
|
|
eventSetBitsLine.append(" | ");
|
|
}
|
|
|
|
@Override
|
|
protected String getSeedWordLineFormatString() {
|
|
return "|%-15s|%-15s|%-15s|";
|
|
}
|
|
} |