Skip to content

Commit 7041db1

Browse files
committed
handicap fix
1 parent 96655df commit 7041db1

File tree

11 files changed

+43
-12
lines changed

11 files changed

+43
-12
lines changed

src/main/client/src/Game.jsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,7 @@ export const Game = () => {
4949
let currentPlayer = useGameStore(state => state.currentPlayer)
5050
let counting = useGameStore(state => state.counting)
5151
let currentColor = useGameStore(state => state.currentColor)
52-
let {board, forbidden, gameHasEnded} = useGameStore(state => state.gameState)
53-
let [forbidden_x, forbidden_y] = forbidden
52+
let {board, forbidden: [forbidden_x, forbidden_y], gameHasEnded} = useGameStore(state => state.gameState)
5453
let initialized = useRef()
5554
let canvasRef = useRef()
5655
let countingGroup = !gameHasEnded && counting() ? getCountingGroup(board, cursor_x, cursor_y) : undefined

src/main/client/src/feature/OpenGames.jsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -141,14 +141,14 @@ function OpenGame({game, acceptableGame, setAcceptableGame}) {
141141
function AcceptDialog({acceptableGame, onAccept}) {
142142
let { top, right } = acceptableGame.rect
143143
let [isFlip, setFlip] = useState(false)
144-
let [handi, setHandi] = useState(0)
144+
let [handi, setHandi] = useState(1)
145145
let auth = useAuthStore(state => state.auth)
146146
return (
147147
<Form
148148
onSubmit={() => onAccept({
149149
game: acceptableGame.game,
150150
flip: isFlip,
151-
handicap: handi,
151+
handicap: handi === 1 ? 0 : handi,
152152
})}
153153
style={{
154154
position: "absolute",
@@ -171,18 +171,18 @@ function AcceptDialog({acceptableGame, onAccept}) {
171171
<div className="mt-2 text-black py-1">
172172
<div className="inline-flex">
173173
<span>Handicap:</span>
174-
<button type="button" disabled={handi === 0} onClick={() => setHandi(Math.max(0, handi - 1))}>
174+
<button type="button" disabled={handi === 1} onClick={() => setHandi(Math.max(1, handi - 1))}>
175175
<IconContext.Provider value={{
176176
size: "1.25em",
177177
className: twJoin(
178178
"px-1",
179-
handi === 0 && "text-slate-400",
179+
handi === 1 && "text-slate-400",
180180
)
181181
}}>
182182
<FaAngleLeft />
183183
</IconContext.Provider>
184184
</button>
185-
<span className="font-bold">{handi === 0 ? 0 : handi + 1}</span>
185+
<span className="font-bold">{handi === 1 ? "0" : handi}</span>
186186
<button type="button" className="" onClick={() => setHandi(handi + 1)}>
187187
<IconContext.Provider value={{ size: "1.25em", className: "pl-1" }}>
188188
<FaAngleRight />

src/main/client/src/store.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ export const useGameStore = create((set, get) => ({
5555
moves: [],
5656
baseBoard: [],
5757
dim: 0,
58+
handicap: 0,
5859
queueStatus: "behind",
5960
black: {
6061
name: "",
@@ -81,6 +82,10 @@ export const useGameStore = create((set, get) => ({
8182
let moves = get().moves
8283
let white = get().white
8384
let black = get().black
85+
let handicap = get().handicap
86+
if (handicap > moves.length) {
87+
return black.name
88+
}
8489
if (!moves.length) {
8590
return black.name
8691
}
@@ -91,6 +96,10 @@ export const useGameStore = create((set, get) => ({
9196
},
9297
currentColor: () => {
9398
let moves = get().moves
99+
let handicap = get().handicap
100+
if (handicap > moves.length) {
101+
return BLACK
102+
}
94103
if (!moves.length) {
95104
return BLACK
96105
}
@@ -158,7 +167,8 @@ export const useGameStore = create((set, get) => ({
158167
state.moves = moves
159168
state.gameState.board = rehydrate(baseBoard)
160169
state.gameState.forbidden = forbidden
161-
state.queueStatue = "up_to_date"
170+
state.handicap = game.handicap
171+
state.queueStatus = "up_to_date"
162172
}))
163173
},
164174
}))

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ private int getCurrentColor(Game game, String principal) {
7878
if (game.gameHasEnded()) {
7979
return 0;
8080
}
81-
if (game.handicap() != 0) {
81+
if (game.remainingHandicap() != 0) {
8282
return Board.B;
8383
}
8484
int color = getColor(game, principal);
@@ -96,8 +96,14 @@ private static int getColor(Game game, String principal) {
9696
if (!(game.isBlack(principal) || game.isWhite(principal))) {
9797
return 0;
9898
}
99+
if (game.remainingHandicap() > 0) {
100+
if (!game.isBlack(principal)) {
101+
return 0;
102+
}
103+
return Board.B;
104+
}
99105
if (game.isSelfPlay()) {
100-
return game.moves().size() + game.handicap() % 2 == 0 ?
106+
return game.moves().size() + game.remainingHandicap() % 2 == 0 ?
101107
Board.B : Board.W;
102108
}
103109
return game.isBlack(principal) ? Board.B : Board.W;

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,8 @@ public ViewGame startEdit(@RequestBody MatchRequest request) {
7777
0,
7878
createEmptyBoard(request.dim()),
7979
request.dim(),
80-
0,
80+
request.handicap(),
81+
request.handicap(),
8182
new int[]{-1, -1},
8283
MoveList.create(request.dim())));
8384
activeGames.put(ActiveGame.fromGame(game));

src/main/java/com/bernd/LobbyUsers.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22

33
import com.bernd.model.User;
44
import com.bernd.model.UserList;
5+
import org.springframework.stereotype.Component;
6+
57
import java.util.Collection;
68
import java.util.LinkedHashMap;
79
import java.util.List;
810
import java.util.Map;
9-
import org.springframework.stereotype.Component;
1011

1112
@Component
1213
public class LobbyUsers {

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public record Game(
2121
int[][] board,
2222
int dim,
2323
int handicap,
24+
int remainingHandicap,
2425
int[] forbidden,
2526
MoveList moves
2627
) {
@@ -82,6 +83,7 @@ private Game updateInternal(Move move) {
8283
.build();
8384
}
8485
return toBuilder()
86+
.withRemainingHandicap(Math.max(0, remainingHandicap - 1))
8587
.withBoard(result)
8688
.withForbidden(NOT_FORBIDDEN)
8789
.build();

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,19 @@ public final class GameBuilder {
55

66
private boolean counting;
77
private int countingAgreed;
8+
private int remainingHandicap;
89
private int[][] board;
910
private int[] forbidden;
1011

1112
private GameBuilder(Game game) {
1213
this.game = game;
1314
}
1415

16+
public GameBuilder withRemainingHandicap(int remainingHandicap) {
17+
this.remainingHandicap = remainingHandicap;
18+
return this;
19+
}
20+
1521
public GameBuilder withCounting(boolean counting) {
1622
this.counting = counting;
1723
return this;
@@ -38,6 +44,7 @@ public GameBuilder withForbidden(int x, int y) {
3844

3945
static GameBuilder builder(Game game) {
4046
GameBuilder builder = new GameBuilder(game);
47+
builder.remainingHandicap = game.remainingHandicap();
4148
builder.counting = game.counting();
4249
builder.countingAgreed = game.countingAgreed();
4350
builder.board = game.board();
@@ -55,6 +62,7 @@ public Game build() {
5562
board,
5663
game.dim(),
5764
game.handicap(),
65+
remainingHandicap,
5866
forbidden,
5967
game.moves()
6068
);

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public Game accept(String opponent, AcceptRequest acceptRequest) {
3030
createEmptyBoard(dim),
3131
dim,
3232
acceptRequest.handicap(),
33+
acceptRequest.handicap(),
3334
new int[]{-1, -1},
3435
MoveList.create(dim));
3536
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ public record ViewGame(
88
User white,
99
int dim,
1010
int handicap,
11+
int remainingHandicap,
1112
List<GameMove> moves
1213
) {
1314

@@ -18,6 +19,7 @@ static ViewGame fromGame(Game game) {
1819
game.white(),
1920
game.dim(),
2021
game.handicap(),
22+
game.remainingHandicap(),
2123
game.moves().asList());
2224
}
2325
}

src/test/java/com/bernd/model/GameBuilderTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ void unchanged() {
2020
createEmptyBoard(9),
2121
9,
2222
2,
23+
2,
2324
new int[]{-1, -1},
2425
MoveList.create(2));
2526
Game game2 = GameBuilder.builder(game).build();

0 commit comments

Comments
 (0)