Feature/move generation optimization #10
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Improved detection of pieces pinned to own king by sliding attackers. Previous implementation only found potentially pinned pieces (because the pieces were on the same file, rank, or diagonal as the sliding attacker). The new implementation finds all actually pinned pieces.
This speeds up resolution of pseudo-legal moves to legal moves by eliminating unnecessary calls to
Board.IsSquareAttacked(kingSquare)
inBoard.IsMoveLegal
method. Its benefit is limited though because many pseudo-legal moves never are examined for legality because a beta cutoff occurs before the move is searched.Eliminated unnecessary call to
Board.PlayNullMove
andBoard.UndoMove
inBoard.IsMoveLegal
method. Instead of actually making a null move, the code flips side-to-move, sets a few other properties of the position, callsIsSquareAttacked(kingSquare)
, then restores original property values.I experimented with eliminating the remaining call to
Board.PlayMove
andBoard.UndoMove
inBoard.IsMoveLegal
method to determine if this speeds up resolution of pseudo-legal moves to legal moves. I did this by intersecting pre-calculated moves from the destination (To) square to the opponent king. And by detecting pieces pinned to the opponent king by own sliding attackers. Moving such pieces in a direction other than the attacking ray creates a discovered check. Unfortunately, this code was slower than simply moving the piece and callingBoard.IsSquareAttacked
.Board.IsSquareAttacked
uses pre-calculated moves (magic bitboards) to determine if own king is attacked after moving the piece, constituting an illegal move. Did not include in this PR.Also, I experimented combining legality checking with playing a move in a
Board.PlayMoveIfLegal
method. Unfortunately this causes numerous complications with futility-pruned moves. MadChess never prunes moves that deliver check, moves aren't known to deliver check until they're played, so this must be detected after the fact (which complicates undoing the move and restoring board state) or detected prior to playing the move (which already has been proven slower). Ugly and prone to bugs. Did not include in this PR.Also adjusted how nodes are counted- affecting Node Per Second (NPS) metric- to a more honest measurement. A node is counted only in...
Board.PlayMove
methodBoard.PlayNullMove
method (because this changes side-to-move plus a few other properties and advances position index)2562 +/- 16 ELO at bullet chess.