add optional 'custom_pieces' feature

This commit is contained in:
fabolous005 2024-04-03 17:36:54 +02:00
parent d75bbb4c6f
commit 0a2d572188
6 changed files with 160 additions and 21 deletions

View File

@ -3,6 +3,12 @@ name = "saltfish"
version = "0.1.0" version = "0.1.0"
edition = "2021" 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] [dependencies]
toml = { version = "0.8.12", optional = true }
serde = { version = "1.0.197", optional = true }
serde_derive = { version = "1.0.197", optional = true }

12
config.toml Normal file
View File

@ -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 ]

View File

@ -1,5 +1,8 @@
use super::{piece::PieceVariant, square::Square}; use super::{piece::PieceVariant, square::Square};
#[cfg(feature = "custom_pieces")]
use serde_derive::Deserialize;
pub struct Move { pub struct Move {
from: Square, from: Square,
to: 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 { pub struct Change {
x: i8, x: i8,
y: i8 y: i8
} }
pub enum MoveVariant { pub enum MoveVariant {
King([Change; 8]), King,
KingCastle([Change; 2]), KingCastle,
Queen([Change; 8]), Queen,
Rook([Change; 4]), Rook,
Bishop([Change; 4]), Bishop,
Knight([Change; 8]), Knight,
PawnWhite([Change; 1]), PawnWhite,
PawnWhiteDouble([Change; 1]), PawnWhiteDouble,
PawnWhiteEnPassent([Change; 2]), PawnWhiteEnPassent,
PawnWhiteCaptureLeft([Change; 1]), PawnWhiteCaptureLeft,
PawnWhiteCaptureRight([Change; 1]), PawnWhiteCaptureRight,
PawnBlack([Change; 1]), PawnBlack,
PawnBlackDouble([Change; 1]), PawnBlackDouble,
PawnBlackEnPassent([Change; 2]), PawnBlackEnPassent,
PawnBlackCaptureLeft([Change; 1]), PawnBlackCaptureLeft,
PawnBlackCaptureRight([Change; 1]), PawnBlackCaptureRight,
} }
impl MoveVariant { impl MoveVariant {
@ -147,4 +158,65 @@ impl MoveVariant {
pub fn pawn_black_capture_right() -> [Change; 1] { pub fn pawn_black_capture_right() -> [Change; 1] {
[ Change { x: -1, y: 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
}
} }

View File

@ -1,3 +1,8 @@
use super::moves::Change;
#[cfg(feature = "custom_pieces")]
use serde_derive::Deserialize;
pub enum PieceVariant { pub enum PieceVariant {
King, King,
Queen, Queen,
@ -8,6 +13,22 @@ pub enum PieceVariant {
} }
pub struct Piece { pub struct Piece {
pub white: bool, white: bool,
pub variant: PieceVariant 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<Change>,
iterable: Vec<Vec<u8>>
} }

View File

@ -3,8 +3,10 @@ use crate::chess::square::Square;
use crate::chess::moves::Move; use crate::chess::moves::Move;
use crate::chess::moves::MoveVariant; use crate::chess::moves::MoveVariant;
use super::moves::Change;
struct Castling {
pub struct Castling {
king_side: bool, king_side: bool,
queen_side: bool, queen_side: bool,
} }
@ -62,6 +64,8 @@ impl Default for Position {
} }
} }
/// Creating Position
impl Position { impl Position {
pub fn new() -> Self { pub fn new() -> Self {
Position::default() Position::default()
@ -70,8 +74,29 @@ impl Position {
pub fn from_fen(fen: String) -> Self { pub fn from_fen(fen: String) -> Self {
todo!("Implement this function") todo!("Implement this function")
} }
/*
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 { impl Position {
pub fn rows(self) -> [[Option<Piece>; 8]; 8] { pub fn rows(self) -> [[Option<Piece>; 8]; 8] {
self.rows self.rows
@ -90,9 +115,9 @@ impl Position {
} }
} }
/// Use position
impl Position { impl Position {
pub fn get_moves(self) -> Vec<Move> { pub fn get_moves(self) -> Vec<Move> {
let king_moves = MoveVariant::king();
todo!("finish") todo!("finish")
} }
} }

View File

@ -1,3 +1,6 @@
#![allow(dead_code)]
use chess::position::Position; use chess::position::Position;
mod chess; mod chess;