将输入迷宫文件中的字符存储到2d数组(Java)

时间:2015-02-12 00:32:48

标签: java arrays

我无法将正确的值输入到二维数组中。

我的代码应该解决迷宫问题。它从输入的.txt文件中读取迷宫结构,将结构排列成2d数组,然后将迷宫绘制到绘图面板中,然后绘制一个解决方案的动画。我坚持让结构进入阵列。

我的代码从输入文件读取如下:

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

前两个数字是迷宫的高度和宽度。因此,这个迷宫的高度为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数组正确存储第一行,但不会移动到&#34; | S | |&#34;之后。

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

提前致谢!

1 个答案:

答案 0 :(得分: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);
    }
}