diff --git a/src/chess/position.rs b/src/chess/position.rs index 6b5f909..5c2e0b5 100644 --- a/src/chess/position.rs +++ b/src/chess/position.rs @@ -1,15 +1,43 @@ use crate::chess::piece::Piece; use crate::chess::square::Square; use crate::chess::moves::Move; -use log::trace; +use log::{debug, trace}; -#[derive(Debug)] +#[derive(Debug, Default)] pub struct Castling { king_side: bool, queen_side: bool, } +impl Castling { + pub fn king_side(self) -> bool { + self.king_side + } + pub fn queen_side(self) -> bool { + self.queen_side + } +} + +impl Castling { + fn from_fen(fen: String) -> [Self; 2] { + let mut castling = [Castling::default(), Castling::default()]; + for c in fen.chars() { + match c { + 'K' => castling[0].king_side = true, + 'Q' => castling[0].queen_side = true, + 'k' => castling[1].king_side = true, + 'q' => castling[1].queen_side = true, + '-' => (), + _ => panic!("Invalid castling character"), + } + } + castling + } +} + + +// TODO: implement halfmove clock and fullmove number #[derive(Debug)] pub struct Position { rows: [[Option; 8]; 8], @@ -74,7 +102,7 @@ impl Position { pub fn from_fen(fen: String) -> Self { trace!("{fen}"); let mut pos = Position::default(); - let mut parts = fen.split_ascii_whitespace(); + let mut parts = fen.split_whitespace(); trace!("{:?}", parts); // parse board @@ -91,8 +119,13 @@ impl Position { }); // parse active color - parts.next().unwrap(); - trace!("{:?}", parts); + pos.whites_turn = parts.next().unwrap().starts_with('w'); + + // parse castling + pos.castling = Castling::from_fen(parts.next().unwrap().to_string()); + + // parse en passent + pos.en_pessant = Square::en_passent_from_fen(parts.next().unwrap().to_string()); pos } diff --git a/src/chess/square.rs b/src/chess/square.rs index fbefcbd..f702320 100644 --- a/src/chess/square.rs +++ b/src/chess/square.rs @@ -1,5 +1,26 @@ +use log::{debug, trace}; + #[derive(Debug)] pub struct Square { x: u8, y: u8, } + +impl Square { + pub fn new(x: u8, y: u8) -> Self { + Square { x, y } + } +} + +impl Square { + pub fn en_passent_from_fen(fen: String) -> Option { + if fen == "-" { + None + } else { + Some(Square::new( + fen.chars().nth(0).unwrap() as u8 - 97, + fen.chars().nth(1).unwrap() as u8 - 49, + )) + } + } +} diff --git a/src/main.rs b/src/main.rs index 7f9840c..1427c17 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,6 +2,7 @@ dead_code, clippy::module_inception )] +#![feature(ascii_char)] use crate::{chess::position::Position, config::get_config};