115 lines
3.7 KiB
Java
115 lines
3.7 KiB
Java
package dev.kske.chess.board;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
import java.util.Objects;
|
|
|
|
import dev.kske.chess.board.Piece.Color;
|
|
|
|
/**
|
|
* Project: <strong>Chess</strong><br>
|
|
* File: <strong>MoveNode.java</strong><br>
|
|
* Created: <strong>02.10.2019</strong><br>
|
|
* Author: <strong>Kai S. K. Engelbart</strong>
|
|
*/
|
|
public class MoveNode {
|
|
|
|
public final Move move;
|
|
public final Piece capturedPiece;
|
|
public final Position enPassant;
|
|
public final Color activeColor;
|
|
public final int fullmoveCounter, halfmoveClock;
|
|
|
|
private MoveNode parent;
|
|
private List<MoveNode> variations;
|
|
|
|
/**
|
|
* Creates a new {@link MoveNode}.
|
|
*
|
|
* @param move The logged {@link Move}
|
|
* @param capturedPiece The {@link Piece} captures by the logged {@link Move}
|
|
* @param enPassant The en passant {@link Position} valid after the logged
|
|
* {@link Move}, or {@code null} if there is none
|
|
* @param activeColor The {@link Color} active after the logged {@link Move}
|
|
* @param fullmoveCounter
|
|
* @param halfmoveClock
|
|
*/
|
|
public MoveNode(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;
|
|
}
|
|
|
|
/**
|
|
* Creates a (deep) copy of another {@link MoveNode}.
|
|
*
|
|
* @param other The {@link MoveNode} to copy
|
|
* @param copyVariations When this is set to {@code true} a deep copy is
|
|
* created, which
|
|
* considers subsequent variations
|
|
*/
|
|
public MoveNode(MoveNode other, boolean copyVariations) {
|
|
this(other.move, other.capturedPiece, other.enPassant, other.activeColor, other.fullmoveCounter,
|
|
other.halfmoveClock);
|
|
if (copyVariations && other.variations != null) {
|
|
if (variations == null) variations = new ArrayList<>();
|
|
other.variations.forEach(variation -> {
|
|
MoveNode copy = new MoveNode(variation, true);
|
|
copy.parent = this;
|
|
variations.add(copy);
|
|
});
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Adds another {@link MoveNode} as a child node.
|
|
*
|
|
* @param variation The {@link MoveNode} to append to this {@link MoveNode}
|
|
*/
|
|
public void addVariation(MoveNode variation) {
|
|
if (variations == null) variations = new ArrayList<>();
|
|
if (!variations.contains(variation)) {
|
|
variations.add(variation);
|
|
variation.parent = this;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @return A list of all variations associated with this {@link MoveNode}
|
|
*/
|
|
public List<MoveNode> getVariations() { return variations; }
|
|
|
|
public boolean hasVariations() {
|
|
return variations != null && variations.size() > 0;
|
|
}
|
|
|
|
public MoveNode getParent() { return parent; }
|
|
|
|
public void setParent(MoveNode parent) { this.parent = parent; }
|
|
|
|
public boolean hasParent() {
|
|
return parent != null;
|
|
}
|
|
|
|
@Override
|
|
public int hashCode() {
|
|
return Objects
|
|
.hash(activeColor, capturedPiece, enPassant, fullmoveCounter, halfmoveClock, move, parent, variations);
|
|
}
|
|
|
|
@Override
|
|
public boolean equals(Object obj) {
|
|
if (this == obj) return true;
|
|
if (obj == null) return false;
|
|
if (getClass() != obj.getClass()) return false;
|
|
MoveNode other = (MoveNode) obj;
|
|
return activeColor == other.activeColor && Objects.equals(capturedPiece, other.capturedPiece)
|
|
&& Objects.equals(enPassant, other.enPassant) && fullmoveCounter == other.fullmoveCounter
|
|
&& halfmoveClock == other.halfmoveClock && Objects.equals(move, other.move)
|
|
&& Objects.equals(parent, other.parent) && Objects.equals(variations, other.variations);
|
|
}
|
|
} |