Skip to content

Commit ea8c232

Browse files
committed
Fix SEE implementation
Not optimal speed-wise, but looks more correct now.
1 parent e7f6f87 commit ea8c232

File tree

1 file changed

+44
-44
lines changed

1 file changed

+44
-44
lines changed

engine/src/movelist.rs

+44-44
Original file line numberDiff line numberDiff line change
@@ -132,21 +132,25 @@ impl MoveVec {
132132
.expect("should only call see on captures");
133133
let target_square = capture.get_dest();
134134
let initial_colour = board.side_to_move();
135-
let mut blockers = board.combined() ^ BitBoard::from_square(capture.get_source());
136-
let white_pieces = board.color_combined(Color::White);
137-
let black_pieces = board.color_combined(Color::Black);
138-
139-
let mut attackers =
140-
chess::get_king_moves(target_square) & blockers & board.pieces(Piece::King)
141-
| chess::get_knight_moves(target_square) & blockers & board.pieces(Piece::Knight)
142-
| chess::get_rook_moves(target_square, blockers)
143-
& blockers
144-
& (board.pieces(Piece::Rook) | board.pieces(Piece::Queen))
145-
| chess::get_bishop_moves(target_square, blockers)
146-
& blockers
147-
& (board.pieces(Piece::Bishop) | board.pieces(Piece::Queen))
148-
| chess::get_pawn_attacks(target_square, Color::Black, *white_pieces)
149-
| chess::get_pawn_attacks(target_square, Color::White, *black_pieces);
135+
let mut occupied = board.combined() ^ BitBoard::from_square(capture.get_source());
136+
137+
let mut attackers = (chess::get_king_moves(target_square)
138+
& (occupied & board.pieces(Piece::King)))
139+
| (chess::get_knight_moves(target_square) & (occupied & board.pieces(Piece::Knight)))
140+
| (chess::get_rook_moves(target_square, occupied)
141+
& (occupied & (board.pieces(Piece::Rook) | board.pieces(Piece::Queen))))
142+
| chess::get_bishop_moves(target_square, occupied)
143+
& (occupied & (board.pieces(Piece::Bishop) | board.pieces(Piece::Queen)))
144+
| chess::get_pawn_attacks(
145+
target_square,
146+
Color::Black,
147+
board.color_combined(Color::White) & occupied,
148+
)
149+
| chess::get_pawn_attacks(
150+
target_square,
151+
Color::White,
152+
board.color_combined(Color::Black) & occupied,
153+
);
150154

151155
let mut target_piece = board.piece_on(capture.get_source()).unwrap();
152156
let mut colour = !initial_colour;
@@ -176,21 +180,19 @@ impl MoveVec {
176180
break;
177181
}
178182

179-
blockers ^= BitBoard::from_square(attacker_square);
183+
occupied ^= BitBoard::from_square(attacker_square);
180184
attackers ^= BitBoard::from_square(attacker_square);
181185

182186
target_piece = attacker_piece;
183187

184188
if matches!(attacker_piece, Piece::Rook | Piece::Queen) {
185-
attackers |= chess::get_rook_moves(target_square, blockers)
186-
& blockers
187-
& (board.pieces(Piece::Rook) | board.pieces(Piece::Queen));
189+
attackers |= chess::get_rook_moves(target_square, occupied)
190+
& (occupied & (board.pieces(Piece::Rook) | board.pieces(Piece::Queen)));
188191
}
189192

190193
if matches!(attacker_piece, Piece::Pawn | Piece::Bishop | Piece::Queen) {
191-
attackers |= chess::get_bishop_moves(target_square, blockers)
192-
& blockers
193-
& (board.pieces(Piece::Bishop) | board.pieces(Piece::Queen));
194+
attackers |= chess::get_bishop_moves(target_square, occupied)
195+
& (occupied & (board.pieces(Piece::Bishop) | board.pieces(Piece::Queen)));
194196
}
195197

196198
colour = !colour;
@@ -278,26 +280,24 @@ mod tests {
278280
0
279281
);
280282

281-
//#[test]
282-
//fn test_see_enough_attackers_but_wrong_order() {
283-
// let board: Board = "k2r2q1/2n2b2/2p5/3n4/2K1P3/3QNB2/3R4/8 w - - 0 1"
284-
// .parse()
285-
// .unwrap();
286-
// let capture: ChessMove = "e4d5".parse().unwrap();
287-
//
288-
// //assert_eq!(Value::INFINITE, MoveVec::see(&board, capture));
289-
//
290-
// for capture in MoveGen::new_legal(&board) {
291-
// if capture.captures(&board).is_none() {
292-
// continue;
293-
// }
294-
//
295-
// println!(
296-
// "{} SEE: {}",
297-
// capture,
298-
// MoveVec::see(&board, capture).as_inner()
299-
// );
300-
// }
301-
// panic!();
302-
//}
283+
test_see!(
284+
test_see_pawn_takes_overprotected_knight,
285+
"k2r2q1/2n2b2/2p5/3n4/2K1P3/3QNB2/3R4/8 w - - 0 1",
286+
"e4d5",
287+
piece_value(Piece::Knight) - piece_value(Piece::Pawn)
288+
);
289+
290+
test_see!(
291+
test_see_knight_takes_protected_knight,
292+
"k2r2q1/2n2b2/2p5/3n4/2K1P3/3QNB2/3R4/8 w - - 0 1",
293+
"e3d5",
294+
0
295+
);
296+
297+
test_see!(
298+
test_see_queeen_takes_underprotected_knight,
299+
"k2r2q1/2n2b2/2p5/3n4/2K1P3/3QNB2/3R4/8 w - - 0 1",
300+
"d3d5",
301+
piece_value(Piece::Knight) + piece_value(Piece::Pawn) - piece_value(Piece::Queen)
302+
);
303303
}

0 commit comments

Comments
 (0)