diff --git a/src/main/java/com/bernd/GameController.java b/src/main/java/com/bernd/GameController.java index 3a1ca82..5d2dd37 100644 --- a/src/main/java/com/bernd/GameController.java +++ b/src/main/java/com/bernd/GameController.java @@ -5,14 +5,12 @@ import com.bernd.model.AcceptRequest; import com.bernd.model.ActiveGame; import com.bernd.model.Game; -import com.bernd.model.GameMove; import com.bernd.model.Move; import com.bernd.model.OpenGame; import com.bernd.model.ViewGame; import com.bernd.util.Auth; import com.bernd.util.RandomString; -import java.security.Principal; -import java.util.Objects; +import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.messaging.core.MessageSendingOperations; import org.springframework.messaging.handler.annotation.MessageMapping; @@ -22,6 +20,12 @@ import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.web.server.ResponseStatusException; + +import java.security.Principal; +import java.util.Objects; + +import static com.bernd.util.Util.COLORS; @Controller public class GameController { @@ -47,7 +51,7 @@ public class GameController { public ViewGame getGame(@PathVariable String id) { Game game = games.get(id); if (game == null) { - return null; + throw new ResponseStatusException(HttpStatus.NOT_FOUND, "no such game"); } return game.toView(); } @@ -58,10 +62,8 @@ public void action(Move move, Principal p) { if (p == null || game == null) { return; } - String principal = Objects.toString(p.getName(), ""); - int color = principal.equals(game.black().name()) ? Board.B : - principal.equals(game.white().name()) ? Board.W : 0; - if (!canMove(game, color)) { + int color = getCurrentColor(game, Objects.toString(p.getName(), "")); + if (color == 0) { return; } Move updatedMove = move.withColor(color).withMoveNumber(game.moves().size()); @@ -74,25 +76,33 @@ public void action(Move move, Principal p) { } } - private boolean canMove(Game game, int color) { + private int getCurrentColor(Game game, String principal) { if (game.gameHasEnded()) { - return false; + return 0; } + if (game.handicap() != 0) { + return Board.B; + } + int color = getColor(game, principal); if (color == 0) { - return false; + return 0; } MoveList moves = game.moves(); - if (game.counting()) { - return true; + if (moves.isEmpty()) { + return Board.B; } - if (game.handicap() != 0) { - return color == Board.B; + return moves.get(moves.size() - 1).color() ^ COLORS; + } + + private static int getColor(Game game, String principal) { + if (!(game.isBlack(principal) || game.isWhite(principal))) { + return 0; } - if (moves.isEmpty()) { - return color == Board.B; + if (game.isSelfPlay()) { + return game.moves().size() + game.handicap() % 2 == 0 ? + Board.B : Board.W; } - GameMove lastMove = moves.get(moves.size() - 1); - return color != lastMove.color(); + return game.isBlack(principal) ? Board.B : Board.W; } @ResponseBody diff --git a/src/main/java/com/bernd/game/MoveList.java b/src/main/java/com/bernd/game/MoveList.java index 5d24ea5..74c5b68 100644 --- a/src/main/java/com/bernd/game/MoveList.java +++ b/src/main/java/com/bernd/game/MoveList.java @@ -2,6 +2,7 @@ import com.bernd.model.GameMove; import com.bernd.model.Move; + import java.util.ArrayList; import java.util.List; @@ -18,7 +19,7 @@ public static MoveList create(int size) { } public void addGameEndMarker() { - moves.add(new GameMove(moves.size(), 0, true, -1, -1, true, false, true)); + moves.add(new GameMove(moves.size(), 0, true, -1, -1, true, false, false, true)); } public void add(Move move, boolean counting) { diff --git a/src/main/java/com/bernd/model/Game.java b/src/main/java/com/bernd/model/Game.java index de8e188..3e6f58b 100644 --- a/src/main/java/com/bernd/model/Game.java +++ b/src/main/java/com/bernd/model/Game.java @@ -39,6 +39,7 @@ public Game update(Move move) { } private Game updateInternal(Move move) { + moves.add(move, counting); if (move.agreeCounting()) { if ((countingAgreed | move.color()) == COLORS) { moves.addGameEndMarker(); @@ -47,7 +48,6 @@ private Game updateInternal(Move move) { .withCountingAgreed(countingAgreed | move.color()) .build(); } - moves.add(move, counting); if (counting) { int[][] updated = move.resetCounting() ? Toggle.resetCounting(board) : @@ -153,4 +153,16 @@ public boolean gameHasEnded() { public GameBuilder toBuilder() { return GameBuilder.builder(this); } + + public boolean isSelfPlay() { + return black.name().equals(white.name()); + } + + public boolean isWhite(String name) { + return white.name().equals(name); + } + + public boolean isBlack(String name) { + return black.name().equals(name); + } } diff --git a/src/main/java/com/bernd/model/GameMove.java b/src/main/java/com/bernd/model/GameMove.java index d432da7..09b0131 100644 --- a/src/main/java/com/bernd/model/GameMove.java +++ b/src/main/java/com/bernd/model/GameMove.java @@ -8,5 +8,6 @@ public record GameMove( int y, boolean counting, boolean resetCounting, + boolean agreeCounting, boolean end) { } diff --git a/src/main/java/com/bernd/model/Move.java b/src/main/java/com/bernd/model/Move.java index 97dd6e1..72444a1 100644 --- a/src/main/java/com/bernd/model/Move.java +++ b/src/main/java/com/bernd/model/Move.java @@ -11,11 +11,11 @@ public record Move( int y) { public GameMove toGameMove(boolean counting) { - return new GameMove(n, color, pass, x, y, counting, resetCounting, false); + return new GameMove(n, color, pass, x, y, counting, resetCounting, agreeCounting, false); } public GameMove gameEnd(boolean counting) { - return new GameMove(n, color, pass, x, y, counting, resetCounting, true); + return new GameMove(n, color, pass, x, y, counting, resetCounting, agreeCounting, true); } public Move withColor(int color) {