From 2f9940ba62b358521dc2746840db87e77c083964 Mon Sep 17 00:00:00 2001 From: Jan Krivanek Date: Mon, 30 Mar 2026 12:10:49 +0200 Subject: [PATCH 1/2] Refactor maze generation and solvability check Refactor maze generation to check solvability after carving paths. Update CarvePath method to return a boolean indicating if the exit is reachable. --- src/MazeSolver/Services/MazeGenerator.cs | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/src/MazeSolver/Services/MazeGenerator.cs b/src/MazeSolver/Services/MazeGenerator.cs index 16cb4e3..c04fb3f 100644 --- a/src/MazeSolver/Services/MazeGenerator.cs +++ b/src/MazeSolver/Services/MazeGenerator.cs @@ -23,10 +23,6 @@ public Maze Generate(int width, int height) var maze = new Maze(width, height); - // Carve paths using DFS from (1, 1) - var startPos = new Position(1, 1); - CarvePath(maze, startPos); - // Set entry at top-left accessible position maze.SetEntry(new Position(1, 0)); maze[1, 0].Type = CellType.Entry; @@ -39,8 +35,12 @@ public Maze Generate(int width, int height) maze[1, 1].Type = CellType.Path; maze[width - 2, height - 2].Type = CellType.Path; + // Carve paths using DFS from (1, 1) + var startPos = new Position(1, 1); + bool isSolvable = CarvePath(maze, startPos); + // Verify the maze is solvable - if (!IsSolvable(maze)) + if (!isSolvable) { Log.Warning("Generated maze was not solvable, regenerating..."); return Generate(width, height); @@ -50,7 +50,7 @@ public Maze Generate(int width, int height) return maze; } - private void CarvePath(Maze maze, Position pos) + private bool CarvePath(Maze maze, Position pos) { maze[pos].Type = CellType.Path; @@ -64,6 +64,11 @@ private void CarvePath(Maze maze, Position pos) var newY = pos.Y + dy; var newPos = new Position(newX, newY); + if (maze[newPos].Type == maze.Exit) + { + return true; + } + // Check if the new position is valid and unvisited if (maze.IsInBounds(newPos) && maze[newPos].Type == CellType.Wall) { @@ -75,6 +80,8 @@ private void CarvePath(Maze maze, Position pos) CarvePath(maze, newPos); } } + + return false; } private void Shuffle(List list) From 49b20abe1dc5e4cfc8e54189a35be0ce579c1bf2 Mon Sep 17 00:00:00 2001 From: Jan Krivanek Date: Mon, 30 Mar 2026 12:43:07 +0200 Subject: [PATCH 2/2] Fix compilation error: compare Position instead of CellType with maze.Exit --- src/MazeSolver/Services/MazeGenerator.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/MazeSolver/Services/MazeGenerator.cs b/src/MazeSolver/Services/MazeGenerator.cs index c04fb3f..7f2701d 100644 --- a/src/MazeSolver/Services/MazeGenerator.cs +++ b/src/MazeSolver/Services/MazeGenerator.cs @@ -64,7 +64,7 @@ private bool CarvePath(Maze maze, Position pos) var newY = pos.Y + dy; var newPos = new Position(newX, newY); - if (maze[newPos].Type == maze.Exit) + if (newPos == maze.Exit) { return true; }