****************** 나는이 문제를 발견했다. 나는 변화 지수를 바꿨다. 그래서 나는 칼럼 대신 행을 추락했다. 고맙습니다 *************************자바 퍼즐 솔버는 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 당 안이다. 어떤 도움이라도 대단히 감사 할 것입니다.
스택 추적을 게시하십시오. 확인할 코드가 너무 많습니다. –
가 스택 추적을 게시했습니다 – Sabersimon
그래서 Maze.java의 34 번째 줄은 어느 라인입니까? –