diff --git a/src/chess/piece.rs b/src/chess/piece.rs index f7414d4..674c6cc 100644 --- a/src/chess/piece.rs +++ b/src/chess/piece.rs @@ -2,6 +2,7 @@ #[cfg(feature = "custom_pieces")] use serde_derive::Deserialize; +#[derive(Debug)] pub enum PieceVariant { King, Queen, @@ -11,11 +12,36 @@ pub enum PieceVariant { Pawn } +#[derive(Debug)] pub struct Piece { white: bool, variant: PieceVariant } +impl Piece { + pub fn new(white: bool, variant: PieceVariant) -> Self { + Self { white, variant } + } + + pub fn from_fen(c: char) -> Option { + match c { + 'K' => Some(Self::new(true, PieceVariant::King)), + 'Q' => Some(Self::new(true, PieceVariant::Queen)), + 'R' => Some(Self::new(true, PieceVariant::Rook)), + 'B' => Some(Self::new(true, PieceVariant::Bishop)), + 'N' => Some(Self::new(true, PieceVariant::Knight)), + 'P' => Some(Self::new(true, PieceVariant::Pawn)), + 'k' => Some(Self::new(false, PieceVariant::King)), + 'q' => Some(Self::new(false, PieceVariant::Queen)), + 'r' => Some(Self::new(false, PieceVariant::Rook)), + 'b' => Some(Self::new(false, PieceVariant::Bishop)), + 'n' => Some(Self::new(false, PieceVariant::Knight)), + 'p' => Some(Self::new(false, PieceVariant::Pawn)), + _ => None + } + } +} + #[cfg(feature = "custom_pieces")] #[derive(Debug, Deserialize)] diff --git a/src/chess/position.rs b/src/chess/position.rs index d2a42d1..6b5f909 100644 --- a/src/chess/position.rs +++ b/src/chess/position.rs @@ -1,13 +1,16 @@ use crate::chess::piece::Piece; use crate::chess::square::Square; use crate::chess::moves::Move; +use log::trace; +#[derive(Debug)] pub struct Castling { king_side: bool, queen_side: bool, } +#[derive(Debug)] pub struct Position { rows: [[Option; 8]; 8], whites_turn: bool, @@ -68,8 +71,29 @@ impl Position { Position::default() } - pub fn from_fen(_fen: String) -> Self { - todo!("Implement this function") + pub fn from_fen(fen: String) -> Self { + trace!("{fen}"); + let mut pos = Position::default(); + let mut parts = fen.split_ascii_whitespace(); + + trace!("{:?}", parts); + // parse board + parts.next().unwrap().split('/').enumerate().for_each(|(y, row)| { + let mut x = 0; + row.chars().for_each(|c| { + if c.is_ascii_digit() { + x += c.to_digit(10).unwrap(); + } else { + pos.rows[y][x as usize] = Piece::from_fen(c); + x += 1; + } + }); + }); + + // parse active color + parts.next().unwrap(); + trace!("{:?}", parts); + pos } /* diff --git a/src/chess/square.rs b/src/chess/square.rs index 019cf76..fbefcbd 100644 --- a/src/chess/square.rs +++ b/src/chess/square.rs @@ -1,3 +1,4 @@ +#[derive(Debug)] pub struct Square { x: u8, y: u8, diff --git a/src/main.rs b/src/main.rs index 557de10..7f9840c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,7 +4,7 @@ )] -use crate::config::get_config; +use crate::{chess::position::Position, config::get_config}; use log::trace; mod chess; @@ -16,4 +16,7 @@ mod logging; fn main() { let config = get_config().unwrap(); trace!("{:?}", config); + + let board = Position::from_fen("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1".to_string()); + trace!("{:?}", board); }