mirror of
https://github.com/szymon-jozef/checkers.git
synced 2026-10-28 11:10:44 +01:00
docs: write documentation for Position and Vector
This commit is contained in:
@@ -166,6 +166,7 @@ impl Board {
|
||||
let player_direction = player
|
||||
.vertical_direction
|
||||
.expect("Player vertical_direction not set!");
|
||||
|
||||
delta_distance.row == player_direction.row
|
||||
&& delta_distance.column.abs() == 1
|
||||
&& to
|
||||
|
||||
@@ -6,6 +6,13 @@ use log::debug;
|
||||
use crate::logic::math::vector::Vector2D;
|
||||
|
||||
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
|
||||
/// Position on the board. Consists of row and column. Should be treated like a point in space on
|
||||
/// unsigned grid.
|
||||
///
|
||||
/// Has:
|
||||
/// - checked_add for adding with `Vector2d`
|
||||
/// - is_in_range for checking if position fits on the board
|
||||
/// - `Position` - `Position` = `Vector2D` operator overloaded
|
||||
pub struct Position {
|
||||
pub row: usize,
|
||||
pub column: usize,
|
||||
@@ -48,3 +55,41 @@ impl Sub for Position {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::logic::math::{position::Position, vector::Vector2D};
|
||||
|
||||
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: Vector2D = Vector2D { 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: Vector2D = Vector2D {
|
||||
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: Vector2D = Vector2D {
|
||||
row: -10,
|
||||
column: -10,
|
||||
};
|
||||
|
||||
let result3: Option<Position> = pos1.checked_add(&delta_not_ok);
|
||||
assert!(result3.is_none());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,11 @@
|
||||
use core::fmt;
|
||||
use std::ops::{Add, Mul, Sub};
|
||||
|
||||
/// Vector for moving `Position`
|
||||
/// 2D Vector consisting of row and column of signed types.
|
||||
///
|
||||
/// Has:
|
||||
/// - display trait
|
||||
/// - `+` | `-` | `*<usize>` overloaded
|
||||
#[derive(Debug, Copy, Clone)]
|
||||
pub struct Vector2D {
|
||||
pub row: i8,
|
||||
@@ -36,6 +40,7 @@ impl Sub for Vector2D {
|
||||
}
|
||||
}
|
||||
|
||||
/// Multiplying by a scalar
|
||||
impl Mul<usize> for Vector2D {
|
||||
type Output = Vector2D;
|
||||
|
||||
@@ -46,41 +51,3 @@ impl Mul<usize> for Vector2D {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::logic::math::{position::Position, vector::Vector2D};
|
||||
|
||||
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: Vector2D = Vector2D { 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: Vector2D = Vector2D {
|
||||
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: Vector2D = Vector2D {
|
||||
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