Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8915363
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T04:57:38+00:00 2026-06-15T04:57:38+00:00

I need to program a method to solve a maze (2-dimensional array). I need

  • 0

I need to program a method to solve a maze (2-dimensional array). I need to stay directly left of the wall at all times and my method should end when either I’ve reached the exit point (which is always at the same position) or when there is no solution possible (and, after running through the maze I’m back at the entry point).

I was able to do all that, no problems, I can visually ensure that it’s doing what I want it to do (we’ve got some other methods from our instructor which output the visuals) and my console debug output is right as well.

This is the relevant code:

public static void main(String[] args) {
    maze = generateMaze(10,10);
    walk(1,0,0);
}

public static void walk(int x, int y, int direction) {
    System.out.println("x = " + x + " y = " + y); //debug output
    draw(x,y,maze); //draws current position
    if (x == maze.length-1 && y == maze[1].length-2) { //terminate when reached exit
        System.out.println("Geschafft!");
        return;
    }
    if (x == 1 && y == 0 && direction == 3) { //terminate when at starting point again (no solution)
        System.out.println("Keine Lösung möglich.");
        return;
    }
    if (direction == 0) { //go down
        if (maze [x][y+1]) {
            walk(x,y,1);
        }
        walk(x,y+1,2);
    }
    if (direction == 1) { //go right
        if(maze [x+1][y]) {
            walk(x,y,3);
        }
        walk(x+1,y,0);
    }
    if (direction == 2) { //go left
        if(maze [x-1][y]) {
            walk(x,y,0);
        }
        walk(x-1,y,3);
    }
    if (direction == 3) { //go up
        if(maze[x][y-1]) {
            walk(x,y,2);
        }
        walk(x,y-1,1);
    }    
}

There’s just one problem: how do I end my recursion correctly? This is what I get form the console:

x = 1 y = 0
x = 1 y = 1
x = 1 y = 1
x = 1 y = 2
and so on...
x = 8 y = 8
x = 9 y = 8
Geschafft!
x = 8 y = 9
x = 8 y = 9
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10
at maze.MazeSolution.walk(MazeSolution.java:26)
at maze.MazeSolution.walk(MazeSolution.java:39)
and some more of that

I do understand the error, the recursion obviously doesn’t end where I want it to and x or y are increased and try to use an index in the array that isn’t there.

Why doesn’t the recursion end with the return statement, when either of these situations come true:

if (x == maze.length-1 && y == maze[1].length-2) { //terminate when reached exit
    System.out.println("Geschafft!");
    return;
}
if (x == 1 && y == 0 && direction == 3) { //terminate when at starting point again (no solution)
    System.out.println("Keine Lösung möglich.");
    return;
} 

What do I need to do to end it correctly?

I greatly appreciate your help, show some love for a beginner and tell me what to do.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T04:57:39+00:00Added an answer on June 15, 2026 at 4:57 am

    Look at your returns and where you may return to. You can return in the middle of your enclosing function which has other calls to walk, without the guards to ensure they’re not called.

    I recommend re-implementing your logic; think about having if/else pairs to ensure mutual exclusion.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a 'Sudoku' program, and I need to write a method to check
I need a program that checks if the difference between all pairs of elements
I need my program to see different colors. Pretty much all I need is
I need to solve a large, sparse system of linear equations from a program
I have a problem to solve, There is a simple supermarket program where all
For a part of a program i need the following 2 methods. The first
I need to call some methods in Wininet.dll from a Java program. I can
I need my program to create and edit a config file, which would contain
I need a program that reads from multiple files and displays a report based
I need to generate multiple databases to SQL, so I need script/program to automatically

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.