-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTicTacToe.java
198 lines (169 loc) · 7.5 KB
/
TicTacToe.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import java.util.Scanner;
import java.awt.Point;
class TicTacToe {
// Stores the game board
class GameState {
// total squares filled
int total = 0;
// owner of each tile (No owner = 0, user = 1, computer = 2)
int grid[][] = new int[3][3];
// default constructor
GameState() {}
// constructor clones an older gameState
GameState(GameState game) {
for (int i = 0; i < 3; i++) {grid[i] = game.grid[i].clone();}
this.total = game.total;
}
// set a point
void set(Point cnt,int owner) {
grid[cnt.x][cnt.y] = owner;
total++;
}
// display the board
void board() {
//System.out.println("∎∎∎∎∎∎∎");
for (int i = 0; i < 3; i++) {
System.out.print("∎");
for (int j = 0; j < 3; j++) {
switch (grid[j][i]) {
case 1: System.out.print("O"); break;
case 2: System.out.print("X"); break;
default: System.out.print("."); break;
}
System.out.print("∎");
}
System.out.println();
}
}
// displays the board in error stream
void debug() {
System.err.println("Debug grid:");
for (int i = 0; i < 3; i++) {
System.err.print("|");
for (int j = 0; j < 3; j++) {
switch (grid[j][i]) {
case 1: System.err.print("O"); break;
case 2: System.err.print("X"); break;
default: System.err.print("."); break;
}
System.err.print("|");
}
System.err.println();
}
}
// returns winner of the game (-1 if no result, 0 if draw, 1 for user, 2 for computer)
int getWinner() {
for (int i = 0; i < 3; i++) {
if (grid[i][0] != 0 && grid[i][0] == grid[i][1] && grid[i][1] == grid[i][2]) return grid[i][1];
if (grid[0][i] != 0 && grid[0][i] == grid[1][i] && grid[2][i] == grid[1][i]) return grid[1][i];
}
if (grid[1][1] != 0 && grid[0][0] == grid[1][1] && grid[1][1] == grid[2][2]) return grid[1][1];
if (grid[1][1] != 0 && grid[2][0] == grid[1][1] && grid[1][1] == grid[0][2]) return grid[1][1];
if (total >= 9) {return 0;}
return -1;
}
}
int maxDepth = 9;
int bestMove;
// minimax method
int minimax(GameState state, int player, int depth, int beta, int alpha) {
// check for winner before goind further
int winner = state.getWinner();
if (winner == 0) {
//state.debug();
//System.err.println("At depth = "+(maxDepth - depth)+" winner = "+winner);
}
if (winner == 1) return -1;
else if (winner == 2) return +1;
else if (winner == 0) return 0;
int best = player == 1 ? 2 : -2;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (state.grid[j][i] != 0) {continue;}
GameState newState = new GameState(state); // clone the game state
newState.set(new Point(j,i),player); // set at position
int tempScore = minimax(newState,2-player+1,depth-1,beta,alpha); // get score by minimax
if (player == 2 && tempScore > best) { // for max-node
if (tempScore >= beta) {return beta;} // alpha-beta pruning
if (depth == maxDepth) {bestMove = j*10+i;}
if (tempScore > alpha) {alpha = tempScore;}
best = tempScore;
} else if (player == 1 && tempScore < best) { // for min node
if (tempScore <= alpha) {return alpha;} // alpha-beta pruning
if (tempScore < beta) {beta = tempScore;}
best = tempScore;
}
}
}
return best;
}
public void main1(Scanner in) {
GameState game = new GameState();
game.board();
while (true) {
System.out.print("Enter move: ");
int x = in.nextInt();
int y = in.nextInt();
if (game.grid[x][y] != 0 ) {System.out.println("You CHEATED! Game will terminate -_-"); return;}
game.set(new Point(x,y) , 1);
game.board();
if (game.total == 9) {System.out.println("Match tied! ;)"); return;}
int winner = game.getWinner();
if (winner == 1) {System.out.println("You beat my AI!!"); return;}
else if (winner == 2) {System.out.println("My AI beat you!!"); return;}
TicTacToe AI = new TicTacToe();
int best = AI.minimax(new GameState(game), 2, AI.maxDepth, 1000, -1000);
int move = AI.bestMove;
x = move/10;
y = move%10;
System.out.println("Computer gives: "+x+" "+y);
if (game.grid[x][y] != 0 ) {System.out.println("Computer CHEATED! Game will terminate :/"); return;}
game.set(new Point(x,y) , 2);
game.board();
winner = game.getWinner();
if (winner == 1) {System.out.println("You beat my AI!!"); return;}
else if (winner == 2) {System.out.println("My AI beat you!!"); return;}
}
}
public void main2(Scanner in) {
GameState game = new GameState();
game.board();
while (true) {
TicTacToe AI = new TicTacToe();
int best = AI.minimax(new GameState(game), 2, AI.maxDepth, 1000, -1000);
int move = AI.bestMove;
int x = move/10;
int y = move%10;
System.out.println("Computer gives: "+x+" "+y);
if (game.grid[x][y] != 0 ) {System.out.println("Computer CHEATED! Game will terminate :/"); return;}
game.set(new Point(x,y) , 2);
game.board();
if (game.total == 9) {System.out.println("Match tied! ;)"); return;}
int winner = game.getWinner();
if (winner == 1) {System.out.println("You beat my AI!!"); return;}
else if (winner == 2) {System.out.println("My AI beat you!!"); return;}
System.out.print("Enter move: ");
x = in.nextInt();
y = in.nextInt();
if (game.grid[x][y] != 0 ) {System.out.println("You CHEATED! Game will terminate -_-"); return;}
game.set(new Point(x,y) , 1);
game.board();
winner = game.getWinner();
if (winner == 1) {System.out.println("You beat my AI!!"); return;}
else if (winner == 2) {System.out.println("My AI beat you!!"); return;}
}
}
public static void main(String[] args) {
System.out.println("Tic Tac Toe: \n\nRules:\n1. Enter your coordinates as \"x y\"\n2. Coordinates are numbered from (0,0) to (2,2)\n3. Entering invalid coordinates will make the game terminate.\n\n\nPlay:");
Scanner in = new Scanner(System.in);
TicTacToe game = new TicTacToe();
System.out.print("1. You start\n2. Computer starts\nEnter choice: ");
int ch = in.nextInt();
System.out.println();
switch (ch) {
case 1: game.main1(in); return;
case 2: game.main2(in); return;
default: System.out.println("Wrong choice -_-");
}
}
}