Skip to content

Commit 25a4054

Browse files
committed
add button to reset the counting
1 parent 350f0f3 commit 25a4054

File tree

8 files changed

+157
-49
lines changed

8 files changed

+157
-49
lines changed

src/main/client/src/Play.jsx

Lines changed: 45 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
import {
1717
StompContext,
1818
BLACK,
19+
WHITE,
1920
TERRITORY,
2021
getColorClassName,
2122
} from "./util.js"
@@ -41,11 +42,8 @@ export const Play = () => {
4142
let stompClient = useContext(StompContext)
4243
let auth = useAuthStore(state => state.auth)
4344
let setGameState = useGameStore(state => state.setGameState)
44-
let black = useGameStore(state => state.black)
45-
let white = useGameStore(state => state.white)
4645
let { board, currentPlayer, counting } = useGameStore(state => state.gameState)
4746
let initialized = useRef()
48-
let opponent = auth.name === black.name ? white : black
4947
useEffect(() => {
5048
if (initialized.current) {
5149
return
@@ -74,6 +72,15 @@ export const Play = () => {
7472
}),
7573
})
7674
}, [stompClient, gameId])
75+
let onResetCounting = useCallback(() => {
76+
stompClient.publish({
77+
destination: "/app/game/move",
78+
body: JSON.stringify({
79+
id: gameId,
80+
resetCounting: true,
81+
}),
82+
})
83+
}, [stompClient, gameId])
7784
let onClick = useCallback(({ x, y }) => {
7885
stompClient.publish({
7986
destination: "/app/game/move",
@@ -87,6 +94,7 @@ export const Play = () => {
8794
if (!board) {
8895
return <div>Spieldaten werden geladen...</div>
8996
}
97+
let result = counting ? getResult(board) : undefined
9098
return (
9199
<div className="mt-2 ml-4">
92100
<div className="relative w-full h-1">
@@ -120,15 +128,24 @@ export const Play = () => {
120128
Pass
121129
</Button>
122130
</div>
123-
<div className="mt-2">
124-
{
125-
counting ? "" : (
126-
currentPlayer === auth.name ?
127-
"Jetzt bin ich dran" :
128-
(opponent.name + " ist dran...")
129-
)
130-
}
131-
</div>
131+
{counting && (
132+
<div className="mt-2">
133+
<Button
134+
onClick={onResetCounting}>
135+
Reset Counting
136+
</Button>
137+
</div>
138+
)}
139+
{result && (
140+
<div className="mt-2">
141+
{"w: " + result.w + ", b: " + result.b}
142+
</div>
143+
)}
144+
{!counting && (
145+
<div className="mt-2">
146+
{currentPlayer + " ist dran..."}
147+
</div>
148+
)}
132149
</div>
133150
</div>
134151
)
@@ -215,3 +232,19 @@ function CountingTile({ groupInfo, onClick }) {
215232
</div>
216233
)
217234
}
235+
236+
function getResult(board) {
237+
let w = 0, b = 0
238+
for (let y = 0; y < board.length; y++) {
239+
for (let x = 0; x < board[y].length; x++) {
240+
let { color } = board[y][x]
241+
if ((color & WHITE) !== 0) {
242+
w++
243+
}
244+
if ((color & BLACK) !== 0) {
245+
b++
246+
}
247+
}
248+
}
249+
return { w, b }
250+
}

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

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
import com.bernd.util.Util;
44
import java.util.Arrays;
55

6-
import static com.bernd.game.Board.B;
76
import static com.bernd.game.Board.TERRITORY;
8-
import static com.bernd.game.Board.W;
7+
import static com.bernd.util.Util.COLORS;
8+
import static com.bernd.util.Util.MARKERS;
99
import static com.bernd.util.Util.isStone;
1010
import static com.bernd.util.Util.keepMarkers;
1111

@@ -67,12 +67,11 @@ static void colorEmptyTerritory(
6767
return;
6868
}
6969
boolean oppositeStonesFound = false;
70-
int oppositeColor = (color & W) != 0 ? B : W;
70+
int oppositeColor = (color & COLORS) ^ COLORS;
7171
int dim = board.length;
7272
PointList tracker = PointList.create(dim);
7373
PointQueue pointsToCheck = PointQueue.create(dim);
74-
acc[yy][xx] = color;
75-
tracker.add(xx, yy);
74+
acc[yy][xx] = keepMarkers(color, board[yy][xx]);
7675
pointsToCheck.offer(xx, yy);
7776
while (!pointsToCheck.isEmpty()) {
7877
int ptId = pointsToCheck.poll();
@@ -81,47 +80,39 @@ static void colorEmptyTerritory(
8180
tracker.add(x, y);
8281
if (y > 0) {
8382
int c = board[y - 1][x];
84-
if (c == oppositeColor) {
85-
oppositeStonesFound = true;
86-
}
83+
oppositeStonesFound |= c == oppositeColor;
8784
if (Util.isEmpty(c) && acc[y - 1][x] == -1) {
88-
acc[y - 1][x] = keepMarkers(color, c);
85+
acc[y - 1][x] = color | c & MARKERS;
8986
pointsToCheck.offer(x, y - 1);
9087
}
9188
}
9289
if (y < dim - 1) {
9390
int c = board[y + 1][x];
94-
if (c == oppositeColor) {
95-
oppositeStonesFound = true;
96-
}
91+
oppositeStonesFound |= c == oppositeColor;
9792
if (Util.isEmpty(c) && acc[y + 1][x] == -1) {
98-
acc[y + 1][x] = keepMarkers(color, c);
93+
acc[y + 1][x] = color | c & MARKERS;
9994
pointsToCheck.offer(x, y + 1);
10095
}
10196
}
10297
if (x > 0) {
10398
int c = board[y][x - 1];
104-
if (c == oppositeColor) {
105-
oppositeStonesFound = true;
106-
}
99+
oppositeStonesFound |= c == oppositeColor;
107100
if (Util.isEmpty(c) && acc[y][x - 1] == -1) {
108-
acc[y][x - 1] = keepMarkers(color, c);
101+
acc[y][x - 1] = color | c & MARKERS;
109102
pointsToCheck.offer(x - 1, y);
110103
}
111104
}
112105
if (x < dim - 1) {
113106
int c = board[y][x + 1];
114-
if (c == oppositeColor) {
115-
oppositeStonesFound = true;
116-
}
107+
oppositeStonesFound |= c == oppositeColor;
117108
if (Util.isEmpty(c) && acc[y][x + 1] == -1) {
118-
acc[y][x + 1] = keepMarkers(color, c);
109+
acc[y][x + 1] = color | c & MARKERS;
119110
pointsToCheck.offer(x + 1, y);
120111
}
121112
}
122113
}
123114
if (oppositeStonesFound) {
124-
tracker.forEach((x, y) -> acc[y][x] = 0);
115+
tracker.forEach((x, y) -> acc[y][x] = board[y][x] & ~TERRITORY);
125116
}
126117
}
127118

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import com.bernd.util.BoardUpdate;
44
import java.util.function.Function;
55

6+
import static com.bernd.game.Board.B;
7+
import static com.bernd.game.Board.W;
68
import static com.bernd.game.Board.REMOVED;
79
import static com.bernd.game.Board.TERRITORY;
810

@@ -68,4 +70,25 @@ public static int[][] toggleStonesAt(
6870
Function<int[][], int[][]> update = toggleRemoved(board, x, y);
6971
return update.apply(board);
7072
}
73+
74+
public static int[][] resetCounting(
75+
int[][] board) {
76+
int dim = board.length;
77+
BoardUpdate update = BoardUpdate.builder(dim);
78+
for (int y = 0; y < dim; y++) {
79+
for (int x = 0; x < board[y].length; x++) {
80+
int color = board[y][x];
81+
if ((color & REMOVED) != 0) {
82+
color = color ^ (B | W);
83+
color &= ~REMOVED;
84+
color &= ~TERRITORY;
85+
}
86+
if ((color & TERRITORY) != 0) {
87+
color = 0;
88+
}
89+
update.add(x, y, color);
90+
}
91+
}
92+
return update.apply(board);
93+
}
7194
}

src/main/java/com/bernd/model/Game.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,12 @@ public Game update(Move move) {
3636

3737
private Game updateInternal(Move move) {
3838
if (counting) {
39+
if (move.resetCounting()) {
40+
int[][] resetted = Toggle.resetCounting(board);
41+
return game(Count.count(resetted));
42+
}
3943
int[][] toggled = Toggle.toggleStonesAt(board, move.x(), move.y());
40-
int[][] counted = Count.count(toggled);
41-
return game(counted);
44+
return game(Count.count(toggled));
4245
}
4346
if (move.pass()) {
4447
if (opponentPassed) {
Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
package com.bernd.model;
22

3-
public record Move(String id, boolean pass, int x, int y) {
3+
public record Move(
4+
String id,
5+
boolean pass,
6+
boolean resetCounting,
7+
int x,
8+
int y) {
49
}

src/main/java/com/bernd/util/Util.java

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
package com.bernd.util;
22

3-
import java.util.Arrays;
4-
53
import static com.bernd.game.Board.B;
64
import static com.bernd.game.Board.REMOVED;
75
import static com.bernd.game.Board.W;
86

97
public final class Util {
10-
private static final int MARKERS = ~B & ~W;
11-
private static final int COLORS = B | W;
8+
public static final int MARKERS = ~B & ~W;
9+
public static final int COLORS = B | W;
1210

1311
private Util() {
1412
}
@@ -41,12 +39,4 @@ public static int divUp(int i, int div) {
4139
public static boolean isStone(int color) {
4240
return (color & COLORS) != 0 && (color & MARKERS) == 0;
4341
}
44-
45-
public static int[][] copyBoard(int[][] board) {
46-
int[][] result = new int[board.length][];
47-
for (int y = 0; y < board.length; y++) {
48-
result[y] = Arrays.copyOf(board[y], board[y].length);
49-
}
50-
return result;
51-
}
5242
}

src/test/java/com/bernd/game/CountTest.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,27 @@ void testCornerEyeCountImpliedColor() {
223223
assertEquals(W | TERRITORY, getImpliedColor(board, 1, 1));
224224
}
225225

226+
@Test
227+
void testCountWhiteL() {
228+
int r = W | REMOVED;
229+
int f = B | REMOVED | TERRITORY;
230+
int t = B | TERRITORY;
231+
int[][] result = Count.count(new int[][]{
232+
new int[]{t, B, r, 0, 0},
233+
new int[]{B, B, r, 0, 0},
234+
new int[]{r, r, r, 0, 0},
235+
new int[]{0, 0, 0, 0, 0},
236+
new int[]{0, 0, 0, 0, 0},
237+
});
238+
assertArrayEquals(new int[][]{
239+
new int[]{t, B, f, t, t},
240+
new int[]{B, B, f, t, t},
241+
new int[]{f, f, f, t, t},
242+
new int[]{t, t, t, t, t},
243+
new int[]{t, t, t, t, t},
244+
}, result);
245+
}
246+
226247
static int[][] createEmptyBoard(int dim) {
227248
int[][] board = new int[dim][];
228249
for (int y = 0; y < dim; y++) {

src/test/java/com/bernd/game/ToggleTest.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,46 @@ void testCornerEyeToggle() {
6767
new int[]{0, 0, 0, 0, 0},
6868
}, result);
6969
}
70+
71+
@Test
72+
void testToggleWhiteL() {
73+
int r = W | REMOVED;
74+
int t = B | TERRITORY;
75+
int[][] position = new int[][]{
76+
new int[]{t, B, W, 0, 0},
77+
new int[]{B, B, W, 0, 0},
78+
new int[]{W, W, W, 0, 0},
79+
new int[]{0, 0, 0, 0, 0},
80+
new int[]{0, 0, 0, 0, 0},
81+
};
82+
int[][] result = Toggle.toggleStonesAt(position, 0, 2);
83+
assertArrayEquals(new int[][]{
84+
new int[]{t, B, r, 0, 0},
85+
new int[]{B, B, r, 0, 0},
86+
new int[]{r, r, r, 0, 0},
87+
new int[]{0, 0, 0, 0, 0},
88+
new int[]{0, 0, 0, 0, 0},
89+
}, result);
90+
}
91+
92+
@Test
93+
void testResetCounting() {
94+
int f = B | REMOVED | TERRITORY;
95+
int t = B | TERRITORY;
96+
int[][] position = new int[][]{
97+
new int[]{t, B, f, t, t},
98+
new int[]{B, B, f, t, t},
99+
new int[]{f, f, f, t, t},
100+
new int[]{t, t, t, t, t},
101+
new int[]{t, t, t, t, t},
102+
};
103+
int[][] result = Toggle.resetCounting(position);
104+
assertArrayEquals(new int[][]{
105+
new int[]{0, B, W, 0, 0},
106+
new int[]{B, B, W, 0, 0},
107+
new int[]{W, W, W, 0, 0},
108+
new int[]{0, 0, 0, 0, 0},
109+
new int[]{0, 0, 0, 0, 0},
110+
}, result);
111+
}
70112
}

0 commit comments

Comments
 (0)