Skip to content

Commit

Permalink
calculate remaining handicap
Browse files Browse the repository at this point in the history
  • Loading branch information
h908714124 committed Aug 10, 2024
1 parent 686a52c commit a3c99c2
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 13 deletions.
3 changes: 3 additions & 0 deletions src/main/java/com/bernd/GameController.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ public void action(Move move, Principal p) {
if (p == null || game == null) {
return;
}
if (game.isForbidden(move)) {
return;
}
int principalColor = getColorFromPrincipal(game, getPrincipal(p));
int color = getColorFromGameState(game);
if (color == 0
Expand Down
1 change: 0 additions & 1 deletion src/main/java/com/bernd/LobbyController.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ public ViewGame startEdit(@RequestBody MatchRequest request) {
createEmptyBoard(request.dim()),
request.dim(),
request.handicap(),
request.handicap(),
new int[]{-1, -1},
MoveList.create(request.dim())));
activeGames.put(ActiveGame.fromGame(game));
Expand Down
10 changes: 8 additions & 2 deletions src/main/java/com/bernd/model/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ public record Game(
int[][] board,
int dim,
int handicap,
int remainingHandicap,
int[] forbidden,
MoveList moves
) {
Expand Down Expand Up @@ -83,7 +82,6 @@ private Game updateInternal(Move move) {
.build();
}
return toBuilder()
.withRemainingHandicap(Math.max(0, remainingHandicap - 1))
.withBoard(result)
.withForbidden(NOT_FORBIDDEN)
.build();
Expand Down Expand Up @@ -175,4 +173,12 @@ boolean opponentPassed() {
}
return getLastMove().pass();
}

public int remainingHandicap() {
return Math.max(0, handicap - moves.size());
}

public boolean isForbidden(Move move) {
return move.x() == forbidden[0] && move.y() == forbidden[1];
}
}
8 changes: 0 additions & 8 deletions src/main/java/com/bernd/model/GameBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,13 @@ 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 @@ -44,7 +38,6 @@ 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 @@ -62,7 +55,6 @@ public Game build() {
board,
game.dim(),
game.handicap(),
remainingHandicap,
forbidden,
game.moves()
);
Expand Down
1 change: 0 additions & 1 deletion src/main/java/com/bernd/model/OpenGame.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ public Game accept(String opponent, AcceptRequest acceptRequest) {
createEmptyBoard(dim),
dim,
acceptRequest.handicap(),
acceptRequest.handicap(),
new int[]{-1, -1},
MoveList.create(dim));
}
Expand Down
1 change: 0 additions & 1 deletion src/test/java/com/bernd/model/GameBuilderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ 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 a3c99c2

Please sign in to comment.