2014-03-07 27 views
-1

我想写一个程序,返回是/否是否迷宫是可穿越的,但不断得到这个异常......怎么了?我怎样才能阻止这种情况发生? (我想保持这种结构尽可能地)ArrayIndexOutOfBounds Java(现在的堆栈溢出错误!?)

迷宫:

WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW 
WSOOOOOOOOOOOOOOWOOOOOOOOOOOOOOOOOWOOOOOOOOOOOOOOOWOOOOOOW 
WWOOOOOOOOOOOOOWWWWWWWWWWWWWOOOOOOOOOOWWWWWWWWWWWWWOOOOOOW 
WWWWWWOOOOOOOOOOOOWWWWWWWOOOOOOOOOOOOWWWWWWWWWWWWWWWWOOOOW 
WOOOOOOWWWWWWWWWWWWWWOOOOOOOOOOOWWWWWWWWOOOOOOOOOOOOOOOWWW 
WOOOOWWWWWWWOOOOOOWWWWOOOOOOWWWWWWWWWWWOOOOWWWWWWWWWOWWWWW 
WOOOWWWWWWWWWWWWOOWWWWWWWWWWWWOOOOOOOOOOOOWWWWWWWWWOOOOOWW 
WOOWWWWWWWWWWWWWOOWWWWWWWWWWWWWWWWWOOOOOOOWWWWWWWWWWWWOOOW 
WOWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWOOOOOOOWWWWWWWWWWWOOW 
WOWWWWWWWWWWWWWOOOOOOOOOOOOOOOOOOOOOOOOOOOOWWWWWWWWWWWWOOW 
WOOOOOOOOOOOOOOOOWWWWOOOOOOOOWWWWWWWOOOOOOWWWWWWWWWWWWWWFW 
WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW 

新代码:

import java.io.File; 
import java.io.FileNotFoundException; 
import java.util.HashSet; 
import java.util.Scanner; 
import java.util.Stack; 
import java.awt.Point; 

public class MazeExplorer { 
    static Point startPoint = new Point(); 
    static Point finishPoint = new Point(); 
    final static int mazeHeight = 12; 
    final static int mazeWidth = 58; 
    static char[][] mazePoints = new char[mazeHeight][mazeWidth]; 
    Stack<Point> pointsNotTraversed = new Stack<Point>(); 
    Point pt = new Point(); 
    static HashSet<Point> previousLocations = new HashSet<Point>(); 
    static Stack<Point> nextPoints = new Stack<Point>(); 

    public static void main(String[] args) throws FileNotFoundException{ 

     System.out.println("Please enter the file name of your Maze"); 
     Scanner console = new Scanner(System.in); 
     File f = new File(console.nextLine()); 
     Scanner sc = new Scanner(f); 

     if(!sc.hasNextLine()){ 
      System.out.println("Sorry, please enter a file name with the extension, that contains a maze!"); 
     } 
     System.out.println("So, you want to know if your maze is solvable.....?"); 

     for (int row = 0; row < mazeHeight && sc.hasNext(); row++) { 
      final String mazeRow = sc.next(); //Get the next row from the scanner. 
      mazePoints[row] = mazeRow.toCharArray(); //Convert the row into a char[]. 
     } 
      //identify the finish point 
     for(int i = 0; i < mazeHeight; i++){ 
      for(int j = 0; j<mazeWidth; j++){ 
       if(mazePoints[i][j] == 'F'){ 
        finishPoint = new Point(i, j); 
       }  
      } 
     } 
     // Identify the start point 
     for(int i = 0; i< mazeHeight; i++){ 
      for(int j = 0; j < mazeWidth; j++){ 
       if(mazePoints[i][j] == 'S'){ 
       startPoint = new Point(i , j); 
       } 
      } 
     } 
     isTraversable(startPoint);  
    } 
     public static boolean isTraversable(Point current){ 
      boolean isSolvable = false; 
      nextPoints.push(current); 

      do { 


       if(current.y < 11) { 
        if((mazePoints[current.y + 1][current.x] != ' ') && (mazePoints[current.y + 1][current.x] != 'W')){ // below direction 
        nextPoints.push(new Point(current.y + 1, current.x)); 
        mazePoints[current.y + 1][current.x] = ' ';   
        isTraversable(nextPoints.pop());  

       } 
       } 
       if(current.y > 0){ 


       if (mazePoints[current.y - 1][current.x] != ' ' && mazePoints[current.y - 1][current.x] != 'W'){ //up dir 
        nextPoints.push(new Point(current.y - 1, current.x)); 
        mazePoints[current.y - 1][current.x] = ' '; //'X' marks where you've already been 
        isTraversable(nextPoints.pop());  

       } 
       } 
       if(current.x < 57){ 
       if(mazePoints[current.y][current.x + 1] != ' ' && mazePoints[current.y][current.x + 1] != 'W'){ // to the right 
        nextPoints.push(new Point(current.y, current.x + 1)); 
        mazePoints[current.y][current.x + 1] = ' '; 
        isTraversable(nextPoints.pop());  

       } 
       } 
       if(current.x > 0){ 


       if(mazePoints[current.y][current.x - 1] != ' ' && mazePoints[current.y][current.x - 1] != 'W') { // to the left 
        nextPoints.push(new Point(current.y, current.x - 1)); 
        mazePoints[current.y][current.x - 1] = ' ';  
        isTraversable(nextPoints.pop());  

       } 
       } 
       if(current.equals(finishPoint)){ 
        isSolvable = true; 
        System.out.println("MAZE IS SOLVABLE, YAHOOOOOO!!!!"); 
       } 




      } while(!current.equals('F') && !nextPoints.isEmpty());  


      return isSolvable;   
     } 
} 
+0

