Java迷宫程序逻辑错误

Java迷宫程序逻辑错误,java,maze,Java,Maze,我正在尝试构建一个迷宫程序,允许您选择迷宫中的起点应为零,它会告诉您迷宫中是否有到maze.txt文件中E表示的出口的路径。它只能通过沿水平或垂直方向的零路径到达出口。如果您尝试从“1”开始,它会返回您不能从这里开始的结果。如果您从没有到“E”或出口的路线的“0”开始,它将返回“我被困时的帮助”。如果它在水平或垂直方向上找到从“0”后面到E的路径,则应将该路径转换为加号以显示该路径。用户输入的起点在迷宫中表示为“S” Maze.txt文件 E0001110000000100100 1110001

我正在尝试构建一个迷宫程序,允许您选择迷宫中的起点应为零,它会告诉您迷宫中是否有到maze.txt文件中E表示的出口的路径。它只能通过沿水平或垂直方向的零路径到达出口。如果您尝试从“1”开始,它会返回您不能从这里开始的结果。如果您从没有到“E”或出口的路线的“0”开始,它将返回“我被困时的帮助”。如果它在水平或垂直方向上找到从“0”后面到E的路径,则应将该路径转换为加号以显示该路径。用户输入的起点在迷宫中表示为“S”

Maze.txt文件

E0001110000000100100
11100011101110001111
11111000101000111000
00001110101100010010
01011000101111000110
00001110000110011110
11011100110110111000
00011110110111111101
01011011110110100001
01000000000110110111
11011011010010000000
01010010011000101011
01111000101110101110
00001110000111011001
01101011101101000011
11000110100111011010
01110000100100110011
11010111110110000000
01110100011000111110
00011001000011100010
我的问题来自提供的代码:

当我选择一个明显的起点,该起点通向迷宫中由“E”表示的出口时,它会返回帮助“我被困在第0行第2列中”,这是不正确的

问题1的B/c,我不知道标记路径的功能是否正常B/c我无法找到路径

代码

//Class 1
public class mazeGame{

   public void solveMaze(char [][] maze, int row1, int column1) {

       if(findpath(maze, row1, column1)) {

           print(maze, row1, column1);

           System.out.println("I am finally free!!");

       }//end of if

       else {

           print(maze,row1,column1);

           System.out.println(" Help me! I am trapped");



       }//end of else

   }//end of solveMaze method







   public boolean findpath(char [][] maze, int a, int b)

   {

       if(out(maze,a,b))

       {

           return false;

       }//end of if



       if(closed(maze,a,b) || marked(maze,a,b)) {

           return false;

       }//end of if



       if(exit(maze,a,b)) {

           return true;

       }//end of if



       mark(maze,a,b);



       if(findpath(maze,a,b-1)) {

           return true;

       }//end of if.... Goes left



       if(findpath(maze, a, b+1))//goes right

       {

           return true;

       }//end of if



       if(findpath(maze,a-1, b)) {



           return true;

       }//end of if... goes up 1... ex: row 5-1 is now row 4(which is above row 5



       if(findpath(maze,a+1, b)) {



           return true;

       }//end of if... goes down 1



       unmark(maze,a,b);//unmarks path

       return false;



   }//end of findpath









   public boolean closed(char [][] maze, int a, int b) {

       boolean c = false;

       if(maze [a][b] == '1')

       {

           c = true;

       }//end of if

       return c;

   }//end of closed method. checks if path is invalid





   public boolean exit(char[][] maze, int a, int b) {



       boolean c = false;

       if(maze [a][b]=='E')

       {

           c = true;

       }//end of if

       return c;

   }//end of exit method... checks if exit has been reached



   public boolean marked(char [][] maze, int a , int b) {

       boolean c = false;

       if(maze[a][b]=='+')

       {

           c = true;

       }//end of if

       return c;



   }//end of marked... checks if path is marked



   public boolean open(char [][] maze, int a, int b) {

       boolean c= false;

       if(maze [a][b]=='0')

       {

           c = true;

       }//end of if

       return c;



   }//end of open method...checks if path is valid



   public boolean out(char [][] maze, int a, int b) {

       boolean c = false;

       if(a >= maze.length || b>= maze[0].length || a<0 || b>0) {

           c = true;

       }//end of if

       return c;

   }//end of out... checks if path is out of range



   public char mark(char [][] maze, int a, int b) {

       return maze[a][b]='+';

   }//end of mark... marks path









   public void unmark(char[][]maze, int a, int b) {

       maze[a][b] = 'x';

   }//end of unmark












   public void print(char [][] maze, int row1, int column1) {

       maze[row1][column1]='S';

       print(maze);

   }//end of print method...prints maze, and users starting point



