2015-02-12 89 views
0

我无法将正确的值读入二维数组中。将输入迷宫文件中的字符存储到二维数组中(Java)

我的代码应该解决迷宫问题。它从输入的.txt文件读取迷宫结构,将结构排列成一个二维数组,然后将迷宫绘制到一个绘图面板中,然后为该解决方案添加动画。我被困在获得结构到数组中。

我的代码从这样一个输入文件读取:

2 3 
+-+-+-+ 
|S| | 
+ + + + 
| |E| 
+-+-+-+ 

前2号是迷宫的高度和宽度。所以,这个迷宫的高度为2行,宽度为3列。

这是我的代码到目前为止。

import java.util.Arrays; 
import java.util.Scanner; 
import java.io.File; 
import java.io.FileNotFoundException; 

public class MazeSolver { 

    // The name of the file describing the maze 
    static String mazefile; 
    static int width; 
    static int height; 
    static int arrayWidth; 
    static int arrayHeight; 
    static char[][] mazeArrays; 
    public static void main(String[] args) throws FileNotFoundException { 
     if (handleArguments(args)) { 

     mazeArrays = readMazeFile(mazefile, arrayHeight, arrayWidth); 
     System.out.println(Arrays.deepToString(mazeArrays)); 
     DrawMaze.draw(mazeArrays, height, width); 

     if (solveMaze()) 
      System.out.println("Solved!"); 
     else 
      System.out.println("Maze has no solution."); 
     } 
     else { 
     System.out.println("The arguments are invalid."); 
     } 
    } 

    // Handle the input arguments 
    static boolean handleArguments(String[] args) { 
     if (args.length > 4 || args.length < 1) { 
     System.out.println("There are too many or too few command line arguments"); 
     return false; 
     } 
     if (args.length == 1) { 
     mazefile = args[0]; 
     File file = new File(mazefile); 
     if (!file.canRead()) { 
      return false; 
     } 
     return true; 
     } 
     if (args.length == 2) { 
     mazefile = args[0]; 
     File file = new File(mazefile); 
     if (!file.canRead()) { 
      return false; 
     } 
     int cellsize = Integer.parseInt(args[1]); 
     if (cellsize < 10) { 
      return false; 
     } 
     return true; 
     } 
     if (args.length == 3) { 
     mazefile = args[0]; 
     File file = new File(mazefile); 
     if (!file.canRead()) { 
      return false; 
     } 
     int cellsize = Integer.parseInt(args[1]); 
     int borderwidth = Integer.parseInt(args[2]); 
     if (borderwidth < 5) { 
      return false; 
     } 
     return true; 
     } 
     if (args.length == 4) { 
     mazefile = args[0]; 
     File file = new File(mazefile); 
     if (!file.canRead()) { 
      return false; 
     } 
     int cellsize = Integer.parseInt(args[1]); 
     int borderwidth = Integer.parseInt(args[2]); 
     int sleeptime = Integer.parseInt(args[3]); 
     if (sleeptime < 0 || sleeptime > 10000) { 
      return false; 
     } 
     return true; 
     } 
     return false; 
    } 

    // Read the file describing the maze. 
    static char[][] readMazeFile(String mazefile, int arrayHeight, int arrayWidth) throws FileNotFoundException { 

     Scanner scanner = new Scanner(new File(mazefile)); 
     height = scanner.nextInt(); 
     System.out.println(height); 
     width = scanner.nextInt(); 
     System.out.println(width); 
     arrayHeight = 2 * height + 1; 
     arrayWidth = 2 * width + 1; 
     char[][] mazeArrays = new char[arrayHeight][arrayWidth]; 
     while (scanner.hasNextLine()) { 
     String line = scanner.nextLine(); 
     System.out.println(line); 
     for (int row = 0; row < arrayHeight; row++) { 
      for (int col = 0; col < line.length(); col++) { 
       mazeArrays[row][col] = line.charAt(col); 




      } 
     } 



     }return mazeArrays; 

    } 

    // Solve the maze.  
    static boolean solveMaze() { 
     return true; 
    } 
} 

的问题是在方法readMazeFile

这里是输出:

2 
3 

+-+-+-+ 
|S| | 
+ + + + 
| |E| 
+-+-+-+ 
[[+, -, +, -, +, -, +], [+, -, +, -, +, -, +], [+, -, +, -, +, -, +], [+, -, +, -, +, -, +], [+, -, +, -, +, -, +]] 
Solved! 

它看起来像我的问题是,扫描仪不会移动到下一行,但我相信我正确写作。 2d数组正确存储第一行,但不移动到“| S | |”之后。

我也在期待将空白存储到数组中的问题。

在此先感谢!

回答

0

你在输出中看到的是输入的最后一行,而不是第一行。这是因为要存储迷宫的每一行中的每一行:

while (scanner.hasNextLine()) { 
    String line = scanner.nextLine(); 
    System.out.println(line); 
    for (int row = 0; row < arrayHeight; row++) { 
     for (int col = 0; col < line.length(); col++) { 
      mazeArrays[row][col] = line.charAt(col); 
     } 
    } 
} 

我怀疑你打算:

for (int row = 0; row < arrayHeight; row++) { 
    String line = scanner.nextLine(); 
    System.out.println(line); 
    for (int col = 0; col < arrayWidth; col++) { 
     mazeArrays[row][col] = line.charAt(col); 
    } 
} 
+0

感谢您的答复。它工作到第二个循环,其中mazeArray [row] [col] = line.charAt(co);引发线程“main”中的异常java.lang.ArrayIndexOutOfBoundsException:5 – boop 2015-02-12 01:44:14

+0

异常中的'5'意味着当您只有分配的行0 - 4时(即大小5 = 2 * 2 + 1)。我猜这是因为它正在阅读额外的一行。停止它的简单方法是测试arrayHeight和arrayWidth而不是'hasNextLine'。我会改变代码。 – sprinter 2015-02-12 02:23:27

+0

谢谢!这改善了产出。并感谢你解释5代表什么。如果您不介意继续,数组现在在mazeArray [0] [0-6(含))中保存'0000',然后正确存储。所以阵列中的迷宫结构从[1] [0]开始。它是否阅读空行? – boop 2015-02-12 02:40:28

相关问题