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
|
rustfmt
|
||||||
];
|
];
|
||||||
|
|
||||||
|
RUST_LOG = "debug";
|
||||||
|
|
||||||
inputsFrom = [
|
inputsFrom = [
|
||||||
(config.packages.default)
|
(config.packages.default)
|
||||||
];
|
];
|
||||||
|
|||||||
@@ -1,8 +1,4 @@
|
|||||||
use crate::logic::{
|
use crate::logic::{field::Field, pawn::Pawn, player::Player, utils::Position};
|
||||||
pawn::{self, Pawn, PawnState},
|
|
||||||
player::Player,
|
|
||||||
utils::{Field, Position},
|
|
||||||
};
|
|
||||||
use std::ops::{Index, IndexMut};
|
use std::ops::{Index, IndexMut};
|
||||||
|
|
||||||
use log::{debug, info, warn};
|
use log::{debug, info, warn};
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
mod board;
|
mod board;
|
||||||
|
mod field;
|
||||||
mod pawn;
|
mod pawn;
|
||||||
mod player;
|
mod player;
|
||||||
mod utils;
|
mod utils;
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
use core::fmt;
|
use core::fmt;
|
||||||
|
|
||||||
use crate::logic::pawn::Pawn;
|
use log::debug;
|
||||||
|
|
||||||
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
|
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
|
||||||
pub struct Position {
|
pub struct Position {
|
||||||
@@ -14,22 +14,63 @@ impl fmt::Display for Position {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Eq)]
|
impl Position {
|
||||||
pub struct Field {
|
pub fn checked_add(&self, delta: &DeltaPosition) -> Option<Position> {
|
||||||
pub position: Position,
|
let new_row = self.row.checked_add_signed(delta.row as isize)?;
|
||||||
pub pawn: Option<Pawn>,
|
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 {
|
Some(Position {
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
row: new_row,
|
||||||
if let Some(pawn) = &self.pawn {
|
column: new_column,
|
||||||
write!(
|
})
|
||||||
f,
|
}
|
||||||
"Field at pos: {}. Pawn here owned by: {}",
|
}
|
||||||
self.position, pawn.owner
|
|
||||||
)
|
/// Vector for moving `Position`
|
||||||
} else {
|
pub struct DeltaPosition {
|
||||||
write!(f, "Field at pos: {}. No pawn here.", self.position)
|
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