From 71a03d9fdecc962f749459f4fef5ff9fb7f084b2 Mon Sep 17 00:00:00 2001 From: KrinjMaster <5opkasuka@gmail.com> Date: Mon, 8 Jan 2024 20:55:14 +0300 Subject: [PATCH] feat: started board representation (wtf did i write??) --- Cargo.toml | 1 + README.md | 3 + src/board.rs | 146 +++++++++++++++++++++++++++++++++++++++++++++++ src/constants.rs | 68 ++++++++++++++++++++++ src/main.rs | 9 ++- 5 files changed, 226 insertions(+), 1 deletion(-) create mode 100644 src/board.rs create mode 100644 src/constants.rs diff --git a/Cargo.toml b/Cargo.toml index 8cfec9c..1f3f8ab 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,3 +6,4 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] + diff --git a/README.md b/README.md index 958abcb..a6c996d 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,6 @@ # krinj-mater My chess engine built in Rust. + +Goald: +[] A way to represent a board (pieces position, which turn is it, who can castle, en passant etc.) diff --git a/src/board.rs b/src/board.rs new file mode 100644 index 0000000..297ef4f --- /dev/null +++ b/src/board.rs @@ -0,0 +1,146 @@ +// i only did pieces inserting everything still left when parsing fen string +use crate::constants::BOARD_SQUARES; + +pub type BitBoard = u64; + +#[derive(Debug, Clone, Copy)] +pub struct BoardState { + pub bitboards: [[BitBoard; 6]; 2], +} + +impl BoardState { + pub fn from_fen(fen_string: &str) -> Result { + let fen: Vec<&str> = fen_string.split_whitespace().collect(); + + if fen.len() != 6 { + return Err("Incorrect fen string number of data!"); + } + + // piece position + // bitboards for future piece inserting + + // white pieces + 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; + let mut bb_black_rooks: BitBoard = 0; + let mut bb_black_queens: BitBoard = 0; + let mut bb_black_king: BitBoard = 0; + + let fen_pieces: Vec<&str> = fen[0].split("/").collect(); + + if fen_pieces.len() != 8 { + return Err("Incorrect pieces placement in the fen string!"); + } + + // 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; + + for char in fen_pieces[row].chars() { + if char.is_numeric() { + col += char.to_digit(10).unwrap(); + } else { + match char { + 'P' => { + bb_white_pawns = + bb_white_pawns | BOARD_SQUARES[8 * row as usize + col as usize] + } + 'N' => { + bb_white_knights = + bb_white_knights | BOARD_SQUARES[8 * row as usize + col as usize] + } + 'B' => { + bb_white_bishops = + bb_white_bishops | BOARD_SQUARES[8 * row as usize + col as usize] + } + 'R' => { + bb_white_rooks = + bb_white_rooks | BOARD_SQUARES[8 * row as usize + col as usize] + } + 'Q' => { + bb_white_queens = + bb_white_queens | BOARD_SQUARES[8 * row as usize + col as usize] + } + 'K' => { + if white_king_count > 1 { + return Err( + "Incorrect fen string! More than 1 white king on the board!", + ); + } + + bb_white_king = + bb_white_king | BOARD_SQUARES[8 * row as usize + col as usize]; + white_king_count += 1; + } + 'p' => { + bb_black_pawns = + bb_black_pawns | BOARD_SQUARES[8 * row as usize + col as usize] + } + 'n' => { + bb_black_knights = + bb_black_knights | BOARD_SQUARES[8 * row as usize + col as usize] + } + 'b' => { + bb_black_bishops = + bb_black_bishops | BOARD_SQUARES[8 * row as usize + col as usize] + } + 'r' => { + bb_black_rooks = + bb_black_rooks | BOARD_SQUARES[8 * row as usize + col as usize] + } + 'q' => { + bb_black_queens = + bb_black_queens | BOARD_SQUARES[8 * row as usize + col as usize] + } + 'k' => { + if black_king_count > 1 { + return Err( + "Incorrect fen string! More than 1 black king on the board!", + ); + } + + bb_black_king = + bb_black_king | BOARD_SQUARES[8 * row as usize + col as usize]; + + black_king_count += 1; + } + _ => return Err("Incorrect piece in fen string!"), + } + col += 1; + } + } + } + + Ok(BoardState { + bitboards: [ + [ + 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, + ], + ], + }) + } +} diff --git a/src/constants.rs b/src/constants.rs new file mode 100644 index 0000000..9e0454f --- /dev/null +++ b/src/constants.rs @@ -0,0 +1,68 @@ +pub const DEFAULT_FEN_STRING: &str = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"; + +pub const BOARD_SQUARES: [u64; 64] = [ + 1, + 2, + 4, + 8, + 16, + 32, + 64, + 128, + 256, + 512, + 1024, + 2048, + 4096, + 8192, + 16384, + 32768, + 65536, + 131072, + 262144, + 524288, + 1048576, + 2097152, + 4194304, + 8388608, + 16777216, + 33554432, + 67108864, + 134217728, + 268435456, + 536870912, + 1073741824, + 2147483648, + 4294967296, + 8589934592, + 17179869184, + 34359738368, + 68719476736, + 137438953472, + 274877906944, + 549755813888, + 1099511627776, + 2199023255552, + 4398046511104, + 8796093022208, + 17592186044416, + 35184372088832, + 70368744177664, + 140737488355328, + 281474976710656, + 562949953421312, + 1125899906842624, + 2251799813685248, + 4503599627370496, + 9007199254740992, + 18014398509481984, + 36028797018963968, + 72057594037927936, + 144115188075855872, + 288230376151711744, + 576460752303423488, + 1152921504606846976, + 2305843009213693952, + 4611686018427387904, + 9223372036854775808, +]; diff --git a/src/main.rs b/src/main.rs index e7a11a9..a980d35 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,10 @@ +use std::process::exit; +mod board; +mod constants; + fn main() { - println!("Hello, world!"); + let board = board::BoardState::from_fen(constants::DEFAULT_FEN_STRING).unwrap_or_else(|err| { + println!("{}", err); + exit(1); + }); }