-
Notifications
You must be signed in to change notification settings - Fork 0
/
TextPlayer.java
46 lines (39 loc) · 1.21 KB
/
TextPlayer.java
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
package amazons;
import static amazons.Move.isGrammaticalMove;
import static amazons.Move.mv;
/** A Player that takes input as text commands from the standard input.
* @author JaniceNg
*/
class TextPlayer extends Player {
/** A new TextPlayer with no piece or controller (intended to produce
* a template). */
TextPlayer() {
this(null, null);
}
/** A new TextPlayer playing PIECE under control of CONTROLLER. */
private TextPlayer(Piece piece, Controller controller) {
super(piece, controller);
}
@Override
Player create(Piece piece, Controller controller) {
return new TextPlayer(piece, controller);
}
@Override
String myMove() {
while (true) {
String line = _controller.readLine();
if (line == null) {
return "quit";
} else if (isGrammaticalMove(line)) {
Move move = mv(line);
if (!_controller.board().isLegal(mv(line))) {
_controller.reportError("Invalid move. "
+ "Please try again.");
}
return line;
} else {
return line;
}
}
}
}