   public void print(char [][] maze) {

       System.out.printf("%-4s", "");//DOUBLE CHECK THIS

       for(int i= 0; i< maze[0].length;i++)

       {

           System.out.printf("%-4d", i);



       }//end of for loop

       System.out.println();



       for(int i = 0; i< maze.length; i++)

       {

           System.out.printf("%-4d",i);

           for(int j = 0; j< maze[0].length; j++)

           {

               System.out.printf("%-4c", maze[i][j]);

           }//end for loop

           System.out.println();

       }//end of for loop  

   }//end of print method...prints maze







}//end of mazeGame class
我求你开怀大笑

代码很难阅读,但很容易解决问题


在公共布尔输出中,您检查b>0而不是bmaze.txt文件是…用调试器调试您的程序。您知道您的问题有多长吗?请用一个。有关更多信息,请参阅,并获取:thx@Zabuza的好提示,因为此处的评论太长,请添加链接,但不要忘记更改链接列表的方式。我想,堆栈更合适……非常感谢。我大概花了2个小时在我的代码中搜索逻辑出错的地方。我为这封被屠杀的邮件道歉。教授要求我们在findpath方法中使用链表。是的,我没有使用链表,因为当时我不确定如何实现它。我是这样做的,因为我知道如何这样做,然后我希望在它工作后改变它,然后悲哀地意识到我可能不得不重写几乎整个程序。
//Class 1
public class MazeGame {

    public void solveMaze(char [][] maze, int row1, int column1) {
        if(findpath(maze, row1, column1)) {
            print(maze, row1, column1);
            System.out.println("I am finally free!!");
        } else {
            print(maze,row1,column1);
            System.out.println(" Help me! I am trapped");
        }
    }

    public boolean findpath(char [][] maze, int a, int b) {
        // check current field
        // check if is walkable
        if(out(maze,a,b) || closed(maze,a,b) || marked(maze,a,b)) return false;
        // found exit?
        if(exit(maze,a,b)) return true;
        mark(maze,a,b);

        // go further (recursion)
        if(findpath(maze,a,b-1))  return true;
        if(findpath(maze, a, b+1))return true;
        if(findpath(maze,a-1, b)) return true;
        if(findpath(maze,a+1, b)) return true;

        //unmark this position
        unmark(maze,a,b);
        return false;
    }

    public boolean closed(char [][] maze, int a, int b) {
        return maze [a][b] == '1';
    }

    public boolean exit(char[][] maze, int a, int b) {
        return maze [a][b]=='E';
    }

    public boolean marked(char [][] maze, int a , int b) {
        return maze[a][b]=='+';
    }

    public boolean open(char [][] maze, int a, int b) {
        return maze[a][b]=='0';
    }

    public boolean out(char [][] maze, int a, int b) {
        return (a >= maze.length || b>= maze[0].length || a<0 || b<0);
    }

    public void mark(char [][] maze, int a, int b) {
        maze[a][b]='+';
    }

    public void unmark(char[][]maze, int a, int b) {
        maze[a][b] = 'x';
    }

    public void print(char [][] maze, int row1, int column1) {
        maze[row1][column1]='S';
        print(maze);
    }

    public void print(char [][] maze) {
        System.out.printf("%-4s", "");
        for(int i= 0; i< maze[0].length;i++)
            System.out.printf("%-4d", i);
        System.out.println();

        for(int i = 0; i< maze.length; i++) {
            System.out.printf("%-4d",i);
            for(int j = 0; j< maze[0].length; j++) {
                System.out.printf("%-4c", maze[i][j]);
            }
            System.out.println();
        }
    }
}
import java.util.*;
import java.io.File;
import java.io.FileNotFoundException;

public class Driver {

    public static void main(String[] args) {
        int rows = 20;
        int columns = 20;
        char [][] maze = new char [rows][columns];//maze size
        Scanner scan = new Scanner(System.in);

        try {
            File maze_file = new File("src/main/maze.txt");
            System.out.println(maze_file.getAbsolutePath());
            Scanner s = new Scanner(maze_file);
            while(s.hasNext()) {
                for(int row = 0; row < maze.length;row++) {
                    String line = s.nextLine();
                    for (int col = 0; col < 20; col++) {
                        maze[row][col] = line.charAt(col);
                    }
                }
            }
            s.close();
        } catch(FileNotFoundException e) {
            System.out.println("ERROR. FILE NOT FOUND");
        }

        MazeGame d = new MazeGame();
        d.print(maze);

        System.out.println("You will now enter the row and column # that you wish to start at.");
        System.out.println("Enter which row you would like to start at(Row # 0-19) ");
        int row1 = scan.nextInt();//fetchs row number from user
        System.out.println("Enter which column you would like to start at (Column# 0-19) ");
        int column1 = scan.nextInt();//fetches column number from user

        if(row1 >= maze.length || column1 >= maze[0].length)
            System.out.println("The number entered is invalid");
        else if(maze[row1][column1]=='E')
            System.out.println(" Can not start at exit");
        else if(maze[row1][column1]=='1')
            System.out.println("You can not start at a value containing 1, only 0");
        else
            d.solveMaze(maze, row1, column1);

    }

}