使用二维数组导航 .txt 文件

Navigating a .txt file with a two dimensional array

对于这个作业,我要读取一个 .txt 文件并递归地导航它产生的迷宫(我将在下面提供一个示例)导航它并打印它。我向下导航,我的合作伙伴正在制作打印方法。但是,我在确定迷宫的起始位置时遇到问题。在迷宫中,起点将用 s 表示。用图片更容易解释,所以我现在提供

5    
#####
#s.##
##..#
###.#
#g..#

顶部的数字决定了数组的大小(我们必须使它们成为正方形)。 '#' 是一堵墙,'.'是可用路径,'g' 是目标。现在我的问题是找到 s 的坐标,以便我知道从哪里开始我的迷宫。下面将是文件选择器 class(问题出在哪里)和 class 来导航迷宫(它有效但我想我会包括它以防万一有人好奇

public static void main(String[] args)
    throws FileNotFoundException, IOException
    {
        FileReader myReader;
        JFileChooser chooser = new JFileChooser();
        if(chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION)
        {
            File myFile = chooser.getSelectedFile();
            myReader = new FileReader(myFile);   
            Scanner myScanner = new Scanner(myReader);
            String placeHolder = myScanner.nextLine();
            int arrayThings = Integer.parseInt(placeHolder);
            char[][] maze = new char[arrayThings][arrayThings];
            int rows = 0;
            int collumns = 0;
            for(int i = 0; i < maze.length(); i++)
            {
                if (maze[rows][collumns] == 's' || maze[rows][collumns] =='S')
                {
                    FindPath(maze[arrayThings][arrayThings],rows,collumns);
                }                    
            }               
            myReader.close();
        }           
    }

我不太确定如何在我的迷宫中导航以找到 's'。现在这里是 FindPath 方法

public static boolean FindPath(char[][] maze,int x,int y)
    {
        try
        {
            if (maze[x][y] == 'g' || maze[x][y] =='G')
            {
                System.out.println("The maze was printed");  //Replace with print method
                return true;
            }
            else if (maze[x][y] == '#' || maze[x][y] == '?' || maze[x][y] == 'P')
                return false;
            else 
            {
                maze[x][y] = 'P';
                System.out.println("The maze was printed"); //Replace with print method
                if(MazeSolver.FindPath(maze,x,y-1)== true)
                    return true;
                else if(MazeSolver.FindPath(maze,x+1,y)== true)
                    return true;
                else if(MazeSolver.FindPath(maze,x,y+1)== true)
                    return true;
                else if(MazeSolver.FindPath(maze,x-1,y)== true)
                    return true;
                maze[x][y] = '?';
                return false;
            }
        } 
        catch (ArrayIndexOutOfBoundsException myEx)
        {
            return false;
        }        
    }

您需要在此之前初始化您的迷宫:

for(int row = 0; row < maze.length; row++)
     {
         for(int col = 0; col < maze[0].length;col++){
             if(maze[row][col] == 's'){
                 System.out.println("x :" + row + " y : " + col);
             }
         }
     }   

这不是您要找的?

有两点要看:

第一
下面的代码遍历二维数组,在二维数组被填充后搜索字符s

for(int row = 0; row < maze.length; row++)
{
    for(int col = 0; col < maze[0].length;col++){
        if(maze[row][col] == 's'){
            System.out.println("x :" + row + " y : " + col);
        }
    }
}

为什么在填充二维数组时不查找并跟踪字符s?这将使您免于再次遍历二维数组。

row = 0;
maze = new char[arraySize][arraySize];
while(input.hasNextLine()) {
    String lineHolder = input.nextLine();
    // get the char array from the String
    maze[row] = lineHolder.toCharArray();
    if(lineHolder.indexOf('s') != -1) {
        // start row equals the current row
        sRow = row;
        // start column equals the index/column of 's'
        sCol = lineHolder.indexOf('s');
    }
    row = row + 1;
}

第二
不要使用 try catch 块来铰链你的递归,只需实施边界检查。

// maze bounds checking, recursion no longer hinges on an exception
// being thrown
if(y < 0 || x < 0 || y > maze.length-1 || x > maze[0].length-1) {
    return false;
}

我加入了以下工作代码来为上面提供的信息提供上下文。

工作代码

public static void main(String[] args) {
    JFileChooser chooser = null;
    Scanner input = null;
    int arraySize = 0;
    char[][] maze;
    int row = 0;
    int sRow = 0;
    int sCol = 0;

    chooser = new JFileChooser();

    if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
        try {
            input = new Scanner(chooser.getSelectedFile());
        } catch(Exception e) {
            System.out.println(e.getLocalizedMessage());
        }
    } else {
        System.out.println("Exiting...");
        System.exit(0);
    }

    if(input.hasNextLine()) {
        arraySize = Integer.parseInt(input.nextLine().trim());
    }

    row = 0;
    maze = new char[arraySize][arraySize];
    while(input.hasNextLine()) {
        String lineHolder = input.nextLine();
        // get the char array from the String
        maze[row] = lineHolder.toCharArray();
        if(lineHolder.indexOf('s') != -1) {
            // start row equals the current row
            sRow = row;
            // start column equals the index/column of 's'
            sCol = lineHolder.indexOf('s');
        }
        row = row + 1;
    }
    input.close();

    System.out.println("The maze has been read in:");
    printMaze(maze);

    FindPath(maze, sCol, sRow);
}

public static boolean FindPath(char[][] maze, int x, int y) {
    // maze bounds checking, recursion no longer hinges on an exception
    // being thrown
    if(y < 0 || x < 0 || y > maze.length-1 || x > maze[0].length-1) {
        return false;
    }

    if (Character.toLowerCase(maze[x][y]) == 'g') {
        System.out.println("The maze has been solved:");
        maze[x][y] = 'P';
        printMaze(maze);
        return true;
    }

    if(maze[x][y] == '.' || maze[x][y] == 's') {
        maze[x][y] = 'P';
        System.out.println("Solving...");
        printMaze(maze);

        if (FindPath(maze, x, y - 1)) {
            return true;
        } else if (FindPath(maze, x + 1, y)) {
            return true;
        } else if (FindPath(maze, x, y + 1)) {
            return true;
        } else if (FindPath(maze, x - 1, y)) {
            return true;
        }

        return true;
    } else {
        // if maze[x][y] equals # or P then return false
        return false;
    }
}

public static void printMaze(char[][] maze) {
    for(int r = 0; r < maze.length; r++) {
        for(int c = 0; c < maze[r].length; c++) {
            System.out.print(maze[r][c]);
        }
        System.out.print("\n");
    }
}

输出

The maze has been read in:
#####
#s.##
##..#
###.#
#g..#
Solving...
#####
#P.##
##..#
###.#
#g..#
Solving...
#####
#PP##
##..#
###.#
#g..#
Solving...
#####
#PP##
##P.#
###.#
#g..#
Solving...
#####
#PP##
##PP#
###.#
#g..#
Solving...
#####
#PP##
##PP#
###P#
#g..#
Solving...
#####
#PP##
##PP#
###P#
#g.P#
Solving...
#####
#PP##
##PP#
###P#
#gPP#
The maze has been solved:
#####
#PP##
##PP#
###P#
#PPP#