Parse FEN string into game state

This commit is contained in:
2024-05-10 17:42:11 +03:00
parent 69946f09be
commit c00c6a7b15
4 changed files with 298 additions and 17 deletions

View File

@@ -1,14 +1,86 @@
use crate::board::Board;
use crate::{
board::{Board, Color},
fen::from_fen,
};
pub struct Game {}
#[derive(Debug, PartialEq)]
pub struct Game {
pub board: Board,
pub state: State,
}
impl Game {
pub fn new() -> Game {
Game {}
Game {
board: Board::new(),
state: State::new(),
}
}
pub fn new_from_fen(fen: &str) -> Game {
from_fen(fen)
}
pub fn run(&self) {
let board = Board::new();
println!("{}", board)
Board::new();
}
}
impl Default for Game {
fn default() -> Self {
Self::new()
}
}
#[derive(Debug, PartialEq)]
pub struct State {
side_to_move: Color,
castling_ability: u8,
en_passant_target_square: u8,
halfmove_clock: u8,
fullmove_counter: u8,
}
impl State {
pub fn new() -> State {
State {
side_to_move: Color::White,
castling_ability: 0b1111,
en_passant_target_square: 0,
halfmove_clock: 0,
fullmove_counter: 1,
}
}
pub fn load_state(
side_to_move: Color,
castling_ability: u8,
en_passant_target_square: u8,
halfmove_clock: u8,
fullmove_counter: u8,
) -> State {
State {
side_to_move,
castling_ability,
en_passant_target_square,
halfmove_clock,
fullmove_counter,
}
}
}
use std::fmt;
impl fmt::Display for State {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
writeln!(f,
"side_to_move: {:?}\ncastling_ability: {:b}\nen_passant_target_square: {:b}\nhalfmove_clock: {}\nfullmove_counter: {}\n",
self.side_to_move, self.castling_ability, 1u64 << self.en_passant_target_square, self.halfmove_clock, self.fullmove_counter)
}
}
impl Default for State {
fn default() -> Self {
Self::new()
}
}