2013-10-11 29 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)的最小值。