-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTabuSearch.java
240 lines (188 loc) · 6.89 KB
/
TabuSearch.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/* ===== ===== =====
Theory of Programming
Artificial Intelligence - MiniMax Algorithm
http://theoryofprogramming.com/2017/12/12/minimax-algorithm/
GitHub - https://github.com/VamsiSangam/theoryofprogramming
Code Contributor - Vamsi Sangam
===== ===== ===== */
import java.util.Scanner;
public class MiniMaxAlgorithm {
static int X = 1; // Player who always maximises score
static int O = -1; // Player who always minimizes score
static int E = 0; // Value which denotes an empty space
public static void main(String[] args) {
int N = 3;
int[][] game = new int[N][N];
Scanner in = new Scanner(System.in);
int currentPlayer = X;
System.out.println("Play TicTacToe! You are X!");
while (true) {
printGame(game);
if (currentPlayer == X) {
System.out.println("It is X's turn. Enter row and column (1 indexed) -");
int row = in.nextInt() - 1;
int col = in.nextInt() - 1;
if (game[row][col] == E) {
game[row][col] = X;
currentPlayer = O;
} else {
System.out.println("Entered row and column is already played!");
}
} else { // Player O's turn
System.out.println("It is O's turn.");
computeAndPlayBestMove(game, O);
currentPlayer = X;
}
if (isTerminalState(game)) {
break;
}
}
if (hasPlayerWon(game, X)) {
System.out.println("Player X has won!");
} else if (hasPlayerWon(game, O)) {
System.out.println("Player O has won!");
} else {
System.out.println("It is a draw!");
}
printGame(game);
}
// Minimax algorithm entry method. Uses minTurn() and maxTurn() to
// compute the best move for the current player and plays that move
public static void computeAndPlayBestMove(int[][] game, int player) {
Result result;
if (player == X) {
result = maxTurn(game, 0);
} else {
result = minTurn(game, 0);
}
Move bestMove = result.move;
game[bestMove.i][bestMove.j] = player;
}
// Method to compute best move for player X (maximising player)
// Recursively calls minTurn()
static Result maxTurn(int[][] game, int depth) {
if (isTerminalState(game)) {
return new Result(score(game, depth), null);
}
Result max = new Result(Integer.MIN_VALUE, new Move(-1, -1));
for (int i = 0; i < game.length; ++i) {
for (int j = 0; j < game[i].length; ++j) {
if (game[i][j] == E) {
game[i][j] = X;
// Recurse for other moves
Result currentMove = minTurn(game, depth + 1);
if (currentMove.score > max.score) {
max.score = currentMove.score;
max.move.i = i;
max.move.j = j;
}
game[i][j] = E;
}
}
}
return max;
}
// Method to compute best move for player O (minimising player)
// Recursively calls maxTurn()
static Result minTurn(int[][] game, int depth) {
if (isTerminalState(game)) {
return new Result(score(game, depth), null);
}
Result min = new Result(Integer.MAX_VALUE, new Move(-1, -1));
for (int i = 0; i < game.length; ++i) {
for (int j = 0; j < game[i].length; ++j) {
if (game[i][j] == E) {
game[i][j] = O;
// Recurse for other moves
Result currentMove = maxTurn(game, depth + 1);
if (currentMove.score < min.score) {
min.score = currentMove.score;
min.move.i = i;
min.move.j = j;
}
game[i][j] = E;
}
}
}
return min;
}
static boolean isTerminalState(int[][] game) {
return hasPlayerWon(game, X) || hasPlayerWon(game, O) || hasGameEnded(game);
}
public static boolean hasPlayerWon(int[][] game, int player) {
for (int i = 0; i < game.length; ++i) {
boolean isRowEqual = true, isColumnEqual = true;
for (int j = 0; j < game[i].length; ++j) {
if (game[i][j] != player) {
isRowEqual = false;
}
if (game[j][i] != player) {
isColumnEqual = false;
}
}
if (isRowEqual || isColumnEqual) {
return true;
}
}
boolean isDiagonalEqual = true, isAntiDiagonalEqual = true;
for (int i = 0; i < game.length; ++i) {
if (game[i][i] != player) {
isDiagonalEqual = false;
}
if (game[game.length - 1 - i][i] != player) {
isAntiDiagonalEqual = false;
}
}
return isDiagonalEqual | isAntiDiagonalEqual;
}
public static int score(int[][] game, int depth) {
if (hasPlayerWon(game, X)) {
return 10 - depth;
} else if (hasPlayerWon(game, O)) {
return depth - 10;
}
return 0;
}
static boolean hasGameEnded(int[][] game) {
for (int i = 0; i < game.length; ++i) {
for (int j = 0; j < game[i].length; ++j) {
if (game[i][j] == E) {
return false;
}
}
}
return true;
}
public static void printGame(int[][] game) {
System.out.println("\nState of TicTacToe game -");
for (int i = 0; i < game.length; ++i) {
for (int j = 0; j < game[i].length; ++j) {
if (game[i][j] == X) {
System.out.print("X ");
} else if (game[i][j] == O) {
System.out.print("O ");
} else {
System.out.print("_ ");
}
}
System.out.println();
}
}
}
class Result
{
int score;
Move move;
public Result(int score, Move move) {
this.score = score;
this.move = move;
}
}
class Move
{
int i, j;
public Move(int i, int j) {
this.i = i;
this.j = j;
}
}