Java迷宫程序逻辑错误

时间:2018-03-01 22:40:41

标签: java maze

我正在尝试建立一个迷宫程序,允许你选择一个起点(迷宫内应为零)并告诉你是否有通往出口的路线(在迷宫的迷宫中用E表示) .txt文件)。它只能通过水平或垂直跟随零路径到达出口。如果您尝试启动' 1',则返回此处无法启动。如果你开始使用' 0'没有通往' E'或退出,它返回"帮助我被困"。如果它从以下' 0'中找到E的路径水平或垂直,它应该转换"路径"进入加号以显示路径。从用户输入的起点表示为' S' S'在迷宫中。

(Maze.txt文件)

E0001110000000100100
11100011101110001111
11111000101000111000
00001110101100010010
01011000101111000110
00001110000110011110
11011100110110111000
00011110110111111101
01011011110110100001
01000000000110110111
11011011010010000000
01010010011000101011
01111000101110101110
00001110000111011001
01101011101101000011
11000110100111011010
01110000100100110011
11010111110110000000
01110100011000111110
00011001000011100010

我提供的代码中的问题:

  1. 当我选择一个明显的出发点时,会出现由' E'表示的出口。在迷宫中,它返回"帮助我被困?#34; (第0行,第2列),这是不正确的

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

  3. 代码

    //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
    

1 个答案:

答案 0 :(得分:0)

你会笑,

硬编码阅读,但易于解决问题

在您public boolean out中检查b&gt; 0而不是b&lt; 0

有了这个,一切正常:)

另外:

类名应以国会大厦字母

开头

我也做了一些代码格式化的东西。如果你想看看......

MazeGame

//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);

    }

}