-
Notifications
You must be signed in to change notification settings - Fork 0
/
Connect4SinglePlayer.java
45 lines (43 loc) · 1.51 KB
/
Connect4SinglePlayer.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
// Single player Connect 4 against Monte Carlo Tree Search AI
import java.util.concurrent.TimeUnit;
import java.util.Scanner;
public class Connect4SinglePlayer {
public static void main(String[] args) {
final long GIVEN_TIME = TimeUnit.SECONDS.toNanos(args.length > 0 ? Integer.parseInt(args[0]) : 2);
Scanner in = new Scanner(System.in);
Connect4Board board = new Connect4Board();
boolean turn = Connect4Board.PLAYER_1_TURN;
Connect4AI ai = new Connect4AI(board, GIVEN_TIME);
while(board.currentGameState() == Connect4Board.ONGOING) {
System.out.println("\n\n"+board);
int moveColumn;
do {
if(board.getNextTurn() == Connect4Board.PLAYER_1_TURN) {
System.out.printf("Enter your move: ", board.getNextTurn() == Connect4Board.PLAYER_1_TURN ? 1 : 2);
moveColumn = in.nextInt();
}
else {
System.out.print("AI determining move: ");
moveColumn = ai.getOptimalMove();
System.out.println(moveColumn);
}
} while(!board.canPlace(moveColumn));
board.place(moveColumn);
ai.update(moveColumn);
}
int gameState = board.currentGameState();
System.out.println("\n\n\n\n\n");
System.out.println(board);
switch(gameState) {
case Connect4Board.PLAYER_1_WON:
System.out.println("You won.\n");
break;
case Connect4Board.PLAYER_2_WON:
System.out.println("AI won.\n");
break;
default:
System.out.println("Tie.\n");
break;
}
}
}