diff --git a/maze/src/lib.rs b/maze/src/lib.rs index 59f6cfa..e936ab7 100644 --- a/maze/src/lib.rs +++ b/maze/src/lib.rs @@ -470,9 +470,42 @@ impl<'s> BufWriter<'s> { } } -pub struct BinaryMap; +#[derive(Debug, Clone, Copy, PartialEq)] +pub enum Direction { + North, + South, + East, + West, +} + +pub struct BinaryMap(pub [MazePart; SIZE]); impl BinaryMap { + pub fn new() -> Self { + Self([MazePart::Noop; SIZE]) + } + + pub fn at(&self, x: usize, y: usize) -> MazePart { + self.0[y * X + x] + } + + pub fn can_move(&self, player: (usize, usize), dir: Direction) -> bool { + match dir { + Direction::West if player.0 == 0 => false, + Direction::West => self.at(player.0 - 1, player.1) != MazePart::Wall, + Direction::East if player.0 == X - 1 => false, + Direction::East => self.at(player.0 + 1, player.1) != MazePart::Wall, + Direction::North if player.1 == 0 => false, + Direction::North => self.at(player.0, player.1 - 1) != MazePart::Wall, + Direction::South if player.1 == Y - 1 => false, + Direction::South => self.at(player.0, player.1 + 1) != MazePart::Wall, + } + } +} + +pub struct BinaryMapVisitor; + +impl BinaryMapVisitor { fn format(&self, grid: &Grid, buffer: &mut [MazePart]) { let mut bw = BufWriter::new(buffer); @@ -574,3 +607,88 @@ mod print_tests { println!("{result}"); } } + +#[cfg(test)] +mod bm_tests { + use crate::BinaryMap; + + #[test] + fn move_n_valid() { + let player = (1, 1); + let bm = BinaryMap::<3, 3, 9>::new(); + assert_eq!(bm.can_move(player, crate::Direction::North), true); + } + #[test] + fn move_n_invalid() { + let player = (0, 0); + let bm = BinaryMap::<3, 3, 9>::new(); + assert_eq!(bm.can_move(player, crate::Direction::North), false); + } + #[test] + fn move_n_invalid_wall() { + let player = (1, 1); + let mut bm = BinaryMap::<3, 3, 9>::new(); + bm.0.fill(crate::MazePart::Wall); + assert_eq!(bm.can_move(player, crate::Direction::North), false); + } + + #[test] + fn move_s_valid() { + let player = (1, 1); + let bm = BinaryMap::<3, 3, 9>::new(); + assert_eq!(bm.can_move(player, crate::Direction::South), true); + } + #[test] + fn move_s_invalid() { + let player = (2, 2); + let bm = BinaryMap::<3, 3, 9>::new(); + assert_eq!(bm.can_move(player, crate::Direction::South), false); + } + #[test] + fn move_s_invalid_wall() { + let player = (1, 1); + let mut bm = BinaryMap::<3, 3, 9>::new(); + bm.0.fill(crate::MazePart::Wall); + assert_eq!(bm.can_move(player, crate::Direction::South), false); + } + + #[test] + fn move_e_valid() { + let player = (1, 1); + let bm = BinaryMap::<3, 3, 9>::new(); + assert_eq!(bm.can_move(player, crate::Direction::East), true); + } + #[test] + fn move_e_invalid() { + let player = (2, 2); + let bm = BinaryMap::<3, 3, 9>::new(); + assert_eq!(bm.can_move(player, crate::Direction::East), false); + } + #[test] + fn move_e_invalid_wall() { + let player = (1, 1); + let mut bm = BinaryMap::<3, 3, 9>::new(); + bm.0.fill(crate::MazePart::Wall); + assert_eq!(bm.can_move(player, crate::Direction::East), false); + } + + #[test] + fn move_w_valid() { + let player = (1, 1); + let bm = BinaryMap::<3, 3, 9>::new(); + assert_eq!(bm.can_move(player, crate::Direction::West), true); + } + #[test] + fn move_w_invalid() { + let player = (0, 0); + let bm = BinaryMap::<3, 3, 9>::new(); + assert_eq!(bm.can_move(player, crate::Direction::West), false); + } + #[test] + fn move_w_invalid_wall() { + let player = (1, 1); + let mut bm = BinaryMap::<3, 3, 9>::new(); + bm.0.fill(crate::MazePart::Wall); + assert_eq!(bm.can_move(player, crate::Direction::West), false); + } +} diff --git a/src/main.rs b/src/main.rs index e3ff2cd..b4c3c23 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,6 +6,8 @@ use display_interface_spi::SPIInterface; use embedded_graphics::{ geometry::Point, mono_font::MonoTextStyle, + pixelcolor::Rgb565, + primitives::{Line, PrimitiveStyle}, text::{Text, TextStyle}, Drawable, }; @@ -23,7 +25,6 @@ use esp_hal::{ }; use heapless::String; use profont::PROFONT_24_POINT; -use shared::GenU32; use weact_studio_epd::{graphics::Display290BlackWhite, Color}; use weact_studio_epd::{graphics::DisplayRotation, WeActStudio290BlackWhiteDriver};