From b53005727860f312e22fd71db01a77a9e94c84b2 Mon Sep 17 00:00:00 2001 From: Andrew Regan Date: Sat, 15 Jan 2022 23:34:11 +0000 Subject: [PATCH] More robust validation --- 27_Civil_War/civilwar.bas | 44 +++++++++++++++++++++++------ 27_Civil_War/java/src/CivilWar.java | 26 +++++++++++++---- 2 files changed, 57 insertions(+), 13 deletions(-) diff --git a/27_Civil_War/civilwar.bas b/27_Civil_War/civilwar.bas index f8fe9701..f9cb256b 100644 --- a/27_Civil_War/civilwar.bas +++ b/27_Civil_War/civilwar.bas @@ -172,23 +172,47 @@ 1840 PRINT 1850 REM - CHOOSE STRATEGIES 1860 IF B$ <> "YES" THEN 1910 -1870 FOR I=1 TO 2 -1880 ON I GOTO 1890,1920 + + +== 2 +1890 PRINT "CONFEDERATE STRATEGY "; +1920 INPUT Y +1930 IF ABS(Y-3)<3 THEN 1960 +1940 PRINT "STRATEGY";Y;"NOT ALLOWED." +1950 GOTO 1910 +2010 LET Y1=Y + +2020 PRINT "UNION STRATEGY "; +1920 INPUT Y +1930 IF ABS(Y-3)<3 THEN 1960 +1940 PRINT "STRATEGY";Y;"NOT ALLOWED." +1950 GOTO 1910 + + +== 1 1890 PRINT "CONFEDERATE STRATEGY "; -1900 GOTO 1920 -1910 PRINT "YOUR STRATEGY "; 1920 INPUT Y 1930 IF ABS(Y-3)<3 THEN 1960 1940 PRINT "STRATEGY";Y;"NOT ALLOWED." 1950 GOTO 1910 -1960 IF B$="YES" THEN 2000 1970 IF Y=5 THEN 2830 1980 GOSUB 3110 1990 GOTO 2170 -2000 IF I=2 THEN 2040 2010 LET Y1=Y -2020 PRINT "UNION STRATEGY "; -2030 NEXT I + +# 2020 PRINT "UNION STRATEGY "; +# 1920 INPUT Y +# 1930 IF ABS(Y-3)<3 THEN 1960 +# 1940 PRINT "STRATEGY";Y;"NOT ALLOWED." +# 1950 GOTO 1910 +# 1970 IF Y=5 THEN 2830 +# 1980 GOSUB 3110 +# 1990 GOTO 2170 + + + + + 2040 LET Y2=Y 2050 LET Y=Y1 2060 IF Y2=5 THEN 2020 @@ -298,6 +322,7 @@ 3080 PRINT S(1);S(2);S(3);S(4) 3090 REM--------------------------------- 3100 STOP + 3110 REM - UNION STRATEGY IS COMPUTER CHOSEN 3120 PRINT "UNION STRATEGY IS "; 3130 IF A <> 0 THEN 3180 @@ -318,6 +343,9 @@ 3270 LET Y2=I 3280 PRINT Y2 3290 RETURN + + + 3300 REM LEARN PRESENT STRATEGY, START FORGETTING OLD ONES 3310 REM - PRESENT STRATEGY OF SOUTH GAINS 3*S, OTHERS LOSE S 3320 REM PROBABILITY POINTS, UNLESS A STRATEGY FALLS BELOW 5%. diff --git a/27_Civil_War/java/src/CivilWar.java b/27_Civil_War/java/src/CivilWar.java index 7b8d8b4c..099a9183 100644 --- a/27_Civil_War/java/src/CivilWar.java +++ b/27_Civil_War/java/src/CivilWar.java @@ -598,17 +598,33 @@ public class CivilWar { } private static String inputString(Predicate validator, String reminder) { - var terminalInput = new Scanner(System.in); - while (true) { - var input = terminalInput.nextLine(); - if (validator.test(input)) { - return input; + try { + var input = new Scanner(System.in).nextLine(); + if (validator.test(input)) { + return input; + } + } catch (InputMismatchException e) { + // Ignore } System.out.println(reminder); } } + private static int inputInt(Predicate validator, Function reminder) { + while (true) { + try { + var input = new Scanner(System.in).nextInt(); + if (validator.test(input)) { + return input; + } + System.out.println(reminder.apply(input)); + } catch (InputMismatchException e) { + System.out.println(reminder.apply(0)); + } + } + } + private static boolean isYes(String s) { if (s == null) { return false;