Skip to content

Commit e7def85

Browse files
committed
do not send counting flag
1 parent e23a0f7 commit e7def85

File tree

8 files changed

+39
-30
lines changed

8 files changed

+39
-30
lines changed

src/main/client/src/Game.jsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ export const Game = () => {
5252
let {board, forbidden: [forbidden_x, forbidden_y], gameHasEnded} = useGameStore(state => state.gameState)
5353
let initialized = useRef()
5454
let canvasRef = useRef()
55-
let countingGroup = !gameHasEnded && counting() ? getCountingGroup(board, cursor_x, cursor_y) : undefined
55+
let countingGroup = !gameHasEnded && counting ? getCountingGroup(board, cursor_x, cursor_y) : undefined
5656

5757
let context = useMemo(() => {
5858
let dim = board.length
@@ -122,7 +122,7 @@ export const Game = () => {
122122
if (!board.length) {
123123
return
124124
}
125-
if (!counting() && currentPlayer() !== auth.name) {
125+
if (!counting && currentPlayer() !== auth.name) {
126126
return
127127
}
128128
let cursor_x = Math.round((e.nativeEvent.offsetX - context.margin) / context.step)
@@ -143,7 +143,7 @@ export const Game = () => {
143143
if (!context.isCursorInBounds(cursor_x, cursor_y)) {
144144
return
145145
}
146-
if (counting()) {
146+
if (counting) {
147147
if (!board[cursor_y][cursor_x].hasStone) {
148148
return
149149
}
@@ -175,7 +175,7 @@ export const Game = () => {
175175
return
176176
}
177177
paintGrid(context)
178-
if (counting()) {
178+
if (counting) {
179179
paintStonesCounting(context, board, countingGroup)
180180
return
181181
} else {

src/main/client/src/feature/GamePanel.jsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,11 +136,11 @@ function Panel({zoom, setZoom}) {
136136
<Button
137137
onClick={onPass}
138138
className="py-1 px-4"
139-
disabled={gameHasEnded || counting() || currentPlayer() !== auth.name}>
139+
disabled={gameHasEnded || counting || currentPlayer() !== auth.name}>
140140
Pass
141141
</Button>
142142
</div>
143-
{counting() && <>
143+
{counting && <>
144144
<div className="mt-2">
145145
<Button
146146
className="py-1 px-4"

src/main/client/src/store.js

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ export const useGameStore = create((set, get) => ({
6464
name: "",
6565
},
6666
countingComplete: () => {
67-
if (!get().counting()) {
67+
if (!get().counting) {
6868
return false
6969
}
7070
let baseBoard = get().baseBoard
@@ -105,13 +105,7 @@ export const useGameStore = create((set, get) => ({
105105
}
106106
return moves[moves.length - 1].color ^ (BLACK | WHITE)
107107
},
108-
counting: () => {
109-
let moves = get().moves
110-
if (!moves.length) {
111-
return false
112-
}
113-
return moves[moves.length - 1].counting
114-
},
108+
counting: false,
115109
gameState: {
116110
board: [],
117111
forbidden: [-1, -1],
@@ -134,11 +128,15 @@ export const useGameStore = create((set, get) => ({
134128
return
135129
}
136130
state.queueStatus = "up_to_date"
137-
let [storedMove, updated, forbidden] = createMoveData(baseBoard, moves, move)
131+
let counting = get().counting
132+
let [storedMove, updated, forbidden] = createMoveData(baseBoard, moves, move, counting)
138133
state.moves.push(storedMove)
139134
state.baseBoard = updated
140135
state.gameState.board = rehydrate(updated)
141136
state.gameState.forbidden = forbidden
137+
if (move.pass && moves.length && moves[moves.length - 1].pass) {
138+
state.counting = true
139+
}
142140
}))
143141
},
144142
setGameState: (game) => {
@@ -151,17 +149,29 @@ export const useGameStore = create((set, get) => ({
151149
}
152150
let moves = []
153151
let forbidden = [-1, -1]
152+
let passes = 0
153+
let counting = false
154154
for (let move of game.moves) {
155155
if (move.end) {
156156
moves.push(move)
157157
state.gameState.gameHasEnded = true
158158
break
159159
}
160-
let [storedMove, updated, newForbidden] = createMoveData(baseBoard, moves, move)
160+
if (move.pass) {
161+
if (passes) {
162+
counting = true
163+
} else {
164+
passes = 1
165+
}
166+
} else {
167+
passes = 0
168+
}
169+
let [storedMove, updated, newForbidden] = createMoveData(baseBoard, moves, move, counting)
161170
moves.push(storedMove)
162171
forbidden = newForbidden
163172
baseBoard = updated
164173
}
174+
state.counting = counting
165175
state.dim = game.dim
166176
state.baseBoard = baseBoard
167177
state.moves = moves
@@ -173,11 +183,11 @@ export const useGameStore = create((set, get) => ({
173183
},
174184
}))
175185

176-
function createMoveData(baseBoard, moves, move) {
186+
function createMoveData(baseBoard, moves, move, counting) {
177187
if (move.pass && moves.length && moves[moves.length - 1].pass) {
178188
return [move, count(baseBoard), [-1, -1]]
179189
}
180-
if (move.counting) {
190+
if (counting) {
181191
let updated = move.resetCounting ?
182192
resetCounting(baseBoard) :
183193
toggleStonesAt(baseBoard, move.x, move.y)

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ public static MoveList create(int size) {
1919
}
2020

2121
public void addGameEndMarker() {
22-
moves.add(new GameMove(moves.size(), 0, true, -1, -1, true, false, false, true));
22+
moves.add(new GameMove(moves.size(), 0, true, -1, -1, false, false, true));
2323
}
2424

25-
public void add(Move move, boolean counting) {
26-
moves.add(move.toGameMove(counting));
25+
public void add(Move move) {
26+
moves.add(move.toGameMove());
2727
}
2828

2929
public GameMove get(int i) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ private Game updateInternal(Move move) {
4242
if (move.agreeCounting() && (countingAgreed | move.color()) == COLORS) {
4343
moves.addGameEndMarker();
4444
} else {
45-
moves.add(move, counting || move.pass() && opponentPassed());
45+
moves.add(move);
4646
}
4747
if (move.agreeCounting()) {
4848
return toBuilder()

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ public record GameMove(
66
boolean pass,
77
int x,
88
int y,
9-
boolean counting,
109
boolean resetCounting,
1110
boolean agreeCounting,
1211
boolean end) {

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ public record Move(
1010
int x,
1111
int y) {
1212

13-
public GameMove toGameMove(boolean counting) {
14-
return new GameMove(n, color, pass, x, y, counting, resetCounting, agreeCounting, false);
13+
public GameMove toGameMove() {
14+
return new GameMove(n, color, pass, x, y, resetCounting, agreeCounting, false);
1515
}
1616

17-
public GameMove gameEnd(boolean counting) {
18-
return new GameMove(n, color, pass, x, y, counting, resetCounting, agreeCounting, true);
17+
public GameMove gameEnd() {
18+
return new GameMove(n, color, pass, x, y, resetCounting, agreeCounting, true);
1919
}
2020

2121
public Move withColor(int color) {

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ class MoveListTest {
1212
@Test
1313
void testGet() {
1414
MoveList list = MoveList.create(9);
15-
list.add(move(B, 0, 1), false);
16-
list.add(move(W, 2, 3), false);
15+
list.add(move(B, 0, 1));
16+
list.add(move(W, 2, 3));
1717
assertEquals(2, list.size());
1818
assertEquals(0, list.get(0).x());
1919
assertEquals(1, list.get(0).y());
@@ -28,7 +28,7 @@ void testGrow() {
2828
MoveList list = MoveList.create(9);
2929
for (int y = 0; y < 9; y++) {
3030
for (int x = 0; x < 9; x++) {
31-
list.add(move(B, x, y), false);
31+
list.add(move(B, x, y));
3232
}
3333
}
3434
assertEquals(81, list.size());

0 commit comments

Comments
 (0)