public class MazeArray {
public static void main(String[] args) throws FileNotFoundException{
File inputFile = new File("maze1.txt");
Scanner sc = new Scanner(inputFile);
int maze[][] = null;
maze = createMaze(sc, maze);
mazePathFinder(sc, maze);
printMaze(maze);
}
public static int[][] createMaze(Scanner sc, int[][] maze){
int arrayRowSize = sc.nextInt();
int arrayColumnSize = sc.nextInt();
maze = new int[arrayRowSize][arrayColumnSize];
for(int i = 0; i < arrayRowSize; i++){
for(int j = 0; j < arrayColumnSize; j++){
maze[i][j] = sc.nextInt();
}
}
return maze;
}
public static void printMaze(int[][] maze){
for(int i = 0; i < maze.length; i++){
for(int j = 0; j < maze[i].length; j++){
System.out.print(maze[i][j]);
}
System.out.println();
}
}
public static void mazePathFinder(Scanner sc, int[][] maze){
Stack mazeStack = new Stack();
int count = 0;
boolean done = false;
String rowCoordinates = sc.next();
String columnCoordinates = sc.next();
int row = Integer.parseInt(rowCoordinates);
int column = Integer.parseInt(columnCoordinates);
Coordinate startingCoord = new Coordinate(row, column);
mazeStack.push(startingCoord);
count++;
maze[row][column] = 1;
//Looks North, if the value is a 0, converts it to 1 and pushes coordinates onto the stack
try{
if(maze[row - 1][column] == 0){
row -= 1;
maze[row][column] = 1;
Coordinate secondCoord = new Coordinate(row, column);
mazeStack.push(secondCoord);
count++;
}
}
catch(ArrayIndexOutOfBoundsException aiooe){
}
//Looks East
try{
if(maze[row][column + 1] == 0){
column += 1;
maze[row][column] = 1;
Coordinate secondCoord = new Coordinate(row, column);
mazeStack.push(secondCoord);
count++;
}
}
catch(ArrayIndexOutOfBoundsException aiooe){
}
//Looks South
try{
if(maze[row + 1][column] == 0){
row += 1;
maze[row + 1][column] = 1;
Coordinate secondCoord = new Coordinate(row, column);
mazeStack.push(secondCoord);
count++;
}
}
catch(ArrayIndexOutOfBoundsException aiooe){
}
//Looks West
try{
if(maze[row][column - 1] == 0){
column -= 1;
maze[row][column] = 1;
Coordinate secondCoord = new Coordinate(row, column);
mazeStack.push(secondCoord);
count++;
}
}
catch(ArrayIndexOutOfBoundsException aiooe){
}
try{
while(!done){
Coordinate nextCoord = new Coordinate(row, column);
//Pushes nextCoord to stack if == 0
if (maze[row - 1][column] == 0){
maze[row - 1][column] = 1;
row -= 1;
mazeStack.push(nextCoord);
count++;
continue;
}
//Pushes nextCoord to stack if == 0
else if(maze[row][column + 1] == 0){
maze[row][column + 1] = 1;
column += 1;
mazeStack.push(nextCoord);
count++;
continue;
}
//Pushes nextCoord to stack if == 0
else if(maze[row + 1][column] == 0){
maze[row + 1][column] = 1;
row += 1;
mazeStack.push(nextCoord);
count++;
continue;
}
//Pushes nextCoord to stack if == 0
else if(maze[row][column - 1] == 0){
maze[row][column - 1] = 1;
column -= 1;
mazeStack.push(nextCoord);
count++;
continue;
}
mazeStack.pop();
row = nextCoord.getRow();
column = nextCoord.getColumn();
break;
}
}
catch(ArrayIndexOutOfBoundsException aiooe){
done = true;
}
Stack reorderCoords = new Stack();
for(int i = 0; i < count - 1; i++){
reorderCoords.push(mazeStack.pop());
}
for(int i = 0; i < count - 1; i++){
System.out.println(reorderCoords.pop());
}
}
}
Recommended Answers
Jump to PostAdd lots of println statements to print out the values of variables used to contol the program's execution.
You need to explain what is wrong with the current output and post an exampleof what the output should look like.
The posted code does not compile without errors. One problem is …
Jump to PostContinue debugging by printing out the variables used in the loop
You should NOT ignore array out of bounds exceptions (line 143). They should NEVER happen in a debugged program. You need to remove the try/catch that ignores them and fix the code!!!
Jump to PostYou should NOT ignore array out of bounds exceptions (line 143). They should NEVER happen in a debugged program. You need to remove the try/catch that ignores them and fix the code!!!
Jump to PostA list of numbers isn't very useful. The printlns should include the variable name:
System.out.println("varName"+varName);
ArrayOutOfBoundsException is how we are supposed to know that we have found the exit to the maze.
That sounds dumb. The code should detect when the index goes past the end of the array.
…
Jump to PostI can't compile and test the code as is. Fix the print outs so they show the varialbe names
and fix the try/catch block so you see where the error happens.
All 18 Replies
ThisIsMeOrIsIt
0
Newbie Poster
NormR1
563
Posting Sage
Team Colleague
ThisIsMeOrIsIt
0
Newbie Poster
NormR1
563
Posting Sage
Team Colleague
ThisIsMeOrIsIt
0
Newbie Poster
NormR1
563
Posting Sage
Team Colleague
ThisIsMeOrIsIt
0
Newbie Poster
NormR1
563
Posting Sage
Team Colleague
ThisIsMeOrIsIt
0
Newbie Poster
NormR1
563
Posting Sage
Team Colleague
ThisIsMeOrIsIt
0
Newbie Poster
ThisIsMeOrIsIt
0
Newbie Poster
ThisIsMeOrIsIt
0
Newbie Poster
ChaiNeeys
commented:
can you show the output?
+0
ThisIsMeOrIsIt
0
Newbie Poster
ThisIsMeOrIsIt
0
Newbie Poster
NormR1
563
Posting Sage
Team Colleague
ChaiNeeys
0
Newbie Poster
stultuske
1,116
Posting Maven
Featured Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.