From 47a120e6515c0f2c9f73e710d6897c4d93dae06b Mon Sep 17 00:00:00 2001 From: CyB3RC0nN0R Date: Wed, 18 Sep 2019 15:00:31 +0200 Subject: [PATCH] Fixed Log with respect to variations --- .classpath | 6 +- src/dev/kske/chess/board/Board.java | 2 +- src/dev/kske/chess/board/Log.java | 92 ++++++++++++++++---------- src/dev/kske/chess/ui/LogPanel.java | 1 + test/dev/kske/chess/board/LogTest.java | 2 +- 5 files changed, 61 insertions(+), 42 deletions(-) diff --git a/.classpath b/.classpath index 83f2c4a..e4beaea 100644 --- a/.classpath +++ b/.classpath @@ -7,11 +7,7 @@ - - - - - + diff --git a/src/dev/kske/chess/board/Board.java b/src/dev/kske/chess/board/Board.java index 4ae9622..1a21ff4 100644 --- a/src/dev/kske/chess/board/Board.java +++ b/src/dev/kske/chess/board/Board.java @@ -545,7 +545,7 @@ public class Board implements Cloneable { board.kingPos = new HashMap<>(); board.kingPos.putAll(kingPos); - board.log = (Log) log.clone(); + board.log = new Log(log); return board; } diff --git a/src/dev/kske/chess/board/Log.java b/src/dev/kske/chess/board/Log.java index 8bdb5e1..c05ae3f 100644 --- a/src/dev/kske/chess/board/Log.java +++ b/src/dev/kske/chess/board/Log.java @@ -1,7 +1,7 @@ package dev.kske.chess.board; -import java.util.HashSet; -import java.util.Set; +import java.util.ArrayList; +import java.util.List; import dev.kske.chess.board.Piece.Color; @@ -11,7 +11,7 @@ import dev.kske.chess.board.Piece.Color; * Created: 09.07.2019
* Author: Kai S. K. Engelbart */ -public class Log implements Cloneable { +public class Log { private MoveNode root, current; @@ -23,21 +23,24 @@ public class Log implements Cloneable { reset(); } - // TODO: Adjust to variations - @Override - public Object clone() { - Log log = null; - try { - log = (Log) super.clone(); - if (!isEmpty()) { - log.current = (MoveNode) current.clone(); - if (current == root) log.root = log.current; - else log.root = (MoveNode) root.clone(); - } - } catch (CloneNotSupportedException e) { - e.printStackTrace(); + /** + * Creates a partial deep copy of another {@link Log} instance which begins with + * the current node. + * + * @param other The {@link Log} instance to copy + */ + public Log(Log other) { + enPassant = other.enPassant; + activeColor = other.activeColor; + fullmoveCounter = other.fullmoveCounter; + halfmoveClock = other.halfmoveClock; + + // The new root is the current node of the copied instance + if (!other.isEmpty()) { + root = new MoveNode(other.current); + root.parent = null; + current = root; } - return log; } /** @@ -69,19 +72,12 @@ public class Log implements Cloneable { * move. */ public void removeLast() { - if (!isEmpty()) { - if (current.parent == null) { - current = null; - root = null; - } else { - current = current.parent; - if (!isEmpty()) { - activeColor = current.activeColor; - enPassant = current.enPassant; - fullmoveCounter = current.fullmoveCounter; - halfmoveClock = current.halfmoveClock; - } else reset(); - } + if (!isEmpty() && current.parent != null) { + current = current.parent; + activeColor = current.activeColor; + enPassant = current.enPassant; + fullmoveCounter = current.fullmoveCounter; + halfmoveClock = current.halfmoveClock; } else reset(); } @@ -135,8 +131,19 @@ public class Log implements Cloneable { public final int fullmoveCounter, halfmoveClock; private MoveNode parent; - private Set variations = new HashSet<>(); + private List variations = new ArrayList<>(); + /** + * 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; @@ -147,11 +154,26 @@ public class Log implements Cloneable { this.halfmoveClock = halfmoveClock; } - @Override - protected Object clone() throws CloneNotSupportedException { - return super.clone(); + /** + * Creates a deep copy of another {@link MoveNode}. + * + * @param other The {@link MoveNode} to copy + */ + public MoveNode(MoveNode other) { + this(other.move, other.capturedPiece, other.enPassant, other.activeColor, other.fullmoveCounter, + other.halfmoveClock); + other.variations.forEach(variation -> { + MoveNode copy = new MoveNode(variation); + 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.contains(variation)) { variations.add(variation); @@ -159,4 +181,4 @@ public class Log implements Cloneable { } } } -} +} \ 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 21488e2..7343fb2 100644 --- a/src/dev/kske/chess/ui/LogPanel.java +++ b/src/dev/kske/chess/ui/LogPanel.java @@ -57,6 +57,7 @@ public class LogPanel extends JPanel implements Subscribable { public void handle(Event event) { if (log == null) return; + // TODO: Display log with variations final List moves = /* log.getLoggedMoves() */ new ArrayList<>(); String[][] data = new String[moves.size() / 2 + moves.size() % 2][2]; for (int i = 0; i < data.length; i++) { diff --git a/test/dev/kske/chess/board/LogTest.java b/test/dev/kske/chess/board/LogTest.java index 2c19a1c..2185ec8 100644 --- a/test/dev/kske/chess/board/LogTest.java +++ b/test/dev/kske/chess/board/LogTest.java @@ -39,7 +39,7 @@ class LogTest { */ @Test void testClone() { - Log other = (Log) log.clone(); + Log other = new Log(log); log.setActiveColor(Color.WHITE); other.setActiveColor(Color.BLACK); assertNotEquals(other.getActiveColor(), log.getActiveColor());