This repository has been archived on 2021-02-18. You can view files and clone it, but cannot push or open issues or pull requests.
chess/src/dev/kske/chess/board/Log.java

76 lines
2.0 KiB
Java
Raw Normal View History

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>Log.java</strong><br>
* Created: <strong>09.07.2019</strong><br>
* Author: <strong>Kai S. K. Engelbart</strong>
*/
public class Log implements Cloneable {
private List<LoggedMove> moves;
private Color activeColor;
public Log() {
moves = new ArrayList<>();
activeColor = Color.WHITE;
}
public void add(Move move, Piece capturedPiece, boolean pawnMove) {
int fullmoveCounter, halfmoveClock;
if (moves.isEmpty()) {
fullmoveCounter = 1;
halfmoveClock = 0;
} else {
fullmoveCounter = getLast().fullmoveCounter;
if (activeColor == Color.BLACK) ++fullmoveCounter;
halfmoveClock = capturedPiece != null || pawnMove ? 0 : getLast().halfmoveClock + 1;
}
activeColor = activeColor.opposite();
moves.add(new LoggedMove(move, capturedPiece, fullmoveCounter, halfmoveClock));
}
public LoggedMove getLast() { return moves.isEmpty() ? null : moves.get(moves.size() - 1); }
public void removeLast() {
if (!moves.isEmpty()) {
activeColor = activeColor.opposite();
moves.remove(moves.size() - 1);
}
}
@Override
public Object clone() {
Log log = null;
try {
log = (Log) super.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
log.moves = new ArrayList<>();
log.moves.addAll(this.moves);
return log;
}
public Color getActiveColor() { return activeColor; }
public static class LoggedMove {
public final Move move;
public final Piece capturedPiece;
public final int fullmoveCounter, halfmoveClock;
public LoggedMove(Move move, Piece capturedPiece, int fullmoveCounter, int halfmoveClock) {
this.move = move;
this.capturedPiece = capturedPiece;
this.fullmoveCounter = fullmoveCounter;
this.halfmoveClock = halfmoveClock;
}
}
}