Skip to content

Commit

Permalink
feat: started board representation (wtf did i write??)
Browse files Browse the repository at this point in the history
  • Loading branch information
KrinjMaster committed Jan 11, 2024
1 parent aece38a commit 71a03d9
Show file tree
Hide file tree
Showing 5 changed files with 226 additions and 1 deletion.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]

3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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.)
146 changes: 146 additions & 0 deletions src/board.rs
Original file line number Diff line number Diff line change
@@ -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<BoardState, &str> {
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,
],
],
})
}
}
68 changes: 68 additions & 0 deletions src/constants.rs
Original file line number Diff line number Diff line change
@@ -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,
];
9 changes: 8 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
@@ -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);
});
}

0 comments on commit 71a03d9

Please sign in to comment.