Moved MoveNode into a separate file
This commit is contained in:
parent
b969ff9e2b
commit
4bcb0a032d
@ -5,7 +5,6 @@ import java.util.HashMap;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import dev.kske.chess.board.Log.MoveNode;
|
|
||||||
import dev.kske.chess.board.Piece.Color;
|
import dev.kske.chess.board.Piece.Color;
|
||||||
import dev.kske.chess.board.Piece.Type;
|
import dev.kske.chess.board.Piece.Type;
|
||||||
|
|
||||||
|
@ -1,10 +1,7 @@
|
|||||||
package dev.kske.chess.board;
|
package dev.kske.chess.board;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import dev.kske.chess.board.Log.MoveNode;
|
|
||||||
import dev.kske.chess.board.Piece.Color;
|
import dev.kske.chess.board.Piece.Color;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -43,7 +40,7 @@ public class Log implements Iterable<MoveNode> {
|
|||||||
// The new root is the current node of the copied instance
|
// The new root is the current node of the copied instance
|
||||||
if (!other.isEmpty()) {
|
if (!other.isEmpty()) {
|
||||||
root = new MoveNode(other.current, copyVariations);
|
root = new MoveNode(other.current, copyVariations);
|
||||||
root.parent = null;
|
root.setParent(null);
|
||||||
current = root;
|
current = root;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -63,7 +60,7 @@ public class Log implements Iterable<MoveNode> {
|
|||||||
@Override
|
@Override
|
||||||
public MoveNode next() {
|
public MoveNode next() {
|
||||||
MoveNode result = current;
|
MoveNode result = current;
|
||||||
if (current.hasVariations()) current = current.variations.get(0);
|
if (current.hasVariations()) current = current.getVariations().get(0);
|
||||||
else hasNext = false;
|
else hasNext = false;
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
@ -100,8 +97,8 @@ public class Log implements Iterable<MoveNode> {
|
|||||||
*/
|
*/
|
||||||
public void removeLast() {
|
public void removeLast() {
|
||||||
if (hasParent()) {
|
if (hasParent()) {
|
||||||
current.parent.variations.remove(current);
|
current.getParent().getVariations().remove(current);
|
||||||
current = current.parent;
|
current = current.getParent();
|
||||||
update();
|
update();
|
||||||
} else reset();
|
} else reset();
|
||||||
}
|
}
|
||||||
@ -109,7 +106,7 @@ public class Log implements Iterable<MoveNode> {
|
|||||||
public boolean isEmpty() { return root == null; }
|
public boolean isEmpty() { return root == null; }
|
||||||
|
|
||||||
public boolean hasParent() {
|
public boolean hasParent() {
|
||||||
return !isEmpty() && current.parent != null;
|
return !isEmpty() && current.hasParent();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -130,8 +127,8 @@ public class Log implements Iterable<MoveNode> {
|
|||||||
* @param index
|
* @param index
|
||||||
*/
|
*/
|
||||||
public void selectNextNode(int index) {
|
public void selectNextNode(int index) {
|
||||||
if (!isEmpty() && current.variations != null && index < current.variations.size()) {
|
if (!isEmpty() && current.hasVariations() && index < current.getVariations().size()) {
|
||||||
current = current.variations.get(index);
|
current = current.getVariations().get(index);
|
||||||
update();
|
update();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -141,7 +138,7 @@ public class Log implements Iterable<MoveNode> {
|
|||||||
*/
|
*/
|
||||||
public void selectPreviousNode() {
|
public void selectPreviousNode() {
|
||||||
if (hasParent()) {
|
if (hasParent()) {
|
||||||
current = current.parent;
|
current = current.getParent();
|
||||||
update();
|
update();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -188,80 +185,4 @@ public class Log implements Iterable<MoveNode> {
|
|||||||
public int getHalfmoveClock() { return halfmoveClock; }
|
public int getHalfmoveClock() { return halfmoveClock; }
|
||||||
|
|
||||||
public void setHalfmoveClock(int halfmoveClock) { this.halfmoveClock = halfmoveClock; }
|
public void setHalfmoveClock(int halfmoveClock) { this.halfmoveClock = halfmoveClock; }
|
||||||
|
|
||||||
public static 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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
96
src/dev/kske/chess/board/MoveNode.java
Normal file
96
src/dev/kske/chess/board/MoveNode.java
Normal file
@ -0,0 +1,96 @@
|
|||||||
|
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>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;
|
||||||
|
}
|
||||||
|
}
|
@ -13,7 +13,7 @@ import javax.swing.border.EmptyBorder;
|
|||||||
import javax.swing.table.DefaultTableModel;
|
import javax.swing.table.DefaultTableModel;
|
||||||
|
|
||||||
import dev.kske.chess.board.Log;
|
import dev.kske.chess.board.Log;
|
||||||
import dev.kske.chess.board.Log.MoveNode;
|
import dev.kske.chess.board.MoveNode;
|
||||||
import dev.kske.chess.event.Event;
|
import dev.kske.chess.event.Event;
|
||||||
import dev.kske.chess.event.EventBus;
|
import dev.kske.chess.event.EventBus;
|
||||||
import dev.kske.chess.event.MoveEvent;
|
import dev.kske.chess.event.MoveEvent;
|
||||||
|
Reference in New Issue
Block a user