Skip to content

Commit 91cfb50

Browse files
committed
add logic to remove dead stones at the end of the game
1 parent 91d74df commit 91cfb50

File tree

4 files changed

+106
-6
lines changed

4 files changed

+106
-6
lines changed

src/main/java/com/bernd/game/Board.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,8 @@ public static int[][] removeDeadStonesAround(
136136
if (groups.isEmpty()) {
137137
return board;
138138
}
139-
Function<int[][], int[][]> remove = removeStonesIn(board.length, groups.values());
140-
return remove.apply(board);
139+
Function<int[][], int[][]> update = removeStonesIn(board.length, groups.values());
140+
return update.apply(board);
141141
}
142142

143143
private static Function<int[][], int[][]> removeStonesIn(
@@ -147,12 +147,12 @@ private static Function<int[][], int[][]> removeStonesIn(
147147
for (StoneGroup group : groups) {
148148
size += group.points().size();
149149
}
150-
BoardUpdate result = BoardUpdate.builder(dim, size);
150+
BoardUpdate update = BoardUpdate.builder(dim, size);
151151
for (StoneGroup group : groups) {
152152
for (Point point : group.points()) {
153-
result.add(point, 0);
153+
update.add(point, 0);
154154
}
155155
}
156-
return result;
156+
return update;
157157
}
158158
}

src/main/java/com/bernd/game/PointQueue.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.bernd.game;
22

3-
class PointQueue {
3+
final class PointQueue {
44
private int pos;
55
private final int dim;
66
private final int[] queue;
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package com.bernd.game;
2+
3+
import com.bernd.util.BoardUpdate;
4+
import java.util.ArrayList;
5+
import java.util.List;
6+
7+
public class Remove {
8+
9+
private static List<Point> findGroup(
10+
int[][] board,
11+
int xx,
12+
int yy) {
13+
int color = board[yy][xx];
14+
if (color == 0) {
15+
return List.of();
16+
}
17+
int dim = board.length;
18+
PointQueue pointsToCheck = PointQueue.create(dim);
19+
boolean[] pointsChecked = new boolean[dim * dim];
20+
pointsToCheck.offer(xx, yy);
21+
List<Point> result = new ArrayList<>();
22+
while (!pointsToCheck.isEmpty()) {
23+
int ptId = pointsToCheck.poll();
24+
int y = ptId / dim;
25+
int x = ptId % dim;
26+
result.add(new Point(x, y));
27+
if (y > 0) {
28+
int c = board[y - 1][x];
29+
int bptId = (y - 1) * dim + x;
30+
if (c == color && !pointsChecked[bptId]) {
31+
pointsChecked[bptId] = true;
32+
pointsToCheck.offer(x, y - 1);
33+
}
34+
}
35+
if (y < dim - 1) {
36+
int c = board[y + 1][x];
37+
int bptId = (y + 1) * dim + x;
38+
if (c == color && !pointsChecked[bptId]) {
39+
pointsChecked[bptId] = true;
40+
pointsToCheck.offer(x, y + 1);
41+
}
42+
}
43+
if (x > 0) {
44+
int c = board[y][x - 1];
45+
int bptId = y * dim + x - 1;
46+
if (c == color && !pointsChecked[bptId]) {
47+
pointsChecked[bptId] = true;
48+
pointsToCheck.offer(x - 1, y);
49+
}
50+
}
51+
if (x < dim - 1) {
52+
int c = board[y][x + 1];
53+
int bptId = y * dim + x + 1;
54+
if (c == color && !pointsChecked[bptId]) {
55+
pointsChecked[bptId] = true;
56+
pointsToCheck.offer(x + 1, y);
57+
}
58+
}
59+
}
60+
return result;
61+
}
62+
63+
public static int[][] removeStonesAt(
64+
int[][] board, int x, int y) {
65+
List<Point> group = findGroup(board, x, y);
66+
if (group.isEmpty()) {
67+
return board;
68+
}
69+
BoardUpdate update = BoardUpdate.builder(board.length, group.size());
70+
for (Point point : group) {
71+
update.add(point, 0);
72+
}
73+
return update.apply(board);
74+
}
75+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.bernd.game;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
6+
7+
class RemoveTest {
8+
9+
@Test
10+
void testRemoveTwoStones() {
11+
int[][] position = new int[][]{
12+
new int[]{0, 4, 4, 0},
13+
new int[]{2, 2, 2, 2},
14+
new int[]{4, 4, 4, 4},
15+
new int[]{0, 0, 0, 0},
16+
};
17+
int[][] result = Remove.removeStonesAt(position, 1, 0);
18+
assertArrayEquals(new int[][]{
19+
new int[]{0, 0, 0, 0},
20+
new int[]{2, 2, 2, 2},
21+
new int[]{4, 4, 4, 4},
22+
new int[]{0, 0, 0, 0},
23+
}, result);
24+
}
25+
}

0 commit comments

Comments
 (0)