import java.util.*; public class MazeGenerator { public void init() { String Maze[][] = new String [20][20]; for (int i =0; i <20; i++) { for (int j = 0; j < 20; j++) { Maze[i][j] = "#"; } } generate(Maze); for (int i =0; i <20; i++) { for (int j = 0; j < 20; j++) { System.out.print(" " + Maze[i][j]); } System.out.println(""); } } public void generate (String Maze[][]) { Stack <String> CellStack = new Stack<String>(); int TotalCells = Maze.length * Maze.length; int x = 10, y = 10; String CurrentCell = Maze[x][y]; Maze[x][y] = "-"; CellStack.push(CurrentCell); int VisitedCell = 1; boolean EastT, WestT, NorthT, SouthT; while(VisitedCell < TotalCells) { String EAST = Maze[x+1][y]; String WEST = Maze[x-1][y]; String NORTH = Maze[x][y+1]; String SOUTH = Maze[x][y-1]; if(EAST == "#") EastT = true; else EastT = false; if(WEST == "#") WestT = true; else WestT = false; if(NORTH == "#") NorthT = true; else NorthT = false; if(SOUTH == "#") SouthT = true; else SouthT = false; if(WestT == true || EastT == true || NorthT == true || SouthT == true) { double Random = (int) (Math.random() * 4) + 1; switch ((int) Random) { case 1: if(EastT == true){ Maze[x+1][y] = "-"; CurrentCell = EAST; break; } else break; case 2: if(WestT == true){ Maze[x-1][y] = "-"; CurrentCell = WEST; break; } else break; case 3: if(NorthT == true){ Maze[x][y+1] = "-"; CurrentCell = NORTH; break; } else break; case 4: if(SouthT == true){ Maze[x][y-1] = "-"; CurrentCell = SOUTH; break; } else break; } CurrentCell = "-"; CellStack.push(CurrentCell); VisitedCell++; } else { CurrentCell = CellStack.pop(); } } } }
The Program is trying to create a maze out of a 2D Array, where "#" represents a wall and "-" represents an empty space. When I run the program, I get the error : EmptyStackException(), in the else part of the if statement. Whats the reason for this error? Any help would be very useful.