Moved activeColor, fullmoveCounter and halfmoveClock to Log
This commit is contained in:
parent
5aa48edb93
commit
dc67b624ce
@ -20,11 +20,8 @@ public class Board implements Cloneable {
|
||||
private Piece[][] boardArr;
|
||||
private Map<Color, Position> kingPos;
|
||||
private Map<Color, Map<Type, Boolean>> castlingRights;
|
||||
private Color activeColor;
|
||||
private Log log;
|
||||
|
||||
private int fullmoveCounter, halfmoveClock;
|
||||
|
||||
private static final Map<Type, int[][]> 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(); }
|
||||
}
|
||||
|
@ -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: <strong>Chess</strong><br>
|
||||
* File: <strong>Log.java</strong><br>
|
||||
@ -11,22 +13,35 @@ import java.util.List;
|
||||
*/
|
||||
public class Log implements Cloneable {
|
||||
|
||||
private List<LoggedMove> moves;
|
||||
private List<LoggedMove> 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user