-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathController.java
More file actions
282 lines (243 loc) · 9.37 KB
/
Copy pathController.java
File metadata and controls
282 lines (243 loc) · 9.37 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
package amazons;
import java.io.PrintStream;
import java.util.Random;
import java.util.Scanner;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
import java.util.function.Consumer;
import static amazons.Utils.*;
import static amazons.Piece.*;
import static amazons.Square.SQ;
/** The input/output and GUI controller for play of Amazons.
* @author JaniceNg
*/
final class Controller {
/** Controller for one or more games of Amazons, using
* MANUALPLAYERTEMPLATE as an exemplar for manual players
* (see the Player.create method) and AUTOPLAYERTEMPLATE
* as an exemplar for automated players. Reports
* board changes to VIEW at appropriate points. Uses REPORTER
* to report moves, wins, and errors to user. If LOGFILE is
* non-null, copies all commands to it. If STRICT, exits the
* program with non-zero code on receiving an erroneous move from a
* player. */
Controller(View view, PrintStream logFile, Reporter reporter,
Player manualPlayerTemplate, Player autoPlayerTemplate) {
_view = view;
_playing = false;
_logFile = logFile;
_input = new Scanner(System.in);
_autoPlayerTemplate = autoPlayerTemplate;
_manualPlayerTemplate = manualPlayerTemplate;
_nonPlayer = manualPlayerTemplate.create(EMPTY, this);
_reporter = reporter;
}
/** Play Amazons. */
void play() {
_playing = true;
_winner = null;
_board.init();
_white = _manualPlayerTemplate.create(WHITE, this);
_black = _autoPlayerTemplate.create(BLACK, this);
while (_playing) {
_view.update(_board);
String command;
if (_winner == null) {
if (_board.turn() == WHITE) {
command = _white.myMove();
} else {
command = _black.myMove();
}
} else {
command = _nonPlayer.myMove();
if (command == null) {
command = "quit";
}
}
try {
executeCommand(command);
} catch (IllegalArgumentException excp) {
reportError("Error: %s%n", excp.getMessage());
}
}
if (_logFile != null) {
_logFile.close();
}
}
/** Return the current board. The value returned should not be
* modified by the caller. */
Board board() {
return _board;
}
/** Return a random integer in the range 0 inclusive to U, exclusive.
* Available for use by AIs that use random selections in some cases.
* Once setRandomSeed is called with a particular value, this method
* will always return the same sequence of values. */
int randInt(int U) {
return _randGen.nextInt(U);
}
/** Re-seed the pseudo-random number generator (PRNG) that supplies randInt
* with the value SEED. Identical seeds produce identical sequences.
* Initially, the PRNG is randomly seeded. */
void setSeed(long seed) {
_randGen.setSeed(seed);
}
/** Return the next line of input, or null if there is no more. First
* prompts for the line. Trims the returned line (if any) of all
* leading and trailing whitespace. */
String readLine() {
System.out.print("> ");
System.out.flush();
if (_input.hasNextLine()) {
return _input.nextLine().trim();
} else {
return null;
}
}
/** Report error by calling reportError(FORMAT, ARGS) on my reporter. */
void reportError(String format, Object... args) {
_reporter.reportError(format, args);
}
/** Report note by calling reportNote(FORMAT, ARGS) on my reporter. */
void reportNote(String format, Object... args) {
_reporter.reportNote(format, args);
}
/** Report move by calling reportMove(MOVE) on my reporter. */
void reportMove(Move move) {
_reporter.reportMove(move);
}
/** A Command is pair (<pattern>, <processor>), where <pattern> is a
* Matcher that matches instances of a particular command, and
* <processor> is a functional object whose .accept method takes a
* successfully matched Matcher and performs some operation. */
private static class Command {
/** A new Command that matches PATN (a regular expression) and uses
* PROCESSOR to process commands that match the pattern. */
Command(String patn, Consumer<Matcher> processor) {
_matcher = Pattern.compile(patn).matcher("");
_processor = processor;
}
/** A Matcher matching my pattern. */
protected final Matcher _matcher;
/** The function object that implements my command. */
protected final Consumer<Matcher> _processor;
}
/** A list of Commands describing the valid textual commands to the
* Amazons program and the methods to process them. */
private Command[] _commands = {
new Command("quit$", this::doQuit),
new Command("seed\\s+(\\d+)$", this::doSeed),
new Command("dump$", this::doDump),
new Command("new", this::doNew),
new Command("auto\\s+black$", this::doautoBlack),
new Command("auto\\s+white$", this::doautoWhite),
new Command("manual\\s+black$", this::doManualBlack),
new Command("manual\\s+white$", this::doManualWhite),
new Command(SQ + "-" + SQ + "\\(" + SQ + "\\)", this::doMove),
new Command(SQ + "\\s" + SQ + "\\s" + SQ, this::doMove)
};
/** A Matcher whose Pattern matches comments. */
private final Matcher _comment = Pattern.compile("#.*").matcher("");
/** Check that CMND is one of the valid Amazons commands and execute it, if
* so, raising an IllegalArgumentException otherwise. */
private void executeCommand(String cmnd) {
if (_logFile != null) {
_logFile.println(cmnd);
_logFile.flush();
}
_comment.reset(cmnd);
cmnd = _comment.replaceFirst("").trim().toLowerCase();
if (cmnd.isEmpty()) {
return;
}
for (Command parser : _commands) {
parser._matcher.reset(cmnd);
if (parser._matcher.matches()) {
parser._processor.accept(parser._matcher);
return;
}
}
throw error("Bad command: %s", cmnd);
}
/** Command "new". */
private void doNew(Matcher unused) {
_board.init();
_winner = null;
}
/** Command "quit". */
private void doQuit(Matcher unused) {
_playing = false;
}
/** Command "seed N" where N is the first group of MAT. */
private void doSeed(Matcher mat) {
try {
setSeed(Long.parseLong(mat.group(1)));
} catch (NumberFormatException excp) {
throw error("number too large");
}
}
/** Dump the contents of the board on standard output. */
private void doDump(Matcher unused) {
System.out.printf("===%n%s===%n", _board);
}
/** Create manual white for player. */
private void doManualWhite(Matcher unused) {
_white = _manualPlayerTemplate.create(WHITE, this);
}
/** Create manual black for player. */
private void doManualBlack(Matcher unused) {
_black = _manualPlayerTemplate.create(BLACK, this);
}
/** Create auto white for player. */
private void doautoWhite(Matcher unused) {
_white = _autoPlayerTemplate.create(WHITE, this);
}
/** Create auto black for player. */
private void doautoBlack(Matcher unused) {
_black = _manualPlayerTemplate.create(BLACK, this);
}
/** Do Move Function with M as input . */
private void doMove(Matcher m) {
if (Move.mv(m.group(0)) != null
&& _board.isLegal(Move.mv(Square.sq(m.group(1)),
Square.sq(m.group(2)), Square.sq(m.group(3))))) {
_board.makeMove(Move.mv
(Square.sq(m.group(1)), Square.sq(m.group(2)),
Square.sq(m.group(3))));
_winner = _board.winner();
if (_winner != null) {
if (_board.winner() == WHITE) {
reportNote("White wins.");
} else {
reportNote("Black wins.");
}
}
} else {
reportError("Move is null");
}
}
/** The board. */
private Board _board = new Board();
/** The winning side of the current game. */
private Piece _winner;
/** True while game is still active. */
private boolean _playing;
/** The object that is displaying the current game. */
private View _view;
/** My pseudo-random number generator. */
private Random _randGen = new Random();
/** Log file, or null if absent. */
private PrintStream _logFile;
/** Input source. */
private Scanner _input;
/** The current White and Black players, each created from
* _autoPlayerTemplate or _manualPlayerTemplate. */
private Player _white, _black;
/** A dummy Player used to return commands but not moves when no
* game is in progress. */
private Player _nonPlayer;
/** The current templates for manual and automated players. */
private Player _autoPlayerTemplate, _manualPlayerTemplate;
/** Reporter for messages and errors. */
private Reporter _reporter;
}