-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKeyboardPlayer.java
59 lines (44 loc) · 1.46 KB
/
KeyboardPlayer.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package assignment2017;
import java.util.Scanner;
import assignment2017.codeprovided.ColumnFullException;
import assignment2017.codeprovided.Connect4GameState;
import assignment2017.codeprovided.Connect4Player;
import assignment2017.codeprovided.IllegalColumnException;
/**
* A human controlled player for Connect 4, with moves entered using the
* keyboard.
*
* @author Daniel Marshall
*/
public class KeyboardPlayer extends Connect4Player {
private final Scanner input = new Scanner(System.in);
/**
* Allows the human player to submit a move, checking it is valid and then
* carrying it out in the game.
*
* @param gamestate
* The current state of the game.
*/
@Override
public void makeMove(Connect4GameState gamestate) {
boolean valid = false;
do {
System.out.println("Please enter a column number, 0 to " + ((Connect4GameState.NUM_COLS) - 1)
+ " followed by return.");
valid = true;
while (!input.hasNextInt()) {
System.out.println("Please enter a column number, 0 to "
+ ((Connect4GameState.NUM_COLS) - 1) + " followed by return.");
input.next();
}
int entry = input.nextInt();
try {
gamestate.move(entry);
} catch (IllegalColumnException exception) {
valid = false;
} catch (ColumnFullException exception) {
valid = false;
}
} while (!valid);
}
}