finish parsing mechanism
This commit is contained in:
parent
2d4f6981ba
commit
b8566bb223
@ -1,15 +1,43 @@
|
|||||||
use crate::chess::piece::Piece;
|
use crate::chess::piece::Piece;
|
||||||
use crate::chess::square::Square;
|
use crate::chess::square::Square;
|
||||||
use crate::chess::moves::Move;
|
use crate::chess::moves::Move;
|
||||||
use log::trace;
|
use log::{debug, trace};
|
||||||
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug, Default)]
|
||||||
pub struct Castling {
|
pub struct Castling {
|
||||||
king_side: bool,
|
king_side: bool,
|
||||||
queen_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)]
|
#[derive(Debug)]
|
||||||
pub struct Position {
|
pub struct Position {
|
||||||
rows: [[Option<Piece>; 8]; 8],
|
rows: [[Option<Piece>; 8]; 8],
|
||||||
@ -74,7 +102,7 @@ impl Position {
|
|||||||
pub fn from_fen(fen: String) -> Self {
|
pub fn from_fen(fen: String) -> Self {
|
||||||
trace!("{fen}");
|
trace!("{fen}");
|
||||||
let mut pos = Position::default();
|
let mut pos = Position::default();
|
||||||
let mut parts = fen.split_ascii_whitespace();
|
let mut parts = fen.split_whitespace();
|
||||||
|
|
||||||
trace!("{:?}", parts);
|
trace!("{:?}", parts);
|
||||||
// parse board
|
// parse board
|
||||||
@ -91,8 +119,13 @@ impl Position {
|
|||||||
});
|
});
|
||||||
|
|
||||||
// parse active color
|
// parse active color
|
||||||
parts.next().unwrap();
|
pos.whites_turn = parts.next().unwrap().starts_with('w');
|
||||||
trace!("{:?}", parts);
|
|
||||||
|
// 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
|
pos
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -1,5 +1,26 @@
|
|||||||
|
use log::{debug, trace};
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct Square {
|
pub struct Square {
|
||||||
x: u8,
|
x: u8,
|
||||||
y: 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,
|
dead_code,
|
||||||
clippy::module_inception
|
clippy::module_inception
|
||||||
)]
|
)]
|
||||||
|
#![feature(ascii_char)]
|
||||||
|
|
||||||
|
|
||||||
use crate::{chess::position::Position, config::get_config};
|
use crate::{chess::position::Position, config::get_config};
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user