finish parsing mechanism
This commit is contained in:
parent
2d4f6981ba
commit
b8566bb223
@ -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
|
||||
}
|
||||
|
||||
|
||||
@ -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,
|
||||
))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -2,6 +2,7 @@
|
||||
dead_code,
|
||||
clippy::module_inception
|
||||
)]
|
||||
#![feature(ascii_char)]
|
||||
|
||||
|
||||
use crate::{chess::position::Position, config::get_config};
|
||||
|
||||
Loading…
Reference in New Issue
Block a user