Fixed Log with respect to variations

This commit is contained in:
Kai S. K. Engelbart 2019-09-18 15:00:31 +02:00
parent ea8d754a72
commit 47a120e651
5 changed files with 61 additions and 42 deletions

View File

@ -7,11 +7,7 @@
<attribute name="test" value="true"/> <attribute name="test" value="true"/>
</attributes> </attributes>
</classpathentry> </classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER">
<attributes>
<attribute name="module" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/5"/> <classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/5"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
<classpathentry kind="output" path="bin"/> <classpathentry kind="output" path="bin"/>
</classpath> </classpath>

View File

@ -545,7 +545,7 @@ public class Board implements Cloneable {
board.kingPos = new HashMap<>(); board.kingPos = new HashMap<>();
board.kingPos.putAll(kingPos); board.kingPos.putAll(kingPos);
board.log = (Log) log.clone(); board.log = new Log(log);
return board; return board;
} }

View File

@ -1,7 +1,7 @@
package dev.kske.chess.board; package dev.kske.chess.board;
import java.util.HashSet; import java.util.ArrayList;
import java.util.Set; import java.util.List;
import dev.kske.chess.board.Piece.Color; import dev.kske.chess.board.Piece.Color;
@ -11,7 +11,7 @@ import dev.kske.chess.board.Piece.Color;
* Created: <strong>09.07.2019</strong><br> * Created: <strong>09.07.2019</strong><br>
* Author: <strong>Kai S. K. Engelbart</strong> * Author: <strong>Kai S. K. Engelbart</strong>
*/ */
public class Log implements Cloneable { public class Log {
private MoveNode root, current; private MoveNode root, current;
@ -23,21 +23,24 @@ public class Log implements Cloneable {
reset(); reset();
} }
// TODO: Adjust to variations /**
@Override * Creates a partial deep copy of another {@link Log} instance which begins with
public Object clone() { * the current node.
Log log = null; *
try { * @param other The {@link Log} instance to copy
log = (Log) super.clone(); */
if (!isEmpty()) { public Log(Log other) {
log.current = (MoveNode) current.clone(); enPassant = other.enPassant;
if (current == root) log.root = log.current; activeColor = other.activeColor;
else log.root = (MoveNode) root.clone(); 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;
} }
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
return log;
} }
/** /**
@ -69,21 +72,14 @@ public class Log implements Cloneable {
* move. * move.
*/ */
public void removeLast() { public void removeLast() {
if (!isEmpty()) { if (!isEmpty() && current.parent != null) {
if (current.parent == null) {
current = null;
root = null;
} else {
current = current.parent; current = current.parent;
if (!isEmpty()) {
activeColor = current.activeColor; activeColor = current.activeColor;
enPassant = current.enPassant; enPassant = current.enPassant;
fullmoveCounter = current.fullmoveCounter; fullmoveCounter = current.fullmoveCounter;
halfmoveClock = current.halfmoveClock; halfmoveClock = current.halfmoveClock;
} else reset(); } else reset();
} }
} else reset();
}
public boolean isEmpty() { return root == null; } public boolean isEmpty() { return root == null; }
@ -135,8 +131,19 @@ public class Log implements Cloneable {
public final int fullmoveCounter, halfmoveClock; public final int fullmoveCounter, halfmoveClock;
private MoveNode parent; private MoveNode parent;
private Set<MoveNode> variations = new HashSet<>(); private List<MoveNode> 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, public MoveNode(Move move, Piece capturedPiece, Position enPassant, Color activeColor, int fullmoveCounter,
int halfmoveClock) { int halfmoveClock) {
this.move = move; this.move = move;
@ -147,11 +154,26 @@ public class Log implements Cloneable {
this.halfmoveClock = halfmoveClock; this.halfmoveClock = halfmoveClock;
} }
@Override /**
protected Object clone() throws CloneNotSupportedException { * Creates a deep copy of another {@link MoveNode}.
return super.clone(); *
* @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) { public void addVariation(MoveNode variation) {
if (!variations.contains(variation)) { if (!variations.contains(variation)) {
variations.add(variation); variations.add(variation);

View File

@ -57,6 +57,7 @@ public class LogPanel extends JPanel implements Subscribable {
public void handle(Event<?> event) { public void handle(Event<?> event) {
if (log == null) return; if (log == null) return;
// TODO: Display log with variations
final List<MoveNode> moves = /* log.getLoggedMoves() */ new ArrayList<>(); final List<MoveNode> moves = /* log.getLoggedMoves() */ new ArrayList<>();
String[][] data = new String[moves.size() / 2 + moves.size() % 2][2]; String[][] data = new String[moves.size() / 2 + moves.size() % 2][2];
for (int i = 0; i < data.length; i++) { for (int i = 0; i < data.length; i++) {

View File

@ -39,7 +39,7 @@ class LogTest {
*/ */
@Test @Test
void testClone() { void testClone() {
Log other = (Log) log.clone(); Log other = new Log(log);
log.setActiveColor(Color.WHITE); log.setActiveColor(Color.WHITE);
other.setActiveColor(Color.BLACK); other.setActiveColor(Color.BLACK);
assertNotEquals(other.getActiveColor(), log.getActiveColor()); assertNotEquals(other.getActiveColor(), log.getActiveColor());