From 4bcb0a032dc6d75e0c63226599af7d953437c3e5 Mon Sep 17 00:00:00 2001 From: CyB3RC0nN0R Date: Fri, 4 Oct 2019 10:25:03 +0200 Subject: [PATCH] Moved MoveNode into a separate file --- src/dev/kske/chess/board/Board.java | 1 - src/dev/kske/chess/board/Log.java | 95 +++---------------------- src/dev/kske/chess/board/MoveNode.java | 96 ++++++++++++++++++++++++++ src/dev/kske/chess/ui/LogPanel.java | 2 +- 4 files changed, 105 insertions(+), 89 deletions(-) create mode 100644 src/dev/kske/chess/board/MoveNode.java diff --git a/src/dev/kske/chess/board/Board.java b/src/dev/kske/chess/board/Board.java index a0b86ee..fed1bea 100644 --- a/src/dev/kske/chess/board/Board.java +++ b/src/dev/kske/chess/board/Board.java @@ -5,7 +5,6 @@ import java.util.HashMap; import java.util.List; import java.util.Map; -import dev.kske.chess.board.Log.MoveNode; import dev.kske.chess.board.Piece.Color; import dev.kske.chess.board.Piece.Type; diff --git a/src/dev/kske/chess/board/Log.java b/src/dev/kske/chess/board/Log.java index f6352f5..afa5f00 100644 --- a/src/dev/kske/chess/board/Log.java +++ b/src/dev/kske/chess/board/Log.java @@ -1,10 +1,7 @@ package dev.kske.chess.board; -import java.util.ArrayList; import java.util.Iterator; -import java.util.List; -import dev.kske.chess.board.Log.MoveNode; import dev.kske.chess.board.Piece.Color; /** @@ -43,7 +40,7 @@ public class Log implements Iterable { // The new root is the current node of the copied instance if (!other.isEmpty()) { root = new MoveNode(other.current, copyVariations); - root.parent = null; + root.setParent(null); current = root; } } @@ -63,7 +60,7 @@ public class Log implements Iterable { @Override public MoveNode next() { MoveNode result = current; - if (current.hasVariations()) current = current.variations.get(0); + if (current.hasVariations()) current = current.getVariations().get(0); else hasNext = false; return result; } @@ -100,8 +97,8 @@ public class Log implements Iterable { */ public void removeLast() { if (hasParent()) { - current.parent.variations.remove(current); - current = current.parent; + current.getParent().getVariations().remove(current); + current = current.getParent(); update(); } else reset(); } @@ -109,7 +106,7 @@ public class Log implements Iterable { public boolean isEmpty() { return root == null; } public boolean hasParent() { - return !isEmpty() && current.parent != null; + return !isEmpty() && current.hasParent(); } /** @@ -130,8 +127,8 @@ public class Log implements Iterable { * @param index */ public void selectNextNode(int index) { - if (!isEmpty() && current.variations != null && index < current.variations.size()) { - current = current.variations.get(index); + if (!isEmpty() && current.hasVariations() && index < current.getVariations().size()) { + current = current.getVariations().get(index); update(); } } @@ -141,7 +138,7 @@ public class Log implements Iterable { */ public void selectPreviousNode() { if (hasParent()) { - current = current.parent; + current = current.getParent(); update(); } } @@ -188,80 +185,4 @@ public class Log implements Iterable { public int getHalfmoveClock() { return 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 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 getVariations() { return variations; } - - public boolean hasVariations() { - return variations != null && variations.size() > 0; - } - } } \ No newline at end of file diff --git a/src/dev/kske/chess/board/MoveNode.java b/src/dev/kske/chess/board/MoveNode.java new file mode 100644 index 0000000..6d4afad --- /dev/null +++ b/src/dev/kske/chess/board/MoveNode.java @@ -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: Chess
+ * File: MoveNode.java
+ * Created: 02.10.2019
+ * Author: Kai S. K. Engelbart + */ +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 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 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; + } +} \ No newline at end of file diff --git a/src/dev/kske/chess/ui/LogPanel.java b/src/dev/kske/chess/ui/LogPanel.java index 25c4eb7..a4b11ec 100644 --- a/src/dev/kske/chess/ui/LogPanel.java +++ b/src/dev/kske/chess/ui/LogPanel.java @@ -13,7 +13,7 @@ import javax.swing.border.EmptyBorder; import javax.swing.table.DefaultTableModel; 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.EventBus; import dev.kske.chess.event.MoveEvent;