Skip to content

Commit

Permalink
server side refactoring, add field color to move
Browse files Browse the repository at this point in the history
  • Loading branch information
h908714124 committed Aug 6, 2024
1 parent 794a329 commit 7234baf
Show file tree
Hide file tree
Showing 11 changed files with 176 additions and 83 deletions.
3 changes: 3 additions & 0 deletions src/main/client/src/Game.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
import {
StompContext,
BLACK,
WHITE,
TERRITORY,
TERRITORY_B,
REMOVED_B,
Expand Down Expand Up @@ -43,6 +44,7 @@ export const Game = () => {
let {gameId} = useParams()
let stompClient = useContext(StompContext)
let auth = useAuthStore(state => state.auth)
let black = useGameStore(state => state.black)
let setGameState = useGameStore(state => state.setGameState)
let queueStatus = useGameStore(state => state.queueStatus)
let queueLength = useGameStore(state => state.queueLength)
Expand Down Expand Up @@ -167,6 +169,7 @@ export const Game = () => {
body: JSON.stringify({
id: gameId,
n: queueLength(),
color: black.name === auth.name ? BLACK : WHITE,
x: cursor_x,
y: cursor_y,
}),
Expand Down
9 changes: 7 additions & 2 deletions src/main/client/src/feature/GamePanel.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,11 @@ function Panel({zoom, setZoom}) {
let queueLength = useGameStore(state => state.queueLength)
let counting = useGameStore(state => state.counting)
let countingComplete = useGameStore(state => state.countingComplete)
let currentColor = useGameStore(state => state.currentColor)
let currentPlayer = useGameStore(state => state.currentPlayer)
let { board, gameHasEnded } = useGameStore(state => state.gameState)
let navigate = useNavigate()
let myColor = black.name === auth.name ? BLACK : WHITE
let onExit = useCallback(() => {
navigate(base + "/lobby")
}, [navigate])
Expand All @@ -62,6 +64,7 @@ function Panel({zoom, setZoom}) {
destination: "/app/game/move",
body: JSON.stringify({
id: gameId,
color: myColor,
n: queueLength(),
pass: true,
}),
Expand All @@ -72,21 +75,23 @@ function Panel({zoom, setZoom}) {
destination: "/app/game/move",
body: JSON.stringify({
id: gameId,
color: myColor,
n: queueLength(),
resetCounting: true,
}),
})
}, [stompClient, gameId, queueLength])
}, [stompClient, gameId, queueLength, currentColor])
let onCountingAgree = useCallback(() => {
stompClient.publish({
destination: "/app/game/move",
body: JSON.stringify({
id: gameId,
color: myColor,
n: queueLength(),
agreeCounting: true,
}),
})
}, [stompClient, gameId, queueLength])
}, [stompClient, gameId, queueLength, currentColor])
if (!board.length) {
return <span>Loading...</span>
}
Expand Down
7 changes: 3 additions & 4 deletions src/main/java/com/bernd/GameController.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@ public void action(Move move, Principal principal) {
return;
}
int moveNumber = game.moves().size();
int color = game.currentColor();
String currentPlayer = game.currentColor() == Board.B ? game.black().name() : game.white().name();
String currentPlayer = move.color() == Board.B ? game.black().name() : game.white().name();
if (!principal.getName().equals(currentPlayer)) {
return; // discard
}
Expand All @@ -68,9 +67,9 @@ public void action(Move move, Principal principal) {
Game updated = game.update(move);
games.put(updated);
if (updated.gameHasEnded()) {
operations.convertAndSend("/topic/move/" + game.id(), move.gameEnd(color, updated.counting()));
operations.convertAndSend("/topic/move/" + game.id(), move.gameEnd(updated.counting()));
} else if (!move.agreeCounting()) {
operations.convertAndSend("/topic/move/" + game.id(), move.toView(color, updated.counting()));
operations.convertAndSend("/topic/move/" + game.id(), move.toView(updated.counting()));
}
}

Expand Down
4 changes: 0 additions & 4 deletions src/main/java/com/bernd/LobbyController.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import static com.bernd.game.Board.B;

