-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBoard.java
258 lines (233 loc) · 6.15 KB
/
Board.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
import java.io.*;
import java.util.Vector;
public class Board {
public static final int GAME_OVER = 1;
public static final int CONTINUE = 0;
public static final int ILLEGAL_MOVE = -1;
static final int PLAYER_WHITE = 1;
static final int PLAYER_BLACK = 2;
static final int OBSERVER = 3;
public int game_state = CONTINUE;
public int to_move = PLAYER_BLACK;
public int serial = 1;
// rules-specific game state
Move previous_move = null;
int square[][] = new int[5][5];
static final int WHITE_CHECKER = PLAYER_WHITE;
static final int BLACK_CHECKER = PLAYER_BLACK;
public Board() {
for (int i = 0; i < 5; i++)
for (int j = 0; j < 5; j++)
square[i][j] = 0;
}
public Board(Board b) {
previous_move = b.previous_move;
for (int i = 0; i < 5; i++)
for (int j = 0; j < 5; j++)
square[i][j] = b.square[i][j];
to_move = b.to_move;
game_state = b.game_state;
serial = b.serial;
}
public void print(PrintStream s) {
if (Gthd.time_controls)
s.print("381 ");
else
s.print("380 ");
s.print(serial);
s.print(" ");
if (Gthd.time_controls) {
s.print(Gthd.secs(Gthd.black_msecs));
s.print(" ");
s.print(Gthd.secs(Gthd.white_msecs));
s.print(" ");
}
if (game_state == GAME_OVER)
s.print("*");
else if (to_move == PLAYER_WHITE)
s.print("w");
else
s.print("b");
s.print("\r\n");
s.flush();
s.print("382\r\n");
print_board(s);
s.flush();
}
public void print_board(PrintStream s) {
for (int j = 4; j >= 0; --j) {
for (int i = 0; i < 5; i++)
switch (square[i][j]) {
case 0: s.print("."); break;
case BLACK_CHECKER: s.print("b"); break;
case WHITE_CHECKER: s.print("w"); break;
default: s.print("?");
}
s.print("\r\n");
}
}
static final int opponent(int player) {
if (player == PLAYER_WHITE)
return PLAYER_BLACK;
if (player == PLAYER_BLACK)
return PLAYER_WHITE;
throw new Error("internal error: bad player");
}
// XXX see declaration of BLACK_CHECKER, WHITE_CHECKER
static final int checker_of(int player) {
return player;
}
static final int owner_of(int checker) {
return checker;
}
static final boolean[][] scratch_board() {
boolean[][] scratch = new boolean[5][5];
for (int i = 0; i < 5; i++)
for (int j = 0; j < 5; j++)
scratch[i][j] = false;
return scratch;
}
void flood(boolean[][] scratch, int color, int x, int y) {
/* off board */
if (!(x >= 0 && x <= 4 && y >= 0 && y <= 4))
return;
/* already done */
if (scratch[x][y])
return;
/* wrong color */
if (square[x][y] != color)
return;
/* ok */
scratch[x][y] = true;
flood(scratch, color, x - 1, y);
flood(scratch, color, x + 1, y);
flood(scratch, color, x, y - 1);
flood(scratch, color, x, y + 1);
}
static final boolean group_border(boolean[][] scratch, int x, int y) {
if (scratch[x][y])
return false;
if (x > 0 && scratch[x - 1][y])
return true;
if (x < 4 && scratch[x + 1][y])
return true;
if (y > 0 && scratch[x][y - 1])
return true;
if (y < 4 && scratch[x][y + 1])
return true;
return false;
}
int liberties(int x, int y) {
boolean[][] scratch = scratch_board();
flood(scratch, square[x][y], x, y);
int n = 0;
for (int i = 0; i < 5; i++)
for (int j = 0; j < 5; j++)
if (square[i][j] == 0 && group_border(scratch, i, j))
n++;
return n;
}
boolean move_ok(Move m) {
if (m.isPass())
return true;
if (square[m.x][m.y] != 0)
return false;
square[m.x][m.y] = to_move;
int n = liberties(m.x, m.y);
square[m.x][m.y] = 0;
if (n == 0)
return false;
return true;
}
Vector<Move> genMoves() {
Vector<Move> result = new Vector<Move>();
for (int i = 0; i < 5; i++)
for (int j = 0; j < 5; j++)
if (square[i][j] == 0) {
Move m = new Move(i, j);
if (move_ok(m))
result.add(m);
}
return result;
}
private boolean has_moves() {
Vector m = genMoves();
return m.size() > 0;
}
void capture(int x, int y) {
if (liberties(x, y) > 0)
return;
/* XXX this duplicates a lot of work, but
who cares? This is just for the referee */
boolean[][] scratch = scratch_board();
flood(scratch, square[x][y], x, y);
for (int i = 0; i < 5; i++)
for (int j = 0; j < 5; j++)
if (scratch[i][j])
square[i][j] = to_move;
}
void do_captures(Move m) {
if (m.x > 0 && square[m.x - 1][m.y] == opponent(to_move))
capture(m.x - 1, m.y);
if (m.x < 4 && square[m.x + 1][m.y] == opponent(to_move))
capture(m.x + 1, m.y);
if (m.y > 0 && square[m.x][m.y - 1] == opponent(to_move))
capture(m.x, m.y - 1);
if (m.y < 4 && square[m.x][m.y + 1] == opponent(to_move))
capture(m.x, m.y + 1);
}
public void makeMove(Move m) {
previous_move = m;
if (m.isPass())
return;
square[m.x][m.y] = to_move;
do_captures(m);
}
private static final boolean debug_try_move = false;
public int try_move(Move m) {
if (debug_try_move)
System.err.println("entering try_move()");
if (game_state != CONTINUE) {
if (debug_try_move)
System.err.println("leaving try_move(): move after game over");
return ILLEGAL_MOVE;
}
if (m.isPass() && previous_move != null && previous_move.isPass()) {
game_state = GAME_OVER;
if (debug_try_move)
System.err.println("leaving try_move(): game over");
return GAME_OVER;
}
if (!move_ok(m)) {
if (debug_try_move)
System.err.println("leaving try_move(): illegal move");
return ILLEGAL_MOVE;
}
if (debug_try_move)
System.err.println("move ok");
makeMove(m);
to_move = opponent(to_move);
if (to_move == PLAYER_BLACK)
serial++;
if (debug_try_move)
System.err.println("leaving try_move(): continue game");
return CONTINUE;
}
public int referee() {
if (game_state != GAME_OVER)
throw new Error("internal error: referee unfinished game");
int nblack = 0;
int nwhite = 0;
for (int i = 0; i < 5; i++)
for (int j = 0; j < 5; j++)
switch (square[i][j]) {
case BLACK_CHECKER: nblack++; break;
case WHITE_CHECKER: nwhite++; break;
}
if (nblack > nwhite)
return PLAYER_BLACK;
if (nwhite > nblack)
return PLAYER_WHITE;
return OBSERVER;
}
}