bhlangonijr / chesslib

chess library for legal move generation, FEN/PGN parsing and more
Apache License 2.0
229 stars 80 forks source link

Include board history when comparing two boards #44

Closed dlbbld closed 3 years ago

dlbbld commented 3 years ago

I suggest to include the board history, that is the initial position and move history in the board comparison. The reason for is that two equal boards (by the equals method) should behave the same for all the same future moves. E.g. the fifty-move rule and the threefold repetition rule look at the board history. E.g. now for two equal boards one can later flag a position as threefold repetition, the other not. Please check the example below.

final Board board1 = new Board();
board1.doMove(new Move("b1c3", Side.WHITE));
board1.doMove(new Move("b8c6", Side.BLACK));
board1.doMove(new Move("c3b1", Side.WHITE));
board1.doMove(new Move("c6b8", Side.BLACK));
board1.doMove(new Move("b1c3", Side.WHITE));
board1.doMove(new Move("b8c6", Side.BLACK));
board1.doMove(new Move("c3b1", Side.WHITE));

final Board board2 = new Board();
board2.loadFromFen(board1.getFen());

System.out.println(board1.equals(board2)); // true - because history is not compared

board2.doMove(new Move("c6b8", Side.BLACK));

System.out.println(board1.isRepetition()); // true - board1 sees the full history
System.out.println(board2.isRepetition()); // false - board2 hasn't got the full history
bhlangonijr commented 3 years ago

Added Board#strictEquals() method in version 1.2.4 which will also compare board history. equals remains the same, ignoring the history - there are use cases where we want to ignore the history when comparing.