Clean up YES/NO input and validation

This commit is contained in:
Andrew Regan
2022-01-15 16:32:12 +00:00
parent 518562645a
commit 1085d282ff

View File

@@ -1,6 +1,7 @@
import java.io.PrintStream; import java.io.PrintStream;
import java.util.List; import java.util.List;
import java.util.Scanner; import java.util.Scanner;
import java.util.function.Predicate;
import static java.util.stream.Collectors.joining; import static java.util.stream.Collectors.joining;
import static java.util.stream.IntStream.range; import static java.util.stream.IntStream.range;
@@ -46,6 +47,9 @@ public class CivilWar {
private double R; private double R;
private boolean wantBattleDescriptions; private boolean wantBattleDescriptions;
private final static String YES_NO_REMINDER = "(ANSWER YES OR NO)";
private final static Predicate<String> YES_NO_CHECKER = i -> isYes(i) || isNo(i);
/** /**
* ORIGINAL GAME DESIGN: CRAM, GOODIE, HIBBARD LEXINGTON H.S. * ORIGINAL GAME DESIGN: CRAM, GOODIE, HIBBARD LEXINGTON H.S.
* MODIFICATIONS: G. PAUL, R. HESS (TIES), 1973 * MODIFICATIONS: G. PAUL, R. HESS (TIES), 1973
@@ -58,17 +62,12 @@ public class CivilWar {
System.out.print("DO YOU WANT INSTRUCTIONS? "); System.out.print("DO YOU WANT INSTRUCTIONS? ");
var terminalInput = new Scanner(System.in); if (isYes(inputString(YES_NO_CHECKER, YES_NO_REMINDER))) {
String s = terminalInput.nextLine();
switch (s) {
case "Y" -> {
x.showHelp(); x.showHelp();
}
x.gameLoop(); x.gameLoop();
} }
case "N" -> x.gameLoop();
default -> System.out.println("YES OR NO -- ");
}
}
private void gameLoop() { private void gameLoop() {
out.println(); out.println();
@@ -76,20 +75,14 @@ public class CivilWar {
out.println(); out.println();
out.print("ARE THERE TWO GENERALS PRESENT (ANSWER YES OR NO)? "); out.print("ARE THERE TWO GENERALS PRESENT (ANSWER YES OR NO)? ");
var terminalInput = new Scanner(System.in); if (isYes(inputString(YES_NO_CHECKER, YES_NO_REMINDER))) {
String twoGeneralsAnswer = terminalInput.nextLine(); this.numGenerals = 2;
switch (twoGeneralsAnswer) { } else {
case "YES" -> this.numGenerals = 2;
case "NO" -> {
this.numGenerals = 1; this.numGenerals = 1;
out.println(); out.println();
out.println("YOU ARE THE CONFEDERACY. GOOD LUCK!"); out.println("YOU ARE THE CONFEDERACY. GOOD LUCK!");
out.println(); out.println();
} }
default -> {
// FIXME Retry!
}
}
out.println("SELECT A BATTLE BY TYPING A NUMBER FROM 1 TO 14 ON"); out.println("SELECT A BATTLE BY TYPING A NUMBER FROM 1 TO 14 ON");
out.println("REQUEST. TYPE ANY OTHER NUMBER TO END THE SIMULATION."); out.println("REQUEST. TYPE ANY OTHER NUMBER TO END THE SIMULATION.");
@@ -101,9 +94,8 @@ public class CivilWar {
out.println(); out.println();
out.print("AFTER REQUESTING A BATTLE, DO YOU WISH BATTLE DESCRIPTIONS (ANSWER YES OR NO)? "); out.print("AFTER REQUESTING A BATTLE, DO YOU WISH BATTLE DESCRIPTIONS (ANSWER YES OR NO)? ");
String descriptionsAnswer = terminalInput.nextLine();
this.wantBattleDescriptions = descriptionsAnswer.equals("YES"); this.wantBattleDescriptions = isYes(inputString(YES_NO_CHECKER, YES_NO_REMINDER));
// FIXME Retry if not "YES" or "NO"
while (true) { while (true) {
var battle = startBattle(); var battle = startBattle();
@@ -584,6 +576,34 @@ public class CivilWar {
return rightAlignInt((int) Math.floor(number)); return rightAlignInt((int) Math.floor(number));
} }
private static String inputString(Predicate<String> validator, String reminder) {
var terminalInput = new Scanner(System.in);
while (true) {
var input = terminalInput.nextLine();
if (validator.test(input)) {
return input;
}
System.out.println(reminder);
}
}
private static boolean isYes(String s) {
if (s == null) {
return false;
}
var uppercase = s.toUpperCase();
return uppercase.equals("Y") || uppercase.equals("YES");
}
private static boolean isNo(String s) {
if (s == null) {
return false;
}
var uppercase = s.toUpperCase();
return uppercase.equals("N") || uppercase.equals("NO");
}
private static class BattleState { private static class BattleState {
private final HistoricalDatum data; private final HistoricalDatum data;
private double F1; private double F1;