-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConsoleReader.java
More file actions
164 lines (138 loc) · 4.46 KB
/
ConsoleReader.java
File metadata and controls
164 lines (138 loc) · 4.46 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
class ConsoleReader implements Runnable {
private Display display;
private Board board;
public void run() {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
display = new Display();
board = display.getChessGame().getBoard();
}
});
System.out.print(">> ");
while (true) {
try {
Thread.sleep(50);
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String input = reader.readLine();
if (input.equals("exit")) {
System.exit(0);
break;
}
List<String> commandAndArgs = parseCommand(input);
if (commandAndArgs.isEmpty()) {
System.out.println("Invalid input.\n>> ");
continue;
}
String command = commandAndArgs.get(0).toLowerCase();
List<String> args = commandAndArgs.subList(1, commandAndArgs.size());
switch (command) {
case "undo":
if (!board.undoLastMove()) {
System.out.println("No moves to undo!");
} else {
System.out.println("Successfully undid the move!");
}
display.resetLegalMoves();
display.refreshDisplay();
break;
case "best_iterative":
int maxDepth = 6;
if (args.isEmpty()) {
System.out.println("Defaulted max depth: " + maxDepth);
} else {
System.out.println("Custom max depth: " + args.get(0));
maxDepth = Integer.parseInt(args.get(0));
}
EvaluationResult result = board.iterativeDeepening(maxDepth);
System.out.println("Bot's Current Evaluation: " + result.value);
board.makeMove(result.move);
display.refreshDisplay();
break;
case "best":
int depth = 4;
if (args.isEmpty()) {
System.out.println("Defaulted depth: " + depth);
} else {
System.out.println("Custom depth: " + args.get(0));
depth = Integer.parseInt(args.get(0));
}
EvaluationResult result2 = board.minimax(depth, Double.NEGATIVE_INFINITY,
Double.POSITIVE_INFINITY, board.isWhiteTurn(), null);
System.out.println("Bot's Current Evaluation: " + result2.value);
board.makeMove(result2.move);
display.refreshDisplay();
break;
case "generate_moves":
int iterations = 10000;
if (args.isEmpty()) {
System.out.println("Defaulted iterations: " + iterations);
} else {
System.out.println("Custom depth: " + args.get(0));
iterations = Integer.parseInt(args.get(0));
}
long total = 0;
for (int i = 0; i < iterations; i++) {
long startTime = System.currentTimeMillis();
board.getAllLegalMoves(true);
long endTime = System.currentTimeMillis();
total += (endTime - startTime);
}
System.out.println("Time: " + (total / iterations));
break;
case "eval":
int evalDepth = 4;
if (args.isEmpty()) {
System.out.println("Defaulted depth: " + evalDepth);
} else {
System.out.println("Custom depth: " + args.get(0));
evalDepth = Integer.parseInt(args.get(0));
}
EvaluationResult evalResult = board.minimax(evalDepth, Double.NEGATIVE_INFINITY,
Double.POSITIVE_INFINITY, board.isWhiteTurn(), null);
System.out.println("Bot's Current Evaluation: " + evalResult.value);
break;
case "shallow":
System.out.println("Current Shallow Evaluation: "
+ board.evaluateCurrentPosition(0, board.getAllLegalMoves(false).size()));
break;
case "fen":
if (args.isEmpty()) {
System.out.println("You need to pass in a FEN string to load the position!");
break;
}
board.parseFEN(args.get(0));
display.refreshDisplay();
System.out.println("Successfully loaded your FEN!");
break;
default:
System.out.println("Unknown command: " + command);
}
System.out.print(">> ");
} catch (Exception e) {
e.printStackTrace();
}
}
}
private static List<String> parseCommand(String input) {
List<String> result = new ArrayList<>();
Pattern pattern = Pattern.compile("\"([^\"]*)\"|(\\S+)");
Matcher matcher = pattern.matcher(input);
while (matcher.find()) {
if (matcher.group(1) != null) {
// Quoted argument
result.add(matcher.group(1));
} else {
// Unquoted argument
result.add(matcher.group(2));
}
}
return result;
}
}