-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChineseCheckerState.java
More file actions
448 lines (366 loc) · 10.6 KB
/
Copy pathChineseCheckerState.java
File metadata and controls
448 lines (366 loc) · 10.6 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
import java.util.*;
public class ChineseCheckerState extends BoardState {
public final int minimumBoardSize = 5;
public final int boardSize;
protected final Player initialBoardCells[][];
protected final Player boardCells[][];
protected Point2D player1cog;
protected Point2D player2cog;
protected Direction player1direction;
private final int X = 1;
private final int Y = 0;
private final int[][] directions = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
private final int Undefined = -1;
private int pivot_x = Undefined;
private int pivot_y = Undefined;
ChineseCheckerState(int boardSize)
{
boardSize = Math.max(boardSize, minimumBoardSize);
this.boardSize = boardSize;
this.initialBoardCells = new Player[boardSize][boardSize];
this.boardCells = new Player[boardSize][boardSize];
createInitialBoard();
}
ChineseCheckerState(ChineseCheckerState state)
{
this.boardSize = state.boardSize;
this.initialBoardCells = state.initialBoardCells;
this.boardCells = new Player[boardSize][boardSize];
this.player1cog = state.player1cog;
this.player2cog = state.player2cog;
this.player1direction = state.player1direction;
this.pivot_x = state.pivot_x;
this.pivot_y = state.pivot_y;
for (int y=0; y<boardSize; y++) {
for (int x=0; x<boardSize; x++) {
this.boardCells[y][x] = state.boardCells[y][x];
}
}
}
private void createInitialBoard()
{
// create initial board configuration
int s = (boardSize - 1) / 2;
if (boardSize % 2 == 0) {
for (int y=0; y<s; y++) {
for (int x=0; x<s; x++) {
this.initialBoardCells[y][x] = Player.One;
this.initialBoardCells[boardSize-y-1][boardSize-x-1] = Player.Two;
}
}
}
else
{
for (int y=0; y<=s; y++) {
for (int x=0; x<=y; x++) {
this.initialBoardCells[s-y][x] = Player.One;
this.initialBoardCells[boardSize-(s-y)-1][boardSize-x-1] = Player.Two;
}
}
}
// assign initial board configuration to game board
for (int y=0; y<boardSize; y++) {
for (int x=0; x<boardSize; x++) {
this.boardCells[y][x] = this.initialBoardCells[y][x];
}
}
// calculate center of gravity for player 1 and player 2
player1cog = calculateCenterOfGravity(Player.One);
player2cog = calculateCenterOfGravity(Player.Two);
// move direction for player 1 (player 2 is the opposite direction)
player1direction = calculateMoveDirection();
}
public Point2D calculateCenterOfGravity(Player player)
{
int count = 0;
double xSum = 0.0;
double ySum = 0.0;
for (int y=0; y<boardSize; y++) {
for (int x=0; x<boardSize; x++) {
if (boardCells[y][x] == player) {
count++;
xSum += x;
ySum += y;
}
}
}
if (count > 0)
return new Point2D(xSum / count, ySum / count);
else
return null;
}
private Direction calculateMoveDirection()
{
Point2D difference = Point2D.subtract(player2cog, player1cog);
int dirx = (int)Math.signum(difference.x);
int diry = (int)Math.signum(difference.y);
return new Direction(dirx, diry);
}
public Direction getPlayer1Direction()
{
return player1direction;
}
public Direction getPlayer2Direction()
{
return player1direction.getOppositeDirection();
}
public final boolean inside(int x, int y) {
return (x >= 0 && x < boardSize && y >= 0 && y < boardSize);
}
@Override
public boolean equals(Object obj)
{
if (this == obj) {
return true;
}
if (obj instanceof ChineseCheckerState) {
ChineseCheckerState state = (ChineseCheckerState)obj;
for (int y=0; y<boardSize; y++) {
for (int x=0; x<boardSize; x++) {
if (this.boardCells[y][x] != state.boardCells[y][x]) {
return false;
}
}
}
return true;
}
return false;
}
@Override
public int hashCode()
{
return toString().hashCode();
}
@Override
boolean set(int x, int y, Player player)
{
if (inside(x, y)) {
boardCells[y][x] = player;
return true;
}
else
return false;
}
@Override
Player get(int x, int y) {
if (inside(x, y))
return boardCells[y][x];
else
return null;
}
@Override
public boolean wins(Player player)
{
Player opponent = player.getOpponent();
for (int y=0; y<boardSize; y++) {
for (int x=0; x<boardSize; x++) {
if (initialBoardCells[y][x] == opponent) {
if (boardCells[y][x] != player) {
return false;
}
}
else {
if (boardCells[y][x] == player) {
return false;
}
}
}
}
return true;
}
@Override
public boolean isTie()
{
return (!wins(Player.One) &&
!wins(Player.Two) &&
getSuccessors(Player.One).isEmpty() &&
getSuccessors(Player.Two).isEmpty());
}
@Override
public boolean isTerminal()
{
return wins(Player.One) || wins(Player.Two) || isTie();
}
public boolean isInOpponentArea(Player player, int x, int y)
{
if (x>=0 && x<boardSize && y>=0 && y<boardSize)
return initialBoardCells[y][x] == player.getOpponent();
else
return false;
}
private void clearPivots()
{
pivot_x = Undefined;
pivot_y = Undefined;
}
public List<Direction> getMovements(ChineseCheckerState state, Player player, int x, int y)
{
List<Direction> movements = new ArrayList<>();
boolean inOpponentArea = isInOpponentArea(player, x, y);
Direction moveDirection = (player == Player.One ? getPlayer1Direction() : getPlayer2Direction());
for (int[] direction : directions) {
int nx = x + direction[X];
int ny = y + direction[Y];
boolean useDirection = (nx>=0 && nx<boardSize && ny>=0 && ny<boardSize && state.boardCells[ny][nx] == null);
if (!inOpponentArea) {
if ((direction[X] != 0 && direction[X] != moveDirection.x) ||
(direction[Y] != 0 && direction[Y] != moveDirection.y))
{
useDirection = false;
}
}
if (useDirection) {
movements.add(new Direction(direction[X], direction[Y]));
}
}
return movements;
}
public List<Direction> getJumps(ChineseCheckerState state, Player player, int x, int y)
{
List<Direction> movements = new ArrayList<>();
boolean inOpponentArea = isInOpponentArea(player, x, y);
Direction moveDirection = (player == Player.One ? getPlayer1Direction() : getPlayer2Direction());
for (int[] direction : directions) {
int nx1 = x + direction[X];
int ny1 = y + direction[Y];
int nx2 = nx1 + direction[X];
int ny2 = ny1 + direction[Y];
boolean useDirection = (inside(nx1, ny1) && inside(nx2, ny2) &&
state.boardCells[ny1][nx1] != null &&
state.boardCells[ny2][nx2] == null);
if (!inOpponentArea) {
if ((direction[X] != 0 && direction[X] != moveDirection.x) ||
(direction[Y] != 0 && direction[Y] != moveDirection.y))
{
useDirection = false;
}
}
if (useDirection) {
movements.add(new Direction(direction[X], direction[Y]));
}
}
return movements;
}
private ChineseCheckerState createSuccessor(BoardState boardState, Player player, int x, int y, int nx, int ny)
{
if (inside(nx, ny)) {
try {
ChineseCheckerState successor = (ChineseCheckerState)boardState.clone();
successor.boardCells[y][x] = null;
successor.boardCells[ny][nx] = player;
return successor;
}
catch (CloneNotSupportedException e)
{
e.printStackTrace();
}
}
return null;
}
@Override
public List<BoardState> getSuccessors(Player player)
{
Set<BoardState> childSet = new HashSet<>();
Stack<ChineseCheckerState> stack = new Stack<>();
// single moves
stack.push(this);
while (!stack.isEmpty()) {
ChineseCheckerState currentState = stack.pop();
for (int y=0; y<boardSize; y++) {
for (int x=0; x<boardSize; x++) {
if (currentState.boardCells[y][x] == player) {
currentState.clearPivots();
for (Direction move : getMovements(currentState, player, x, y)) {
int nx = x + move.x;
int ny = y + move.y;
ChineseCheckerState childState = createSuccessor(currentState, player, x, y, nx, ny);
if (childState != null && !childSet.contains(childState)) {
childSet.add(childState);
}
}
}
}
}
}
// jumps
for (int y=0; y<boardSize; y++) {
for (int x=0; x<boardSize; x++) {
if (boardCells[y][x] == player) {
for (Direction move : getJumps(this, player, x, y)) {
int pivot_x = x + 2*move.x;
int pivot_y = y + 2*move.y;
ChineseCheckerState childState = createSuccessor(this, player, x, y, pivot_x, pivot_y);
if (childState != null) {
childState.pivot_x = pivot_x;
childState.pivot_y = pivot_y;
if (!childSet.contains(childState)) {
stack.push(childState);
childSet.add(childState);
}
}
}
}
}
}
while (!stack.isEmpty()) {
ChineseCheckerState currentState = stack.pop();
int x = currentState.pivot_x;
int y = currentState.pivot_y;
for (Direction move : getJumps(currentState, player, x, y)) {
int pivot_x = x + 2*move.x;
int pivot_y = y + 2*move.y;
ChineseCheckerState childState = createSuccessor(currentState, player, x, y, pivot_x, pivot_y);
if (childState != null) {
childState.pivot_x = pivot_x;
childState.pivot_y = pivot_y;
if (!childSet.contains(childState)) {
stack.push(childState);
childSet.add(childState);
}
}
}
}
clearPivots();
return new ArrayList<>(childSet);
}
@Override
public BoardState getSwitchedBoard()
{
ChineseCheckerState switchedBoard = new ChineseCheckerState(boardSize);
for (int y=0; y<boardSize; y++) {
for (int x=0; x<boardSize; x++) {
if (boardCells[y][x] == null)
switchedBoard.boardCells[y][x] = null;
else
switchedBoard.boardCells[y][x] = boardCells[y][x].getOpponent();
if (initialBoardCells[y][x] == null)
switchedBoard.initialBoardCells[y][x] = null;
else
switchedBoard.initialBoardCells[y][x] = initialBoardCells[y][x].getOpponent();
}
}
switchedBoard.player1direction = player1direction.getOppositeDirection();
switchedBoard.player1cog = player2cog;
switchedBoard.player2cog = player1cog;
return switchedBoard;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
for (int y=0; y<boardSize; y++) {
for (int x=0; x<boardSize; x++) {
if (boardCells[y][x] == null)
sb.append(".");
else
sb.append(boardCells[y][x].toString());
}
sb.append('\n');
}
return sb.toString();
}
@Override
public BoardState clone() throws CloneNotSupportedException
{
return new ChineseCheckerState(this);
}
}