-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTextPlayer.java
More file actions
50 lines (44 loc) · 1.35 KB
/
Copy pathTextPlayer.java
File metadata and controls
50 lines (44 loc) · 1.35 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
package amazons;
import static amazons.Move.isGrammaticalMove;
import static amazons.Move.mv;
import static amazons.Board.*;
/** A Player that takes input as text commands from the standard input.
* @author Khang and Paul
*/
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() {
/**
* line g1-g7(b7)
*/
while (true) {
String line = _controller.readLine();
if (line == null) {
return "quit";
} else if (isGrammaticalMove(line)
&& _controller.board().winner() == null) {
if (!_controller.board().isLegal(mv(line))) {
_controller.reportError("Invalid move. "
+ "Please try again.");
} else {
return line;
}
} else {
return line;
}
}
}
}