Skip to content

Commit

Permalink
fix self play
Browse files Browse the repository at this point in the history
  • Loading branch information
h908714124 committed Aug 7, 2024
1 parent 862c6de commit 88f10fe
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 23 deletions.
48 changes: 29 additions & 19 deletions src/main/java/com/bernd/GameController.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand All @@ -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();
}
Expand All @@ -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());
Expand All @@ -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
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/bernd/game/MoveList.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.bernd.model.GameMove;
import com.bernd.model.Move;

import java.util.ArrayList;
import java.util.List;

Expand All @@ -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) {
Expand Down
14 changes: 13 additions & 1 deletion src/main/java/com/bernd/model/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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) :
Expand Down Expand Up @@ -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);
}
}
1 change: 1 addition & 0 deletions src/main/java/com/bernd/model/GameMove.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ public record GameMove(
int y,
boolean counting,
boolean resetCounting,
boolean agreeCounting,
boolean end) {
}
4 changes: 2 additions & 2 deletions src/main/java/com/bernd/model/Move.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 88f10fe

Please sign in to comment.