Skip to content

Commit 4615645

Browse files
author
Christopher Strauss
committed
handicap
1 parent 96655df commit 4615645

File tree

8 files changed

+35
-6
lines changed

8 files changed

+35
-6
lines changed

src/main/client/src/store.js

Lines changed: 3 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: "",
@@ -158,7 +159,8 @@ export const useGameStore = create((set, get) => ({
158159
state.moves = moves
159160
state.gameState.board = rehydrate(baseBoard)
160161
state.gameState.forbidden = forbidden
161-
state.queueStatue = "up_to_date"
162+
state.handicap = game.handicap
163+
state.queueStatus = "up_to_date"
162164
}))
163165
},
164166
}))

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

Lines changed: 2 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);
@@ -97,7 +97,7 @@ private static int getColor(Game game, String principal) {
9797
return 0;
9898
}
9999
if (game.isSelfPlay()) {
100-
return game.moves().size() + game.handicap() % 2 == 0 ?
100+
return game.moves().size() + game.remainingHandicap() % 2 == 0 ?
101101
Board.B : Board.W;
102102
}
103103
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/model/Game.java

Lines changed: 7 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
) {
@@ -71,9 +72,13 @@ private Game updateInternal(Move move) {
7172
}
7273
int x = move.x();
7374
int y = move.y();
75+
int handicapUpdated = 0;
7476
int color = move.color();
7577
int[][] updated = BoardUpdateImpl.create(board.length, x, y, color).apply(board);
7678
int[][] result = removeDeadStonesAround(updated, x, y);
79+
if (remainingHandicap > 0) {
80+
handicapUpdated = remainingHandicap - 1;
81+
}
7782
Direction direction = getDirection(x, y, result, updated);
7883
if (isKo(x, y, color, result, direction)) {
7984
return toBuilder()
@@ -82,6 +87,8 @@ private Game updateInternal(Move move) {
8287
.build();
8388
}
8489
return toBuilder()
90+
.withRemainingHandicap(handicapUpdated)
91+
.withHandicap(handicap)
8592
.withBoard(result)
8693
.withForbidden(NOT_FORBIDDEN)
8794
.build();

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,25 @@ public final class GameBuilder {
55

66
private boolean counting;
77
private int countingAgreed;
8+
private int handicap;
9+
private int remainingHandicap;
810
private int[][] board;
911
private int[] forbidden;
1012

1113
private GameBuilder(Game game) {
1214
this.game = game;
1315
}
1416

17+
public GameBuilder withHandicap(int handicap) {
18+
this.handicap = handicap;
19+
return this;
20+
}
21+
22+
public GameBuilder withRemainingHandicap(int remainingHandicap) {
23+
this.remainingHandicap = remainingHandicap;
24+
return this;
25+
}
26+
1527
public GameBuilder withCounting(boolean counting) {
1628
this.counting = counting;
1729
return this;
@@ -38,6 +50,8 @@ public GameBuilder withForbidden(int x, int y) {
3850

3951
static GameBuilder builder(Game game) {
4052
GameBuilder builder = new GameBuilder(game);
53+
builder.handicap = game.handicap();
54+
builder.remainingHandicap = game.remainingHandicap();
4155
builder.counting = game.counting();
4256
builder.countingAgreed = game.countingAgreed();
4357
builder.board = game.board();
@@ -54,7 +68,8 @@ public Game build() {
5468
countingAgreed,
5569
board,
5670
game.dim(),
57-
game.handicap(),
71+
handicap,
72+
remainingHandicap,
5873
forbidden,
5974
game.moves()
6075
);

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: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ public record ViewGame(
77
User black,
88
User white,
99
int dim,
10-
int handicap,
10+
int initialHandicap,
11+
int currentHandicap,
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)