Fixed Log with respect to variations

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

View File

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

View File

@ -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;
}

View File

@ -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: <strong>09.07.2019</strong><br>
* Author: <strong>Kai S. K. Engelbart</strong>
*/
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<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,
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 {
}
}
}
}
}

View File

@ -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<MoveNode> moves = /* log.getLoggedMoves() */ new ArrayList<>();
String[][] data = new String[moves.size() / 2 + moves.size() % 2][2];
for (int i = 0; i < data.length; i++) {

View File

@ -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());