Skip to content

Commit 862c6de

Browse files
committed
some cleanup
1 parent 4972489 commit 862c6de

File tree

6 files changed

+56
-146
lines changed

6 files changed

+56
-146
lines changed

src/main/client/src/Game.jsx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ export const Game = () => {
4545
let auth = useAuthStore(state => state.auth)
4646
let setGameState = useGameStore(state => state.setGameState)
4747
let queueStatus = useGameStore(state => state.queueStatus)
48-
let queueLength = useGameStore(state => state.queueLength)
4948
let addMove = useGameStore(state => state.addMove)
5049
let currentPlayer = useGameStore(state => state.currentPlayer)
5150
let counting = useGameStore(state => state.counting)
@@ -166,12 +165,11 @@ export const Game = () => {
166165
destination: "/app/game/move",
167166
body: JSON.stringify({
168167
id: gameId,
169-
n: queueLength(),
170168
x: cursor_x,
171169
y: cursor_y,
172170
}),
173171
})
174-
}, [context, currentPlayer, currentColor, auth, board, gameId, stompClient, counting, forbidden_x, forbidden_y, queueLength, gameHasEnded])
172+
}, [context, currentPlayer, currentColor, auth, board, gameId, stompClient, counting, forbidden_x, forbidden_y, gameHasEnded])
175173

176174
useEffect(() => {
177175
if (!board.length) {

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

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,31 +62,28 @@ function Panel({zoom, setZoom}) {
6262
destination: "/app/game/move",
6363
body: JSON.stringify({
6464
id: gameId,
65-
n: queueLength(),
6665
pass: true,
6766
}),
6867
})
69-
}, [stompClient, gameId, queueLength])
68+
}, [stompClient, gameId])
7069
let onResetCounting = useCallback(() => {
7170
stompClient.publish({
7271
destination: "/app/game/move",
7372
body: JSON.stringify({
7473
id: gameId,
75-
n: queueLength(),
7674
resetCounting: true,
7775
}),
7876
})
79-
}, [stompClient, gameId, queueLength])
77+
}, [stompClient, gameId])
8078
let onCountingAgree = useCallback(() => {
8179
stompClient.publish({
8280
destination: "/app/game/move",
8381
body: JSON.stringify({
8482
id: gameId,
85-
n: queueLength(),
8683
agreeCounting: true,
8784
}),
8885
})
89-
}, [stompClient, gameId, queueLength])
86+
}, [stompClient, gameId])
9087
if (!board.length) {
9188
return <span>Loading...</span>
9289
}

src/main/java/com/bernd/GameController.java

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
import com.bernd.model.ViewGame;
1212
import com.bernd.util.Auth;
1313
import com.bernd.util.RandomString;
14+
import java.security.Principal;
15+
import java.util.Objects;
1416
import org.springframework.http.ResponseEntity;
1517
import org.springframework.messaging.core.MessageSendingOperations;
1618
import org.springframework.messaging.handler.annotation.MessageMapping;
@@ -21,8 +23,6 @@
2123
import org.springframework.web.bind.annotation.RequestBody;
2224
import org.springframework.web.bind.annotation.ResponseBody;
2325

