-
Notifications
You must be signed in to change notification settings - Fork 0
/
SimPlay.java
352 lines (337 loc) · 11.8 KB
/
SimPlay.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
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
/**
* Write a description of class Play here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class SimPlay
{
private int x_;
private int y_;
private int[][] board_;
private int currentPlayer_;//Represents the player number who will make the next move with the board in its current state
/**
* Constructor for objects of class SimPlay
*/
public SimPlay(int[][] board, int currentPlayer)
{
x_ = -1;
y_ = -1;
board_ = board;
currentPlayer_ = currentPlayer;
}
/**
* Returns the best score player can achieve in depth number of rounds should both players play their most optimal moves each round
* In addition, if this object's currentPlayer can make a move, this function will also store in x_ and y_ the x and y coordinates
* that the current player must play in order to get that best score.
*/
public int bestCase(int player, int depth)
{
if( depth == 0 )
{
return getScore(player);
}
else
{
boolean playMade = false;
int bestScore = -1;
//Starting traversal through the board, seeing if the current player can play at any of the squares
for( int x = 0; x < board_.length; x++ )
{
for( int y = 0; y < board_.length; y++ )
{
if( canAttempt(x, y) )
{
int[][] copyOfBoard = copyBoard();
if( checkPlay(currentPlayer_, copyOfBoard, x, y) )//The board changes when the current player plays at (x, y)
{
playMade = true;
int opponent = currentPlayer_ == 1? 2:1;
SimPlay neighbour = new SimPlay(copyOfBoard, opponent);
int branchScore = neighbour.bestCase(player, depth--);
if( branchScore > bestScore )
{
bestScore = branchScore;
x_ = x;//The x coordinates of this move is stored
y_ = y;//The y coordinates of this move is stored
}
}
}
}
}
//If at any point in the previous traversal, the current player had a valid move
if( playMade )
{
return bestScore;
}
//Otherwise, it is certain that the current player has no valid moves.
//Therefore, a player switch occurs
currentPlayer_ = currentPlayer_ == 1? 2:1;
//After the player switch, the same logic is applied with the other player
for( int x = 0; x < board_.length; x++ )
{
for( int y = 0; y < board_.length; y++ )
{
if( canAttempt(x, y) )
{
int[][] copyOfBoard = copyBoard();
if( checkPlay(currentPlayer_, copyOfBoard, x, y) )//The board changes when the current player plays at (x, y)
{
playMade = true;
int opponent = currentPlayer_ == 1? 2:1;
SimPlay neighbour = new SimPlay(copyOfBoard, opponent);
int branchScore = neighbour.bestCase(player, depth--);
if( branchScore > bestScore )
{
bestScore = branchScore;
//The x and y coordinates are not stored since they are not moves that the original current player will make
}
}
}
}
}
if( playMade )
{
return bestScore;
}
else//Neither player has any valid moves, game is over at this level
{
return getScore(player);
}
}
}
/**
*
* North: xDif = 0, yDif = -1;
* NorthEast: xDif = 1, yDif = -1;
* East: xDif = 1, yDif = 0;
* SouthEast: xDif = 1, yDif = 1;
* South: xDif = 0, yDif = 1;
* SouthWest: xDif = -1, yDif = 1;
* West: xDif = -1, yDif = 0;
* NorthWest: xDif = -1, yDif = -1;
*
* @param attacker The integer representing the player attempting to make a play
* @param x The x coordinate of the starting piece
* @param y The y coordinate of the starting piece
* @param xDif The difference in x values in order to go a certain direction
* @param yDif The difference in y values in order to go a certain direction
* @return A 2D array containing all of the coordinates of pieces in the chain, if there is one
* Array of zero length is returned if no chain is found
*/
public void claimFor(int attacker, int[][] board, int x, int y, int xDif, int yDif)
{
int defender = attacker == 1? 2:1;//The player being attacked upon
int n = 1;
while( board[x + xDif * n][y + yDif * n] == defender )
{
board[x + xDif * n][y + yDif * n] = attacker;
n++;
}
board[x][y] = attacker;//Changes the first square to the attacker
}
private boolean checkPlay(int player, int[][] board, int x, int y)
{
boolean boardChanged = false;
if( hasChain(player, board, x, y, 0, -1) )//North
{
claimFor(player, board, x, y, 0, -1);
boardChanged = true;
}
if( hasChain(player, board, x, y, 1, -1) )//NorthEast
{
claimFor(player, board, x, y, 1, -1);
boardChanged = true;
}
if( hasChain(player, board, x, y, 1, 0) )//East
{
claimFor(player, board, x, y, 1, 0);
boardChanged = true;
}
if( hasChain(player, board, x, y, 1, 1) )//SouthEast
{
claimFor(player, board, x, y, 1, 1);
boardChanged = true;
}
if( hasChain(player, board, x, y, 0, 1) )//South
{
claimFor(player, board, x, y, 0, 1);
boardChanged = true;
}
if( hasChain(player, board, x, y, -1, 1) )//SouthWest
{
claimFor(player, board, x, y, -1, 1);
boardChanged = true;
}
if( hasChain(player, board, x, y, -1, 0) )//West
{
claimFor(player, board, x, y, -1, 0);
boardChanged = true;
}
if( hasChain(player, board, x, y, -1, -1) )//NorthWest
{
claimFor(player, board, x, y, -1, -1);
boardChanged = true;
}
return boardChanged;
}
/**
* Founds out whether or not if there is a chain for the attacker to
* claim in a certain direction.
* Below is a guide to the directions:
*
* North: xDif = 0, yDif = -1;
* NorthEast: xDif = 1, yDif = -1;
* East: xDif = 1, yDif = 0;
* SouthEast: xDif = 1, yDif = 1;
* South: xDif = 0, yDif = 1;
* SouthWest: xDif = -1, yDif = 1;
* West: xDif = -1, yDif = 0;
* NorthWest: xDif = -1, yDif = -1;
*
* @param attacker The integer representing the player attempting to make a play
* @param x The x coordinate of the starting piece
* @param y The y coordinate of the starting piece
* @param xDif The difference in x values in order to go a certain direction
* @param yDif The difference in y values in order to go a certain direction
* @return true if there is a chain, false otherwise
*/
public boolean hasChain(int attacker, int[][] board, int x, int y, int xDif, int yDif)
{
int defender = attacker == 1? 2:1;//The player being attacked upon
int chainSize = 0;//The size of a chain, if there is one
int n = 1;
while( board[x + xDif * n][y + yDif * n] == defender )
{
//Attempts to find a chain by looping through and seeing if consecutive chips are defenders
chainSize++;
n++;
}
int terminalChip = board[x + xDif * n][y + yDif * n];//The chip that stopped the loop, aka the last chip
return terminalChip == attacker && chainSize != 0;//Last chip was the same colour as the attacker and there were defender chips in between them
}
/**
* Determines whether or not if currentPlayer_ can attempt a play at the space (x, y)
* on board_.
*
* canAttempt returns true if:
* - (x, y) is an empty space and
* - At least one of the squares surrounding (x, y) is within the bounds
* of the board and belongs to the opponent of the current player
* canAttempt returns false otherwise
*
* Requires that (x, y) is within the bounds of board_.
*
* @param x The x coordinates of the space
* @param y The y coordinates of the space
*/
public boolean canAttempt(int x, int y)
{
if( board_[x][y] != 0 )//If the space (x, y) is not an empty space
{
return false;
}
//All logic from here on assumes (x, y) is an empty space
int opponent = currentPlayer_ == 1? 2:1;
int xP1 = x + 1;
int xS1 = x - 1;
int yP1 = y + 1;
int yS1 = y - 1;
if( xP1 < board_.length )
{
if( yP1 < board_.length && board_[xP1][yP1] == opponent )
{
return true;
}
else if( yS1 >= 0 && board_[xP1][yS1] == opponent )
{
return true;
}
else if( board_[xP1][y] == opponent )
{
return true;
}
}
if( xS1 >= 0 )
{
if( yP1 < board_.length && board_[xS1][yP1] == opponent )
{
return true;
}
else if( yS1 >= 0 && board_[xS1][yS1] == opponent )
{
return true;
}
else if( board_[xS1][y] == opponent )
{
return true;
}
}
if( yP1 < board_.length && board_[x][yP1] == opponent )
{
return true;
}
if( yS1 >= 0 && board_[x][yS1] == opponent )
{
return true;
}
return false;
}
/**
* Returns the total number of squares in this SimPlay's board that is of player's colour.
* That is to say, returns the total number of elements in SimPlay's board that is equal
* to player.
*
* @param player The number representing the player that this function is going to find the
* the score of.
*/
public int getScore(int player)
{
int result = 0;
for( int x = 0; x < board_.length; x++ )
{
for( int y = 0; y < board_.length; y++ )
{
if( board_[x][y] == player )
{
result++;
}
}
}
return result;
}
/**
* Creates a deep copy of this SimPlay's board_
*
* @return A 2D int array representing this SimPlay's board
*/
public int[][] copyBoard()
{
int sideLength = board_.length;
int[][] copy = new int[sideLength][sideLength];
for( int x = 0; x < sideLength; x++ )
{
for( int y = 0; y < sideLength; y++ )
{
copy[x][y] = board_[x][y];
}
}
return copy;
}
public int getX()
{
return x_;
}
public int getY()
{
return y_;
}
public int[][] getBoard()
{
return board_;
}
public int getCurrentPlayer()
{
return currentPlayer_;
}
}