Skip to content

Commit

Permalink
handicap fix
Browse files Browse the repository at this point in the history
  • Loading branch information
h908714124 committed Aug 7, 2024
1 parent 96655df commit 7041db1
Show file tree
Hide file tree
Showing 11 changed files with 43 additions and 12 deletions.
3 changes: 1 addition & 2 deletions src/main/client/src/Game.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ export const Game = () => {
let currentPlayer = useGameStore(state => state.currentPlayer)
let counting = useGameStore(state => state.counting)
let currentColor = useGameStore(state => state.currentColor)
let {board, forbidden, gameHasEnded} = useGameStore(state => state.gameState)
let [forbidden_x, forbidden_y] = forbidden
let {board, forbidden: [forbidden_x, forbidden_y], gameHasEnded} = useGameStore(state => state.gameState)
let initialized = useRef()
let canvasRef = useRef()
let countingGroup = !gameHasEnded && counting() ? getCountingGroup(board, cursor_x, cursor_y) : undefined
Expand Down
10 changes: 5 additions & 5 deletions src/main/client/src/feature/OpenGames.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -141,14 +141,14 @@ function OpenGame({game, acceptableGame, setAcceptableGame}) {
function AcceptDialog({acceptableGame, onAccept}) {
let { top, right } = acceptableGame.rect
let [isFlip, setFlip] = useState(false)
let [handi, setHandi] = useState(0)
let [handi, setHandi] = useState(1)
let auth = useAuthStore(state => state.auth)
return (
<Form
onSubmit={() => onAccept({
game: acceptableGame.game,
flip: isFlip,
handicap: handi,
handicap: handi === 1 ? 0 : handi,
})}
style={{
position: "absolute",
Expand All @@ -171,18 +171,18 @@ function AcceptDialog({acceptableGame, onAccept}) {
<div className="mt-2 text-black py-1">
<div className="inline-flex">
<span>Handicap:</span>
<button type="button" disabled={handi === 0} onClick={() => setHandi(Math.max(0, handi - 1))}>
<button type="button" disabled={handi === 1} onClick={() => setHandi(Math.max(1, handi - 1))}>
<IconContext.Provider value={{
size: "1.25em",
className: twJoin(
"px-1",
handi === 0 && "text-slate-400",
handi === 1 && "text-slate-400",
)
}}>
<FaAngleLeft />
</IconContext.Provider>
</button>
<span className="font-bold">{handi === 0 ? 0 : handi + 1}</span>
<span className="font-bold">{handi === 1 ? "0" : handi}</span>
<button type="button" className="" onClick={() => setHandi(handi + 1)}>
<IconContext.Provider value={{ size: "1.25em", className: "pl-1" }}>
<FaAngleRight />
Expand Down
12 changes: 11 additions & 1 deletion src/main/client/src/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export const useGameStore = create((set, get) => ({
moves: [],
baseBoard: [],
dim: 0,
handicap: 0,
queueStatus: "behind",
black: {
name: "",
Expand All @@ -81,6 +82,10 @@ export const useGameStore = create((set, get) => ({
let moves = get().moves
let white = get().white
let black = get().black
let handicap = get().handicap
if (handicap > moves.length) {
return black.name
}
if (!moves.length) {
return black.name
}
Expand All @@ -91,6 +96,10 @@ export const useGameStore = create((set, get) => ({
},
currentColor: () => {
let moves = get().moves
let handicap = get().handicap
if (handicap > moves.length) {
return BLACK
}
if (!moves.length) {
return BLACK
}
Expand Down Expand Up @@ -158,7 +167,8 @@ export const useGameStore = create((set, get) => ({
state.moves = moves
state.gameState.board = rehydrate(baseBoard)
state.gameState.forbidden = forbidden
state.queueStatue = "up_to_date"
state.handicap = game.handicap
state.queueStatus = "up_to_date"
}))
},
}))
Expand Down
10 changes: 8 additions & 2 deletions src/main/java/com/bernd/GameController.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ private int getCurrentColor(Game game, String principal) {
if (game.gameHasEnded()) {
return 0;
}
if (game.handicap() != 0) {
if (game.remainingHandicap() != 0) {
return Board.B;
}
int color = getColor(game, principal);
Expand All @@ -96,8 +96,14 @@ private static int getColor(Game game, String principal) {
if (!(game.isBlack(principal) || game.isWhite(principal))) {
return 0;
}
if (game.remainingHandicap() > 0) {
if (!game.isBlack(principal)) {
return 0;
}
return Board.B;
}
if (game.isSelfPlay()) {
return game.moves().size() + game.handicap() % 2 == 0 ?
return game.moves().size() + game.remainingHandicap() % 2 == 0 ?
Board.B : Board.W;
}
return game.isBlack(principal) ? Board.B : Board.W;
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/bernd/LobbyController.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ public ViewGame startEdit(@RequestBody MatchRequest request) {
0,
createEmptyBoard(request.dim()),
request.dim(),
0,
request.handicap(),
request.handicap(),
new int[]{-1, -1},
MoveList.create(request.dim())));
activeGames.put(ActiveGame.fromGame(game));
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/bernd/LobbyUsers.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

import com.bernd.model.User;
import com.bernd.model.UserList;
import org.springframework.stereotype.Component;

import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.springframework.stereotype.Component;

@Component
public class LobbyUsers {
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/bernd/model/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public record Game(
int[][] board,
int dim,
int handicap,
int remainingHandicap,
int[] forbidden,
MoveList moves
) {
Expand Down Expand Up @@ -82,6 +83,7 @@ private Game updateInternal(Move move) {
.build();
}
return toBuilder()
.withRemainingHandicap(Math.max(0, remainingHandicap - 1))
.withBoard(result)
.withForbidden(NOT_FORBIDDEN)
.build();
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/com/bernd/model/GameBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,19 @@ public final class GameBuilder {

private boolean counting;
private int countingAgreed;
private int remainingHandicap;
private int[][] board;
private int[] forbidden;

private GameBuilder(Game game) {
this.game = game;
}

public GameBuilder withRemainingHandicap(int remainingHandicap) {
this.remainingHandicap = remainingHandicap;
return this;
}

public GameBuilder withCounting(boolean counting) {
this.counting = counting;
return this;
Expand All @@ -38,6 +44,7 @@ public GameBuilder withForbidden(int x, int y) {

static GameBuilder builder(Game game) {
GameBuilder builder = new GameBuilder(game);
builder.remainingHandicap = game.remainingHandicap();
builder.counting = game.counting();
builder.countingAgreed = game.countingAgreed();
builder.board = game.board();
Expand All @@ -55,6 +62,7 @@ public Game build() {
board,
game.dim(),
game.handicap(),
remainingHandicap,
forbidden,
game.moves()
);
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/bernd/model/OpenGame.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public Game accept(String opponent, AcceptRequest acceptRequest) {
createEmptyBoard(dim),
dim,
acceptRequest.handicap(),
acceptRequest.handicap(),
new int[]{-1, -1},
MoveList.create(dim));
}
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/bernd/model/ViewGame.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public record ViewGame(
User white,
int dim,
int handicap,
int remainingHandicap,
List<GameMove> moves
) {

Expand All @@ -18,6 +19,7 @@ static ViewGame fromGame(Game game) {
game.white(),
game.dim(),
game.handicap(),
game.remainingHandicap(),
game.moves().asList());
}
}
1 change: 1 addition & 0 deletions src/test/java/com/bernd/model/GameBuilderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ void unchanged() {
createEmptyBoard(9),
9,
2,
2,
new int[]{-1, -1},
MoveList.create(2));
Game game2 = GameBuilder.builder(game).build();
Expand Down

0 comments on commit 7041db1

Please sign in to comment.