mirror of
https://github.com/szymon-jozef/checkers.git
synced 2026-10-27 19:20:44 +01:00
chore: add DeltaPosition
This commit is contained in:
@@ -5,6 +5,8 @@
|
||||
rustfmt
|
||||
];
|
||||
|
||||
RUST_LOG = "debug";
|
||||
|
||||
inputsFrom = [
|
||||
(config.packages.default)
|
||||
];
|
||||
|
||||
@@ -1,8 +1,4 @@
|
||||
use crate::logic::{
|
||||
pawn::{self, Pawn, PawnState},
|
||||
player::Player,
|
||||
utils::{Field, Position},
|
||||
};
|
||||
use crate::logic::{field::Field, pawn::Pawn, player::Player, utils::Position};
|
||||
use std::ops::{Index, IndexMut};
|
||||
|
||||
use log::{debug, info, warn};
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
mod board;
|
||||
mod field;
|
||||
mod pawn;
|
||||
mod player;
|
||||
mod utils;
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
use core::fmt;
|
||||
|
||||
use crate::logic::pawn::Pawn;
|
||||
use log::debug;
|
||||
|
||||
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
|
||||
pub struct Position {
|
||||
@@ -14,22 +14,63 @@ impl fmt::Display for Position {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Eq)]
|
||||
pub struct Field {
|
||||
pub position: Position,
|
||||
pub pawn: Option<Pawn>,
|
||||
}
|
||||
impl Position {
|
||||
pub fn checked_add(&self, delta: &DeltaPosition) -> Option<Position> {
|
||||
let new_row = self.row.checked_add_signed(delta.row as isize)?;
|
||||
let new_column = self.column.checked_add_signed(delta.column as isize)?;
|
||||
debug!("Adding row: {} + {} = {}", self.row, delta.row, new_row);
|
||||
debug!(
|
||||
"Adding column: {} + {} = {}",
|
||||
self.column, delta.column, new_column
|
||||
);
|
||||
|
||||
impl fmt::Display for Field {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
if let Some(pawn) = &self.pawn {
|
||||
write!(
|
||||
f,
|
||||
"Field at pos: {}. Pawn here owned by: {}",
|
||||
self.position, pawn.owner
|
||||
)
|
||||
} else {
|
||||
write!(f, "Field at pos: {}. No pawn here.", self.position)
|
||||
}
|
||||
Some(Position {
|
||||
row: new_row,
|
||||
column: new_column,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// Vector for moving `Position`
|
||||
pub struct DeltaPosition {
|
||||
pub row: i8,
|
||||
pub column: i8,
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn init_logger() {
|
||||
let _ = env_logger::builder().is_test(true).try_init();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_adding_positions() {
|
||||
init_logger();
|
||||
|
||||
let pos1: Position = Position { row: 1, column: 1 };
|
||||
let delta_ok: DeltaPosition = DeltaPosition { row: 1, column: 1 };
|
||||
|
||||
let result1: Option<Position> = pos1.checked_add(&delta_ok);
|
||||
assert!(result1.is_some());
|
||||
assert_eq!(result1.unwrap(), Position { row: 2, column: 2 });
|
||||
|
||||
let delta_minus_ok: DeltaPosition = DeltaPosition {
|
||||
row: -1,
|
||||
column: -1,
|
||||
};
|
||||
|
||||
let result1: Option<Position> = pos1.checked_add(&delta_minus_ok);
|
||||
assert!(result1.is_some());
|
||||
assert_eq!(result1.unwrap(), Position { row: 0, column: 0 });
|
||||
|
||||
let delta_not_ok: DeltaPosition = DeltaPosition {
|
||||
row: -10,
|
||||
column: -10,
|
||||
};
|
||||
|
||||
let result3: Option<Position> = pos1.checked_add(&delta_not_ok);
|
||||
assert!(result3.is_none());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user