More robust validation

This commit is contained in:
Andrew Regan
2022-01-15 23:34:11 +00:00
parent c256a7b0fb
commit b530057278
2 changed files with 57 additions and 13 deletions

View File

@@ -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%.

View File

@@ -598,17 +598,33 @@ public class CivilWar {
}
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;
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<Integer> validator, Function<Integer, String> 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;