From 838116ae3ff734d9441a56e5b99b98716b1567cc Mon Sep 17 00:00:00 2001 From: Kristofers Solo Date: Wed, 6 Nov 2024 13:23:10 +0200 Subject: [PATCH] feat(maze): create with radius --- src/maze.rs | 84 +++++++++++++++++++++++++++++++++++++++++------------ src/tile.rs | 9 ++++++ 2 files changed, 75 insertions(+), 18 deletions(-) diff --git a/src/maze.rs b/src/maze.rs index 8d3ab03..14c5095 100644 --- a/src/maze.rs +++ b/src/maze.rs @@ -22,6 +22,32 @@ impl HexMaze { } } + /// Creates a new empty maze with the specified layout + pub fn with_layout(layout: HexLayout) -> Self { + Self { + tiles: HashMap::new(), + layout, + } + } + + /// Creates a hexagonal maze with the given radius + /// Uses axial coordinates (q, r) to create a perfect hexagon + #[inline] + pub fn with_radius(mut self, radius: u32) -> Self { + let radius = radius as i32; + for q in -radius..=radius { + let r1 = (-radius).max(-q - radius); + let r2 = radius.min(-q + radius); + for r in r1..=r2 { + let pos = Hex::new(q, r); + let tile = HexTile::new(pos); + self.tiles.insert(pos, tile); + } + } + + self + } + /// Adds a new tile at the specified coordinates #[inline] pub fn add_tile(&mut self, coords: Hex) { @@ -189,24 +215,6 @@ mod tests { maze.add_wall(coord, EdgeDirection::FLAT_TOP); } - /* #[test] - fn multiple_tile_operations() { - let mut maze = HexMaze::default(); - let coord = Hex::ZERO; - - // Add same tile multiple times - maze.add_tile(coord); - let first_tile = maze.get_tile(&coord).unwrap(); - maze.add_tile(coord); - let second_tile = maze.get_tile(&coord).unwrap(); - - assert_eq!( - first_tile, second_tile, - "Repeated add_tile should update existing tile" - ); - assert_eq!(maze.len(), 1, "Repeated add_tile should not increase size"); - } */ - #[test] fn maze_boundaries() { let mut maze = HexMaze::default(); @@ -254,4 +262,44 @@ mod tests { ); } } + + #[test] + fn maze_builder() { + // Test builder pattern + let layout = HexLayout::default(); + let maze = HexMaze::with_layout(layout).with_radius(2); + + assert_eq!(maze.len(), 19, "Radius 2 should create 19 hexes"); + assert!( + maze.get_tile(&Hex::ZERO).is_some(), + "Center hex should exist" + ); + } + + #[test] + fn different_layouts() { + // Test with different layouts + let layouts = [ + HexLayout { + orientation: hexx::HexOrientation::Flat, + ..Default::default() + }, + HexLayout { + orientation: hexx::HexOrientation::Pointy, + ..Default::default() + }, + ]; + + for layout in layouts { + let maze = HexMaze::with_layout(layout).with_radius(1); + assert_eq!(maze.len(), 7, "Should work with different layouts"); + } + } + + #[test] + fn empty_maze() { + let layout = HexLayout::default(); + let maze = HexMaze::with_layout(layout); + assert!(maze.is_empty(), "New maze should be empty"); + } } diff --git a/src/tile.rs b/src/tile.rs index 7b35c34..a628214 100644 --- a/src/tile.rs +++ b/src/tile.rs @@ -27,6 +27,15 @@ impl HexTile { } } +impl From for HexTile { + fn from(value: Hex) -> Self { + Self { + pos: value, + walls: Walls::default(), + } + } +} + #[cfg(test)] mod tests { use hexx::EdgeDirection;