From d121e85897a36a123d3162b2b320b7b13ed2abd7 Mon Sep 17 00:00:00 2001 From: CyB3RC0nN0R Date: Mon, 12 Aug 2019 06:44:55 +0200 Subject: [PATCH] Cleaned up and improved Log + Current log state properties - Removed Log delegates from Board --- src/dev/kske/chess/board/Board.java | 10 -- src/dev/kske/chess/board/Log.java | 128 ++++++++++++++++-------- src/dev/kske/chess/board/Pawn.java | 6 +- src/dev/kske/chess/game/Game.java | 4 +- test/dev/kske/chess/test/BoardTest.java | 2 +- 5 files changed, 95 insertions(+), 55 deletions(-) diff --git a/src/dev/kske/chess/board/Board.java b/src/dev/kske/chess/board/Board.java index ec597b1..184140b 100644 --- a/src/dev/kske/chess/board/Board.java +++ b/src/dev/kske/chess/board/Board.java @@ -571,14 +571,4 @@ public class Board implements Cloneable { * @return The move log */ public Log getLog() { return log; } - - /** - * @return The active color for the next move - */ - public Color getActiveColor() { return log.getActiveColor(); } - - /** - * @return The current en passant square, or null if there isn't any - */ - public Position getEnPassantSquare() { return log.getLast() == null ? null : log.getLast().enPassant; } } diff --git a/src/dev/kske/chess/board/Log.java b/src/dev/kske/chess/board/Log.java index b5aae82..544ebbf 100644 --- a/src/dev/kske/chess/board/Log.java +++ b/src/dev/kske/chess/board/Log.java @@ -13,45 +13,15 @@ import dev.kske.chess.board.Piece.Color; */ public class Log implements Cloneable { - private List moves; - private Color activeColor; + private List moves; + + private Position enPassant; + private Color activeColor; + private int fullmoveCounter, halfmoveClock; public Log() { - moves = new ArrayList<>(); - activeColor = Color.WHITE; - } - - public void add(Move move, Piece capturedPiece, boolean pawnMove) { - // En passant availability - Position enPassant = null; - if (pawnMove && move.yDist == 2) enPassant = new Position(move.pos.x, move.pos.y + move.ySign); - - // Fullmove counter and halfmove clock - int fullmoveCounter, halfmoveClock; - if (moves.isEmpty()) { - fullmoveCounter = 1; - halfmoveClock = 0; - } else { - fullmoveCounter = getLast().fullmoveCounter; - if (activeColor == Color.BLACK) ++fullmoveCounter; - halfmoveClock = capturedPiece != null || pawnMove ? 0 : getLast().halfmoveClock + 1; - } - activeColor = activeColor.opposite(); - moves.add(new LoggedMove(move, capturedPiece, enPassant, fullmoveCounter, halfmoveClock)); - } - - public LoggedMove getLast() { return moves.isEmpty() ? null : moves.get(moves.size() - 1); } - - public void removeLast() { - if (!moves.isEmpty()) { - activeColor = activeColor.opposite(); - moves.remove(moves.size() - 1); - } - } - - public void reset() { - moves.clear(); - activeColor = Color.WHITE; + moves = new ArrayList<>(); + reset(); } @Override @@ -67,23 +37,103 @@ public class Log implements Cloneable { return log; } + /** + * Adds a move to the move history and adjusts the log to the new position. + * + * @param move The move to log + * @param capturedPiece The piece captured with the move + * @param pawnMove {@code true} if the move was made by a pawn + */ + public void add(Move move, Piece capturedPiece, boolean pawnMove) { + // En passant availability + enPassant = pawnMove && move.yDist == 2 ? new Position(move.pos.x, move.pos.y + move.ySign) : null; + + // Fullmove counter and halfmove clock + int fullmoveCounter, halfmoveClock; + if (moves.isEmpty()) { + fullmoveCounter = 1; + halfmoveClock = 0; + } else { + fullmoveCounter = getLast().fullmoveCounter; + if (activeColor == Color.BLACK) ++fullmoveCounter; + halfmoveClock = capturedPiece != null || pawnMove ? 0 : getLast().halfmoveClock + 1; + } + activeColor = activeColor.opposite(); + moves.add(new LoggedMove(move, capturedPiece, enPassant, activeColor, fullmoveCounter, halfmoveClock)); + } + + /** + * @return the last logged move, or {@code null} if there is none + */ + public LoggedMove getLast() { return moves.isEmpty() ? null : moves.get(moves.size() - 1); } + + /** + * Removed the last move from the log and adjusts its state to the previous + * move. + */ + public void removeLast() { + if (!isEmpty()) { + moves.remove(moves.size() - 1); + if (!isEmpty()) { + LoggedMove last = moves.get(moves.size() - 1); + activeColor = last.activeColor; + enPassant = last.enPassant; + fullmoveCounter = last.fullmoveCounter; + halfmoveClock = last.halfmoveClock; + } else reset(); + } else reset(); + } + + public boolean isEmpty() { return moves.isEmpty(); } + + /** + * Reverts the log to its initial state corresponding to the default board + * position. + */ + public void reset() { + moves.clear(); + enPassant = null; + activeColor = Color.WHITE; + fullmoveCounter = 1; + halfmoveClock = 0; + } + + public List getLoggedMoves() { return moves; } + + public List getMoves() { return moves; } + + public void setMoves(List moves) { this.moves = moves; } + + public Position getEnPassant() { return enPassant; } + + public void setEnPassant(Position enPassant) { this.enPassant = enPassant; } + public Color getActiveColor() { return activeColor; } public void setActiveColor(Color activeColor) { this.activeColor = activeColor; } - public List getLoggedMoves() { return moves; } + public int getFullmoveCounter() { return fullmoveCounter; } + + public void setFullmoveCounter(int fullmoveCounter) { this.fullmoveCounter = fullmoveCounter; } + + public int getHalfmoveClock() { return halfmoveClock; } + + public void setHalfmoveClock(int halfmoveClock) { this.halfmoveClock = halfmoveClock; } public static class LoggedMove { public final Move move; public final Piece capturedPiece; public final Position enPassant; + public final Color activeColor; public final int fullmoveCounter, halfmoveClock; - public LoggedMove(Move move, Piece capturedPiece, Position enPassant, int fullmoveCounter, int halfmoveClock) { + public LoggedMove(Move move, Piece capturedPiece, Position enPassant, Color activeColor, int fullmoveCounter, + int halfmoveClock) { this.move = move; this.capturedPiece = capturedPiece; this.enPassant = enPassant; + this.activeColor = activeColor; this.fullmoveCounter = fullmoveCounter; this.halfmoveClock = halfmoveClock; } diff --git a/src/dev/kske/chess/board/Pawn.java b/src/dev/kske/chess/board/Pawn.java index 596dc38..a9cac51 100644 --- a/src/dev/kske/chess/board/Pawn.java +++ b/src/dev/kske/chess/board/Pawn.java @@ -29,7 +29,7 @@ public class Pawn extends Piece { move.type = Move.Type.PAWN_PROMOTION; // Mark the move as en passant if necessary - if (strafe && move.dest.equals(board.getEnPassantSquare())) { + if (strafe && move.dest.equals(board.getLog().getEnPassant())) { enPassant = true; move.type = Move.Type.EN_PASSANT; } @@ -84,8 +84,8 @@ public class Pawn extends Piece { moves.parallelStream().forEach(m -> m.type = Move.Type.PAWN_PROMOTION); // Add en passant move if necessary - if (board.getEnPassantSquare() != null) { - Move move = new Move(pos, board.getEnPassantSquare(), Move.Type.EN_PASSANT); + if (board.getLog().getEnPassant() != null) { + Move move = new Move(pos, board.getLog().getEnPassant(), Move.Type.EN_PASSANT); if (move.isDiagonal() && move.xDist == 1) moves.add(move); } diff --git a/src/dev/kske/chess/game/Game.java b/src/dev/kske/chess/game/Game.java index 47cfca7..16ce291 100644 --- a/src/dev/kske/chess/game/Game.java +++ b/src/dev/kske/chess/game/Game.java @@ -79,7 +79,7 @@ public class Game { case CHECK: System.out.printf("%s in check!%n", player.color.opposite()); default: - players.get(board.getActiveColor()).requestMove(); + players.get(board.getLog().getActiveColor()).requestMove(); } } else player.requestMove(); } @@ -111,7 +111,7 @@ public class Game { black.setColor(Color.WHITE); players.put(Color.WHITE, black); players.put(Color.BLACK, white); - players.get(board.getActiveColor()).requestMove(); + players.get(board.getLog().getActiveColor()).requestMove(); } public Board getBoard() { return board; } diff --git a/test/dev/kske/chess/test/BoardTest.java b/test/dev/kske/chess/test/BoardTest.java index 44212ee..a4cff22 100644 --- a/test/dev/kske/chess/test/BoardTest.java +++ b/test/dev/kske/chess/test/BoardTest.java @@ -41,6 +41,6 @@ class BoardTest { clone.getBoardArr()[0][0] = new Queen(Color.BLACK, clone); clone.move(new Move(1, 1, 1, 2)); assertNotEquals(clone.getBoardArr()[0][0], board.getBoardArr()[0][0]); - assertNotEquals(clone.getActiveColor(), board.getActiveColor()); + assertNotEquals(clone.getLog().getActiveColor(), board.getLog().getActiveColor()); } }