2013-10-11 3 views
-1

****************** 나는이 문제를 발견했다. 나는 변화 지수를 바꿨다. 그래서 나는 칼럼 대신 행을 추락했다. 고맙습니다 *************************자바 퍼즐 솔버는 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이 무엇인지 알려주지는 않을 것입니다 - 조회하기가 너무 쉽습니다.

는 또한 스택 트레이스 (다른 줄 번호)이이 호출 된 곳을 알려줍니다. 경우 0에 대한

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

예 알아요. 나는 -1로 전화했다. 그것은 인덱스 교환 때문이었습니다. 시간 내 주셔서 감사합니다. – Sabersimon

1

, 당신은 X-1

maze.board(x-1, y) 

그래서 보드 방법은

return myBoard[-1][y]; 

배열에 대해 호출됩니다 보드 메서드를 호출하는 것은 인덱스 0으로 시작됩니다. 따라서 당신이 당신의 MazeSolver 클래스에 maze.board()를 호출 할 때 어떤 가장자리 케이스를 처리하지 않는 것처럼

0

이 보인다 ArrayIndexOutOfBound.

특정 방향으로 가려고하기 전에 위치가 실제로 있는지 확인하십시오. 예를 들어, maze.board (x, y-1)를 호출하기 전에 y (-> 0)의 최소값이 아닌지 확인해야합니다.