Column iter

This commit is contained in:
eraden 2024-09-16 18:26:56 +02:00
parent f650894988
commit e63383570e
2 changed files with 81 additions and 13 deletions

View File

@ -501,12 +501,63 @@ impl<const X: usize, const Y: usize, const SIZE: usize> BinaryMap<X, Y, SIZE> {
Direction::South => self.at(player.0, player.1 + 1) != MazePart::Wall, Direction::South => self.at(player.0, player.1 + 1) != MazePart::Wall,
} }
} }
pub fn lines(&self) -> impl Iterator<Item = &[MazePart]> {
self.0.windows(X)
}
/// 0 1 2
/// 0
/// 1
/// 2
///
/// Column 1 (0,0) (0,1) (0,2)
/// Column 2 (1,0) (1,1) (1,2)
pub fn columns(&self) -> ColumnIter<X, Y, MazePart> {
ColumnIter::<X, Y, MazePart>(&self.0, 0)
}
}
pub struct ColumnIter<'grid, const X: usize, const Y: usize, T>(&'grid [T], usize);
impl<'grid, const X: usize, const Y: usize, T: Clone> Iterator for ColumnIter<'grid, X, Y, T> {
type Item = [T; Y];
fn next(&mut self) -> Option<Self::Item> {
let x = self.1;
if x == X {
return None;
}
self.1 += 1;
Some(core::array::from_fn(move |y| self.0[x + (y * X)].clone()))
}
}
#[cfg(test)]
mod column_iter_tests {
use super::*;
#[test]
fn check() {
let grid = [
1, 2, 3, //
4, 5, 6, //
7, 8, 9, //
10, 11, 12, //
];
let mut it = ColumnIter::<3, 4, u8>(&grid, 0);
assert_eq!(it.next(), Some([1, 4, 7, 10]));
assert_eq!(it.next(), Some([2, 5, 8, 11]));
assert_eq!(it.next(), Some([3, 6, 9, 12]));
assert_eq!(it.next(), None);
}
} }
pub struct BinaryMapVisitor<const X: usize, const Y: usize, const SIZE: usize>; pub struct BinaryMapVisitor<const X: usize, const Y: usize, const SIZE: usize>;
impl<const X: usize, const Y: usize, const SIZE: usize> BinaryMapVisitor<X, Y, SIZE> { impl<const X: usize, const Y: usize, const SIZE: usize> BinaryMapVisitor<X, Y, SIZE> {
fn format(&self, grid: &Grid<X, Y, SIZE>, buffer: &mut [MazePart]) { pub fn format(&self, grid: &Grid<X, Y, SIZE>, buffer: &mut [MazePart]) {
let mut bw = BufWriter::new(buffer); let mut bw = BufWriter::new(buffer);
bw.push(MazePart::Wall); bw.push(MazePart::Wall);
@ -606,6 +657,15 @@ mod print_tests {
AsciiBroad.format(&mut grid, &mut result); AsciiBroad.format(&mut grid, &mut result);
println!("{result}"); println!("{result}");
} }
#[test]
fn production_maze_60_60() {
let mut trng = rand::thread_rng();
let mut grid = Grid::<60, 60, 3600>::new();
RecursiveDivision.generate(&mut grid, &mut trng);
let mut map = BinaryMap::<122, 122, 14884>::new();
BinaryMapVisitor.format(&mut grid, &mut map.0);
}
} }
#[cfg(test)] #[cfg(test)]

View File

@ -24,9 +24,10 @@ use esp_hal::{
system::SystemControl, system::SystemControl,
}; };
use heapless::String; use heapless::String;
use maze::{BinaryMap, BinaryMapVisitor};
use profont::PROFONT_24_POINT; use profont::PROFONT_24_POINT;
use weact_studio_epd::{graphics::Display290BlackWhite, Color}; use weact_studio_epd::{graphics::Display290TriColor, TriColor};
use weact_studio_epd::{graphics::DisplayRotation, WeActStudio290BlackWhiteDriver}; use weact_studio_epd::{graphics::DisplayRotation, WeActStudio290TriColorDriver};
#[entry] #[entry]
fn main() -> ! { fn main() -> ! {
@ -76,12 +77,12 @@ fn main() -> ! {
// Setup EPD // Setup EPD
log::info!("Intializing EPD..."); log::info!("Intializing EPD...");
let mut driver = WeActStudio290BlackWhiteDriver::new(spi_interface, busy, rst, delay); let mut driver = WeActStudio290TriColorDriver::new(spi_interface, busy, rst, delay);
let mut display = Display290BlackWhite::new(); let mut display = Display290TriColor::new();
display.set_rotation(DisplayRotation::Rotate90); display.set_rotation(DisplayRotation::Rotate90);
driver.init().unwrap(); driver.init().unwrap();
let style = MonoTextStyle::new(&PROFONT_24_POINT, Color::Black); let style = MonoTextStyle::new(&PROFONT_24_POINT, TriColor::Black);
let _ = Text::with_text_style( let _ = Text::with_text_style(
"Hello World!", "Hello World!",
Point::new(8, 68), Point::new(8, 68),
@ -101,7 +102,7 @@ fn main() -> ! {
log::info!("Wake up!"); log::info!("Wake up!");
driver.wake_up().unwrap(); driver.wake_up().unwrap();
display.clear(Color::White); display.clear(TriColor::White);
let mut string_buf = String::<30>::new(); let mut string_buf = String::<30>::new();
write!(string_buf, "Hello World {}!", n).unwrap(); write!(string_buf, "Hello World {}!", n).unwrap();
@ -121,15 +122,22 @@ fn main() -> ! {
} }
} }
#[derive(Debug, Default)]
struct MazeGame { struct MazeGame {
maze: Option<maze::Grid<100, 100, 10_000>>, map: maze::BinaryMap<122, 122, 14884>,
} }
impl MazeGame { impl MazeGame {
pub fn new() -> Self {
Self {
map: maze::BinaryMap::new(),
}
}
pub fn start(&mut self, trng: &mut Trng) { pub fn start(&mut self, trng: &mut Trng) {
let mut maze = maze::Grid::new(); let mut grid = maze::Grid::<60, 60, 3600>::new();
maze::RecursiveDivision.generate(&mut maze, trng); maze::RecursiveDivision.generate(&mut grid, trng);
self.maze = Some(maze); BinaryMapVisitor.format(&mut grid, &mut self.map.0);
} }
pub fn draw(&self, dispay: Display290TriColor) {}
} }