@@ -626,6 +626,7 @@ impl BoardMap {
626626 }
627627 pub fn gen_sliding ( & self , from : Position , piece_type : PieceType ) -> ArrayVec < Position , 32 > {
628628 let piece_from = self . squares [ from[ 0 ] ] [ from[ 1 ] ] ;
629+ let piece_color = piece_from. get_color ( ) ;
629630 let mut positions = ArrayVec :: new ( ) ;
630631 let start = if piece_type == PieceType :: Bishop {
631632 4
@@ -640,14 +641,14 @@ impl BoardMap {
640641 let target_position = [ target_index / 8 , target_index % 8 ] ;
641642 let target_piece = self . squares [ target_position[ 0 ] ] [ target_position[ 1 ] ] ;
642643
643- if target_piece. is_piece ( ) && target_piece. get_color ( ) == piece_from . get_color ( ) {
644+ if target_piece. is_piece ( ) && target_piece. get_color ( ) == piece_color {
644645 // your own color is in the way
645646 break ;
646647 }
647648 positions. push ( target_position) ;
648649 // self.squares[target_move[0]][target_move[1]] = Piece(100);
649650
650- if target_piece. is_piece ( ) && target_piece. get_color ( ) != piece_from . get_color ( ) {
651+ if target_piece. is_piece ( ) && target_piece. get_color ( ) != piece_color {
651652 // Enemy piece and capturable
652653 break ;
653654 }
@@ -657,6 +658,7 @@ impl BoardMap {
657658 }
658659 pub fn gen_king ( & self , from : Position ) -> ArrayVec < Position , 32 > {
659660 let piece_from = self . squares [ from[ 0 ] ] [ from[ 1 ] ] ;
661+ let piece_color = piece_from. get_color ( ) ;
660662 let mut positions = ArrayVec :: new ( ) ;
661663 for ( direction, offset) in DIRECTION_OFFSETS . iter ( ) . enumerate ( ) {
662664 let index = from[ 0 ] * 8 + from[ 1 ] ;
@@ -669,27 +671,27 @@ impl BoardMap {
669671 let target_move = [ target_index as usize / 8 , target_index as usize % 8 ] ;
670672 let target_piece = self . squares [ target_move[ 0 ] ] [ target_move[ 1 ] ] ;
671673
672- if target_piece. is_piece ( ) && target_piece. get_color ( ) == piece_from . get_color ( ) {
674+ if target_piece. is_piece ( ) && target_piece. get_color ( ) == piece_color {
673675 // your own color is in the way
674676 continue ;
675677 }
676678 positions. push ( target_move) ;
677679
678- if target_piece. is_piece ( ) && target_piece. get_color ( ) != piece_from . get_color ( ) {
680+ if target_piece. is_piece ( ) && target_piece. get_color ( ) != piece_color {
679681 // Enemy piece and capturable
680682 continue ;
681683 }
682684 }
683685
684686 // castling - use piece color, not active color
685- if piece_from . get_color ( ) == PieceColor :: Black {
687+ if piece_color == PieceColor :: Black {
686688 if self . black_can_short_castle ( ) {
687689 positions. push ( [ 0 , 6 ] ) ;
688690 }
689691 if self . black_can_long_castle ( ) {
690692 positions. push ( [ 0 , 2 ] ) ;
691693 }
692- } else if piece_from . get_color ( ) == PieceColor :: White {
694+ } else {
693695 if self . white_can_short_castle ( ) {
694696 positions. push ( [ 7 , 6 ] ) ;
695697 }
@@ -703,10 +705,9 @@ impl BoardMap {
703705 pub fn gen_pawn ( & self , from : Position ) -> ArrayVec < Position , 32 > {
704706 let piece_from = self . squares [ from[ 0 ] ] [ from[ 1 ] ] ;
705707 let mut moves = ArrayVec :: new ( ) ;
706- let shift = match piece_from. get_color ( ) {
707- PieceColor :: Black => 1 ,
708- PieceColor :: White => -1 ,
709- } ;
708+ let piece_color = piece_from. get_color ( ) ;
709+ let is_black = piece_color == PieceColor :: Black ;
710+ let shift = if is_black { 1 } else { -1 } ;
710711
711712 // piece blocking
712713 let vertical = ( from[ 0 ] as i32 + shift) as usize ;
@@ -720,10 +721,7 @@ impl BoardMap {
720721 let vertical = ( from[ 0 ] as i32 + shift * 2 ) as usize ;
721722 if vertical < 8 {
722723 let is_blocking = is_blocking || self . squares [ vertical] [ from[ 1 ] ] . is_piece ( ) ;
723- if ( ( piece_from. is_black ( ) && from[ 0 ] == 1 )
724- || ( piece_from. is_white ( ) && from[ 0 ] == 6 ) )
725- && !is_blocking
726- {
724+ if ( ( is_black && from[ 0 ] == 1 ) || ( !is_black && from[ 0 ] == 6 ) ) && !is_blocking {
727725 moves. push ( [ vertical, from[ 1 ] ] ) ;
728726 }
729727 }
@@ -736,7 +734,7 @@ impl BoardMap {
736734 let to_top_left_pos = [ ( from[ 0 ] as i32 + shift) as usize , from[ 1 ] - 1 ] ;
737735 if to_top_left_pos[ 0 ] < 8 {
738736 let to_top_left = self . get_piece ( to_top_left_pos) ;
739- if to_top_left. is_piece ( ) && to_top_left. get_color ( ) != piece_from . get_color ( ) {
737+ if to_top_left. is_piece ( ) && to_top_left. get_color ( ) != piece_color {
740738 moves. push ( to_top_left_pos) ;
741739 }
742740
@@ -745,7 +743,7 @@ impl BoardMap {
745743 // _ p .
746744 let to_left = self . squares [ from[ 0 ] ] [ from[ 1 ] - 1 ] ;
747745 if let Some ( PieceType :: Pawn ( en_passantable) ) = to_left. get_type ( ) {
748- if en_passantable && to_left. get_color ( ) != piece_from . get_color ( ) {
746+ if en_passantable && to_left. get_color ( ) != piece_color {
749747 let to_en_passant = [ ( from[ 0 ] as i32 + shift) as usize , from[ 1 ] - 1 ] ;
750748 moves. push ( to_en_passant) ;
751749 }
@@ -760,7 +758,7 @@ impl BoardMap {
760758 let to_top_right_pos = [ ( from[ 0 ] as i32 + shift) as usize , from[ 1 ] + 1 ] ;
761759 if to_top_right_pos[ 0 ] < 8 {
762760 let to_top_right = self . squares [ to_top_right_pos[ 0 ] ] [ to_top_right_pos[ 1 ] ] ;
763- if to_top_right. is_piece ( ) && to_top_right. get_color ( ) != piece_from . get_color ( ) {
761+ if to_top_right. is_piece ( ) && to_top_right. get_color ( ) != piece_color {
764762 moves. push ( to_top_right_pos) ;
765763 }
766764
@@ -769,7 +767,7 @@ impl BoardMap {
769767 // . p _
770768 let to_right = self . squares [ from[ 0 ] ] [ from[ 1 ] + 1 ] ;
771769 if let Some ( PieceType :: Pawn ( en_passantable) ) = to_right. get_type ( ) {
772- if en_passantable && to_right. get_color ( ) != piece_from . get_color ( ) {
770+ if en_passantable && to_right. get_color ( ) != piece_color {
773771 let to_en_passant = [ ( from[ 0 ] as i32 + shift) as usize , from[ 1 ] + 1 ] ;
774772 moves. push ( to_en_passant) ;
775773 }
@@ -780,6 +778,7 @@ impl BoardMap {
780778 }
781779 pub fn gen_knight ( & self , from : Position ) -> ArrayVec < Position , 32 > {
782780 let piece_from = self . squares [ from[ 0 ] ] [ from[ 1 ] ] ;
781+ let piece_color = piece_from. get_color ( ) ;
783782 KNIGHT_DIRECTION_OFFSETS
784783 . iter ( )
785784 . filter_map ( |direction| {
@@ -789,9 +788,7 @@ impl BoardMap {
789788 ] ;
790789 if new_pos[ 0 ] < 8 && new_pos[ 1 ] < 8 {
791790 let target_piece = self . squares [ new_pos[ 0 ] ] [ new_pos[ 1 ] ] ;
792- if !( target_piece. is_piece ( )
793- && target_piece. get_color ( ) == piece_from. get_color ( ) )
794- {
791+ if !( target_piece. is_piece ( ) && target_piece. get_color ( ) == piece_color) {
795792 return Some ( new_pos) ;
796793 }
797794 }
@@ -826,23 +823,23 @@ impl BoardMap {
826823 promotion,
827824 ..
828825 } = position_move;
826+
827+ let piece = self . get_piece ( from) ;
828+ let is_white = piece. get_color ( ) == PieceColor :: White ;
829+ let target_piece = self . get_piece ( to) ;
830+
829831 if en_passant {
830- let shift = if self . get_piece ( from) . get_color ( ) == PieceColor :: Black {
831- 1
832- } else {
833- -1
834- } ;
832+ let shift = if is_white { -1 } else { 1 } ;
835833 let to_step = [ ( to[ 0 ] as isize - shift) as usize , to[ 1 ] ] ;
836834 self . set_piece ( to_step, 0 ) ;
837835 }
838836
839837 // track castling rights
840838 // mark kings and rooks as moved
841- let piece = self . get_piece ( from) ;
842839 if let Some ( piece_type) = piece. get_type ( ) {
843840 match piece_type {
844841 PieceType :: King => {
845- if piece . get_color ( ) == PieceColor :: White {
842+ if is_white {
846843 self . white_king_moved = true ;
847844 self . white_king_pos = to;
848845 } else {
@@ -852,7 +849,7 @@ impl BoardMap {
852849 }
853850 PieceType :: Rook => {
854851 // check if rook move
855- if piece . get_color ( ) == PieceColor :: White {
852+ if is_white {
856853 if from == [ 7 , 0 ] {
857854 self . white_queenside_rook_moved = true ;
858855 } else if from == [ 7 , 7 ] {
@@ -869,7 +866,6 @@ impl BoardMap {
869866 }
870867
871868 // invalidate castling if a rook is captured on its starting square
872- let target_piece = self . get_piece ( to) ;
873869 if target_piece. is_piece ( ) {
874870 if let Some ( PieceType :: Rook ) = target_piece. get_type ( ) {
875871 match to {
@@ -889,25 +885,24 @@ impl BoardMap {
889885 // kingside castle - move rook from h-file to f-file
890886 let rook_from = [ from[ 0 ] , 7 ] ;
891887 let rook_to = [ from[ 0 ] , 5 ] ;
892- self . set_piece ( rook_to, self . get_piece ( rook_from) . 0 ) ;
888+ let rook_piece = self . get_piece ( rook_from) . 0 ;
889+ self . set_piece ( rook_to, rook_piece) ;
893890 self . set_piece ( rook_from, 0 ) ;
894891 } else if from[ 1 ] == 4 && to[ 1 ] == 2 {
895892 // queenside castle - move rook from a-file to d-file
896893 let rook_from = [ from[ 0 ] , 0 ] ;
897894 let rook_to = [ from[ 0 ] , 3 ] ;
898- self . set_piece ( rook_to, self . get_piece ( rook_from) . 0 ) ;
895+ let rook_piece = self . get_piece ( rook_from) . 0 ;
896+ self . set_piece ( rook_to, rook_piece) ;
899897 self . set_piece ( rook_from, 0 ) ;
900898 }
901899 }
902900
903901 if promotion {
904- let color = match self . get_piece ( from) . get_color ( ) {
905- PieceColor :: Black => BLACK ,
906- PieceColor :: White => WHITE ,
907- } ;
902+ let color = if is_white { WHITE } else { BLACK } ;
908903 self . set_piece ( to, position_move. promotion_piece | color) ;
909904 } else {
910- self . set_piece ( to, self . get_piece ( from ) . 0 ) ;
905+ self . set_piece ( to, piece . 0 ) ;
911906 }
912907 self . set_piece ( from, 0 ) ;
913908
@@ -931,42 +926,37 @@ impl BoardMap {
931926 ..
932927 } = piece_move;
933928
934- // undo castling rook movement if this was a castling move
935929 let piece = self . get_piece ( to) ;
930+ let is_white = piece. get_color ( ) == PieceColor :: White ;
931+
932+ // undo castling rook movement if this was a castling move
936933 if let Some ( PieceType :: King ) = piece. get_type ( ) {
937934 if from[ 1 ] == 4 && to[ 1 ] == 6 {
938935 // undo kingside castle - move rook back from f-file to h-file
939936 let rook_from = [ from[ 0 ] , 5 ] ;
940937 let rook_to = [ from[ 0 ] , 7 ] ;
941- self . set_piece ( rook_to, self . get_piece ( rook_from) . 0 ) ;
938+ let rook_piece = self . get_piece ( rook_from) . 0 ;
939+ self . set_piece ( rook_to, rook_piece) ;
942940 self . set_piece ( rook_from, 0 ) ;
943941 } else if from[ 1 ] == 4 && to[ 1 ] == 2 {
944942 // undo queenside castle - move rook back from d-file to a-file
945943 let rook_from = [ from[ 0 ] , 3 ] ;
946944 let rook_to = [ from[ 0 ] , 0 ] ;
947- self . set_piece ( rook_to, self . get_piece ( rook_from) . 0 ) ;
945+ let rook_piece = self . get_piece ( rook_from) . 0 ;
946+ self . set_piece ( rook_to, rook_piece) ;
948947 self . set_piece ( rook_from, 0 ) ;
949948 }
950949 }
951950
952- self . set_piece ( from, self . get_piece ( to ) . 0 ) ;
951+ self . set_piece ( from, piece . 0 ) ;
953952 self . set_piece ( to, last_piece) ;
954953
955954 // undo en passant - restore the captured pawn
956955 if en_passant {
957- let moving_piece = self . get_piece ( from) ;
958- let shift = if moving_piece. get_color ( ) == PieceColor :: Black {
959- 1
960- } else {
961- -1
962- } ;
956+ let shift = if is_white { -1 } else { 1 } ;
963957 let captured_pawn_pos = [ ( to[ 0 ] as isize - shift) as usize , to[ 1 ] ] ;
964958 // restore the captured pawn (it was the opponent's pawn with en-passant flag)
965- let opponent_color = if moving_piece. get_color ( ) == PieceColor :: Black {
966- WHITE
967- } else {
968- BLACK
969- } ;
959+ let opponent_color = if is_white { BLACK } else { WHITE } ;
970960 self . set_piece ( captured_pawn_pos, PAWN | opponent_color | 32 ) ;
971961 }
972962 }
0 commit comments