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

时间:2015-03-31 20:59:45

标签: java arrays character-arrays

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

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

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

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();
        }           
    }

我不完全确定如何浏览我的迷宫以找到&#39; 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;
        }        
    }

2 个答案:

答案 0 :(得分:1)

有两件事需要注意:

<强>第一
以下代码在二维数组填充后通过二维数组搜索字符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#

答案 1 :(得分:0)

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

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

这不是你想要的?