2015-02-12 92 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

相關問題