205 lines
5.5 KiB
Java
205 lines
5.5 KiB
Java
package dev.kske.chess.board;
|
|
|
|
import java.util.Iterator;
|
|
import java.util.Objects;
|
|
|
|
import dev.kske.chess.board.Piece.Color;
|
|
|
|
/**
|
|
* Project: <strong>Chess</strong><br>
|
|
* File: <strong>Log.java</strong><br>
|
|
* Created: <strong>09.07.2019</strong><br>
|
|
* Author: <strong>Kai S. K. Engelbart</strong>
|
|
*/
|
|
public class Log implements Iterable<MoveNode> {
|
|
|
|
private MoveNode root, current;
|
|
|
|
private Position enPassant;
|
|
private Color activeColor;
|
|
private int fullmoveNumber, halfmoveClock;
|
|
|
|
public Log() {
|
|
reset();
|
|
}
|
|
|
|
/**
|
|
* Creates a (partially deep) copy of another {@link Log} instance which begins
|
|
* with the current {@link MoveNode}.
|
|
*
|
|
* @param other The {@link Log} instance to copy
|
|
* @param copyVariations If set to {@code true}, subsequent variations of the
|
|
* current {@link MoveNode} are copied with the
|
|
* {@link Log}
|
|
*/
|
|
public Log(Log other, boolean copyVariations) {
|
|
enPassant = other.enPassant;
|
|
activeColor = other.activeColor;
|
|
fullmoveNumber = other.fullmoveNumber;
|
|
halfmoveClock = other.halfmoveClock;
|
|
|
|
// The new root is the current node of the copied instance
|
|
if (!other.isEmpty()) {
|
|
root = new MoveNode(other.current, copyVariations);
|
|
root.setParent(null);
|
|
current = root;
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public Iterator<MoveNode> iterator() {
|
|
return new Iterator<MoveNode>() {
|
|
|
|
private MoveNode current = root;
|
|
private boolean hasNext = true;
|
|
|
|
@Override
|
|
public boolean hasNext() {
|
|
return hasNext;
|
|
}
|
|
|
|
@Override
|
|
public MoveNode next() {
|
|
MoveNode result = current;
|
|
if (current.hasVariations()) current = current.getVariations().get(0);
|
|
else hasNext = false;
|
|
return result;
|
|
}
|
|
};
|
|
}
|
|
|
|
/**
|
|
* 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) {
|
|
enPassant = pawnMove && move.yDist == 2 ? new Position(move.pos.x, move.pos.y + move.ySign) : null;
|
|
if (activeColor == Color.BLACK) ++fullmoveNumber;
|
|
if (pawnMove || capturedPiece != null) halfmoveClock = 0;
|
|
else++halfmoveClock;
|
|
activeColor = activeColor.opposite();
|
|
final MoveNode leaf = new MoveNode(move, capturedPiece, enPassant, activeColor, fullmoveNumber, halfmoveClock);
|
|
|
|
if (isEmpty()) {
|
|
root = leaf;
|
|
current = leaf;
|
|
} else {
|
|
current.addVariation(leaf);
|
|
current = leaf;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Removed the last move from the log and adjusts its state to the previous
|
|
* move.
|
|
*/
|
|
public void removeLast() {
|
|
if (hasParent()) {
|
|
current.getParent().getVariations().remove(current);
|
|
current = current.getParent();
|
|
update();
|
|
} else reset();
|
|
}
|
|
|
|
public boolean isEmpty() { return root == null; }
|
|
|
|
public boolean hasParent() {
|
|
return !isEmpty() && current.hasParent();
|
|
}
|
|
|
|
/**
|
|
* Reverts the log to its initial state corresponding to the default board
|
|
* position.
|
|
*/
|
|
public void reset() {
|
|
root = null;
|
|
current = null;
|
|
enPassant = null;
|
|
activeColor = Color.WHITE;
|
|
fullmoveNumber = 1;
|
|
halfmoveClock = 0;
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @param index
|
|
*/
|
|
public void selectNextNode(int index) {
|
|
if (!isEmpty() && current.hasVariations() && index < current.getVariations().size()) {
|
|
current = current.getVariations().get(index);
|
|
update();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Selects the parent of the current {@link MoveNode} as the current node.
|
|
*/
|
|
public void selectPreviousNode() {
|
|
if (hasParent()) {
|
|
current = current.getParent();
|
|
update();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Selects the root {@link MoveNode} as the current node.
|
|
*/
|
|
public void selectRootNode() {
|
|
if (!isEmpty()) {
|
|
current = root;
|
|
update();
|
|
}
|
|
}
|
|
|
|
private void update() {
|
|
activeColor = current.activeColor;
|
|
enPassant = current.enPassant;
|
|
fullmoveNumber = current.fullmoveCounter;
|
|
halfmoveClock = current.halfmoveClock;
|
|
}
|
|
|
|
@Override
|
|
public int hashCode() {
|
|
return Objects.hash(activeColor, current, enPassant, fullmoveNumber, halfmoveClock, root);
|
|
}
|
|
|
|
@Override
|
|
public boolean equals(Object obj) {
|
|
if (this == obj) return true;
|
|
if (obj == null) return false;
|
|
if (getClass() != obj.getClass()) return false;
|
|
Log other = (Log) obj;
|
|
return activeColor == other.activeColor && Objects.equals(current, other.current)
|
|
&& Objects.equals(enPassant, other.enPassant) && fullmoveNumber == other.fullmoveNumber
|
|
&& halfmoveClock == other.halfmoveClock && Objects.equals(root, other.root);
|
|
}
|
|
|
|
/**
|
|
* @return The first logged move, or {@code null} if there is none
|
|
*/
|
|
public MoveNode getRoot() { return root; }
|
|
|
|
/**
|
|
* @return the last logged move, or {@code null} if there is none
|
|
*/
|
|
public MoveNode getLast() { return current; }
|
|
|
|
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 int getFullmoveNumber() { return fullmoveNumber; }
|
|
|
|
public void setFullmoveNumber(int fullmoveCounter) { this.fullmoveNumber = fullmoveCounter; }
|
|
|
|
public int getHalfmoveClock() { return halfmoveClock; }
|
|
|
|
public void setHalfmoveClock(int halfmoveClock) { this.halfmoveClock = halfmoveClock; }
|
|
} |