哪条语句导致错误?例外应该给你一条线# –

+0

今天有这么多的迷宫问题... – Solace

+0

什么是你的堆栈跟踪? –

回答

0
if(current.x < 58){ 
       if(mazePoints[current.y][current.x + 1] == 'O'){ // to the right 
        nextPoints.push(new Point(current.y, current.x + 1)); 
        mazePoints[current.y][current.x + 1] = ' '; 
       } 
       } 

当current.x = 57,你会得到数组索引绑定异常用57替换58,因为你做current.x + 1和58 indx不存在

+0

谢谢,改为57,同样对于其他问题...现在我有一个堆栈溢出错误? – bazookyelmo

+0

isTraversable调用自己,这会导致堆栈溢出异常,你可以先用少于58的数字来测试你的函数,然后将isTraversable函数从递归改变为迭代函数 –

+0

我该如何迭代地执行操作? @PulkitSethi? – bazookyelmo

0

这是因为你的rec ursive方法没有适当考虑。我已经自由地清理了一些方法,以便看到它应该是什么样子。

我知道递归可能是一个棘手的话题,但如果您打算有效地学习,您将需要了解代码或使用递归进行更多练习。

public static boolean isTraversable(Point current){ 
     boolean isSolvable = false; 
     mazePoints[current.x][current.y] = ' '; 

     if(current.y < 57 && current.y > 0 && current.x > 0 && current.x < 12){ 
      if (mazePoints[current.x - 1][current.y] == 'O'){ // Up dir 
       Point upPoint = new Point(current.x-1, current.y); 
       nextPoints.push(upPoint); 
      } 

      if(mazePoints[current.x+1][current.y] == 'O'){ // Down dir 
       Point downPoint = new Point(current.x+1, current.y); 
       nextPoints.push(downPoint); 
      } 

      if(mazePoints[current.x][current.y + 1] == 'O'){ // to the right 
       Point rightPoint = new Point(current.x, current.y+1); 
       nextPoints.push(rightPoint); 
      } 

      if(mazePoints[current.x][current.y - 1] == 'O'){ // to the left 
       Point leftPoint = new Point(current.x, current.y-1); 
       nextPoints.push(leftPoint); 
      } 

      if(mazePoints[current.x - 1][current.y] == 'F' || 
       mazePoints[current.x + 1][current.y] == 'F' || 
       mazePoints[current.x][current.y - 1] == 'F' || 
       mazePoints[current.x][current.y + 1] == 'F'){ 
       System.out.println("MAZE IS SOLVABLE, YAHOOOOOO!!!!"); 
       return true; 
      } 

     } 

     if(nextPoints.isEmpty()){ 
      return false; 
     } 
     else{ 
      current = nextPoints.pop(); 
     } 

     return(isTraversable(current)); 

    } 
+0

@bazookyelmo我在那里结束了一个else语句。这应该做到这一点。 – leigero

+0

不,不是那样的...我有错误的迷宫文件! * facepalm *它与你所示例的迷宫完美配合..(这是正确的迷宫) – bazookyelmo

+0

@bazookyelmo很高兴我能帮上忙。你有13个问题,只接受3个答案。如果他们没有很好的答案,但不要在没有标记答案的情况下放弃问题。祝你好运=] – leigero