finish parsing mechanism
Some checks failed
CI / Saltfish (push) Failing after -2m49s
CI / Saltfish [custom_pieces] (push) Failing after -2m49s

This commit is contained in:
fabolous005 2024-04-14 22:58:46 +02:00
parent 2d4f6981ba
commit b8566bb223
3 changed files with 60 additions and 5 deletions

View File

@ -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<Piece>; 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
}

View File

@ -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<Self> {
if fen == "-" {
None
} else {
Some(Square::new(
fen.chars().nth(0).unwrap() as u8 - 97,
fen.chars().nth(1).unwrap() as u8 - 49,
))
}
}
}

View File

@ -2,6 +2,7 @@
dead_code,
clippy::module_inception
)]
#![feature(ascii_char)]
use crate::{chess::position::Position, config::get_config};