24-
import java.security.Principal;
25-
2626
@Controller
2727
public class GameController {
2828

@@ -55,40 +55,44 @@ public ViewGame getGame(@PathVariable String id) {
5555
@MessageMapping("/game/move")
5656
public void action(Move move, Principal p) {
5757
Game game = games.get(move.id());
58-
if (game == null) {
58+
if (p == null || game == null) {
5959
return;
6060
}
61-
String principal = p.getName();
62-
MoveList moves = game.moves();
63-
int moveNumber = moves.size();
61+
String principal = Objects.toString(p.getName(), "");
6462
int color = principal.equals(game.black().name()) ? Board.B :
6563
principal.equals(game.white().name()) ? Board.W : 0;
66-
if (color == 0) {
64+
if (!canMove(game, color)) {
6765
return;
6866
}
69-
if (!game.counting()) {
70-
if (moves.isEmpty()) {
71-
if (color == Board.W) {
72-
return;
73-
}
74-
} else {
75-
GameMove lastMove = moves.get(moves.size() - 1);
76-
if (lastMove.color() == color) {
77-
return;
78-
}
79-
}
80-
if (move.n() != moveNumber) {
81-
return;
82-
}
83-
}
84-
Move updatedMove = move.withColor(color);
67+
Move updatedMove = move.withColor(color).withMoveNumber(game.moves().size());
8568
Game updated = game.update(updatedMove);
8669
games.put(updated);
8770
if (updated.gameHasEnded()) {
8871
operations.convertAndSend("/topic/move/" + game.id(), updatedMove.gameEnd(updated.counting()));
8972
} else if (!move.agreeCounting()) {
90-
operations.convertAndSend("/topic/move/" + game.id(), updatedMove.toView(updated.counting()));
73+
operations.convertAndSend("/topic/move/" + game.id(), updatedMove.toGameMove(updated.counting()));
74+
}
75+
}
76+
77+
private boolean canMove(Game game, int color) {
78+
if (game.gameHasEnded()) {
79+
return false;
80+
}
81+
if (color == 0) {
82+
return false;
83+
}
84+
MoveList moves = game.moves();
85+
if (game.counting()) {
86+
return true;
87+
}
88+
if (game.handicap() != 0) {
89+
return color == Board.B;
90+
}
91+
if (moves.isEmpty()) {
92+
return color == Board.B;
9193
}
94+
GameMove lastMove = moves.get(moves.size() - 1);
95+
return color != lastMove.color();
9296
}
9397

9498
@ResponseBody

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

Lines changed: 12 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -2,137 +2,47 @@
22

33
import com.bernd.model.GameMove;
44
import com.bernd.model.Move;
5-
65
import java.util.ArrayList;
7-
import java.util.Arrays;
86
import java.util.List;
97

10-
import static com.bernd.util.Util.divUp;
11-
128
public final class MoveList {
139

14-
private static final int LO = 0xffff;
15-
private static final int HI = 0xffff0000;
16-
private static final int WHITE = 0x1000;
17-
private static final int PASS = 0x2000;
18-
private static final int COUNTING = 0x4000;
19-
private static final int GAME_END = 0x8000;
20-
private static final int DATA = 0x0fff;
21-
22-
private int pos;
23-
private int capacity;
24-
private final int dim;
25-
private int[] buffer;
10+
private final List<GameMove> moves;
2611

27-
private MoveList(
28-
int dim,
29-
int[] buffer) {
30-
this.capacity = 2 * buffer.length;
31-
this.dim = dim;
32-
this.buffer = buffer;
12+
private MoveList(List<GameMove> moves) {
13+
this.moves = moves;
3314
}
3415

35-
public static MoveList create(int dim, int size) {
36-
return new MoveList(dim, new int[divUp(size, 2)]);
37-
}
38-
39-
public static MoveList create(int dim) {
40-
return new MoveList(dim, new int[16]);
16+
public static MoveList create(int size) {
17+
return new MoveList(new ArrayList<>(size));
4118
}
4219

4320
public void addGameEndMarker() {
44-
ensureCapacity();
45-
set(GAME_END);
46-
pos++;
21+
moves.add(new GameMove(moves.size(), 0, true, -1, -1, true, false, true));
4722
}
4823

4924
public void add(Move move, boolean counting) {
50-
ensureCapacity();
51-
int ptId;
52-
if (move.pass()) {
53-
ptId = PASS;
54-
} else {
55-
ptId = dim * move.y() + move.x();
56-
}
57-
if (move.color() == Board.W) {
58-
ptId |= WHITE;
59-
}
60-
if (counting) {
61-
ptId |= COUNTING;
62-
}
63-
set(ptId);
64-
pos++;
65-
}
66-
67-
private void ensureCapacity() {
68-
if (pos >= capacity) {
69-
int boardSize = dim * dim;
70-
int newCapacity = capacity < boardSize ? boardSize : capacity + boardSize;
71-
buffer = Arrays.copyOf(buffer, divUp(newCapacity, 2));
72-
capacity = newCapacity;
73-
}
25+
moves.add(move.toGameMove(counting));
7426
}
7527

7628
public GameMove get(int i) {
77-
int code = buffer[i / 2];
78-
int ptId = i % 2 == 0 ? code & LO : (code >> 16);
79-
int color = (ptId & WHITE) != 0 ? Board.W : Board.B;
80-
if ((ptId & GAME_END) != 0) {
81-
return new GameMove(i, 0, true, -1, -1, true, false, true);
82-
}
83-
boolean counting = (ptId & COUNTING) != 0;
84-
if ((ptId & PASS) != 0) {
85-
return new GameMove(i, color, true, -1, -1, counting, false, false);
86-
} else {
87-
int data = ptId & DATA;
88-
int x = data % dim;
89-
int y = data / dim;
90-
return new GameMove(i, color, false, x, y, counting, false, false);
91-
}
29+
return moves.get(i);
9230
}
9331

9432
public boolean isEmpty() {
95-
return pos == 0;
96-
}
97-
98-
private void set(int ptId) {
99-
int i = pos / 2;
100-
if (pos % 2 == 0) {
101-
buffer[i] = (buffer[i] & HI) | ptId;
102-
} else {
103-
buffer[i] = (ptId << 16) | (buffer[i] & LO);
104-
}
33+
return moves.isEmpty();
10534
}
10635

10736
public int size() {
108-
return pos;
37+
return moves.size();
10938
}
11039

11140
public List<GameMove> asList() {
112-
List<GameMove> result = new ArrayList<>(size());
113-
for (int i = 0; i < pos; i++) {
114-
result.add(get(i));
115-
}
116-
return result;
117-
}
118-
119-
public int dim() {
120-
return dim;
41+
return List.copyOf(moves);
12142
}
12243

12344
@Override
12445
public String toString() {
125-
if (pos == 0) {
126-
return "[]";
127-
}
128-
StringBuilder sb = new StringBuilder("[");
129-
for (int i = 0; i < pos; i++) {
130-
sb.append("(")
131-
.append(get(i))
132-
.append("), ");
133-
}
134-
sb.setLength(sb.length() - 2);
135-
sb.append("]");
136-
return sb.toString();
46+
return moves.toString();
13747
}
13848
}

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

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -49,24 +49,19 @@ private Game updateInternal(Move move) {
4949
}
5050
moves.add(move, counting);
5151
if (counting) {
52-
if (move.resetCounting()) {
53-
int[][] resetted = Toggle.resetCounting(board);
54-
return toBuilder()
55-
.withBoard(Count.count(resetted))
56-
.withForbidden(NOT_FORBIDDEN)
57-
.build();
58-
}
59-
int[][] toggled = Toggle.toggleStonesAt(board, move.x(), move.y());
52+
int[][] updated = move.resetCounting() ?
53+
Toggle.resetCounting(board) :
54+
Toggle.toggleStonesAt(board, move.x(), move.y());
6055
return toBuilder()
61-
.withBoard(Count.count(toggled))
56+
.withBoard(Count.count(updated))
6257
.withForbidden(NOT_FORBIDDEN)
6358
.build();
6459
}
6560
if (move.pass()) {
6661
if (opponentPassed) {
6762
return toBuilder()
68-
.withBoard(Count.count(board))
6963
.withCounting(true)
64+
.withBoard(Count.count(board))
7065
.withForbidden(NOT_FORBIDDEN)
7166
.build();
7267
}
@@ -85,11 +80,13 @@ private Game updateInternal(Move move) {
8580
return toBuilder()
8681
.withBoard(result)
8782
.withForbidden(direction.moveX(x), direction.moveY(y))
83+
.withOpponentPassed(false)
8884
.build();
8985
}
9086
return toBuilder()
9187
.withBoard(result)
9288
.withForbidden(NOT_FORBIDDEN)
89+
.withOpponentPassed(false)
9390
.build();
9491
}
9592

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public record Move(
1010
int x,
1111
int y) {
1212

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

@@ -21,4 +21,8 @@ public GameMove gameEnd(boolean counting) {
2121
public Move withColor(int color) {
2222
return new Move(id, color, n, pass, resetCounting, agreeCounting, x, y);
2323
}
24+
25+
public Move withMoveNumber(int n) {
26+
return new Move(id, color, n, pass, resetCounting, agreeCounting, x, y);
27+
}
2428
}

0 commit comments

Comments
 (0)