-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathboard.java
More file actions
283 lines (264 loc) · 8.93 KB
/
board.java
File metadata and controls
283 lines (264 loc) · 8.93 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
//This is our implimentation of a the othello game board.
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;
public class board {
public int[][] curBoard;
int size = 8;
String rows = "abcdefgh"; // Needed for moveReader
//
// A two int coord Pair
public record CoordPair(int x, int y) {
}
/**
* Constructs a new Othello board with all cells set to empty.
* Initializes an 8x8 board.
*/
board() {
curBoard = new int[size][size];
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
curBoard[i][j] = 0;
}
}
}
/**
* Prints the current state of the board to standard output, displaying columns
* as letters (A-H) and rows as numbers (1-8). Empty cells show as '#', white as
* 'W', black as 'B'.
*/
public void printBoard() {
// Prints the x axis & letters
for (int i = size; i > 0; i--) {
System.out.print(i);
// Prints the y axis & numbers
for (int j = 0; j < size; j++) {
if (curBoard[j][i - 1] == 0) {
System.out.print("#");
} else if (curBoard[j][i - 1] == 1) {
System.out.print("W");
} else {
System.out.print("B");
}
}
System.out.println();
}
System.out.print(" ABCDEFGH\n");
System.out.print(" 12345678\n");
}
/**
* Resets the board to the standard starting position for an Othello game
*/
public void setBoard() {
curBoard[3][3] = 2;
curBoard[3][4] = 1;
curBoard[4][4] = 2;
curBoard[4][3] = 1;
}
/**
* Parses a move string (e.g., "A4" or "f2") into a board coordinate pair.
*
* @param move The move string in the format letter+number (e.g., "A4").
* @return A CoordPair representing the board coordinates for the move.
*/
public CoordPair moveReader(String move) {
CoordPair out = new CoordPair(
rows.indexOf(Character.toLowerCase(move.charAt(0))),
Integer.parseInt(move.substring(1, 2)) - 1);
return out;
}
/**
* Checks if a move is valid for the specified color.
*
* @param move The coordinate pair for the proposed move.
* @param checkRange If true, checks that the move is within bounds.
* @param color The color making the move (1 for white, 2 for black).
* @return True if the move is valid, false otherwise.
*/
public boolean moveChecker(CoordPair move, boolean checkRange, int color) {
// check in bounds
if (checkRange &&
(move.x < 0 || move.x >= size || move.y < 0 || move.y >= size)) {
return false;
}
// check empty
if (curBoard[move.x][move.y] != 0) {
return false;
}
// check if move captures in any direction
return canCaptureInAnyDirection(move, color);
}
/**
* Checks if they can capture in any direction. Does not give direction or how
* many
*
* @param move
* @param color
* @return
*/
private boolean canCaptureInAnyDirection(CoordPair move, int color) {
int[] dx = { -1, -1, -1, 0, 0, 1, 1, 1 };
int[] dy = { -1, 0, 1, -1, 1, -1, 0, 1 };
for (int dir = 0; dir < 8; dir++) {
if (capturesInDirectionTest(move, color, dx[dir], dy[dir])) {
return true;
}
}
return false;
}
/**
* Checks if a move in a given direction results in capturing any opponent's
* pieces. Used as a helper in move checking logic.
*
* @param move The coordinate pair representing the move.
* @param color The color making the move (1 for white, 2 for black).
* @param dx The horizontal direction delta.
* @param dy The vertical direction delta.
* @return true if the move captures any pieces in this direction; false
* otherwise.
*/
private boolean capturesInDirectionTest(CoordPair move, int color, int dx, int dy) {
int opposite = (color == 1) ? 2 : 1;
int x = move.x + dx;
int y = move.y + dy;
boolean foundOpponent = false;
// First, must find at least one opponent piece
while (x >= 0 && x < size && y >= 0 && y < size) {
if (curBoard[x][y] == opposite) {
foundOpponent = true;
} else if (curBoard[x][y] == color && foundOpponent) {
return true; // Sequence: our piece after opponent(s)
} else {
break;
}
x += dx;
y += dy;
}
return false;
}
/**
* Sets the contents of a board slot.
*
* @param move The coordinate pair of the slot to modify.
* @param state The state to set (0 = empty, 1 = white, 2 = black).
*/
public void setSlot(CoordPair move, int state) {
curBoard[move.x][move.y] = state;
}
/**
* Attempts to make a move for the given color, updating the board if the move
* is valid.
*
* @param move The coordinate pair for the move.
* @param color The color making the move (1 for white, 2 for black).
* @return True if the move was valid and made, false otherwise.
*/
public Boolean makeMove(CoordPair move, int color) {
if (moveChecker(move, true, color)) {
curBoard[move.x][move.y] = color; // Place piece
flipCapturedInAnyDirection(move, color); // Flip captured pieces!
return true;
}
return false;
}
/**
* Flips any pieces captured by the last move in all directions.
* Called after placing a valid move to update the board.
*
* @param move The coordinate pair for the move.
* @param color The color that just made the move.
*/
private void flipCapturedInAnyDirection(CoordPair move, int color) {
int[] dx = { -1, -1, -1, 0, 0, 1, 1, 1 };
int[] dy = { -1, 0, 1, -1, 1, -1, 0, 1 };
for (int dir = 0; dir < 8; dir++) {
flipCapturedInDirection(move, color, dx[dir], dy[dir]);
}
}
/**
* Flips any captured opponent pieces in the specified direction after a move.
*
* @param move The coordinate pair for the move.
* @param color The color making the move.
* @param dx The horizontal direction delta.
* @param dy The vertical direction delta.
*/
private void flipCapturedInDirection(CoordPair move, int color, int dx, int dy) {
int opposite = (color == 1) ? 2 : 1;
int x = move.x + dx;
int y = move.y + dy;
java.util.List<CoordPair> toFlip = new java.util.ArrayList<>();
// First find a stretch of opponent pieces
while (x >= 0 && x < size && y >= 0 && y < size && curBoard[x][y] == opposite) {
toFlip.add(new CoordPair(x, y));
x += dx;
y += dy;
}
// If next cell is player's own piece, flip collected
if (x >= 0 && x < size && y >= 0 && y < size && curBoard[x][y] == color) {
for (CoordPair c : toFlip) {
curBoard[c.x][c.y] = color;
}
}
}
/**
* Sets the board state using a 64-character string of digits ('0', '1', '2')
* in row-major order, filling curBoard accordingly.
*
* @param state The string representation of the board state.
*/
public void setState(String state) {
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
curBoard[j][i] = Character.getNumericValue(state.charAt(i * 8 + j));
}
}
}
/**
* Outputs a 64-character string representation of the board,
* suitable for restoring the board via setState. The string is in row-major
* order.
*
* @return the current state string of the board
*/
public String getState() {
StringBuilder out = new StringBuilder(64);
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
out.append(curBoard[j][i]);
}
}
return out.toString();
}
/**
* Gives a list of CoordPairs that boarder a given color & may be possible
* moves.
*
* @param color the color you want to find borders for
* @return An ArrayList<CoordPair> containg possible spots
*/
public ArrayList<CoordPair> getBorderingEmptySlots(int color) {
ArrayList<CoordPair> bordering = new ArrayList<>();
Set<String> seen = new HashSet<>();
int[] dx = { -1, -1, -1, 0, 0, 1, 1, 1 };
int[] dy = { -1, 0, 1, -1, 1, -1, 0, 1 };
for (int x = 0; x < size; x++) {
for (int y = 0; y < size; y++) {
if (curBoard[x][y] == color) {
for (int dir = 0; dir < 8; dir++) {
int nx = x + dx[dir];
int ny = y + dy[dir];
if (nx >= 0 && nx < size && ny >= 0 && ny < size && curBoard[nx][ny] == 0) {
String key = nx + "," + ny;
if (!seen.contains(key)) {
seen.add(key);
bordering.add(new CoordPair(nx, ny));
}
}
}
}
}
}
return bordering;
}
}