Moved activeColor, fullmoveCounter and halfmoveClock to Log

This commit is contained in:
Kai S. K. Engelbart 2019-07-24 07:32:59 +02:00
parent 3d232dd131
commit c20060b1ca
3 changed files with 42 additions and 48 deletions

View File

@ -20,11 +20,8 @@ public class Board implements Cloneable {
private Piece[][] boardArr; private Piece[][] boardArr;
private Map<Color, Position> kingPos; private Map<Color, Position> kingPos;
private Map<Color, Map<Type, Boolean>> castlingRights; private Map<Color, Map<Type, Boolean>> castlingRights;
private Color activeColor;
private Log log; private Log log;
private int fullmoveCounter, halfmoveClock;
private static final Map<Type, int[][]> positionScores; private static final Map<Type, int[][]> positionScores;
static { static {
@ -147,19 +144,7 @@ public class Board implements Cloneable {
if (piece.getType() == Type.KING) kingPos.put(piece.getColor(), move.dest); if (piece.getType() == Type.KING) kingPos.put(piece.getColor(), move.dest);
// Update log // Update log
log.add(move, capturePiece); log.add(move, capturePiece, piece.getType() == Type.PAWN);
// 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;
updateCastlingRights(); updateCastlingRights();
} }
@ -211,15 +196,6 @@ public class Board implements Cloneable {
// Update log // Update log
log.removeLast(); log.removeLast();
// Swap active color
activeColor = activeColor.opposite();
// Decrement fullmove counter
if (getPos(move).getColor() == Color.BLACK) --fullmoveCounter;
// Decrement halfmove clock
--halfmoveClock;
updateCastlingRights(); updateCastlingRights();
} }
@ -395,11 +371,6 @@ public class Board implements Cloneable {
blackCastling.put(Type.QUEEN, true); blackCastling.put(Type.QUEEN, true);
castlingRights.put(Color.WHITE, whiteCastling); castlingRights.put(Color.WHITE, whiteCastling);
castlingRights.put(Color.BLACK, blackCastling); 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 = new HashMap<>();
board.kingPos.putAll(kingPos); board.kingPos.putAll(kingPos);
// TODO: activeColor clone ?
board.log = (Log) log.clone(); board.log = (Log) log.clone();
return board; return board;
@ -477,7 +447,7 @@ public class Board implements Cloneable {
} }
// Active color // Active color
sb.append(" " + (activeColor == Color.WHITE ? 'w' : 'b')); sb.append(" " + log.getActiveColor().firstChar());
sb.append(' '); sb.append(' ');
StringBuilder castlingSb = new StringBuilder(); StringBuilder castlingSb = new StringBuilder();
@ -492,10 +462,10 @@ public class Board implements Cloneable {
sb.append(" -"); sb.append(" -");
// Halfmove clock // Halfmove clock
sb.append(" " + halfmoveClock); sb.append(" " + log.getLast().halfmoveClock);
// Fullmove counter // Fullmove counter
sb.append(" " + fullmoveCounter); sb.append(" " + log.getLast().fullmoveCounter);
return sb.toString(); return sb.toString();
} }
@ -532,5 +502,5 @@ public class Board implements Cloneable {
/** /**
* @return The active color for the next move * @return The active color for the next move
*/ */
public Color getActiveColor() { return activeColor; } public Color getActiveColor() { return log.getActiveColor(); }
} }

View File

@ -3,6 +3,8 @@ package dev.kske.chess.board;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import dev.kske.chess.board.Piece.Color;
/** /**
* Project: <strong>Chess</strong><br> * Project: <strong>Chess</strong><br>
* File: <strong>Log.java</strong><br> * File: <strong>Log.java</strong><br>
@ -11,22 +13,35 @@ import java.util.List;
*/ */
public class Log implements Cloneable { public class Log implements Cloneable {
private List<LoggedMove> moves; private List<LoggedMove> moves;
private Color activeColor;
public Log() { public Log() {
moves = new ArrayList<>(); moves = new ArrayList<>();
activeColor = Color.WHITE;
} }
public void add(Move move, Piece capturedPiece) { public void add(Move move, Piece capturedPiece, boolean pawnMove) {
moves.add(new LoggedMove(move, capturedPiece)); 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() { public LoggedMove getLast() { return moves.get(moves.size() - 1); }
return moves.get(moves.size() - 1);
}
public void removeLast() { public void removeLast() {
if (!moves.isEmpty()) moves.remove(moves.size() - 1); if (!moves.isEmpty()) {
activeColor = activeColor.opposite();
moves.remove(moves.size() - 1);
}
} }
@Override @Override
@ -42,14 +57,19 @@ public class Log implements Cloneable {
return log; return log;
} }
public Color getActiveColor() { return activeColor; }
public static class LoggedMove { public static class LoggedMove {
public final Move move; public final Move move;
public final Piece capturedPiece; public final Piece capturedPiece;
public final int fullmoveCounter, halfmoveClock;
public LoggedMove(Move move, Piece capturedPiece) { public LoggedMove(Move move, Piece capturedPiece, int fullmoveCounter, int halfmoveClock) {
this.move = move; this.move = move;
this.capturedPiece = capturedPiece; this.capturedPiece = capturedPiece;
this.fullmoveCounter = fullmoveCounter;
this.halfmoveClock = halfmoveClock;
} }
} }
} }

View File

@ -94,5 +94,9 @@ public abstract class Piece implements Cloneable {
public Color opposite() { public Color opposite() {
return this == WHITE ? BLACK : WHITE; return this == WHITE ? BLACK : WHITE;
} }
public char firstChar() {
return this == WHITE ? 'w' : 'b';
}
} }
} }