Skip to content

Commit 07e3c77

Browse files
committed
Fixed a bug that missed having a legal move for White in a rare case.
The WhiteCanMove() function returned false (i.e. saying that White did not have any legal moves) in the rare case where the only legal move was an en passant capture toward White's left. The problem was testing (board[x] == BP_MASK) instead of (board[x] & BP_MASK). The piece bit mask is only part of the data inside Chenard's representation of a square on the board. I discovered this while generating unit test data for my new C# chess engine "Gearbox". Ironically, Gearbox got it right and Chenard got it wrong. It has been a long time since I found a bug in Chenard!
1 parent 4994c8c commit 07e3c77

File tree

1 file changed

+5
-9
lines changed

1 file changed

+5
-9
lines changed

src/canmove.cpp

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,7 @@ bool ChessBoard::WP_CanMove ( int ofs, int ybase )
158158
CHECK_WHITE_MOVE ( move, unmove );
159159
}
160160
else if ( prev_move.dest == ofs + EAST &&
161-
((prev_move.source & BOARD_OFFSET_MASK) ==
162-
ofs + OFFSET(1,2)) &&
161+
((prev_move.source & BOARD_OFFSET_MASK) == ofs + OFFSET(1,2)) &&
163162
(board [prev_move.dest] & BP_MASK) )
164163
{
165164
move.dest = SPECIAL_MOVE_EP_EAST;
@@ -171,9 +170,8 @@ bool ChessBoard::WP_CanMove ( int ofs, int ybase )
171170
CHECK_WHITE_MOVE ( move, unmove );
172171
}
173172
else if ( prev_move.dest == ofs + WEST &&
174-
((prev_move.source & BOARD_OFFSET_MASK) ==
175-
ofs + OFFSET(-1,2)) &&
176-
(board [prev_move.dest] == BP_MASK) )
173+
((prev_move.source & BOARD_OFFSET_MASK) == ofs + OFFSET(-1,2)) &&
174+
(board [prev_move.dest] & BP_MASK) )
177175
{
178176
move.dest = SPECIAL_MOVE_EP_WEST;
179177
CHECK_WHITE_MOVE ( move, unmove );
@@ -640,8 +638,7 @@ bool ChessBoard::BP_CanMove ( int ofs, int ybase )
640638
CHECK_BLACK_MOVE ( move, unmove );
641639
}
642640
else if ( prev_move.dest == ofs + EAST &&
643-
((prev_move.source & BOARD_OFFSET_MASK) ==
644-
ofs + OFFSET(1,-2)) &&
641+
((prev_move.source & BOARD_OFFSET_MASK) == ofs + OFFSET(1,-2)) &&
645642
(board [prev_move.dest] & WP_MASK) )
646643
{
647644
move.dest = SPECIAL_MOVE_EP_EAST;
@@ -653,8 +650,7 @@ bool ChessBoard::BP_CanMove ( int ofs, int ybase )
653650
CHECK_BLACK_MOVE ( move, unmove );
654651
}
655652
else if ( prev_move.dest == ofs + WEST &&
656-
((prev_move.source & BOARD_OFFSET_MASK) ==
657-
ofs + OFFSET(-1,-2)) &&
653+
((prev_move.source & BOARD_OFFSET_MASK) == ofs + OFFSET(-1,-2)) &&
658654
(board [prev_move.dest] & WP_MASK) )
659655
{
660656
move.dest = SPECIAL_MOVE_EP_WEST;

0 commit comments

Comments
 (0)