From 701197df3f44465648e68a3c4916b8447109fe71 Mon Sep 17 00:00:00 2001 From: c0repwn3r Date: Wed, 15 Feb 2023 16:37:35 -0500 Subject: [PATCH] make move traits/structs/etc --- bamboo/src/error.rs | 15 ++++++++++ bamboo/src/lib.rs | 3 +- bamboo/src/moves.rs | 73 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 90 insertions(+), 1 deletion(-) create mode 100644 bamboo/src/moves.rs diff --git a/bamboo/src/error.rs b/bamboo/src/error.rs index 9aed6ae..50b85e2 100644 --- a/bamboo/src/error.rs +++ b/bamboo/src/error.rs @@ -104,4 +104,19 @@ impl Display for AlgebraicNotationError { Self::InvalidLength { got } => write!(f, "Invalid length, expected 2, got `{got}`") } } +} + +#[derive(Debug)] +/// Represents an error that occured making or unmaking an error. +pub enum MoveApplicationError { + +} +impl Error for MoveApplicationError {} +impl Display for MoveApplicationError { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + return Ok(()); + /*match self { + + }*/ + } } \ No newline at end of file diff --git a/bamboo/src/lib.rs b/bamboo/src/lib.rs index 90ccd89..d636eb7 100644 --- a/bamboo/src/lib.rs +++ b/bamboo/src/lib.rs @@ -34,4 +34,5 @@ pub mod board; #[macro_use] pub mod utils; pub mod nn; -pub mod error; \ No newline at end of file +pub mod error; +pub mod moves; \ No newline at end of file diff --git a/bamboo/src/moves.rs b/bamboo/src/moves.rs new file mode 100644 index 0000000..2d48676 --- /dev/null +++ b/bamboo/src/moves.rs @@ -0,0 +1,73 @@ +// Bamboo, a NNUE chess engine written in Rust. +// Copyright (C) 2023 c0repwn3r +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +//! Traits and functions to make and unmake moves on a chessboard. + +use crate::error::MoveApplicationError; +use crate::piece::{PieceColor, PieceOnBoard, PieceType}; + +/// Represents a move on a chessboard. +pub enum Move { + /// Represents a linear move, from A to B on the chessboard. No capture. + Linear { + /// The boardloc the piece is coming from. + from: isize, + /// The boardloc the piece is going to. + to: isize, + /// The `PieceOnBoard` struct of the piece BEFORE the move. + piece: PieceOnBoard + }, + /// Represents a linear from, move A to B on the chessboard, and capturing a piece. + LinearCapture { + /// The boardloc the piece is coming from. + from: isize, + /// The boardloc the piece is going to. + to: isize, + /// The `PieceOnBoard` struct of the piece BEFORE the move. + piece: PieceOnBoard, + /// The `PieceOnBoard` struct of the piece being captured, BEFORE the move. + capturing: PieceOnBoard + }, + /// Represents a castle, either white or black, kingside or queenside. + Castle { + /// The player that is performing the castle + player: PieceColor, + /// Which direction that player is castling, kingside or queenside. + queenside: bool + }, + /// Represents a pawn promotion. + Promotion { + /// The pawn that is being promoted, BEFORE the move + piece: PieceOnBoard, + /// The location of the pawn, BEFORE the move + from: isize, + /// The location of the pawn, AFTER the move + to: isize, + /// The type of piece the pawn is promoting to. + becoming: PieceType + } +} + +/// A trait to provide the operations of making and unmaking moves. +pub trait MoveApplicator { + /// Perform the given move on the chessboard. + /// # Errors + /// This function will error if any error occurs while applying the move. + fn make_move(&mut self, make: Move) -> Result<(), MoveApplicationError>; + /// Undo the given move on the chessboard. This will probably break if that move wasn't the last move made. + /// # Errors + /// This function will error if any error occurs while unmaking the move. + fn unmake_move(&mut self, unmake: Move) -> Result<(), MoveApplicationError>; +} \ No newline at end of file