Skip to content

Commit fd69770

Browse files
committed
Move generation without check validation
1 parent ed51831 commit fd69770

File tree

3 files changed

+157
-16
lines changed

3 files changed

+157
-16
lines changed

src/board.rs

Lines changed: 154 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -471,12 +471,10 @@ impl Board {
471471

472472
moves.extend(&self.generate_pawn_moves());
473473
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());
480478

481479
println!("Possible {:?} moves:", moves.len());
482480
moves.iter().for_each(|m: &Move| {
@@ -657,28 +655,29 @@ impl Board {
657655
moves
658656
}
659657

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> {
661664
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-
};
666665

667666
for i in 0..BOARD_SIZE {
668-
if !bishops.is_set(i) {
667+
if !pieces.is_set(i) {
669668
continue;
670669
}
671670

672671
let from = i;
673672

674-
for direction in BISHOP_DIRECTIONS.iter() {
673+
for direction in directions.iter() {
675674
let mut to = from as i32 + direction;
676675
while Board::is_index_in_bounds(to) {
677676
if self.is_square_empty(to as usize) {
678677
moves.push(Move {
679678
from,
680679
to: to as usize,
681-
piece: Piece::Bishop,
680+
piece,
682681
color: self.turn,
683682
en_passant: false,
684683
castling: false,
@@ -689,7 +688,7 @@ impl Board {
689688
moves.push(Move {
690689
from,
691690
to: to as usize,
692-
piece: Piece::Bishop,
691+
piece,
693692
color: self.turn,
694693
en_passant: false,
695694
castling: false,
@@ -710,6 +709,146 @@ impl Board {
710709
}
711710
}
712711

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+
713852
moves
714853
}
715854
}

src/constans.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,5 @@ pub const STARTING_POSITION: &str = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR
3030
pub const ROOK_DIRECTIONS: [i32; 4] = [1, -1, 8, -8];
3131
pub const BISHOP_DIRECTIONS: [i32; 4] = [9, 7, -7, -9];
3232
pub const KNIGHT_DIRECTIONS: [i32; 8] = [6, 10, 15, 17, -6, -10, -15, -17];
33+
pub const KING_DIRECTIONS: [i32; 8] = [1, 9, 8, 7, -1, -9, -8, -7];
34+
pub const QUEEN_DIRECTIONS: [i32; 8] = [1, 9, 8, 7, -1, -9, -8, -7];

src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ fn main() {
44
let mut board = Board::init();
55
board.print();
66
let _ = board.generate_possible_moves();
7-
board.set_fen("rnbqkbnr/pppp1ppp/8/4p3/3B4/8/PPP1PPPP/RNBQKBNR w KQkq - 0 1");
7+
board.set_fen("rnbqkbnr/pppp1ppp/8/4p3/3Q4/8/PPP1PPPP/RNBQKBNR b KQkq - 0 1");
88
board.print();
99
board.generate_possible_moves();
1010
}

0 commit comments

Comments
 (0)