diff options
Diffstat (limited to 'model')
| -rw-r--r-- | model/Cargo.toml | 1 | ||||
| -rw-r--r-- | model/src/color.rs | 5 | ||||
| -rw-r--r-- | model/src/lib.rs | 2 | ||||
| -rw-r--r-- | model/src/moves.rs | 36 |
4 files changed, 34 insertions, 10 deletions
diff --git a/model/Cargo.toml b/model/Cargo.toml index 9f2402a..e732e99 100644 --- a/model/Cargo.toml +++ b/model/Cargo.toml @@ -9,7 +9,6 @@ publish = false [dependencies] serde = { version = "1", optional = true, features = ["derive"] } -rayon = "1" [dev-dependencies] proptest = "1" 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}") } } |
