-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathGenericTicTacToe.java
More file actions
275 lines (238 loc) · 9.05 KB
/
GenericTicTacToe.java
File metadata and controls
275 lines (238 loc) · 9.05 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
package me.squaretictactoe.genericTicTacToe;
public 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);
numberOfMoves = 0;
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;
//patch work
numberOfMoves++;
}
public void setComputerMove(int index)
{
boardValues[index] = COMPUTER_VALUE;
//patch work
numberOfMoves++;
}
private void printBoardStatus()
{
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("--------------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("-------------------------------------");
}
}