nvzqz / Sage

A cross-platform chess library for Swift
Apache License 2.0
375 stars 43 forks source link

Fixed bugs in `_redoMove()` and `_undoMove()` #15

Open Hengyu opened 7 years ago

Hengyu commented 7 years ago

En Passant move

An inconsistency may be created by performing undoMove() and redoMove() several times.

For example: Let a game starts with the following moves: e2->e4, f7->f5, e4->f5. Then, let's perform undoMove and redoMove. Then check the available moves for black pawn in g7. An inconsistency may be expected.

Reason: In private function _redoMove(), we calls _execute(uncheckedMove: promotion:) and passes the move that will be redone. But the property enPassantMove has not been updated in _redoMove. So there will be an inconsistency in the future calls performed on the game.

Solution: Updates the enPassantMove in _redoMove().

King is checked

kingIsChecked may returns the wrong value.

Reason: This is caused by copying a wrong value to _undoHistory in the function _undoMove(). Solution: Pass the correct value to _undoHistory.append(:).

Hengyu commented 7 years ago

Wondering Let's see the property

var _moveHistory: [(move: Move,  piece: Piece, capture: Piece?, enPassantTarget: Square?, kingAttackers: Bitboard, halfmoves: UInt, rights: CastlingRights)]

in file Game.swift. Does the element in _moveHistory have the following meanings?