-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAI.java
More file actions
361 lines (293 loc) · 7.17 KB
/
AI.java
File metadata and controls
361 lines (293 loc) · 7.17 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
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
class GameAI
{
class Move
{
int row, col,bead;
};
static char player = 'o', opponent = 'x';
boolean isMovesLeft(char board[][][])
{
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
for(int k=0;k<4;k++)
{
if (board[i][j][k] == '_'){
//dis(board);
return true; }
}
}
}
return false;
}
int evaluate(char[][][] board)
{
for(int i=0;i<4;i++)
{
for(int j=0;j<4;j++)
{
if(board[i][j][0]!= '_' && board[i][j][0]==board[i][j][1] && board[i][j][1]==board[i][j][2] && board[i][j][2]==board[i][j][3] )
{
//System.out.println(board[i][j][0]);
if(board[i][j][0] == player)
{
return 10;
}
else if(board[i][j][0] == opponent)
{
return -10;
}
}
else if(board[i][0][j]!= '_' && board[i][0][j]==board[i][1][j] && board[i][1][j]==board[i][2][j] && board[i][2][j]==board[i][3][j])
{
if(board[i][0][j] == player)
{
return 10;
}
else if(board[i][0][j] == opponent)
{
return -10;
}
}
else if(board[0][i][j]!= '_' && board[0][i][j]==board[1][i][j] && board[1][i][j]==board[2][i][j] && board[2][i][j]==board[3][i][j])
{
if(board[0][i][j] == player)
{
return 10;
}
else if(board[0][i][j] == opponent)
{
return -10;
}
}
}
}
for(int i=0;i<4;i++)
{
if(board[i][0][0]!= '_' && board[i][0][0]==board[i][1][1] && board[i][1][1]==board[i][2][2] && board[i][2][2]==board[i][3][3])
{
//System.out.println(board[i][0][0]);
//dis(board);
if(board[i][0][0] == player)
{
return 10;
}
else if(board[i][0][0] == opponent)
{
return -10;
}
}
else if(board[i][0][3] != '_' && board[i][0][3]==board[i][1][2] && board[i][1][2]==board[i][2][1] && board[i][2][1]==board[i][3][0])
{
if(board[i][0][3] == player)
{
return 10;
}
else if(board[i][0][3] == opponent)
{
return -10;
}
}
else if(board[0][i][0]!= '_' && board[0][i][0]==board[1][i][1] && board[1][i][1]==board[2][i][2] && board[2][i][2]==board[3][i][3])
{
//System.out.println(board[0][i][0]);
if(board[0][i][0] == player)
{
return 10;
}
else if(board[0][i][0] == opponent)
{
return -10;
}
}
else if(board[0][i][3]!= '_' && board[0][i][3]==board[1][i][2] && board[1][i][2]==board[2][i][1] && board[2][i][1]==board[3][i][0])
{
if(board[0][i][3] == player)
{
return 10;
}
else if(board[0][i][3] == opponent)
{
return -10;
}
}
}
return 0;
}
// This is the minimax function. It considers all
// the possible ways the game can go and returns
// the value of the board
int minimax(char board[][][],int depth, boolean isMax)
{
int score = evaluate(board);
//dis(board);
//System.out.println(score);
// If Maximizer has won the game
// return his/her evaluated score
if (score == 10){
//System.out.println(score);
return score;}
// If Minimizer has won the game
// return his/her evaluated score
else if (score == -10) {
return score; }
// If there are no more moves and
// no winner then it is a tie
else if (isMovesLeft(board) == false)
{return 0; }
// If this maximizer's move
else if (isMax)
{
int best = -1000;
// Traverse all cells
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
for(int k=0;k<4;k++)
{
// Check if cell is empty
if (board[i][j][k]=='_')
{
// Make the move
board[i][j][k] = player;
// Call minimax recursively and choose
// the maximum value
best = Math.max(best, minimax(board,
depth + 1, !isMax));
//System.out.println(best);
// Undo the move
board[i][j][k] = '_';
}
}
}
}
return best;
}
// If this minimizer's move
else
{
int best = 1000;
// Traverse all cells
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
for(int k=0;k<4;k++)
// Check if cell is empty
if (board[i][j][k] == '_')
{
// Make the move
board[i][j][k] = opponent;
// Call minimax recursively and choose
// the minimum value
best = Math.min(best, minimax(board,
depth + 1, !isMax));
// Undo the move
board[i][j][k] = '_';
}
}
}
return best;
}
}
// This will return the best possible
// move for the player
Move findBestMove(char board[][][])
{
int bestVal = -1000;
Move bestMove = new Move();
bestMove.row = -1;
bestMove.col = -1;
bestMove.bead = -1;
// Traverse all cells, evaluate minimax function
// for all empty cells. And return the cell
// with optimal value.
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
for(int k=0;k<4;k++)
{
// Check if cell is empty
if (board[i][j][k] == '_')
{
// Make the move
board[i][j][k] = player;
dis(board);
// compute evaluation function for this
// move.
int moveVal = minimax(board, 0, false);
System.out.println(moveVal);
// Undo the move
board[i][j][k] = '_';
// If the value of the current move is
// more than the best value, then update
// best/
if (moveVal > bestVal)
{
bestMove.row = i;
bestMove.col = j;
bestMove.bead = k;
bestVal = moveVal;
}
}
}
}
}
System.out.printf("The value of the best Move " +
"is : %d-%d-%d",bestMove.row,bestMove.col,bestMove.bead);
return bestMove;
}
void dis(char board[][][])
{
for(int i=0;i<4;i++)
{
for(int j=0;j<4;j++)
{
for(int k=0;k<4;k++)
{
System.out.print(board[i][j][k]);
}
System.out.print(" ");
}
System.out.println();
}
}
}
public class AI
{
public static void main(String arg[])
{
GameAI ai = new GameAI();
char[][][] board = new char[4][4][4];
for(int i=0;i<4;i++)
{
for(int j=0;j<4;j++)
{
for(int k=0;k<4;k++)
{
board[i][j][k] ='_';
}
}
}
board[0][3][3]='x';
board[1][3][3]='x';
board[2][3][3]='x';
//board[3][3][3]='x';
for(int i=0;i<4;i++)
{
for(int j=0;j<4;j++)
{
for(int k=0;k<4;k++)
{
System.out.print(board[i][j][k]);
}
System.out.print(" ");
}
System.out.println();
}
System.out.println(ai.findBestMove(board));
}
}