Skip to content

Commit 46acd25

Browse files
committed
fix castling san
1 parent a3f0aca commit 46acd25

1 file changed

Lines changed: 39 additions & 5 deletions

File tree

src/game.rs

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use crate::color::Color;
44
use crate::error::Error;
55
use crate::movegen::MoveGen;
66
use crate::piece::Piece;
7+
use std::borrow::Borrow;
78
use std::str::FromStr;
89
#[cfg(any(feature = "instrument_game", feature = "instrument_all"))]
910
use tracing::instrument;
@@ -389,6 +390,18 @@ impl Game {
389390
let piece = initial_board
390391
.piece_on(chess_move.get_source())
391392
.expect("the move is valid");
393+
394+
// if the move is a castle, return the appropriate string
395+
if piece == Piece::King
396+
&& chess_move.get_source().get_file() == crate::file::File::E
397+
&& chess_move.get_dest().get_rank() == chess_move.get_source().get_rank()
398+
{
399+
if chess_move.get_dest().get_file() == crate::file::File::G {
400+
return "O-O".to_string();
401+
} else if chess_move.get_dest().get_file() == crate::file::File::C {
402+
return "O-O-O".to_string();
403+
}
404+
}
392405
if piece != Piece::Pawn {
393406
san.push(
394407
// white is uppercase??
@@ -645,18 +658,39 @@ pub fn test_make_move() {
645658
(ChessMove::new(Square::D5, Square::E6, None), "dxe6"), // en passant
646659
(ChessMove::new(Square::D8, Square::H4, None), "Qh4"),
647660
(ChessMove::new(Square::C4, Square::D5, None), "Qd5"),
648-
(ChessMove::new(Square::H4, Square::D4, None), "Qxd4"),
649-
(ChessMove::new(Square::E6, Square::D7, None), "exd7+"),
650-
(ChessMove::new(Square::E8, Square::E7, None), "Ke7"),
651-
(ChessMove::new(Square::D7, Square::D8, Some(Piece::Queen)), "d8=Q#"),
661+
(ChessMove::new(Square::F8, Square::B4, None), "Bb4+"),
662+
(ChessMove::new(Square::C1, Square::D2, None), "Bd2"),
663+
(ChessMove::new(Square::E8, Square::G8, None), "O-O"),
664+
(ChessMove::new(Square::B1, Square::C3, None), "Nc3"),
665+
(ChessMove::new(Square::H4, Square::H6, None), "Qh6"),
666+
(ChessMove::new(Square::E1, Square::C1, None), "O-O-O"),
667+
(ChessMove::new(Square::H6, Square::H4, None), "Qh4"),
668+
(ChessMove::new(Square::E6, Square::D7, None), "exd7"),
669+
(ChessMove::new(Square::H4, Square::H3, None), "Qh3"),
670+
(ChessMove::new(Square::D7, Square::D8, Some(Piece::Queen)), "d8=Q"),
671+
(ChessMove::new(Square::F8, Square::D8, None), "Rxd8"),
672+
(ChessMove::new(Square::C3, Square::B1, None), "Nb1"),
673+
(ChessMove::new(Square::B4, Square::D2, None), "Bxd2+"),
674+
(ChessMove::new(Square::B1, Square::D2, None), "Nxd2"),
675+
(ChessMove::new(Square::H3, Square::D3, None), "Qd3"),
676+
(ChessMove::new(Square::D5, Square::D8, None), "Qxd8#"),
652677
];
653678

654679
for (mv, expected_san) in move_list.iter() {
655680
let san = game.make_move(*mv);
656681
if (*expected_san).len() == 0 {
657682
assert_eq!(san, None);
658683
} else {
659-
assert_eq!(san.unwrap(), *expected_san);
684+
assert_eq!(
685+
san,
686+
Some(expected_san.to_string()),
687+
"\n'{}' for move: '{}' didn't match the expected san ({}). position: \n{}",
688+
san.clone().unwrap_or_default(),
689+
mv,
690+
expected_san,
691+
game.current_position().color_combined(Color::White)
692+
| game.current_position().color_combined(Color::Black)
693+
);
660694
}
661695
}
662696
}

0 commit comments

Comments
 (0)