2013-10-11 40 views
-1

******************我找到了問題。我交換了變化指數...因此我正在連續而不是在一列上。謝謝大家***************************java拼圖求解器ArrayIndexOutOfBoundsException

我目前正在研究Java拼圖求解器。除了實際的解算器部分,整個程序都可以工作......我知道這個難題。我做了遞歸。我知道爲什麼數組會拋出這個異常。 PathChoices.java應該防止它拋出它......除了最後一行外。最後一行不是問題。進展從1,1到這裏是輸入:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1 
at Maze.board(Maze.java:34) 
at MazeSolver.mazeSolver(MazeSolver.java:38) 
at MazeSolver.mazeSolver(MazeSolver.java:65) 
at MazeSolver.mazeSolver(MazeSolver.java:65) 
at MazeSolver.mazeSolver(MazeSolver.java:43) 
at MazeSolver.mazeSolver(MazeSolver.java:43) 
at MazeSolver.mazeSolver(MazeSolver.java:32) 
at MazeSolver.mazeSolver(MazeSolver.java:43) 
at Maze.main(Maze.java:66) 

String input = " ___________________ \n" 
    + "|_ | ___  _ _|\n" 
    + "| | | _|___| |_ | |\n" 
    + "| _____|_ | _| |\n" 
    + "| | | _ | _|_ | |\n" 
    + "|___| | | | _ | | |\n" 
    + "| |_ | _____| | |_|\n" 
    + "| |___| | _| |_ |\n" 
    + "|  | |___ |_ | |\n" 
    + "|_| | | _ |_| |_| |\n" 
    + "|___|___|_______|___|\n" 

我在哪裏試圖從左上角到右下角解決這個問題。這裏是我的課程:

import java.awt.Point; 
import java.util.Stack; 


public class Maze { 

    private char [][] myBoard; 
    private Point myLocation; 
    private Point boardSize; 
    private Stack<String> sol; 
    private MazeParser mp; 

    public Maze(String m){ 
    mp = new MazeParser(m); 
    myBoard = mp.parseMaze(); 
    myLocation = new Point(1,1); 
    boardSize = mp.sizeOfBoard(); 
    sol = new Stack<String>(); 
    } 

    public void appendSol(String s){ 
    sol.push(s); 
    } 

    public String printSol(){ 
    String solution = ""; 
    while(!sol.isEmpty()) { 
     solution += sol.pop(); 
    } 
    return solution; 
    } 

    public char board(int x, int y) { 
line 34  return myBoard[x][y]; 
    } 
    public char [][] getBoard() { 
     return myBoard; 
    } 

    public Point size(){ 
    return boardSize; 
    } 

    public Point getLoc(){ 
    return myLocation; 
    } 

    public void setLoc(Point p){ 
    myLocation = p; 
    } 

    public static void main(String[] args) { 

    Maze testMaze = new Maze(" ___________________ \n" 
      + "|_ | ___  _ _|\n" 
      + "| | | _|___| |_ | |\n" 
      + "| _____|_ | _| |\n" 
      + "| | | _ | _|_ | |\n" 
      + "|___| | | | _ | | |\n" 
      + "| |_ | _____| | |_|\n" 
      + "| |___| | _| |_ |\n" 
      + "|  | |___ |_ | |\n" 
      + "|_| | | _ |_| |_| |\n" 
      + "|___|___|_______|___|\n"); 

    MazeSolver.mazeSolver(testMaze,new Point(1,1),3); 
    System.out.println(testMaze.printSol()); 
     } 

} 

這是我如何選擇我可以去的地方。 PathChoices:

public class PathChoices { 

public static boolean isSouth(int x, int y, Maze myMaze){ 
if (myMaze.board(x+1, y) == '|'){ 
    return false; 
} else { 
     return (myMaze.board(x,y) != '_' && !(myMaze.board(x+1,y) == '|')) && 
      (myMaze.board(x+1,y) == '_' || 
      myMaze.board(x+1,y) == ' '); 
} 
} 

public static boolean isNorth(int x, int y, Maze myMaze){ 
if (myMaze.board(x-1, y) == '|'){ 
    return false; 
} else { 
     return myMaze.board(x-1,y) == ' '; 
} 
} 

public static boolean isEast(int x, int y, Maze myMaze){ 
if (myMaze.board(x, y+1) == '|'){ 
    return false; 
} else { 
     return myMaze.board(x,y+1) == '_' || 
      myMaze.board(x,y+1) == ' ' ; 
} 
} 

public static boolean isWest(int x, int y, Maze myMaze){ 
if (myMaze.board(x, y-1) == '|'){ 
    return false; 
} else { 
     return myMaze.board(x,y-1) == '_' || 
      myMaze.board(x,y-1) == ' ' ; 
} 
} 

}

這是我的解析器:

import java.awt.Point; 


public class MazeParser { 

    private int xLin; 
    private int yLin; 
    private char [][] mappedMaze; 
    private String [] myMazeArray; 

    public MazeParser(String m){ 
    String maze = m; 
     myMazeArray = maze.split("\n"); 
    xLin = myMazeArray[0].toCharArray().length; 
    yLin = myMazeArray.length; 
    mappedMaze = new char[yLin][xLin]; 
    } 
    public Point sizeOfBoard(){ 
    return new Point(yLin,xLin); 
     } 

