From 0a2d57218804fbff4edb3becbab394b8c79aee4e Mon Sep 17 00:00:00 2001 From: fabolous005 Date: Wed, 3 Apr 2024 17:36:54 +0200 Subject: [PATCH] add optional 'custom_pieces' feature --- Cargo.toml | 8 +++- config.toml | 12 +++++ src/chess/moves.rs | 104 +++++++++++++++++++++++++++++++++++------- src/chess/piece.rs | 25 +++++++++- src/chess/position.rs | 29 +++++++++++- src/main.rs | 3 ++ 6 files changed, 160 insertions(+), 21 deletions(-) create mode 100644 config.toml diff --git a/Cargo.toml b/Cargo.toml index ac26632..ffccbf8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,6 +3,12 @@ name = "saltfish" version = "0.1.0" edition = "2021" -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[features] +custom_pieces = ["dep:toml", "dep:serde", "dep:serde_derive"] + [dependencies] +toml = { version = "0.8.12", optional = true } +serde = { version = "1.0.197", optional = true } +serde_derive = { version = "1.0.197", optional = true } diff --git a/config.toml b/config.toml new file mode 100644 index 0000000..71b388e --- /dev/null +++ b/config.toml @@ -0,0 +1,12 @@ +[[piece]] +[piece.turtle] +change = [ + [ 1, 2 ], + [ 2, 1 ] +] + +# INFO: when one: number of must-moves +# if 0 infinite +# INFO: when two: [0] = min, [1] = max +iterable = [ 1, 2 ] + diff --git a/src/chess/moves.rs b/src/chess/moves.rs index cc75235..721afe0 100644 --- a/src/chess/moves.rs +++ b/src/chess/moves.rs @@ -1,5 +1,8 @@ use super::{piece::PieceVariant, square::Square}; +#[cfg(feature = "custom_pieces")] +use serde_derive::Deserialize; + pub struct Move { from: Square, to: Square, @@ -20,28 +23,36 @@ impl Move { } } +#[cfg(feature = "custom_pieces")] +#[derive(Debug, Deserialize)] +pub struct Change { + x: i8, + y: i8 +} + +#[cfg(not(feature = "custom_pieces"))] pub struct Change { x: i8, y: i8 } pub enum MoveVariant { - King([Change; 8]), - KingCastle([Change; 2]), - Queen([Change; 8]), - Rook([Change; 4]), - Bishop([Change; 4]), - Knight([Change; 8]), - PawnWhite([Change; 1]), - PawnWhiteDouble([Change; 1]), - PawnWhiteEnPassent([Change; 2]), - PawnWhiteCaptureLeft([Change; 1]), - PawnWhiteCaptureRight([Change; 1]), - PawnBlack([Change; 1]), - PawnBlackDouble([Change; 1]), - PawnBlackEnPassent([Change; 2]), - PawnBlackCaptureLeft([Change; 1]), - PawnBlackCaptureRight([Change; 1]), + King, + KingCastle, + Queen, + Rook, + Bishop, + Knight, + PawnWhite, + PawnWhiteDouble, + PawnWhiteEnPassent, + PawnWhiteCaptureLeft, + PawnWhiteCaptureRight, + PawnBlack, + PawnBlackDouble, + PawnBlackEnPassent, + PawnBlackCaptureLeft, + PawnBlackCaptureRight, } impl MoveVariant { @@ -147,4 +158,65 @@ impl MoveVariant { pub fn pawn_black_capture_right() -> [Change; 1] { [ Change { x: -1, y: 1 } ] } + + + pub fn number_king() -> u8 { + 8 + } + + pub fn number_queen() -> u8 { + 8 + } + + pub fn number_rook() -> u8 { + 4 + } + + pub fn number_bishop() -> u8 { + 4 + } + + pub fn number_knight() -> u8 { + 8 + } + + pub fn number_pawn_white() -> u8 { + 1 + } + + pub fn number_pawn_white_double() -> u8 { + 1 + } + + pub fn number_pawn_white_en_passent() -> u8 { + 2 + } + + pub fn number_pawn_white_capture_left() -> u8 { + 1 + } + + pub fn number_pawn_white_capture_right() -> u8 { + 1 + } + + pub fn number_pawn_black() -> u8 { + 1 + } + + pub fn number_pawn_black_double() -> u8 { + 1 + } + + pub fn number_pawn_black_en_passent() -> u8 { + 2 + } + + pub fn number_pawn_black_capture_left() -> u8 { + 1 + } + + pub fn number_pawn_black_capture_right() -> u8 { + 1 + } } diff --git a/src/chess/piece.rs b/src/chess/piece.rs index 2256de2..13c61a6 100644 --- a/src/chess/piece.rs +++ b/src/chess/piece.rs @@ -1,3 +1,8 @@ +use super::moves::Change; + +#[cfg(feature = "custom_pieces")] +use serde_derive::Deserialize; + pub enum PieceVariant { King, Queen, @@ -8,6 +13,22 @@ pub enum PieceVariant { } pub struct Piece { - pub white: bool, - pub variant: PieceVariant + white: bool, + variant: PieceVariant +} + + +#[cfg(feature = "custom_pieces")] +enum Iterablity { + Infinite(u8), + ForcedNum(u8), + Range([u8; 2]) +} + + +#[cfg(feature = "custom_pieces")] +#[derive(Debug, Deserialize)] +struct CustomPiece { + change: Vec, + iterable: Vec> } diff --git a/src/chess/position.rs b/src/chess/position.rs index 48f1c8f..fbea0cb 100644 --- a/src/chess/position.rs +++ b/src/chess/position.rs @@ -3,8 +3,10 @@ use crate::chess::square::Square; use crate::chess::moves::Move; use crate::chess::moves::MoveVariant; +use super::moves::Change; -struct Castling { + +pub struct Castling { king_side: bool, queen_side: bool, } @@ -62,6 +64,8 @@ impl Default for Position { } } + +/// Creating Position impl Position { pub fn new() -> Self { Position::default() @@ -70,8 +74,29 @@ impl Position { pub fn from_fen(fen: String) -> Self { todo!("Implement this function") } + + /* + TODO: Decide if Position should be mutable or not + pub fn set_rows(mut self, rows: [[Option; 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) -> Self { + self.en_pessant = en_passent; + self + } + */ } +/// Exposing position fields impl Position { pub fn rows(self) -> [[Option; 8]; 8] { self.rows @@ -90,9 +115,9 @@ impl Position { } } +/// Use position impl Position { pub fn get_moves(self) -> Vec { - let king_moves = MoveVariant::king(); todo!("finish") } } diff --git a/src/main.rs b/src/main.rs index 0e09b75..6dd5364 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,6 @@ +#![allow(dead_code)] + + use chess::position::Position; mod chess;