@Controller
public class LobbyController {

Expand Down Expand Up @@ -77,8 +75,6 @@ public ViewGame startEdit(@RequestBody MatchRequest request) {
user,
false,
0,
principal,
B,
false,
createEmptyBoard(request.dim()),
request.dim(),
Expand Down
5 changes: 3 additions & 2 deletions 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.Arrays;
import java.util.List;
Expand Down Expand Up @@ -45,15 +46,15 @@ public void addGameEndMarker() {
pos++;
}

public void add(int color, Move move, boolean counting) {
public void add(Move move, boolean counting) {
ensureCapacity();
int ptId;
if (move.pass()) {
ptId = PASS;
} else {
ptId = dim * move.y() + move.x();
}
if (color == Board.W) {
if (move.color() == Board.W) {
ptId |= WHITE;
}
if (counting) {
Expand Down
94 changes: 35 additions & 59 deletions src/main/java/com/bernd/model/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import static com.bernd.game.Board.B;
import static com.bernd.game.Board.W;
import static com.bernd.game.Board.removeDeadStonesAround;
import static com.bernd.util.Util.COLORS;

Expand All @@ -20,8 +18,6 @@ public record Game(
User white,
boolean counting,
int countingAgreed,
String currentPlayer,
int currentColor,
boolean opponentPassed,
int[][] board,
int dim,
Expand All @@ -44,36 +40,57 @@ public Game update(Move move) {

private Game updateInternal(Move move) {
if (move.agreeCounting()) {
if ((countingAgreed | currentColor()) == COLORS) {
if ((countingAgreed | move.color()) == COLORS) {
moves.addGameEndMarker();
}
return countingAgreed(countingAgreed | currentColor());
return toBuilder()
.withCountingAgreed(countingAgreed | move.color())
.build();
}
moves.add(currentColor, move, counting);
moves.add(move, counting);
if (counting) {
if (move.resetCounting()) {
int[][] resetted = Toggle.resetCounting(board);
return game(Count.count(resetted), NOT_FORBIDDEN);
return toBuilder()
.withBoard(Count.count(resetted))
.withForbidden(NOT_FORBIDDEN)
.build();
}
int[][] toggled = Toggle.toggleStonesAt(board, move.x(), move.y());
return game(Count.count(toggled), NOT_FORBIDDEN);
return toBuilder()
.withBoard(Count.count(toggled))
.withForbidden(NOT_FORBIDDEN)
.build();
}
if (move.pass()) {
if (opponentPassed) {
return startCounting();
return toBuilder()
.withBoard(Count.count(board))
.withCounting(true)
.withForbidden(NOT_FORBIDDEN)
.build();
}
return game(board, counting, 0, true, NOT_FORBIDDEN);
return toBuilder()
.withOpponentPassed(true)
.withForbidden(NOT_FORBIDDEN)
.build();
}
int x = move.x();
int y = move.y();
int color = currentColor();
int color = move.color();
int[][] updated = BoardUpdateImpl.create(board.length, x, y, color).apply(board);
int[][] result = removeDeadStonesAround(updated, x, y);
Direction direction = getDirection(x, y, result, updated);
if (isKo(x, y, color, result, direction)) {
return game(result, new int[]{direction.moveX(x), direction.moveY(y)});
return toBuilder()
.withBoard(result)
.withForbidden(direction.moveX(x), direction.moveY(y))
.build();
}
return game(result, NOT_FORBIDDEN);
return toBuilder()
.withBoard(result)
.withForbidden(NOT_FORBIDDEN)
.build();
}

private boolean isKo(
Expand Down Expand Up @@ -128,56 +145,15 @@ private Direction getDirection(
return Direction.from(xx, yy, col, row);
}

private Game game(
int[][] board,
boolean counting,
int countingAgreed,
boolean opponentPassed,
int[] forbidden) {
return new Game(
id,
black,
white,
counting,
countingAgreed,
nextPlayer(),
nextColor(),
opponentPassed,
board,
dim,
Math.max(0, handicap - 1),
forbidden,
moves);
}

private Game game(int[][] board, int[] forbidden) {
return game(board, counting, 0, false, forbidden);
}

private Game startCounting() {
return game(Count.count(board), true, 0, true, NOT_FORBIDDEN);
}

private String nextPlayer() {
return currentPlayer.equals(black.name()) ? white().name() : black().name();
}

private int nextColor() {
if (handicap > 0) {
return currentColor;
}
return currentColor == B ? W : B;
}

public ViewGame toView() {
return ViewGame.fromGame(this);
}

private Game countingAgreed(int countingAgreed) {
return game(board, counting, countingAgreed, opponentPassed, forbidden);
}

public boolean gameHasEnded() {
return countingAgreed == COLORS;
}

public GameBuilder toBuilder() {
return GameBuilder.builder(this);
}
}
84 changes: 84 additions & 0 deletions src/main/java/com/bernd/model/GameBuilder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package com.bernd.model;

public final class GameBuilder {
private final Game game;

private boolean counting;
private int countingAgreed;
private boolean opponentPassed;
private int[][] board;
private int dim;
private int handicap;
private int[] forbidden;

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

public GameBuilder withCounting(boolean counting) {
this.counting = counting;
return this;
}

public GameBuilder withCountingAgreed(int countingAgreed) {
this.countingAgreed = countingAgreed;
return this;
}

public GameBuilder withOpponentPassed(boolean opponentPassed) {
this.opponentPassed = opponentPassed;
return this;
}

public GameBuilder withBoard(int[][] board) {
this.board = board;
return this;
}

public GameBuilder withDim(int dim) {
this.dim = dim;
return this;
}

public GameBuilder withHandicap(int handicap) {
this.handicap = handicap;
return this;
}

public GameBuilder withForbidden(int[] forbidden) {
this.forbidden = forbidden;
return this;
}

public GameBuilder withForbidden(int x, int y) {
return withForbidden(new int[]{x, y});
}

static GameBuilder builder(Game game) {
GameBuilder builder = new GameBuilder(game);
builder.counting = game.counting();
builder.countingAgreed = game.countingAgreed();
builder.opponentPassed = game.opponentPassed();
builder.board = game.board();
builder.dim = game.dim();
builder.handicap = game.handicap();
builder.forbidden = game.forbidden();
return builder;
}

public Game build() {
return new Game(
game.id(),
game.black(),
game.white(),
counting,
countingAgreed,
opponentPassed,
board,
dim,
handicap,
forbidden,
game.moves()
);
}
}
5 changes: 3 additions & 2 deletions src/main/java/com/bernd/model/Move.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@

public record Move(
String id,
int color,
int n,
boolean pass,
boolean resetCounting,
boolean agreeCounting,
int x,
int y) {

public GameMove toView(int color, boolean counting) {
public GameMove toView(boolean counting) {
return new GameMove(n, color, pass, x, y, counting, resetCounting, false);
}

public GameMove gameEnd(int color, boolean counting) {
public GameMove gameEnd(boolean counting) {
return new GameMove(n, color, pass, x, y, counting, resetCounting, true);
}
}
Loading

0 comments on commit 7234baf

Please sign in to comment.