summaryrefslogtreecommitdiff
path: root/model
diff options
context:
space:
mode:
authorMicha White <botahamec@outlook.com>2023-12-28 10:26:06 -0500
committerMicha White <botahamec@outlook.com>2023-12-28 10:26:06 -0500
commit923aeb11d61b6e20ad33598ef7f44d0a6dfa6c15 (patch)
tree03c2139625f32257b2ac3c246ae623e8fdfce16f /model
parentef59ee8eb6562dfd724c1cb0bbd37aebc7d798ad (diff)
Changes made to support the eventual C API
Diffstat (limited to 'model')
-rw-r--r--model/Cargo.toml1
-rw-r--r--model/src/color.rs5
-rw-r--r--model/src/lib.rs2
-rw-r--r--model/src/moves.rs36
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}")
}
}