Skip to content

Commit

Permalink
feat: pseudo legal pawn moves generation
Browse files Browse the repository at this point in the history
  • Loading branch information
KrinjMaster committed Jan 15, 2024
1 parent 538d5ee commit bd643b3
Show file tree
Hide file tree
Showing 5 changed files with 378 additions and 14 deletions.
144 changes: 132 additions & 12 deletions src/board.rs
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
// i only did pieces inserting everything still left when parsing fen string
use crate::constants::BOARD_SQUARES;

pub type BitBoard = u64;
use crate::constants::{BitBoard, BOARD_SQUARES};

#[derive(Debug, Clone, Copy)]
pub struct BoardState {
pub bitboards: [[BitBoard; 6]; 2],
pub bb_pieces: [[BitBoard; 6]; 2],
pub bb_colors: [BitBoard; 2],
pub bb_fullboard: BitBoard,
pub bb_to_move: BitBoard,
pub bb_castling_rigths: [[BitBoard; 2]; 2],
pub bb_en_passant: BitBoard,
pub halfmove: u32,
pub fullmove: u32,
}

impl BoardState {
pub fn from_fen(fen_string: &str) -> Result<BoardState, &str> {
let fen: Vec<&str> = fen_string.split_whitespace().collect();

if fen.len() != 6 {
return Err("Incorrect fen string number of data!");
return Err("Incorrect fen string!");
}

// piece position
// bitboards for future piece inserting

// white pieces
// pieces position parsing
let mut bb_white_pawns: BitBoard = 0;
let mut bb_white_knights: BitBoard = 0;
let mut bb_white_bishops: BitBoard = 0;
let mut bb_white_rooks: BitBoard = 0;
let mut bb_white_queens: BitBoard = 0;
let mut bb_white_king: BitBoard = 0;
// black pieces
let mut bb_black_pawns: BitBoard = 0;
let mut bb_black_knights: BitBoard = 0;
let mut bb_black_bishops: BitBoard = 0;
Expand All @@ -43,6 +43,7 @@ impl BoardState {
// not permanent solution (maybe)
let mut white_king_count = 0;
let mut black_king_count = 0;
//

for row in 0..fen_pieces.len() {
let mut col = 0;
Expand Down Expand Up @@ -122,8 +123,93 @@ impl BoardState {
}
}

// parsing move to move
let to_move_fen: &str = fen[1];
let mut bb_to_move: BitBoard = 0;

match to_move_fen {
"w" => bb_to_move = 0,
"b" => bb_to_move = 1,
_ => return Err("Incorrect to move color in fen string!"),
}

// castling rights parsing
let castling_rigths_fen: &str = fen[2];

let mut bb_castling_white_kingside: BitBoard = 0;
let mut bb_castling_white_queenside: BitBoard = 0;
let mut bb_castling_black_kingside: BitBoard = 0;
let mut bb_castling_black_queenside: BitBoard = 0;

for char in castling_rigths_fen.chars() {
match char {
'K' => bb_castling_white_kingside = BOARD_SQUARES[0],
'Q' => bb_castling_white_queenside = BOARD_SQUARES[7],
'k' => bb_castling_black_kingside = BOARD_SQUARES[56],
'q' => bb_castling_black_queenside = BOARD_SQUARES[63],
_ => {
if char != '-' {
println!("{}", castling_rigths_fen);
return Err("Incorrect castlings rights in fen string!");
}
}
}
}

// en passant square parsing
let mut en_passant: Vec<usize> = vec![];
let mut bb_en_passant: BitBoard = 0;

if fen[3].len() > 2 {
return Err("Incorrect length of en passant square in fen string!");
}

for char in fen[3].chars() {
match char {
'a' => en_passant.push(0),
'b' => en_passant.push(1),
'c' => en_passant.push(2),
'd' => en_passant.push(3),
'e' => en_passant.push(4),
'f' => en_passant.push(5),
'g' => en_passant.push(6),
'h' => en_passant.push(7),
_ => {
if char.is_numeric() && en_passant.len() == 1 {
en_passant.push(char.to_digit(10).unwrap() as usize - 1);
} else if char != '-' {
return Err("Incorrect en passant square position in fen string!");
}
}
}
}

if en_passant.len() == 2 {
bb_en_passant = bb_en_passant | BOARD_SQUARES[63 - (en_passant[1] * 8 + en_passant[0])];
}

// halfmove parsing
let mut halfmove = 0;

match fen[4].parse::<u32>() {
Ok(int) => halfmove = int,
Err(_) => return Err("Incorrect halfmove count in a fen string!"),
};

if halfmove > 1 {
return Err("Incorrect halfmove count in a fen string!");
}

// fullmove parsing
let mut fullmove = 0;

match fen[5].parse::<u32>() {
Ok(int) => fullmove = int,
Err(_) => return Err("Incorrect halfmove count in a fen string!"),
};

Ok(BoardState {
bitboards: [
bb_pieces: [
[
bb_white_pawns,
bb_white_knights,
Expand All @@ -141,6 +227,40 @@ impl BoardState {
bb_black_king,
],
],
bb_colors: [
bb_white_pawns
| bb_white_knights
| bb_white_bishops
| bb_white_rooks
| bb_white_queens
| bb_white_king,
bb_black_pawns
| bb_black_knights
| bb_black_bishops
| bb_black_rooks
| bb_black_queens
| bb_black_king,
],
bb_fullboard: bb_white_pawns
| bb_white_knights
| bb_white_bishops
| bb_white_rooks
| bb_white_queens
| bb_white_king
| bb_black_pawns
| bb_black_knights
| bb_black_bishops
| bb_black_rooks
| bb_black_queens
| bb_black_king,
bb_to_move,
bb_castling_rigths: [
[bb_castling_white_kingside, bb_castling_white_queenside],
[bb_castling_black_kingside, bb_castling_black_queenside],
],
bb_en_passant,
halfmove,
fullmove,
})
}
}
140 changes: 140 additions & 0 deletions src/constants.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
pub const DEFAULT_FEN_STRING: &str = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1";

pub type BitBoard = u64;

// pub const BB_A_FILE: u64 = 18374403900871474942;
// pub const BB_H_FILE: u64 = 9187201950435737471;

pub const BOARD_SQUARES: [u64; 64] = [
1,
2,
Expand Down Expand Up @@ -66,3 +71,138 @@ pub const BOARD_SQUARES: [u64; 64] = [
4611686018427387904,
9223372036854775808,
];

pub const PAWN_ATTACK_SQUARES: [[u64; 64]; 2] = [
[
0,
0,
0,
0,
0,
0,
0,
0,
2,
5,
10,
20,
40,
80,
160,
64,
512,
1280,
2560,
5120,
10240,
20480,
40960,
16384,
131072,
327680,
655360,
1310720,
2621440,
5242880,
10485760,
4194304,
33554432,
83886080,
167772160,
335544320,
671088640,
1342177280,
2684354560,
1073741824,
8589934592,
21474836480,
42949672960,
85899345920,
171798691840,
343597383680,
687194767360,
274877906944,
2199023255552,
5497558138880,
10995116277760,
21990232555520,
43980465111040,
87960930222080,
175921860444160,
70368744177664,
562949953421312,
1407374883553280,
2814749767106560,
5629499534213120,
11258999068426240,
22517998136852480,
45035996273704960,
18014398509481984,
],
[
512,
1280,
2560,
5120,
10240,
20480,
40960,
16384,
131072,
327680,
655360,
1310720,
2621440,
5242880,
10485760,
4194304,
33554432,
83886080,
167772160,
335544320,
671088640,
1342177280,
2684354560,
1073741824,
8589934592,
21474836480,
42949672960,
85899345920,
171798691840,
343597383680,
687194767360,
274877906944,
2199023255552,
5497558138880,
10995116277760,
21990232555520,
43980465111040,
87960930222080,
175921860444160,
70368744177664,
562949953421312,
1407374883553280,
2814749767106560,
5629499534213120,
11258999068426240,
22517998136852480,
45035996273704960,
18014398509481984,
144115188075855872,
360287970189639680,
720575940379279360,
1441151880758558720,
2882303761517117440,
5764607523034234880,
11529215046068469760,
4611686018427387904,
0,
0,
0,
0,
0,
0,
0,
0,
],
];
21 changes: 19 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,27 @@
use std::process::exit;
mod board;

mod constants;
use crate::constants::DEFAULT_FEN_STRING;

mod board;
use crate::board::BoardState;

mod piece_parsing;
use crate::piece_parsing::parse_bitboards_into_vector;

mod move_generation;
use crate::move_generation::generate_pawn_moves;

fn main() {
let board = board::BoardState::from_fen(constants::DEFAULT_FEN_STRING).unwrap_or_else(|err| {
let board = BoardState::from_fen(DEFAULT_FEN_STRING).unwrap_or_else(|err| {
println!("{}", err);
exit(1);
});

// generate pseudo legal moves for black pawns
let pawn_moves = generate_pawn_moves(
parse_bitboards_into_vector(1, board.bb_pieces[1][0]),
board.bb_colors[1],
board.bb_colors[0],
);
}
Loading

0 comments on commit bd643b3

Please sign in to comment.