Skip to content

### FIxes<Mazerecursion> - #7705

Open
krishnayak986-lang wants to merge 7 commits into
TheAlgorithms:masterfrom
krishnayak986-lang:fix/maze-recursion-bounds-and-validation
Open

krishnayak986-lang wants to merge 7 commits into
TheAlgorithms:masterfrom
krishnayak986-lang:fix/maze-recursion-bounds-and-validation

Conversation

@krishnayak986-lang

Copy link
Copy Markdown
  • [x ] I have read CONTRIBUTING.md.
  • [ x] This pull request is all my own work -- I have not plagiarized it.
  • [ x] All filenames are in PascalCase.
  • [ x] All functions and variable names follow Java naming conventions.
  • [ x] All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.
  • [ x] All new algorithms include a corresponding test class that validates their functionality.
  • [ x] All new code is formatted with clang-format -i --style=file path/to/your/file.java

Description

This PR improves MazeRecursion so it no longer crashes on small mazes, supports custom start and target positions, validates its input, and no longer duplicates the solving logic.

Problem

  • solveMazeUsingFirstStrategy and solveMazeUsingSecondStrategy always read the hard-coded destination map[6][5], so any maze smaller than that (for example 2x2) threw an ArrayIndexOutOfBoundsException.
  • The start (1,1) and destination (6,5) were hard-coded, so the solver couldn't be used with mazes of other sizes.
  • setWay and setWay2 were nearly identical and differed only in direction order.

Changes

  • Added a bounds check before any cell is accessed, so the solver can no longer go out of bounds.
  • Added overloads of both solve methods that accept startRow, startCol, targetRow, targetCol. The existing single-argument methods are kept and still use start (1,1) and target (6,5).
  • Added input validation. An IllegalArgumentException is thrown for a null or empty maze, jagged rows, or a start/target position outside the maze.
  • Replaced setWay and setWay2 with one shared recursive method that takes the direction order as a parameter.
  • Preserved both traversal orders:
    • First strategy: down -> right -> up -> left
    • Second strategy: up -> right -> down -> left
  • Paths are still marked 2 and dead ends 3. A maze with no route still returns null.

Tests

Added tests to MazeRecursionTest covering:

  • A small maze solved with a custom start and target
  • Both strategies keeping their traversal order
  • A small maze with the default target throwing IllegalArgumentException instead of ArrayIndexOutOfBoundsException
  • Null, empty and jagged mazes
  • Out-of-bounds start and target
  • An unsolvable maze returning null

The existing tests for the default 8x7 maze are unchanged and still pass.

Design note

For invalid input (including a default target outside a small maze) I chose to throw IllegalArgumentException, while "no route found" keeps returning null as before. If maintainers prefer returning null for invalid input, that is a small change in validate, so let me know.

@codecov-commenter

codecov-commenter commented Nov 12, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 82.34%. Comparing base (07c7ea3) to head (e2484be).

Additional details and impacted files
@@             Coverage Diff              @@
##             master    #7705      +/-   ##
============================================
- Coverage     82.34%   82.34%   -0.01%     
+ Complexity     8452     8450       -2     
============================================
  Files           843      843              
  Lines         26158    26154       -4     
  Branches       5110     5107       -3     
============================================
- Hits          21541    21537       -4     
  Misses         3828     3828              
  Partials        789      789              

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@krishnayak986-lang

Copy link
Copy Markdown
Author

Hi! I've resolved the merge conflicts with master and fixed the formatting and test issues, so all checks are passing now.

Summary of the change:

  • MazeRecursion no longer throws ArrayIndexOutOfBoundsException on small mazes
  • Added overloads with custom start/goal coordinates (the existing methods still use (1,1) -> (6,5))
  • Input validation: IllegalArgumentException for null, empty or non-rectangular mazes and out-of-range explicit coordinates; the default-endpoint methods return null if the fixed endpoints fall outside the maze
  • Both strategies now share one solver, with their traversal orders unchanged
  • Added tests for these cases

Happy to adjust anything if you'd prefer different behavior. Thanks for your time!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants