-
Notifications
You must be signed in to change notification settings - Fork 0
/
RandomPlayer.java
48 lines (37 loc) · 1.15 KB
/
RandomPlayer.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
package assignment2017;
import assignment2017.codeprovided.ColumnFullException;
import assignment2017.codeprovided.Connect4GameState;
import assignment2017.codeprovided.Connect4Player;
import assignment2017.codeprovided.IllegalColumnException;
/**
* An AI player of Connect 4, making random moves on the board. Should be easy
* to beat!
*
* @author Daniel Marshall
*/
public class RandomPlayer extends Connect4Player {
/**
* Allows the AI 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 = true;
int entry = 0;
do {
valid = true;
entry = (int) (Math.random() * Connect4GameState.NUM_COLS);
try {
gamestate.move(entry);
} catch (IllegalColumnException exception) {
valid = false;
} catch (ColumnFullException exception) {
valid = false;
}
} while (!valid);
System.out.println("Computer dropped counter in column " + entry);
}
}