2016-03-29 10 views
0

Ich versuche Stacks zu verwenden, um meinen Weg durch ein Labyrinth zu finden.Labyrinth Wegfindung, Array 2-D

Bevor ich mich um Stacks Gedanken mache, versuche ich es so zu bekommen, dass es zuerst zu einer Sackgasse kommt.

Allerdings, wenn ich meinen Code ausführen, bekomme ich java.lang.ArrayIndexOutofBoundsException: -1. Das macht keinen Sinn, da ich die Zeilen und Spalten ständig aktualisiere.

Hier ist ein Teil meines Codes ist, dass ich stecke auf:

Maze myMaze = new Maze (rows, columns); 
    MazeDisplay myDisplay = new MazeDisplay (myMaze); 

    myMaze.buildMaze(10); 
    myMaze.setSolveAnimationDelay(75); 

    boolean [][] mazeArray = new boolean [rows][columns]; 

    for (int row=0; row<mazeArray.length; row++) 
     for (int col=0; col<mazeArray[row].length; col++) 
      mazeArray[row][col] = false; 
      mazeArray [myMaze.getCurrentRow()][myMaze.getCurrentCol()] = true; 


    for (int i = 0; i < 9999999 ; i++) //Temporary for now 
    { 
     int arrayRows = myMaze.getCurrentRow(); 
     int arrayCols = myMaze.getCurrentCol(); 

     if (myMaze.isOpen(Maze.Direction.RIGHT) && mazeArray [arrayRows][1 + arrayCols] == false) 
     { 
      myMaze.move(Maze.Direction.RIGHT); 
      //mazeStack.push(Maze.Direction.RIGHT); 
      mazeArray[arrayRows][arrayCols] = true; 
     } 
     else if (myMaze.isOpen(Maze.Direction.UP) && mazeArray [1 + arrayRows][arrayCols] == false) 
     { 
      myMaze.move(Maze.Direction.UP); 
      //mazeStack.push(Maze.Direction.UP); 
      mazeArray[arrayRows][arrayCols] = true; 
     } 
     else if (myMaze.isOpen(Maze.Direction.LEFT) && mazeArray [arrayRows][arrayCols - 1] == false) // <---Getting an error here. 
     { 
      myMaze.move(Maze.Direction.LEFT); 
      //mazeStack.push(Maze.Direction.LEFT); 
      mazeArray[arrayRows][arrayCols] = true; 
     } 
     else if (myMaze.isOpen(Maze.Direction.DOWN) && mazeArray [arrayRows - 1][arrayCols] == false) // <---Getting an error here. 
     { 
      myMaze.move(Maze.Direction.DOWN); 
      //mazeStack.push(Maze.Direction.DOWN); 
      mazeArray[arrayRows][arrayCols] = true; 
     } 

Gibt es eine Möglichkeit, dies zu umgehen? Ich möchte auf die ArrayList-Position nach unten oder links von der aktuellen Position zugreifen.

Hier sind einige der Methoden des Maze-Klasse:

//-------- isOpen - returns true if there is no wall in the direction that is passed in 
public boolean isOpen(Direction direction) 
{ 
    boolean result = false; 
    if (direction == Direction.UP && mazeArray[currentArrayRow-1][currentArrayCol]==0) 
     result = true; 
    else if (direction == Direction.DOWN && mazeArray[currentArrayRow+1][currentArrayCol]==0) 
     result = true; 
    else if (direction == Direction.LEFT && mazeArray[currentArrayRow][currentArrayCol-1]==0) 
     result = true; 
    else if (direction == Direction.RIGHT && mazeArray[currentArrayRow][currentArrayCol+1]==0) 
     result = true; 

    return result; 
} 

//-------- getCurrentRow - returns the current (real) row 
public int getCurrentRow() 
{ 
    return currentArrayRow/2; 
} 

//-------- getCurrentCol - returns the current (real) col 
public int getCurrentCol() 
{ 
    return currentArrayCol/2; 
} 

// -------- move - receives a Direction and moves there if OK. Calls the other 
//     arrayMove to do this 
public boolean move(Direction direction) 
{ 
    boolean success = false; 

    if (direction == Direction.UP) 
     success = arrayMove(currentArrayRow-2, currentArrayCol); 
    else if (direction == Direction.DOWN) 
     success = arrayMove(currentArrayRow+2, currentArrayCol); 
    else if (direction == Direction.LEFT) 
     success = arrayMove(currentArrayRow, currentArrayCol-2); 
    else if (direction == Direction.RIGHT) 
     success = arrayMove(currentArrayRow, currentArrayCol+2); 

    return success; 
} 

//This is Maze's enumerated data type: moves can be UP, DOWN, LEFT, RIGHT 
public enum Direction 
{ 
    UP, DOWN, LEFT, RIGHT 
} 

//-------- getMazeArray - returns the mazeArray 
public int[][] getMazeArray() 
{ 
    return mazeArray; 
} 
+0

Sie sagen 'ArrayList', aber ich sehe keine' List' von irgendeinem Typ. Meinst du Array? Nicht verwandt, aber 'arrayRows' und' arrayCols' sind falsch benannt; Sie sollten nur 'currentRow' und' currentCol' genannt werden, um zu vermeiden, dass es schwieriger wird, darüber nachzudenken. –

+0

@DaveNewton Sorry, ich meinte Array 2-D. Selbst mit den umbenannten 'currentRow' und' currentCol' bekomme ich immer noch die Ausnahme. – Paincakes

+0

Deshalb habe ich gesagt, dass es nichts miteinander zu tun hat - Dinge zu benennen ändert nichts an ihren Werten, sie ändert ihre Namen. Sie werden nur falsch genannt. –

Antwort

1

Bevor die linke Zelle überprüft, sollten Sie überprüfen, ob Sie sind nicht am linken Rand. Andernfalls wird mazeArray [arrayRows][arrayCols - 1] eine Ausnahme auslösen, da arrayCols - 1 = -1.

Wenn ich Ihren Code richtig verstehe, ist Ihr Algorithmus nicht perfekt. Es bleibt in Sackgassen hängen. Ich denke, der shortest path Algorithmus ist der einfachste zu implementieren.