-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameInterface.java
More file actions
87 lines (72 loc) · 2.62 KB
/
Copy pathGameInterface.java
File metadata and controls
87 lines (72 loc) · 2.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
//package project_frontiers;
import java.util.Scanner;
public class GameInterface {
private static final Scanner scanner = new Scanner(System.in);
public static void delay() {
delay(600);
}
public static void delay(int millis) {
try{
Thread.sleep(millis);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
public static void output(String output) {
System.out.println(output);
}
public static int query(String query, String[] options, String[] inputs) {
int result = -1;
if (options.length == inputs.length && options.length > 0) {
// ask the question
output(query);
delay();
int i = 0;
while (i < options.length) {
output(String.format("OPTION %1$x: %2$s [%3$s]", i+1, options[i], inputs[i++]));
delay(300);
}
System.out.print("SELECT: ");
String playerInput = scanner.nextLine();
// verify that the question was answered correctly
int j = 0;
while (result < 0 && j < inputs.length) {
if (playerInput.equalsIgnoreCase(inputs[j])) {
result = j;
}
j++;
}
// repeat if the question was answered incorrectly
while (result == -1) {
// scold them
System.out.println("INVALID INPUT");
delay();
// ask the question again
output(query);
delay();
i = 0;
while (i < options.length) {
output(String.format("OPTION %1$x: %2$s [%3$s]", i+1, options[i], inputs[i++]));
delay(300);
}
System.out.print("SELECT: ");
playerInput = scanner.nextLine();
// verify that the question was answered correctly
j = 0;
while (result < 0 && j < inputs.length) {
if (playerInput.equalsIgnoreCase(inputs[j++])) {
result = j-1;
}
}
}
} else {
output("QUERY FAILED - INVALID ARGUMENTS");
delay();
}
System.out.println();
return result;
}
public static void main(String[] args) {
System.out.println(GameInterface.query("TEST", new String[] {"One", "Two", "Three"}, new String[] {"z", "x", "c"}));
}
}