Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

The purpose of my code is to create a program that will read in a maze and store it into a 2D array and solve it. I got the program to read the maze and put it into the array but my recursive algorithm is not working and this is the third different time I have changed it trying to get it to work. So could you help me get this to work?

Edit:I found out the problem with my original algorithm in the sense I got it to solve the maze but I have ran into another problem. The program is not printing out the correct things. Also this is just for my curiosity. I have already found out how to get it to work another way.

Maze Code:

 public static boolean goNorth(){
        boolean success;
        if (maze[currCol][currRow - 1] == maze[finishCol][finishRow]) {
            success = true;
            }
        if(maze[currCol][currRow - 1] == CLEAR){
            maze[currCol][currRow - 1] = PATH;
            currRow = currRow - 1;
            success = goNorth();
                if(!success){
                success = goWest();
                    if(!success){
                    success = goEast();
                        if(!success){
                            maze[currCol][currRow] = VISITED;
                            currRow = currRow + 1;
                            }
                        }
                    }
                    return success;
                } else {
                    return false;
            }
        }

    public static boolean goWest(){
        boolean success;
        if (maze[currCol - 1][currRow] == maze[finishCol][finishRow]) {
            success = true;
            }
        if(maze[currCol - 1][currRow] == CLEAR){
            maze[currCol - 1][currRow] = PATH;
            currCol = currCol - 1;
            success = goWest();
                if(!success){
                success = goSouth();
                    if(!success){
                    success = goNorth();
                        if(!success){
                            maze[currCol][currRow] = VISITED;
                            currCol = currCol + 1;
                            }
                        }
                    }
                    return success;
                } else {
                    return false;
            }
        }

        public static boolean goEast(){
        boolean success;
        if (maze[currCol + 1][currRow] == maze[finishCol][finishRow]) {
            success = true;
            }
        if(maze[currCol + 1][currRow] == CLEAR){
            maze[currCol + 1][currRow] = PATH;
            currCol = currCol + 1;
            success = goEast();
                if(!success){
                success = goNorth();
                    if(!success){
                    success = goSouth();
                        if(!success){
                            maze[currCol][currRow] = VISITED;
                            currCol = currCol - 1;
                            }
                        }
                    }
                    return success;
                } else {
                    return false;
            }
        }

        public static boolean goSouth(){
        boolean success;
        if (maze[currCol][currRow + 1] == maze[finishCol][finishRow]){
            success = true;
            }
        if(maze[currCol][currRow + 1] == CLEAR){
            maze[currCol][currRow + 1] = PATH;
            currRow = currRow + 1;
            success = goSouth();
                if(!success){
                success = goEast();
                    if(!success){
                    success = goWest();
                        if(!success){
                            maze[currCol][currRow] = VISITED;
                            currRow = currRow - 1;
                            }
                        }
                    }
                    return success;
                } else {
                    return false;
            }
        }

    }   

Maze Solver Code:

public class SolveMaze1
{
    public static void main (String[] args)
    {
        Maze1 maze = new Maze1();
        maze.readMaze();
        maze.printMaze();
        maze.goNorth();
        maze.printMaze();
    }
}

Desired outcome:

xxxxxxxxxxxxxxxxxxFx
x     x       xxxx x
x xxxxx xxxxx   xx x
x xxxxx xxxxxxx xx x
x            xx xx x
x xxxxxxxxxx xx    x
xxxxxxxxxxxxSxxxxxxx

xxxxxxxxxxxxxxxxxxFx
xVVVVVxPPPPPPPxxxxPx
xVxxxxxPxxxxxPPPxxPx
xVxxxxxPxxxxxxxPxxPx
xVVVVVVPPPPPPxxPxxPx
xVxxxxxxxxxxPxxPPPPx
xxxxxxxxxxxxSxxxxxxx

My Outcome:

    xxxxxxxxxxxxxxxxxxFx
    xVVVVVxVVVVVVVxxxxVx
    xVxxxxxVxxxxxVVVxxVx
    xVxxxxxVxxxxxxxVxxVx
    xVVVVVVVVVVVVxxVxxVx
    xVxxxxxxxxxxVxxVVVVx
    xxxxxxxxxxxxSxxxxxxx
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
297 views
Welcome To Ask or Share your Answers For Others

1 Answer

Determine what your base condition (case) is. In this case, your base case would probably be to determine if the present position (x, y) is greater than the ceiling, less than the floor, less than the left wall or greater than the right wall.

Next, you should probably determine if the present position (x, y) is the 'end of maze' character or treasure.

Then determine if the present position (x, y) is the 'start of maze' character.

At this point, I would have my algorithm draw a character in that position if all other conditions have been met. This character would represent that the position has been visited. Later on, you could choose to traverse the coordinates that you have collected and replace this character with a blank space if you choose.

The hard part is done. Now you call the method recursively, this time passing in the new coordinates, once for each direction you want your iterator/solver to go. Ex. if (checkPath(r, c-1) == true) // go west

At which point, you can now change the visited path of the maze to spaces. Ex. maze[r][c] = ' ';

This is the best I can do without giving you the entire answer.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...