@@ -471,12 +471,10 @@ impl Board {
471
471
472
472
moves. extend ( & self . generate_pawn_moves ( ) ) ;
473
473
moves. extend ( & self . generate_bishop_moves ( ) ) ;
474
-
475
- // TODO: Generate bishop moves
476
- // TODO: Generate knight moves
477
- // TODO: Generate rook moves
478
- // TODO: Generate queen moves
479
- // TODO: Generate king moves
474
+ moves. extend ( & self . generate_knight_moves ( ) ) ;
475
+ moves. extend ( & self . generate_rook_moves ( ) ) ;
476
+ moves. extend ( & self . generate_queen_moves ( ) ) ;
477
+ moves. extend ( & self . generate_king_moves ( ) ) ;
480
478
481
479
println ! ( "Possible {:?} moves:" , moves. len( ) ) ;
482
480
moves. iter ( ) . for_each ( |m : & Move | {
@@ -657,28 +655,29 @@ impl Board {
657
655
moves
658
656
}
659
657
660
- pub fn generate_bishop_moves ( & self ) -> Vec < Move > {
658
+ fn generate_slider_moves (
659
+ & self ,
660
+ directions : & [ i32 ] ,
661
+ pieces : Bitboard ,
662
+ piece : Piece ,
663
+ ) -> Vec < Move > {
661
664
let mut moves = Vec :: new ( ) ;
662
- let bishops = match self . turn {
663
- Color :: White => self . white_pieces . bishops ,
664
- Color :: Black => self . black_pieces . bishops ,
665
- } ;
666
665
667
666
for i in 0 ..BOARD_SIZE {
668
- if !bishops . is_set ( i) {
667
+ if !pieces . is_set ( i) {
669
668
continue ;
670
669
}
671
670
672
671
let from = i;
673
672
674
- for direction in BISHOP_DIRECTIONS . iter ( ) {
673
+ for direction in directions . iter ( ) {
675
674
let mut to = from as i32 + direction;
676
675
while Board :: is_index_in_bounds ( to) {
677
676
if self . is_square_empty ( to as usize ) {
678
677
moves. push ( Move {
679
678
from,
680
679
to : to as usize ,
681
- piece : Piece :: Bishop ,
680
+ piece,
682
681
color : self . turn ,
683
682
en_passant : false ,
684
683
castling : false ,
@@ -689,7 +688,7 @@ impl Board {
689
688
moves. push ( Move {
690
689
from,
691
690
to : to as usize ,
692
- piece : Piece :: Bishop ,
691
+ piece,
693
692
color : self . turn ,
694
693
en_passant : false ,
695
694
castling : false ,
@@ -710,6 +709,146 @@ impl Board {
710
709
}
711
710
}
712
711
712
+ moves
713
+ }
714
+ pub fn generate_bishop_moves ( & self ) -> Vec < Move > {
715
+ let bishops = match self . turn {
716
+ Color :: White => self . white_pieces . bishops ,
717
+ Color :: Black => self . black_pieces . bishops ,
718
+ } ;
719
+
720
+ // TODO: Validate check
721
+
722
+ self . generate_slider_moves ( & BISHOP_DIRECTIONS , bishops, Piece :: Bishop )
723
+ }
724
+
725
+ pub fn generate_knight_moves ( & self ) -> Vec < Move > {
726
+ let mut moves = Vec :: new ( ) ;
727
+ let knights = match self . turn {
728
+ Color :: White => self . white_pieces . knights ,
729
+ Color :: Black => self . black_pieces . knights ,
730
+ } ;
731
+
732
+ // TODO: Validate check
733
+
734
+ for i in 0 ..BOARD_SIZE {
735
+ if !knights. is_set ( i) {
736
+ continue ;
737
+ }
738
+
739
+ let from = i;
740
+ for direction in KNIGHT_DIRECTIONS . iter ( ) {
741
+ let to = from as i32 + direction;
742
+ if !Board :: is_index_in_bounds ( to) {
743
+ continue ;
744
+ }
745
+
746
+ if ( to % BOARD_WIDTH as i32 - ( from % BOARD_WIDTH ) as i32 ) . abs ( ) > 2 {
747
+ continue ;
748
+ }
749
+
750
+ if self . is_square_empty ( to as usize ) {
751
+ moves. push ( Move {
752
+ from,
753
+ to : to as usize ,
754
+ piece : Piece :: Knight ,
755
+ color : self . turn ,
756
+ en_passant : false ,
757
+ castling : false ,
758
+ promotion : None ,
759
+ capture : None ,
760
+ } ) ;
761
+ } else if self . is_enemy ( to as usize ) {
762
+ moves. push ( Move {
763
+ from,
764
+ to : to as usize ,
765
+ piece : Piece :: Knight ,
766
+ color : self . turn ,
767
+ en_passant : false ,
768
+ castling : false ,
769
+ promotion : None ,
770
+ capture : self . piece_at ( to as usize ) ,
771
+ } ) ;
772
+ }
773
+ }
774
+ }
775
+
776
+ moves
777
+ }
778
+
779
+ pub fn generate_rook_moves ( & self ) -> Vec < Move > {
780
+ let rooks = match self . turn {
781
+ Color :: White => self . white_pieces . rooks ,
782
+ Color :: Black => self . black_pieces . rooks ,
783
+ } ;
784
+
785
+ // TODO: Validate check
786
+
787
+ self . generate_slider_moves ( & ROOK_DIRECTIONS , rooks, Piece :: Rook )
788
+ }
789
+
790
+ pub fn generate_queen_moves ( & self ) -> Vec < Move > {
791
+ let queens = match self . turn {
792
+ Color :: White => self . white_pieces . queens ,
793
+ Color :: Black => self . black_pieces . queens ,
794
+ } ;
795
+
796
+ // TODO: Validate check
797
+
798
+ self . generate_slider_moves ( & QUEEN_DIRECTIONS , queens, Piece :: Queen )
799
+ }
800
+
801
+ pub fn generate_king_moves ( & self ) -> Vec < Move > {
802
+ let mut moves = Vec :: new ( ) ;
803
+ let king = match self . turn {
804
+ Color :: White => self . white_pieces . king ,
805
+ Color :: Black => self . black_pieces . king ,
806
+ } ;
807
+
808
+ // TODO: Validate check
809
+
810
+ for i in 0 ..BOARD_SIZE {
811
+ if !king. is_set ( i) {
812
+ continue ;
813
+ }
814
+
815
+ let from = i;
816
+ for direction in KING_DIRECTIONS . iter ( ) {
817
+ let to = from as i32 + direction;
818
+ if !Board :: is_index_in_bounds ( to) {
819
+ continue ;
820
+ }
821
+
822
+ if ( to % BOARD_WIDTH as i32 - ( from % BOARD_WIDTH ) as i32 ) . abs ( ) > 1 {
823
+ continue ;
824
+ }
825
+
826
+ if self . is_square_empty ( to as usize ) {
827
+ moves. push ( Move {
828
+ from,
829
+ to : to as usize ,
830
+ piece : Piece :: King ,
831
+ color : self . turn ,
832
+ en_passant : false ,
833
+ castling : false ,
834
+ promotion : None ,
835
+ capture : None ,
836
+ } ) ;
837
+ } else if self . is_enemy ( to as usize ) {
838
+ moves. push ( Move {
839
+ from,
840
+ to : to as usize ,
841
+ piece : Piece :: King ,
842
+ color : self . turn ,
843
+ en_passant : false ,
844
+ castling : false ,
845
+ promotion : None ,
846
+ capture : self . piece_at ( to as usize ) ,
847
+ } ) ;
848
+ }
849
+ }
850
+ }
851
+
713
852
moves
714
853
}
715
854
}
0 commit comments