saltfish/src/chess/position.rs
fabolous005 2d4f6981ba
All checks were successful
CI / Saltfish (push) Successful in -2m50s
CI / Saltfish [custom_pieces] (push) Successful in -2m49s
add test position
2024-04-13 16:45:49 +02:00

145 lines
4.0 KiB
Rust

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<Piece>; 8]; 8],
whites_turn: bool,
castling: [Castling; 2],
en_pessant: Option<Square>
}
// |-----------------------------------------------|
// | 0;0 | 1;0 | 2;0 | 3;0 | 4;0 | 5;0 | 6;0 | 7;0 |
// |-----------------------------------------------|
// | 0;1 | 1;1 | 2;1 | 3;1 | 4;1 | 5;1 | 6;1 | 7;1 | Black
// |-----------------------------------------------|
// | 0;2 | 1;2 | 2;2 | 3;2 | 4;2 | 5;2 | 6;2 | 7;2 |
// |-----------------------------------------------|
// | 0;3 | 1;3 | 2;3 | 3;3 | 4;3 | 5;3 | 6;3 | 7;3 |
// |-----------------------------------------------|
// | 0;4 | 1;4 | 2;4 | 3;4 | 4;4 | 5;4 | 6;4 | 7;4 |
// |-----------------------------------------------|
// | 0;5 | 1;5 | 2;5 | 3;5 | 4;5 | 5;5 | 6;5 | 7;5 |
// |-----------------------------------------------|
// | 0;6 | 1;6 | 2;6 | 3;6 | 4;6 | 5;6 | 6;6 | 7;6 | White
// |-----------------------------------------------|
// | 0;7 | 1;7 | 2;7 | 3;7 | 4;7 | 5;7 | 6;7 | 7;7 |
// |-----------------------------------------------|
// Chess board repesented in code
//
// NOTE: capture left/right is implemented relative to color
// TODO: make this a doc comment
impl Default for Position {
fn default() -> Self {
Position {
rows: [
[None, None, None, None, None, None, None, None],
[None, None, None, None, None, None, None, None],
[None, None, None, None, None, None, None, None],
[None, None, None, None, None, None, None, None],
[None, None, None, None, None, None, None, None],
[None, None, None, None, None, None, None, None],
[None, None, None, None, None, None, None, None],
[None, None, None, None, None, None, None, None],
],
whites_turn: true,
castling: [
Castling { king_side: false, queen_side: false },
Castling { king_side: false, queen_side: false },
],
en_pessant: None
}
}
}
/// Creating Position
impl Position {
pub fn new() -> Self {
Position::default()
}
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
}
/*
TODO: Decide if Position should be mutable or not
pub fn set_rows(mut self, rows: [[Option<Piece>; 8]; 8]) -> Self {
self.rows = rows;
self
}
pub fn set_whites_turn(mut self, whites_turn: bool) -> Self {
self.whites_turn = whites_turn;
self
}
pub fn set_castling(mut self, castling: [Castling; 2]) -> Self {
self.castling = castling;
self
}
pub fn set_en_passent(mut self, en_passent: Option<Square>) -> Self {
self.en_pessant = en_passent;
self
}
*/
}
/// Exposing position fields
impl Position {
pub fn rows(self) -> [[Option<Piece>; 8]; 8] {
self.rows
}
pub fn whites_turn(self) -> bool {
self.whites_turn
}
pub fn castling(self) -> [Castling; 2] {
self.castling
}
pub fn en_passent(self) -> Option<Square> {
self.en_pessant
}
}
/// Use position
impl Position {
pub fn get_moves(self) -> Vec<Move> {
todo!("finish")
}
}