-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGame.java
More file actions
59 lines (47 loc) · 1.91 KB
/
Game.java
File metadata and controls
59 lines (47 loc) · 1.91 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
51
52
53
54
55
56
57
58
59
import java.io.Console;
class Game{
public static void main(String[] args){
System.out.println("Welcome to Connect4 against a minimax AI!");
System.out.println("\t-To play your piece simply enter the column then click enter");
System.out.println("\t-If you ever want to leave the game type 'exit'");
Board board = new Board();
System.out.println(board);
// Keep playing until somebody wins
while(!board.isDraw() && !board.checkWin('X') && !board.checkWin('O')){
// Try to play a piece
Console console = System.console();
String input = console.readLine("Enter column to play: ");
int result=-1;
try{
result = Integer.parseInt(input);
}
catch(Exception e){
if(input.equals("exit")){
System.exit(0);
}
System.out.println("\nPlease enter a number!\n");
continue;
}
if(board.isColPlayable(result)){
board.playPiece('X', result);
}else{
System.out.println("\nPlease enter a playable column!\n");
continue;
}
// Print the board after a piece is played
System.out.println(board);
// If the game hasn't been won have the AI play a piece
if(!board.isDraw() && !board.checkWin('X') && !board.checkWin('O')){
int move = AIHelper.minimax(4, 'O', Integer.MIN_VALUE, Integer.MAX_VALUE, board)[1]; // Generate best move
board.playPiece('O', move);
}
// Print the board after the AI plays
System.out.println(board);
}
if(board.checkWin('X')){
System.out.println("You win!");
}else if(board.checkWin('O')){
System.out.println("The AI wins!");
}
}
}