From c20060b1ca9f063ed2ea8b30f77418d6abd86c58 Mon Sep 17 00:00:00 2001 From: CyB3RC0nN0R Date: Wed, 24 Jul 2019 07:32:59 +0200 Subject: [PATCH] Moved activeColor, fullmoveCounter and halfmoveClock to Log --- src/dev/kske/chess/board/Board.java | 40 ++++--------------------- src/dev/kske/chess/board/Log.java | 46 +++++++++++++++++++++-------- src/dev/kske/chess/board/Piece.java | 4 +++ 3 files changed, 42 insertions(+), 48 deletions(-) diff --git a/src/dev/kske/chess/board/Board.java b/src/dev/kske/chess/board/Board.java index a507405..da98313 100644 --- a/src/dev/kske/chess/board/Board.java +++ b/src/dev/kske/chess/board/Board.java @@ -20,11 +20,8 @@ public class Board implements Cloneable { private Piece[][] boardArr; private Map kingPos; private Map> castlingRights; - private Color activeColor; private Log log; - private int fullmoveCounter, halfmoveClock; - private static final Map positionScores; static { @@ -147,19 +144,7 @@ public class Board implements Cloneable { if (piece.getType() == Type.KING) kingPos.put(piece.getColor(), move.dest); // Update log - log.add(move, capturePiece); - - // Swap active color - activeColor = activeColor.opposite(); - - // Increment fullmove counter - if (piece.getColor() == Color.BLACK) ++fullmoveCounter; - - // Increment halfmove clock - ++halfmoveClock; - - // Reset halfmove clock - if (piece.getType() == Type.PAWN || capturePiece != null) halfmoveClock = 0; + log.add(move, capturePiece, piece.getType() == Type.PAWN); updateCastlingRights(); } @@ -211,15 +196,6 @@ public class Board implements Cloneable { // Update log log.removeLast(); - // Swap active color - activeColor = activeColor.opposite(); - - // Decrement fullmove counter - if (getPos(move).getColor() == Color.BLACK) --fullmoveCounter; - - // Decrement halfmove clock - --halfmoveClock; - updateCastlingRights(); } @@ -395,11 +371,6 @@ public class Board implements Cloneable { blackCastling.put(Type.QUEEN, true); castlingRights.put(Color.WHITE, whiteCastling); castlingRights.put(Color.BLACK, blackCastling); - - activeColor = Color.WHITE; - - fullmoveCounter = 1; - halfmoveClock = 0; } /** @@ -424,7 +395,6 @@ public class Board implements Cloneable { board.kingPos = new HashMap<>(); board.kingPos.putAll(kingPos); - // TODO: activeColor clone ? board.log = (Log) log.clone(); return board; @@ -477,7 +447,7 @@ public class Board implements Cloneable { } // Active color - sb.append(" " + (activeColor == Color.WHITE ? 'w' : 'b')); + sb.append(" " + log.getActiveColor().firstChar()); sb.append(' '); StringBuilder castlingSb = new StringBuilder(); @@ -492,10 +462,10 @@ public class Board implements Cloneable { sb.append(" -"); // Halfmove clock - sb.append(" " + halfmoveClock); + sb.append(" " + log.getLast().halfmoveClock); // Fullmove counter - sb.append(" " + fullmoveCounter); + sb.append(" " + log.getLast().fullmoveCounter); return sb.toString(); } @@ -532,5 +502,5 @@ public class Board implements Cloneable { /** * @return The active color for the next move */ - public Color getActiveColor() { return activeColor; } + public Color getActiveColor() { return log.getActiveColor(); } } diff --git a/src/dev/kske/chess/board/Log.java b/src/dev/kske/chess/board/Log.java index 05554df..3311cd6 100644 --- a/src/dev/kske/chess/board/Log.java +++ b/src/dev/kske/chess/board/Log.java @@ -3,6 +3,8 @@ package dev.kske.chess.board; import java.util.ArrayList; import java.util.List; +import dev.kske.chess.board.Piece.Color; + /** * Project: Chess
* File: Log.java
@@ -11,22 +13,35 @@ import java.util.List; */ public class Log implements Cloneable { - private List moves; + private List moves; + private Color activeColor; public Log() { - moves = new ArrayList<>(); + moves = new ArrayList<>(); + activeColor = Color.WHITE; } - public void add(Move move, Piece capturedPiece) { - moves.add(new LoggedMove(move, capturedPiece)); + public void add(Move move, Piece capturedPiece, boolean pawnMove) { + 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, fullmoveCounter, halfmoveClock)); } - public LoggedMove getLast() { - return moves.get(moves.size() - 1); - } - + public LoggedMove getLast() { return moves.get(moves.size() - 1); } + public void removeLast() { - if (!moves.isEmpty()) moves.remove(moves.size() - 1); + if (!moves.isEmpty()) { + activeColor = activeColor.opposite(); + moves.remove(moves.size() - 1); + } } @Override @@ -42,14 +57,19 @@ public class Log implements Cloneable { return log; } + public Color getActiveColor() { return activeColor; } + public static class LoggedMove { - public final Move move; + public final Move move; public final Piece capturedPiece; + public final int fullmoveCounter, halfmoveClock; - public LoggedMove(Move move, Piece capturedPiece) { - this.move = move; - this.capturedPiece = capturedPiece; + public LoggedMove(Move move, Piece capturedPiece, int fullmoveCounter, int halfmoveClock) { + this.move = move; + this.capturedPiece = capturedPiece; + this.fullmoveCounter = fullmoveCounter; + this.halfmoveClock = halfmoveClock; } } } diff --git a/src/dev/kske/chess/board/Piece.java b/src/dev/kske/chess/board/Piece.java index 0006e84..5df4314 100644 --- a/src/dev/kske/chess/board/Piece.java +++ b/src/dev/kske/chess/board/Piece.java @@ -94,5 +94,9 @@ public abstract class Piece implements Cloneable { public Color opposite() { return this == WHITE ? BLACK : WHITE; } + + public char firstChar() { + return this == WHITE ? 'w' : 'b'; + } } }