-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBoardState.java
executable file
·299 lines (227 loc) · 8.51 KB
/
BoardState.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
import java.util.ArrayList;
/**
*
* @author leyantong
*/
public class BoardState {
private int[][] board;
public int colour;
public BoardState(){
// Create board
board= new int[8][8];
}
// Clone the current object
public BoardState deepCopy(){
BoardState newBoardState= new BoardState();
for(int i= 0; i< 8; i++)
for(int j= 0; j < 8; j++)
newBoardState.setContents(i, j, getContents(i,j));
newBoardState.colour= colour;
return newBoardState;
}
public int getContents(int x, int y){
return board[x][y];
}
public void setContents(int x, int y, int piece){
board[x][y]= piece;
}
// Under construction Assumes coordinates are in order.
private boolean checkOnBoard(int x, int y){
return (x >=0) && (x < 8) && (y >=0) && (y < 8);
}
// Returns the list of legal moves for current player.
public ArrayList<Move> getLegalMoves(){
// Very simple method
ArrayList<Move> ans= new ArrayList<Move>();
for(int i= 0; i < 8; i++)
for(int j= 0; j < 8; j++)
if(checkLegalMove(i,j))
ans.add(new Move(i,j));
return ans;
}
// Tests whether the game is over
public boolean gameOver(){
boolean ans= false;
if(getLegalMoves().isEmpty()){
colour= -colour; // change colour temporarily
if(getLegalMoves().isEmpty())
ans= true;
colour= -colour; // change colour back
}
return ans;
}
public boolean checkLegalMove(int x, int y){
return
checkOnBoard(x,y) && (getContents(x,y) == 0) &&
((checkLeft(x, y) != -1) || (checkRight(x, y) != -1) ||
(checkUp(x, y) != -1) || (checkDown(x, y) != -1) ||
(checkUpLeft(x, y) != -1) || (checkUpRight(x, y) != -1) ||
(checkDownLeft(x, y) != -1) || (checkDownRight(x, y) != -1));
}
public void makeLegalMove(int x, int y){
int a,b; // work variables (indices)
// Current square
// setContents(x, y, colour);
// Squares to left
a= checkLeft(x, y);
if(a != -1)
for(int i=x; i > a; i--)
setContents(i,y, colour);
// Squares to right
a= checkRight(x, y);
if(a != -1)
for(int i=x; i < a; i++)
setContents(i,y, colour);
// Squares above
a= checkUp(x, y);
if(a != -1)
for(int j=y; j > a; j--)
setContents(x, j, colour);
// Squares below
a= checkDown(x, y);
if(a != -1)
for(int j=y; j < a; j++)
setContents(x, j, colour);
// Squares to above left
a= checkUpLeft(x, y);
if(a != -1)
for(int i=x, j= y; i > a; i--, j--)
setContents(i,j, colour);
// Squares above right
a= checkUpRight(x, y);
if(a != -1)
for(int i=x, j= y; i < a; i++,j--)
setContents(i,j, colour);
// Squares to below left
a= checkDownLeft(x, y);
if(a != -1)
for(int i=x, j= y; i > a; i--, j++)
setContents(i,j, colour);
// Squares below right
a= checkDownRight(x, y);
if(a != -1)
for(int i=x, j= y; i < a; i++,j++)
setContents(i,j, colour);
colour= -colour; // Change player
// Now the computer moves
}
// Return largest value of i < x st board[j,i] is opposite to color for all
// j (i < j < x) and board[i,y] is colour, and return -1 if none in range
private int checkLeft(int x, int y){
int i= x-1;
while((i > 0) && (getContents(i,y) == -colour)) //check almost to start
i--;
if((i < x-1) && getContents(i,y) == colour)
return i;
else
return -1;
}
// Return largest value of i > x st board[j,i] is opposite to color for all
// j (i > j > x) and board[i,y] is colour, and return -1 if none in range
private int checkRight(int x, int y){
int i= x+1;
while((i < 7) && (getContents(i,y) == -colour)) // Check almost to end
i++;
if((i > x+1) && getContents(i,y) == colour)
return i;
else
return -1;
}
// Return largest value of i < y st board[x,j] is opposite to color for all
// j (i < j < y) and board[x,i] is colour, and return -1 if none in range
private int checkUp(int x, int y){
int i= y-1;
while((i > 0) && (getContents(x,i) == -colour)) // Check almost to start
i--;
if((i < y-1) && getContents(x,i) == colour)
return i;
else
return -1;
}
// Return largest value of i > y st board[x,j] is opposite to color for all
// j (i > j > y) and board[x,i] is colour, and return -1 if none in range
private int checkDown(int x, int y){
int i= y+1;
while((i < 7) && (getContents(x,i) == -colour)) // Check almost to end
i++;
if((i > y+1) && getContents(x,i) == colour)
return i;
else
return -1;
}
// Return largest value of i < x st board[x-j,y-j] is opposite to color for all
// j (i < j < endOfBoard) and board[i,y] is colour, and return -1 if none in range
private int checkUpLeft(int x, int y){
int i= x-1;
int j= y-1;
while((i > 0) && (j > 0) && (getContents(i,j) == -colour)){ //check almost to start
i--;
j--;
}
if((i < x-1) && getContents(i,j) == colour)
return i;
else
return -1;
}
// Return largest value of i > x st board[j,i] is opposite to color for all
// j (i > j > x) and board[i,y] is colour, and return -1 if none in range
private int checkUpRight(int x, int y){
int i= x+1;
int j= y-1;
while((i < 7) && (j > 0) && (getContents(i,j) == -colour)){ // Check almost to end
i++;
j--;
}
if((i > x+1) && getContents(i,j) == colour)
return i;
else
return -1;
}
// Return largest value of i < x st board[x-j,y-j] is opposite to color for all
// j (i < j < endOfBoard) and board[i,y] is colour, and return -1 if none in range
private int checkDownLeft(int x, int y){
int i= x-1;
int j= y+1;
while((i > 0) && (j <7) && (getContents(i,j) == -colour)){ //check almost to start
i--;
j++;
}
if((i < x-1) && getContents(i,j) == colour)
return i;
else
return -1;
}
// Return largest value of i > x st board[j,i] is opposite to color for all
// j (i > j > x) and board[i,y] is colour, and return -1 if none in range
private int checkDownRight(int x, int y){
int i= x+1;
int j= y+1;
while((i < 7) && (j < 7) && (getContents(i,j) == -colour)){ // Check almost to end
i++;
j++;
}
if((i > x+1) && getContents(i,j) == colour)
return i;
else
return -1;
}
public String resultString(){
String ans;
int white= 0;
int black= 0;
for(int i= 0; i < 8; i++)
for(int j=0; j < 8; j++)
if(getContents(i,j)==1)
white++;
else if(getContents(i,j)==-1)
black++;
if(white > black)
ans= "White wins";
else if(black > white)
ans= "Black wins";
else
ans= "Draw";
ans+= "[White: " + white+"; black: "+black+"]";
return ans;
}
}