Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions src/MazeSolver/Services/MazeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Comment on lines 35 to 46
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Action required

4. Exit unreachable by dfs 🐞 Bug ✓ Correctness

With enforced odd dimensions, the exit is placed at y = height - 1 (even), but CarvePath moves
in steps of 2 from y = 1 (odd), so it can never reach the exit position; additionally, pre-marking
(width-2,height-2) as Path prevents carving into the only cell adjacent to the exit.
Agent Prompt
### Issue description
The current `CarvePath`-based solvability detection cannot succeed with the current exit placement/move step and the pre-opened `(width-2,height-2)` cell.

### Issue Context
- `Generate()` normalizes dimensions to odd.
- Exit is placed at `(width-2, height-1)`.
- `CarvePath` moves by +/-2, so it only visits odd coordinates starting from `(1,1)`.
- `(width-2,height-2)` is set to `Path` before carving, but the carve only enters `Wall` cells.

### Fix Focus Areas
- src/MazeSolver/Services/MazeGenerator.cs[20-47]
- src/MazeSolver/Services/MazeGenerator.cs[53-85]
- src/MazeSolver/Services/MazeGenerator.cs[96-128]

### Implementation direction (choose one)
1) Restore BFS verification: keep `CarvePath` as `void` and use `IsSolvable(maze)` after carving; or
2) If keeping `CarvePath` returning `bool`, make the goal a reachable carve-cell (e.g., reaching `(width-2,height-2)`), ensure that cell is not pre-marked as `Path` before carving (or allow carving/connect-to it), and propagate return values up the recursion chain.

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools

Expand All @@ -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;

Expand All @@ -64,6 +64,11 @@ private void CarvePath(Maze maze, Position pos)
var newY = pos.Y + dy;
var newPos = new Position(newX, newY);

if (newPos == maze.Exit)
{
return true;
}

// Check if the new position is valid and unvisited
if (maze.IsInBounds(newPos) && maze[newPos].Type == CellType.Wall)
{
Comment thread
qodo-code-review[bot] marked this conversation as resolved.
Expand All @@ -75,6 +80,8 @@ private void CarvePath(Maze maze, Position pos)
CarvePath(maze, newPos);
}
}

return false;
Comment on lines 80 to +84
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Action required

3. Carvepath result not propagated 🐞 Bug ✓ Correctness

Recursive calls to CarvePath ignore the returned boolean, so even if a deeper recursion determines
reachability, the initial call can still return false, causing repeated regeneration in
Generate().
Agent Prompt
### Issue description
`CarvePath` now returns `bool` but recursive invocations discard the return, so the top-level solvability signal is lost.

### Issue Context
`Generate()` depends on `bool isSolvable = CarvePath(...)`. For DFS-style discovery, successful results must be propagated upward (e.g., `if (CarvePath(...)) return true;`).

### Fix Focus Areas
- src/MazeSolver/Services/MazeGenerator.cs[38-47]
- src/MazeSolver/Services/MazeGenerator.cs[73-85]

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools

}

private void Shuffle<T>(List<T> list)
Expand Down
Loading