    public char [][] parseMaze() { 

    for(int i = 0;i < yLin; i++) { 
     for (int j = 0; j < xLin; j++){ 
     mappedMaze[i][j] = myMazeArray[i].toCharArray()[j]; 
     } 
    } 
    return mappedMaze; 
    } 

} 

最後,但至少......這就是求解方法(我有問題的):

import java.awt.Point; 

public class MazeSolver { 

    /** 
    * Given the maze, the x and y coordinates (which must be odd), 
    * and the direction we came from, return true if the maze is 
    * solvable, and draw the solution if so. 
    */ 
    public static boolean mazeSolver (Maze m, Point p, int dirFrom) { 

    int x = p.x; 
    int y = p.y; 
    Maze maze = m; 

    boolean ok = false; 

    for (int i = 0 ; i < 4 && !ok ; i++) { 
     if (i != dirFrom) { 

     switch (i) { 
     // 0 = North, 1 = East, 2 = South, 3 = West 

     case 2: 
      if (PathChoices.isSouth(x,y,maze)) { 
      System.out.println(maze.board(x, y) + "\t::"+ maze.board(x+1, y) +"::W: " 
       + ""+maze.board(x, y-1) + "N: " 
        + ""+ maze.board(x-1, y) + "E: " 
         + ""+ maze.board(x, y+1) + ":*S: " 
          +maze.board(x+1, y) + ": " + x + " " + y + ": i " + i + " :" + dirFrom); 

      ok = mazeSolver (maze, new Point(x, y + 1), 0); 
      } 
      break; 
     case 1: 
      if (PathChoices.isEast(x,y,maze)){ 
      System.out.println(maze.board(x, y) + "\t::::W: " 
       + ""+maze.board(x, y-1) + "N: " 
        + ""+ maze.board(x-1, y) + "*E: " 
         + ""+ maze.board(x, y+1) + "S: " 
          +maze.board(x+1, y) + " " + x + " " + y + ": i " + i + " :" + dirFrom); 

      ok = mazeSolver (maze, new Point(x + 1, y), 3); 
      } 
      break; 
     case 3: 
      if (PathChoices.isWest(x,y,maze)){ 
      System.out.println(maze.board(x, y) + "\t::::*W: " 
       + ""+maze.board(x, y-1) + "N: " 
        + ""+ maze.board(x-1, y) + "E: " 
         + ""+ maze.board(x, y+1) + "S: " 
          +maze.board(x+1, y) + " " + x + " " + y + ": i " + i + " :" + dirFrom); 

      ok = mazeSolver (maze, new Point(x - 1, y), 1); 
      } 
      break; 
     case 0: 
      if (PathChoices.isNorth(x,y,maze)){ 
      System.out.println(maze.board(x, y) + "\t::::W: " 
       + ""+maze.board(x, y-1) + "*N: " 
        + ""+ maze.board(x-1, y) + "E: " 
         + ""+ maze.board(x, y+1) + "S: " 
          +maze.board(x+1, y) + " " + x + " " + y+ ": i " + i + " :" + dirFrom); 

      ok = mazeSolver (maze, new Point(x, y - 1), 2); 
      } 
      break; 
     default: 
      break; 
     } 
     } 
    } 
    // check for end condition 
    if (x == maze.size().x-1 && y == maze.size().y-2) 
     ok = true; 
    // once we have found a solution, draw it as we unwind the recursion 
    if (ok) { 
     switch (dirFrom) { 
     case 0: 
     maze.appendSol("N"); 
     break; 
     case 1: 
     maze.appendSol("E"); 
     break; 
     case 2: 
     maze.appendSol("S"); 
     break; 
     case 3: 
     maze.appendSol("W"); 
     break; 
     } 
    } 
    return ok; 
    } 
} 

它拋出異常,問題是它不應該按照PathChoice.java。任何幫助將非常感激。

+1

發佈堆棧跟蹤。太多的代碼要檢查。 –

+0

張貼堆棧跟蹤 – Sabersimon

+0

所以,這行是行Maze.java的34? –

回答

1
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1 
at Maze.board(Maze.java:34) 

幸運的是,只有一行,所以我們不需要看到行號。

public char board(int x, int y) { 
    return myBoard[x][y]; 
} 

幾乎可以肯定你用-1作爲參數之一調用了這個方法。要求大小爲n的數組的第-1或第n個元素沒有任何意義。只能使用索引0到n-1。除此之外,我不會告訴你ArrayIndexOutOFBoundsException是什麼 - 這太容易查找了。

此外,堆棧跟蹤(其他行號)告訴你這是從哪裏調用的。

at MazeSolver.mazeSolver(MazeSolver.java:38) 
... 
+0

是的,我知道。我用-1調用。這是由於指數的交換。感謝您的時間。 – Sabersimon

1

對於情況0,則表示調用板的方法,其中x-1

maze.board(x-1, y) 

所以板的方法將被稱爲用於

return myBoard[-1][y]; 

陣列具有索引零開始。因此ArrayIndexOutOfBound

0

好像你在你的MazeSolver類中調用maze.board()時不處理任何邊界情況。

在嘗試進入某個方向之前,請確保該位置實際存在。例如,在調用maze.board(x,y-1)之前,您應該確保您尚未處於y( - > 0)的最小值。