-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtest.java
More file actions
451 lines (395 loc) · 15.7 KB
/
test.java
File metadata and controls
451 lines (395 loc) · 15.7 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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
class GenericTicTacToe
{
private int lineSize, boardArrayLength;
private int boardValues[];
private int allLinesIndex[][];
private int playerBoardStatus[];
private int computerBoardStatus[];
private int numberOfLines;
private int playerSum, computerSum, playerPreSum, computerPreSum;
//Patch work :(
private int numberOfMoves;
private final static int EMPTY_VALUE = 0;
private final static int PLAYER_VALUE = 1;
private static int COMPUTER_VALUE;
public final static int GAME_INPLAY = 0;
public final static int GAME_PLAYER_WINS = 1;
public final static int GAME_COMPUTER_WINS = 2;
public final static int GAME_TIE = 3;
public GenericTicTacToe(int bSize)
{
this.lineSize = bSize;
boardArrayLength = lineSize * lineSize;
//ensures value of single computer move is higher than playerSum
COMPUTER_VALUE = lineSize + 1;
//to find if any player has won
playerSum = PLAYER_VALUE * lineSize;
computerSum = COMPUTER_VALUE * lineSize;
//to find winning move
playerPreSum = PLAYER_VALUE * (lineSize -1);
computerPreSum = COMPUTER_VALUE * (lineSize -1);
initializeArrays();
getAllLinesIndex();
}
private void initializeArrays()
{
boardValues = new int[boardArrayLength];
playerBoardStatus = new int[boardArrayLength];
computerBoardStatus = new int[boardArrayLength];
for(int i : boardValues)
i = EMPTY_VALUE;
}
public int getComputerMove()
{
int lineSum, index;
int playerPreSumIndex = -1;
int computerPreSumIndex = -1;
boolean indexMatch;
int playerMaxStrength = 0;
int computerMaxStrength = 0;
//to detect tie, initialize index as -1
int playerMaxStrengthIndex = -1;
int computerMaxStrengthIndex = -1;
//initialize board status for every function call
for(int i = 0; i < boardArrayLength; i++)
{
playerBoardStatus[i] = 0;
computerBoardStatus[i] = 0;
}
//for each empty board index, iterate over all lines to calculate index strength
for(int i = 0; i < boardArrayLength; i++)
{
//strength of only empty cells are needed
if(boardValues[i] != EMPTY_VALUE)
continue;
for(int j = 0; j < numberOfLines; j++)
{
lineSum = 0;
indexMatch = false;
for(int k = 0; k < lineSize; k++)
{
index = allLinesIndex[j][k];
lineSum += boardValues[index];
if(i == index)
indexMatch = true;
}
//if board index isn't part of this line, don't use it for index strength calculation
if(indexMatch == false)
continue;
//empty line or line with only player moves
if(lineSum >= 0 && lineSum <= playerSum)
playerBoardStatus[i] += lineSum + 1;
//empty line or line with only computer moves
if(lineSum == 0 || lineSum%COMPUTER_VALUE == 0)
computerBoardStatus[i] += lineSum/COMPUTER_VALUE + 1;
//save winning move board index (line with one empty cell)
if(lineSum == playerPreSum && playerPreSumIndex == -1)
playerPreSumIndex = i;
if(lineSum == computerPreSum && computerPreSumIndex == -1)
computerPreSumIndex = i;
}
//save board index with highest strength for both player and computer
if(playerBoardStatus[i] > playerMaxStrength)
{
playerMaxStrengthIndex = i;
playerMaxStrength = playerBoardStatus[i];
}
if(computerBoardStatus[i] > computerMaxStrength)
{
computerMaxStrengthIndex = i;
computerMaxStrength = computerBoardStatus[i];
}
}
//prints board and status matrices
//matrix orientation won't match with actual game board
//printBoardStatus();
//decision making to choose computer's next move
// 1) Choose computer winning move if available
// 2) Block player winning move if available
// 3) If player max index strength is >= computer max, choose that
// 4) Else choose computer max index strength
// 5) Check for 3x3 corner case - patch work
if(computerPreSumIndex != -1)
index = computerPreSumIndex;
else if(playerPreSumIndex != -1)
index = playerPreSumIndex;
else if(playerMaxStrength >= computerMaxStrength)
index = playerMaxStrengthIndex;
else
index = computerMaxStrengthIndex;
//patch work
if(numberOfMoves == 3 && lineSize == 3)
{
//diagonal 1
if(boardValues[0] == PLAYER_VALUE && boardValues[4] == COMPUTER_VALUE && boardValues[8] == PLAYER_VALUE)
index = 1;
//diagonal 2
if(boardValues[2] == PLAYER_VALUE && boardValues[4] == COMPUTER_VALUE && boardValues[6] == PLAYER_VALUE)
index = 1;
}
//game is heading to a tie if index = -1
//playerMaxStrengthIndex and computerMaxStrengthIndex will be -1 if neither player has possibility to form a line
if(index == -1)
{
//return the first empty index
for(int i = 0; i < boardArrayLength; i++)
if(boardValues[i] == EMPTY_VALUE)
index = i;
}
setComputerMove(index);
return index;
}
//to detect if game is over or inplay
public int getGameState()
{
for(int i = 0; i < numberOfLines; i++)
{
int lineSum = 0;
for(int j = 0; j < lineSize; j++)
lineSum += boardValues[allLinesIndex[i][j]];
if(lineSum == playerSum)
return GAME_PLAYER_WINS;
else if(lineSum == computerSum)
return GAME_COMPUTER_WINS;
}
for(int i = 0; i < boardArrayLength; i++)
if(boardValues[i] == EMPTY_VALUE)
return GAME_INPLAY;
return GAME_TIE;
}
//indices of winning line
public int[] getWinningIndices()
{
int winningIndex = 0;
int winningArray[] = new int[lineSize];
for(int i = 0; i < numberOfLines; i++)
{
int lineSum = 0;
for(int j = 0; j < lineSize; j++)
lineSum += boardValues[allLinesIndex[i][j]];
if(lineSum == playerSum || lineSum == computerSum)
{
winningIndex = i;
break;
}
}
for(int i = 0; i < lineSize; i++)
winningArray[i] = allLinesIndex[winningIndex][i];
return winningArray;
}
private void getAllLinesIndex()
{
numberOfLines = (lineSize * 2) + 2;
allLinesIndex = new int[numberOfLines][lineSize];
//Horizontal Lines
for(int i = 0; i < lineSize; i++)
for(int j = 0; j < lineSize; j++)
allLinesIndex[i][j] = i*lineSize + j;
//Vertical Lines
for(int i = 0; i < lineSize; i++)
for(int j = 0; j < lineSize; j++)
allLinesIndex[i + lineSize][j] = i + j*lineSize;
//Diagonal Lines
for(int i = 0; i < lineSize; i++)
allLinesIndex[lineSize *2][i] = i + i*lineSize;
for(int i = 0; i < lineSize; i++)
allLinesIndex[lineSize *2 + 1][i] = (lineSize - 1) + i*lineSize - i;
}
public void setPlayerMove(int index)
{
boardValues[index] = PLAYER_VALUE;
numberOfMoves++;
}
public void setComputerMove(int index)
{
boardValues[index] = COMPUTER_VALUE;
numberOfMoves++;
}
private void printBoardStatus()
{
printBoard();
System.out.println("--------------Player--------------");
for(int i = 0; i < boardArrayLength; i++)
{
System.out.print(" " + playerBoardStatus[i]);
if((i+1)% lineSize == 0)
System.out.println();
}
System.out.println("-------------------------------------");
System.out.println("--------------Computer--------------");
for(int i = 0; i < boardArrayLength; i++)
{
System.out.print(" " + computerBoardStatus[i]);
if((i+1)% lineSize == 0)
System.out.println();
}
System.out.println("-------------------------------------");
}
public void printBoard()
{
System.out.println("--------------Board--------------");
for(int i = 0; i < boardArrayLength; i++)
{
System.out.print(" " + boardValues[i]);
if((i+1)% lineSize == 0)
System.out.println();
}
System.out.println("-------------------------------------");
}
}
class test
{
static int board[];
static int gameState;
static GenericTicTacToe ticTacToeBoard;
//3 initial points - 0,1,4 are enough as board is symmetrical
//but better to check all indices as AI implementation may not be truly symmetrical
static int level1[] = {0,1,2,3,4,5,6,7,8};
static int level2[] = {0,1,2,3,4,5,6,7,8};
static int level3[] = {0,1,2,3,4,5,6,7,8};
static int level4[] = {0,1,2,3,4,5,6,7,8};
static int level5[] = {0,1,2,3,4,5,6,7,8};
static int playerMoves[], computerMoves[];
static int playerIndex, computerIndex;
static int l2_index, l3_index, l4_index, l5_index;
static int moveLevel;
public static void main(String args[])
{
playerMoves = new int[5];
computerMoves = new int[4];
for(int l1 = 0; l1 < 9; l1++)
{
initGameBoard();
playerIndex = level1[l1];
setPlayerMove(playerIndex);
computerIndex = setComputerMove();
playerMoves[0] = playerIndex;
computerMoves[0] = computerIndex;
for(int l2 = 0; l2 < 9; l2++)
{
if(board[l2] != 0)
continue;
else
{
playerIndex = level2[l2];
setPlayerMove(playerIndex);
computerIndex = setComputerMove();
playerMoves[1] = playerIndex;
computerMoves[1] = computerIndex;
for(int l3 = 0; l3 < 9; l3++)
{
if(board[l3] != 0)
continue;
else
{
playerIndex = level3[l3];
setPlayerMove(playerIndex);
playerMoves[2] = playerIndex;
if(gameInPlay() == true)
{
computerIndex = setComputerMove();
computerMoves[2] = computerIndex;
if(gameInPlay() == true)
{
for(int l4 = 0; l4 < 9; l4++)
{
if(board[l4] != 0)
continue;
else
{
playerIndex = level4[l4];
setPlayerMove(playerIndex);
playerMoves[3] = playerIndex;
if(gameInPlay() == true)
{
computerIndex = setComputerMove();
computerMoves[3] = computerIndex;
if(gameInPlay() == true)
{
//fifth player move - has to be only one empty cell
//so no need to restart game at end of iteration
for(int l5 = 0; l5 < 9; l5++)
{
if(board[l5] != 0)
continue;
else
{
playerIndex = level5[l5];
setPlayerMove(playerIndex);
playerMoves[4] = playerIndex;
if(gameInPlay() == true)
{
computerIndex = setComputerMove();
computerMoves[4] = computerIndex;
}
}
}
}
}
}
startGameSpecificFlow(3);
}
}
}
}
startGameSpecificFlow(2);
}
}
startGameSpecificFlow(1);
}
}
}
private static void startGameSpecificFlow(int level)
{
initGameBoard();
for(int i = 0; i < level; i++)
{
int playerIndex = playerMoves[i];
setPlayerMove(playerIndex);
setComputerMove();
}
}
private static void initGameBoard()
{
ticTacToeBoard = new GenericTicTacToe(3);
board = new int[9];
for(int i = 0; i < 9; i++)
board[i] = 0;
}
private static boolean gameInPlay()
{
int gameState = ticTacToeBoard.getGameState();
if(gameState == GenericTicTacToe.GAME_INPLAY)
return true;
else
{
if(gameState == GenericTicTacToe.GAME_PLAYER_WINS)
System.out.println("Player Wins");
else if(gameState == GenericTicTacToe.GAME_COMPUTER_WINS)
System.out.println("Computer Wins");
else
System.out.println("Tie");
System.out.println("---------Player Moves--------");
for(int i = 0; i < 5; i++)
System.out.print(" " + playerMoves[i]);
System.out.println("\n----------------------------------");
System.out.println("---------Computer Moves--------");
for(int i = 0; i < 4; i++)
System.out.print(" " + computerMoves[i]);
System.out.println("\n----------------------------------");
ticTacToeBoard.printBoard();
return false;
}
}
private static int setComputerMove()
{
int computerIndex = ticTacToeBoard.getComputerMove();
board[computerIndex] = 2;
return computerIndex;
}
private static void setPlayerMove(int index)
{
board[index] = 1;
ticTacToeBoard.setPlayerMove(index);
}
}