From 923aeb11d61b6e20ad33598ef7f44d0a6dfa6c15 Mon Sep 17 00:00:00 2001 From: Micha White Date: Thu, 28 Dec 2023 10:26:06 -0500 Subject: Changes made to support the eventual C API --- model/src/color.rs | 5 +++-- model/src/lib.rs | 2 +- model/src/moves.rs | 36 ++++++++++++++++++++++++++++++------ 3 files changed, 34 insertions(+), 9 deletions(-) (limited to 'model/src') diff --git a/model/src/color.rs b/model/src/color.rs index 26a1069..8a4d2a5 100644 --- a/model/src/color.rs +++ b/model/src/color.rs @@ -5,9 +5,10 @@ use std::fmt::Display; /// The color of a piece #[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] +#[repr(C)] pub enum PieceColor { - Light, - Dark, + Light = 0, + Dark = 1, } impl Display for PieceColor { diff --git a/model/src/lib.rs b/model/src/lib.rs index 61ab2cc..b3d8007 100644 --- a/model/src/lib.rs +++ b/model/src/lib.rs @@ -8,6 +8,6 @@ mod possible_moves; pub use board::CheckersBitBoard; pub use color::PieceColor; pub use coordinates::SquareCoordinate; -pub use moves::Move; +pub use moves::{Move, MoveDirection}; pub use piece::Piece; pub use possible_moves::PossibleMoves; diff --git a/model/src/moves.rs b/model/src/moves.rs index c266f77..c840e8f 100644 --- a/model/src/moves.rs +++ b/model/src/moves.rs @@ -2,6 +2,7 @@ use crate::{CheckersBitBoard, SquareCoordinate}; use std::fmt::{Display, Formatter}; #[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)] +#[repr(C)] pub enum MoveDirection { ForwardLeft = 0, ForwardRight = 1, @@ -72,6 +73,21 @@ impl Move { dest as usize } + /// Calculates the value of the position that was jumped over + /// + /// # Safety + /// + /// The result of this function is undefined if the move isn't a jump + pub const unsafe fn jump_position(self) -> usize { + let pos = match self.direction() { + MoveDirection::ForwardLeft => (self.start() + 7) % 32, + MoveDirection::ForwardRight => (self.start() + 1) % 32, + MoveDirection::BackwardLeft => self.start().wrapping_sub(1) % 32, + MoveDirection::BackwardRight => self.start().wrapping_sub(7) % 32, + }; + pos as usize + } + /// Apply the move to a board. This does not mutate the original board, /// but instead returns a new one. /// @@ -128,12 +144,20 @@ impl Move { impl Display for Move { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { - write!( - f, - "{}-{}", - SquareCoordinate::from_ampere_value(self.start() as usize), - SquareCoordinate::from_ampere_value(self.end_position()) - ) + let Some(start) = + SquareCoordinate::from_ampere_value(self.start() as usize).to_normal_value() + else { + return Err(std::fmt::Error); + }; + + let separator = if self.is_jump() { "x" } else { "-" }; + + let Some(end) = SquareCoordinate::from_ampere_value(self.end_position()).to_normal_value() + else { + return Err(std::fmt::Error); + }; + + write!(f, "{start}{separator}{end}") } } -- cgit v1.2.3