Skip to content

Commit a422985

Browse files
committed
add edit mode
1 parent e0d4276 commit a422985

File tree

12 files changed

+172
-56
lines changed

12 files changed

+172
-56
lines changed

src/main/client/src/Lobby.jsx

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ import {
88
import {
99
useNavigate,
1010
} from "react-router-dom"
11+
import {
12+
Button,
13+
} from "./component/Button.jsx"
1114
import {
1215
base,
1316
StompContext,
@@ -51,27 +54,37 @@ export function Lobby() {
5154
}),
5255
})
5356
return () => {
54-
sub1.unsubscribe
55-
sub2.unsubscribe
56-
sub3.unsubscribe
57+
sub1.unsubscribe()
58+
sub2.unsubscribe()
59+
sub3.unsubscribe()
5760
}
5861
}, [setInit, setUsers, setMatchRequested, auth, initialized, stompClient, navigate])
59-
let matchRequest = useCallback(() => {
62+
let matchRequest = useCallback((editMode) => {
6063
stompClient.publish({
6164
destination: "/app/lobby/match",
6265
body: JSON.stringify({
6366
name: auth.name,
67+
dim: 9,
68+
editMode: editMode,
6469
}),
6570
})
6671
}, [auth, stompClient])
6772
if (!matchRequested) {
6873
return (
6974
<div className="m-4">
70-
<button type="button"
71-
className="p-2 border border-black"
72-
onClick={matchRequest}>
73-
Find match
74-
</button>
75+
<div>
76+
<button type="Button"
77+
onClick={() => matchRequest(true)}>
78+
Create
79+
</button>
80+
</div>
81+
<div className="mt-2">
82+
<button type="button"
83+
className="p-2 border border-black"
84+
onClick={() => matchRequest(false)}>
85+
Find match
86+
</button>
87+
</div>
7588
<div className="mt-2">
7689
{users.map(user => (
7790
<div key={user.name}>{user.name}</div>

src/main/client/src/Play.jsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ export const Play = () => {
6161
}),
6262
})
6363
return () => {
64-
sub1.unsubscribe
64+
sub1.unsubscribe()
6565
}
6666
}, [setGameState, initialized, stompClient, auth, gameId])
6767
let onPass = useCallback(() => {
@@ -174,8 +174,8 @@ function getStyle(check) {
174174
}
175175

176176
function EmptyActive({ onClick }) {
177-
let symbol = useGameStore(state => state.symbol)
178-
let hovercolor = symbol === BLACK ? "hover:text-black" : "hover:text-white"
177+
let currentColor = useGameStore(state => state.gameState.currentColor)
178+
let hovercolor = currentColor === BLACK ? "hover:text-black" : "hover:text-white"
179179
let classes = twJoin(
180180
tileClasses,
181181
"text-transparent",

src/main/client/src/store.js

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,9 @@ export const useAuthStore = create((set) => ({
2929
},
3030
}))
3131

32-
export const useGameStore = create((set, get) => ({
32+
export const useGameStore = create((set) => ({
3333
symbol: 0,
34+
editMode: false,
3435
black: {
3536
name: "",
3637
},
@@ -39,24 +40,20 @@ export const useGameStore = create((set, get) => ({
3940
},
4041
gameState: {
4142
currentPlayer: undefined,
43+
currentColor: BLACK,
4244
counting: false,
4345
},
4446
setGameState: (game, auth) => {
4547
set(produce(state => {
46-
let oldState = get()
47-
if (oldState.black.name !== game.black.name) {
48-
state.black = game.black
49-
}
50-
if (oldState.white.name !== game.white.name) {
51-
state.white = game.white
52-
}
48+
state.black = game.black
49+
state.white = game.white
50+
state.editMode = game.editMode
5351
state.gameState.board = game.board
5452
state.gameState.currentPlayer = game.currentPlayer
53+
state.gameState.currentColor = game.currentColor
5554
state.gameState.counting = game.counting
5655
let symbol = game.black.name === auth.name? BLACK : WHITE
57-
if (oldState.symbol !== symbol) {
58-
state.symbol = symbol
59-
}
56+
state.symbol = symbol
6057
}))
6158
},
6259
}))

src/main/java/com/bernd/LobbyController.java

Lines changed: 42 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
import org.springframework.messaging.handler.annotation.MessageMapping;
1212
import org.springframework.stereotype.Controller;
1313

14+
import static com.bernd.game.Board.B;
15+
1416
@Controller
1517
public class LobbyController {
1618

@@ -36,31 +38,55 @@ public void lobbyJoinedAction(Principal principal) {
3638
}
3739

3840
@MessageMapping("/lobby/match")
39-
public void matchAction(MatchRequest message, Principal principal) {
41+
public void matchAction(MatchRequest request, Principal principal) {
4042
User user = lobbyUsers.get(principal);
43+
if (request.editMode()) {
44+
lobbyUsers.remove(principal.getName());
45+
operations.convertAndSend("/topic/lobby/users",
46+
new UserList(lobbyUsers.users()));
47+
Game game = games.put(new Game(
48+
RandomString.get(),
49+
user,
50+
user,
51+
true,
52+
false,
53+
user.name(),
54+
B,
55+
false,
56+
createEmptyBoard(request)));
57+
operations.convertAndSend("/topic/lobby/gamestart", game);
58+
}
4159
if (lookingForMatch == null) {
4260
lookingForMatch = user;
4361
operations.convertAndSend("/topic/lobby/gamerequest",
44-
new Status(message.name(), "ready"));
62+
new Status(request.name(), "ready"));
4563
return;
4664
}
65+
User black = lookingForMatch;
66+
lookingForMatch = null;
4767
lobbyUsers.remove(principal.getName());
48-
lobbyUsers.remove(lookingForMatch.name());
68+
lobbyUsers.remove(black.name());
4969
operations.convertAndSend("/topic/lobby/users",
5070
new UserList(lobbyUsers.users()));
51-
String gameId = RandomString.get();
52-
Game game = games.put(new Game(gameId, user, lookingForMatch, false, user.name(), false, new int[][]{
53-
new int[9],
54-
new int[9],
55-
new int[9],
56-
new int[9],
57-
new int[9],
58-
new int[9],
59-
new int[9],
60-
new int[9],
61-
new int[9]
62-
}));
71+
Game game = games.put(new Game(
72+
RandomString.get(),
73+
black,
74+
user,
75+
false,
76+
false,
77+
user.name(),
78+
B,
79+
false,
80+
createEmptyBoard(request)));
6381
operations.convertAndSend("/topic/lobby/gamestart", game);
64-
lookingForMatch = null;
82+
}
83+
84+
private static int[][] createEmptyBoard(MatchRequest request) {
85+
int dim = Math.max(request.dim(), 2);
86+
int[][] board = new int[dim][];
87+
for (int y = 0; y < dim; y++) {
88+
board[y] = new int[dim];
89+
}
90+
return board;
6591
}
6692
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
import java.util.Arrays;
55

66
import static com.bernd.game.Board.B;
7-
import static com.bernd.game.Board.REMOVED;
87
import static com.bernd.game.Board.TERRITORY;
98
import static com.bernd.game.Board.W;
9+
import static com.bernd.util.Util.isPlainColor;
1010
import static com.bernd.util.Util.keepMarkers;
1111

1212
public class Count {
@@ -15,7 +15,7 @@ static int getImpliedColor(
1515
int[][] board,
1616
int xx,
1717
int yy) {
18-
if (board[yy][xx] != 0) {
18+
if (isPlainColor(board[yy][xx])) {
1919
return board[yy][xx];
2020
}
2121
int dim = board.length;
@@ -27,7 +27,7 @@ static int getImpliedColor(
2727
int ptId = pointsToCheck.poll();
2828
int y = ptId / dim;
2929
int x = ptId % dim;
30-
if (board[y][x] != 0 && (board[y][x] & REMOVED) == 0) {
30+
if (isPlainColor(board[y][x])) {
3131
return board[y][x] | TERRITORY;
3232
}
3333
if (y > 0 && !pointsChecked.has(x, y - 1)) {

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,11 @@ public String toString() {
7676
return "[]";
7777
}
7878
StringBuilder sb = new StringBuilder("[");
79-
forEach((x, y) -> sb.append("(")
80-
.append(x).append(" ").append(y)
81-
.append("), "));
79+
for (int i = 0; i < pos; i++) {
80+
sb.append("(")
81+
.append(x(i)).append(" ").append(y(i))
82+
.append("), ");
83+
}
8284
sb.setLength(sb.length() - 2);
8385
sb.append("]");
8486
return sb.toString();

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
public class Toggle {
1010

11-
private static Function<int[][], int[][]> toggle(
11+
private static Function<int[][], int[][]> toggleRemoved(
1212
int[][] board,
1313
int xx,
1414
int yy) {
@@ -65,7 +65,7 @@ private static Function<int[][], int[][]> toggle(
6565

6666
public static int[][] toggleStonesAt(
6767
int[][] board, int x, int y) {
68-
Function<int[][], int[][]> update = toggle(board, x, y);
68+
Function<int[][], int[][]> update = toggleRemoved(board, x, y);
6969
return update.apply(board);
7070
}
7171
}

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

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,17 @@
88
import org.apache.logging.log4j.LogManager;
99
import org.apache.logging.log4j.Logger;
1010

11+
import static com.bernd.game.Board.B;
12+
import static com.bernd.game.Board.W;
13+
1114
public record Game(
1215
String id,
1316
User black,
1417
User white,
18+
boolean editMode,
1519
boolean counting,
1620
String currentPlayer,
21+
int currentColor,
1722
boolean opponentPassed,
1823
int[][] board
1924
) {
@@ -31,7 +36,9 @@ public Game update(Move move) {
3136

3237
private Game updateInternal(Move move) {
3338
if (counting) {
34-
return game(Count.count(Toggle.toggleStonesAt(board, move.x(), move.y())));
39+
int[][] toggled = Toggle.toggleStonesAt(board, move.x(), move.y());
40+
int[][] counted = Count.count(toggled);
41+
return game(counted);
3542
}
3643
if (move.pass()) {
3744
if (opponentPassed) {
@@ -54,8 +61,10 @@ private Game game(
5461
id,
5562
black,
5663
white,
64+
editMode,
5765
counting,
5866
nextPlayer(),
67+
nextColor(),
5968
opponentPassed,
6069
board);
6170
}
@@ -69,10 +78,13 @@ private Game startCounting() {
6978
}
7079

7180
private String nextPlayer() {
81+
if (editMode) {
82+
return currentPlayer;
83+
}
7284
return currentPlayer.equals(black.name()) ? white().name() : black().name();
7385
}
7486

75-
private int currentColor() {
76-
return currentPlayer.equals(black().name()) ? Board.B : Board.W;
87+
private int nextColor() {
88+
return currentColor == B ? W : B;
7789
}
7890
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
package com.bernd.model;
22

3-
public record MatchRequest(String name) {
4-
}
3+
public record MatchRequest(String name, int dim, boolean editMode) {
4+
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
public final class Util {
88
private static final int MARKERS = ~B & ~W;
9+
private static final int COLORS = B | W;
910

1011
private Util() {
1112
}
@@ -34,4 +35,8 @@ public static int keepMarkers(int color, int colorWithMarkers) {
3435
public static int divUp(int i, int div) {
3536
return i % div == 0 ? i / div : (i / div) + 1;
3637
}
38+
39+
public static boolean isPlainColor(int color) {
40+
return (color & COLORS) != 0 && (color & MARKERS) == 0;
41+
}
3742
}

0 commit comments

Comments
 (0)