@@ -15,6 +15,7 @@ const RANKS: [char; 8] = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'];
1515const FILES : [ char ; 8 ] = [ '1' , '2' , '3' , '4' , '5' , '6' , '7' , '8' ] ;
1616
1717// Lookup tables for converting bit index (0-63) to board coordinates
18+ #[ rustfmt:: skip]
1819const SQUARE_TO_RANK : [ usize ; 64 ] = [
1920 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ,
2021 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 ,
@@ -26,6 +27,7 @@ const SQUARE_TO_RANK: [usize; 64] = [
2627 7 , 7 , 7 , 7 , 7 , 7 , 7 , 7 ,
2728] ;
2829
30+ #[ rustfmt:: skip]
2931const SQUARE_TO_FILE : [ usize ; 64 ] = [
3032 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 ,
3133 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 ,
@@ -862,17 +864,18 @@ impl BoardMap {
862864 let from_bit = 1u64 << ( from[ 0 ] * 8 + from[ 1 ] ) ;
863865 let to_bit = 1u64 << ( to[ 0 ] * 8 + to[ 1 ] ) ;
864866
867+ let piece = self . get_piece ( from) ;
868+ let piece_color = piece. get_color ( ) ;
869+ let is_white = piece_color == PieceColor :: White ;
870+ let target_piece = self . get_piece ( to) ;
871+
865872 if en_passant {
866- let shift = if self . get_piece ( from) . get_color ( ) == PieceColor :: Black {
867- 1
868- } else {
869- -1
870- } ;
873+ let shift = if is_white { -1 } else { 1 } ;
871874 let to_step = [ ( to[ 0 ] as isize - shift) as usize , to[ 1 ] ] ;
872875 let ep_bit = 1u64 << ( to_step[ 0 ] * 8 + to_step[ 1 ] ) ;
873876
874877 // remove captured pawn from occupancy
875- if self . get_piece ( from ) . get_color ( ) == PieceColor :: White {
878+ if is_white {
876879 self . black_occupancy ^= ep_bit;
877880 } else {
878881 self . white_occupancy ^= ep_bit;
@@ -882,11 +885,10 @@ impl BoardMap {
882885
883886 // track castling rights
884887 // mark kings and rooks as moved
885- let piece = self . get_piece ( from) ;
886888 if let Some ( piece_type) = piece. get_type ( ) {
887889 match piece_type {
888890 PieceType :: King => {
889- if piece . get_color ( ) == PieceColor :: White {
891+ if is_white {
890892 self . white_king_moved = true ;
891893 self . white_king_pos = to;
892894 } else {
@@ -896,7 +898,7 @@ impl BoardMap {
896898 }
897899 PieceType :: Rook => {
898900 // check if rook move
899- if piece . get_color ( ) == PieceColor :: White {
901+ if is_white {
900902 if from == [ 7 , 0 ] {
901903 self . white_queenside_rook_moved = true ;
902904 } else if from == [ 7 , 7 ] {
@@ -913,7 +915,6 @@ impl BoardMap {
913915 }
914916
915917 // invalidate castling if a rook is captured on its starting square
916- let target_piece = self . get_piece ( to) ;
917918 if target_piece. is_piece ( ) {
918919 if let Some ( PieceType :: Rook ) = target_piece. get_type ( ) {
919920 match to {
@@ -935,11 +936,11 @@ impl BoardMap {
935936 let rook_to = [ from[ 0 ] , 5 ] ;
936937 let rook_from_bit = 1u64 << ( rook_from[ 0 ] * 8 + rook_from[ 1 ] ) ;
937938 let rook_to_bit = 1u64 << ( rook_to[ 0 ] * 8 + rook_to[ 1 ] ) ;
938- if piece . get_color ( ) == PieceColor :: White {
939- self . white_occupancy ^= rook_from_bit;
939+ if is_white {
940+ self . white_occupancy ^= rook_from_bit | rook_to_bit ;
940941 self . white_occupancy |= rook_to_bit;
941942 } else {
942- self . black_occupancy ^= rook_from_bit;
943+ self . black_occupancy ^= rook_from_bit | rook_to_bit ;
943944 self . black_occupancy |= rook_to_bit;
944945 }
945946 self . set_piece ( rook_to, self . get_piece ( rook_from) . 0 ) ;
@@ -950,42 +951,38 @@ impl BoardMap {
950951 let rook_to = [ from[ 0 ] , 3 ] ;
951952 let rook_from_bit = 1u64 << ( rook_from[ 0 ] * 8 + rook_from[ 1 ] ) ;
952953 let rook_to_bit = 1u64 << ( rook_to[ 0 ] * 8 + rook_to[ 1 ] ) ;
953- if piece . get_color ( ) == PieceColor :: White {
954- self . white_occupancy ^= rook_from_bit;
954+ if is_white {
955+ self . white_occupancy ^= rook_from_bit | rook_to_bit ;
955956 self . white_occupancy |= rook_to_bit;
956957 } else {
957- self . black_occupancy ^= rook_from_bit;
958+ self . black_occupancy ^= rook_from_bit | rook_to_bit ;
958959 self . black_occupancy |= rook_to_bit;
959960 }
960961 self . set_piece ( rook_to, self . get_piece ( rook_from) . 0 ) ;
961962 self . set_piece ( rook_from, 0 ) ;
962963 }
963964 }
964965
965- let target_piece = self . get_piece ( to) ;
966- if piece. get_color ( ) == PieceColor :: White {
967- self . white_occupancy ^= from_bit;
966+ if is_white {
967+ self . white_occupancy ^= from_bit | to_bit;
968968 self . white_occupancy |= to_bit;
969969
970970 if target_piece. is_piece ( ) {
971971 self . black_occupancy ^= to_bit;
972972 }
973973 } else {
974- self . black_occupancy ^= from_bit;
974+ self . black_occupancy ^= from_bit | to_bit ;
975975 self . black_occupancy |= to_bit;
976976 if target_piece. is_piece ( ) {
977977 self . white_occupancy ^= to_bit;
978978 }
979979 }
980980
981981 if promotion {
982- let color = match self . get_piece ( from) . get_color ( ) {
983- PieceColor :: Black => BLACK ,
984- PieceColor :: White => WHITE ,
985- } ;
982+ let color = if is_white { WHITE } else { BLACK } ;
986983 self . set_piece ( to, position_move. promotion_piece | color) ;
987984 } else {
988- self . set_piece ( to, self . get_piece ( from ) . 0 ) ;
985+ self . set_piece ( to, piece . 0 ) ;
989986 }
990987 self . set_piece ( from, 0 ) ;
991988
@@ -1014,18 +1011,20 @@ impl BoardMap {
10141011
10151012 // undo castling rook movement if this was a castling move
10161013 let piece = self . get_piece ( to) ;
1014+ let is_white = piece. get_color ( ) == PieceColor :: White ;
1015+
10171016 if let Some ( PieceType :: King ) = piece. get_type ( ) {
10181017 if from[ 1 ] == 4 && to[ 1 ] == 6 {
10191018 // undo kingside castle - move rook back from f-file to h-file
10201019 let rook_from = [ from[ 0 ] , 5 ] ;
10211020 let rook_to = [ from[ 0 ] , 7 ] ;
10221021 let rook_from_bit = 1u64 << ( rook_from[ 0 ] * 8 + rook_from[ 1 ] ) ;
10231022 let rook_to_bit = 1u64 << ( rook_to[ 0 ] * 8 + rook_to[ 1 ] ) ;
1024- if piece . get_color ( ) == PieceColor :: White {
1025- self . white_occupancy ^= rook_from_bit;
1023+ if is_white {
1024+ self . white_occupancy ^= rook_from_bit | rook_to_bit ;
10261025 self . white_occupancy |= rook_to_bit;
10271026 } else {
1028- self . black_occupancy ^= rook_from_bit;
1027+ self . black_occupancy ^= rook_from_bit | rook_to_bit ;
10291028 self . black_occupancy |= rook_to_bit;
10301029 }
10311030 self . set_piece ( rook_to, self . get_piece ( rook_from) . 0 ) ;
@@ -1036,58 +1035,49 @@ impl BoardMap {
10361035 let rook_to = [ from[ 0 ] , 0 ] ;
10371036 let rook_from_bit = 1u64 << ( rook_from[ 0 ] * 8 + rook_from[ 1 ] ) ;
10381037 let rook_to_bit = 1u64 << ( rook_to[ 0 ] * 8 + rook_to[ 1 ] ) ;
1039- if piece . get_color ( ) == PieceColor :: White {
1040- self . white_occupancy ^= rook_from_bit;
1038+ if is_white {
1039+ self . white_occupancy ^= rook_from_bit | rook_to_bit ;
10411040 self . white_occupancy |= rook_to_bit;
10421041 } else {
1043- self . black_occupancy ^= rook_from_bit;
1042+ self . black_occupancy ^= rook_from_bit | rook_to_bit ;
10441043 self . black_occupancy |= rook_to_bit;
10451044 }
10461045 self . set_piece ( rook_to, self . get_piece ( rook_from) . 0 ) ;
10471046 self . set_piece ( rook_from, 0 ) ;
10481047 }
10491048 }
10501049
1051- if piece . get_color ( ) == PieceColor :: White {
1052- self . white_occupancy ^= to_bit;
1050+ if is_white {
1051+ self . white_occupancy ^= to_bit | from_bit ;
10531052 self . white_occupancy |= from_bit;
10541053
10551054 if last_piece != 0 {
10561055 self . black_occupancy |= to_bit;
10571056 }
10581057 } else {
1059- self . black_occupancy ^= to_bit;
1058+ self . black_occupancy ^= to_bit | from_bit ;
10601059 self . black_occupancy |= from_bit;
10611060 if last_piece != 0 {
10621061 self . white_occupancy |= to_bit;
10631062 }
10641063 }
10651064
1066- self . set_piece ( from, self . get_piece ( to ) . 0 ) ;
1065+ self . set_piece ( from, piece . 0 ) ;
10671066 self . set_piece ( to, last_piece) ;
10681067
10691068 // undo en passant - restore the captured pawn
10701069 if en_passant {
1071- let moving_piece = self . get_piece ( from) ;
1072- let shift = if moving_piece. get_color ( ) == PieceColor :: Black {
1073- 1
1074- } else {
1075- -1
1076- } ;
1070+ let shift = if is_white { -1 } else { 1 } ;
10771071 let captured_pawn_pos = [ ( to[ 0 ] as isize - shift) as usize , to[ 1 ] ] ;
10781072 let ep_bit = 1u64 << ( captured_pawn_pos[ 0 ] * 8 + captured_pawn_pos[ 1 ] ) ;
10791073 // Restore captured pawn in occupancy
1080- if moving_piece . get_color ( ) == PieceColor :: White {
1074+ if is_white {
10811075 self . black_occupancy |= ep_bit;
10821076 } else {
10831077 self . white_occupancy |= ep_bit;
10841078 }
10851079 // restore the captured pawn (it was the opponent's pawn with en-passant flag)
1086- let opponent_color = if moving_piece. get_color ( ) == PieceColor :: Black {
1087- WHITE
1088- } else {
1089- BLACK
1090- } ;
1080+ let opponent_color = if is_white { BLACK } else { WHITE } ;
10911081 self . set_piece ( captured_pawn_pos, PAWN | opponent_color | 32 ) ;
10921082 }
10931083 }
0 commit comments