Maze Runner Java Code I\'m getting errors in my Maze runner code and just going
ID: 3829191 • Letter: M
Question
Maze Runner Java Code
I'm getting errors in my Maze runner code and just going to scrap it. The maze was supposed to output the solution and the steps it took and if it ran into a wall or double back. It was using reversion and backtracking.
Main///
package Maze;
import java.util.*;
import java.io.*;
public class Main {
public static void main(String[] args) throws IOException {
File myFile = new File("Mazes.txt");
Scanner input = new Scanner (myFile);
int numRows = input.nextInt();
int numCols = input.nextInt();
input.nextLine();
int start0 = 0;
int start1 = 0;
char[][] maze = new char[numRows][numCols];
for (int i = 0; i < numRows; i++)
{
String nextLine = input.nextLine();
for (int j = 0; j < numCols; j++)
{
char nextChar = nextLine.charAt(j);
maze[i][j] = nextChar;
System.out.print(nextChar);
}
System.out.println();
}
for (int i = 0; i < numRows; i++)
{
for (int j = 0; j < numCols; j++)
{
if (maze[i][j] == '+')
{
System.out.println("Starting coordinates: " + i + ", " + j);
start0 = i;
start1 = j;
}
}
}
for (int i = 0; i < numRows; i++)
{
for (int j = 0; j < numCols; j++)
{
if (maze[i][j] == '-')
{
System.out.println("End coordinates: " + i + ", " + j);
}
}
}
long starttimer = System.currentTimeMillis();
MazeEngine newMaze = new MazeEngine(maze);
System.out.println();
newMaze.findPath(start0, start1);
newMaze.printMaze(numRows, numCols);
long stoptimer = System.currentTimeMillis();
long elapsed = stoptimer - starttimer;
System.out.println("Solution found in: " + elapsed + " milliseconds");
char [][] a = newMaze.fileout();
PrintWriter out = new PrintWriter("Answer.txt");
for(int i = 0; i< numRows; i++)
{
for(int j=0; j< numCols; j++)
{
out.print(a[i][j]);
} out.println();
}
out.close();
}
}
///
Maze Engine
package Maze;
public class MazeEngine{
boolean wall = false;
char[][] maze;
boolean solved;
int start0;
int start1;
public MazeEngine(char[][] input)
{
maze = input;
}
public void findPath(int row, int col)
{
if (maze[row][col] == '-')
{
solved = true;
return;
}
maze[row][col] = '.';
/*
* If the value on bottom is empty or is an exit,
* we recall the function.
*/
if (maze[row + 1][col] == ' ' || maze[row + 1][col] == '-')
{
findPath(row + 1, col);
}
/*
* If the value on right is empty or is an exit,
*/
else if (maze[row][col + 1] == ' ' || maze[row][col + 1] == '-')
{
findPath(row, col + 1);
}
/*
* If the value on top is empty or is an exit,
*/
else if (maze[row - 1][col] == ' ' || maze[row - 1][col] == '-')
{
findPath(row -1, col);
}
/*
* If the value on left is empty or is an exit,
*/
else if (maze[row][col - 1] == ' ' || maze[row][col - 1] == '-')
{
findPath(row, col - 1);
}
else
{
wall = true;
return;
}
if (wall)
{
wall = false;
findPath(row, col);
}
if (solved)
{
maze[row][col] = '+';
}
}
public void printMaze(int rows, int cols)
{
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
System.out.print(maze[i][j]);
}
System.out.println();
}
}
public char[][] fileout()
{
return maze;
}
}
Here the Maze.Txt
000100000000000
000100001000010
000111111111000
000100000001000
000111110001000
000000010001000
000011110001000
000010010001010
000010010000000
000010000000000
000011111110000
000000000010000
000000000010000
000001000011110
000000000010000
000000000000000
0000000000000A0
000000A00000000
000000000000000
000000000000000
000000000000000
000000000000000
00000000000B0B0
0000000C0000000
000000000000000
0000D0000000000
000000000000000
000000000000000
00000D0000C0000
000000000000000
010000000000000
010000000000000
010000000000000
011100000000000
000111110001000
011100010000000
000100010001000
000000010001000
000000011111100
000000000000100
000000000000100
000000001111100
000000000000100
000000000000100
000000000000100
000000000000000
000000000000000
000000000000000
000000000000000
0000000B000C000
0A0000000000000
000B0000000C000
000000000000000
000000000000000
000000000000000
000000000000000
000000000000000
000000000000000
000000000000000
000000000000A00
000000000000010
000000000000010
000000000000010
000000000000010
000000000011110
000000000010010
000000000010010
000001111110010
000001000000010
000001000000010
000001000000010
000011000000010
000010000000010
011110000000010
010000000000000
000000000000000
000000000000000
000000000000000
000000000000000
000000000000000
0000000000B0000
000000000000000
00000000C000000
0000000000000B0
000000000000000
000000000000000
000000000C00000
000000000000000
0000000000000A0
0A0000000000000
Explanation / Answer
As your error is ArrayIndex is out of bound . Beacause in recursion you are not checking for row and column index. When you are doing row-1 OR col-1 check if is it less then 0. As we cannot access negative index in array.Same for index which are out of matrix size.
As you are reading numRows and numCols from file then please add first row as numof Row and col in file as I did it then read it in numRows and numCols.
For row and col checking i am passing total Row (R) and total column(C) with findPath function.
//Main.java
package sample; // Change it to your local package Name e.i. Maze
import java.util.*;
import java.io.*;
public class Main {
public static void main(String[] args) throws IOException {
File myFile = new File("D:\WS17.2\Sample\src\sample\Maze.txt"); // Providing the path of file
Scanner input = new Scanner (myFile);
int numRows = input.nextInt();
int numCols = input.nextInt();
input.nextLine();
int start0 = 0;
int start1 = 0;
char[][] maze = new char[numRows][numCols];
for (int i = 0; i < numRows; i++)
{
String nextLine = input.nextLine();
for (int j = 0; j < numCols; j++)
{
char nextChar = nextLine.charAt(j);
maze[i][j] = nextChar;
System.out.print(nextChar);
}
System.out.println();
}
for (int i = 0; i < numRows; i++)
{
for (int j = 0; j < numCols; j++)
{
if (maze[i][j] == '+')
{
System.out.println("Starting coordinates: " + i + ", " + j);
start0 = i;
start1 = j;
}
}
}
for (int i = 0; i < numRows; i++)
{
for (int j = 0; j < numCols; j++)
{
if (maze[i][j] == '-')
{
System.out.println("End coordinates: " + i + ", " + j);
}
}
}
long starttimer = System.currentTimeMillis();
MazeEngine newMaze = new MazeEngine(maze);
System.out.println();
/* add two additional argument in function as total Row and Total Column*/
newMaze.findPath(start0, start1,numRows,numRows);
newMaze.printMaze(numRows, numCols);
long stoptimer = System.currentTimeMillis();
long elapsed = stoptimer - starttimer;
System.out.println("Solution found in: " + elapsed + " milliseconds");
char [][] a = newMaze.fileout();
PrintWriter out = new PrintWriter("Answer.txt");
for(int i = 0; i< numRows; i++)
{
for(int j=0; j< numCols; j++)
{
out.print(a[i][j]);
} out.println();
}
out.close();
}
}
//MazeEngine.java
package sample;
public class MazeEngine{
boolean wall = false;
char[][] maze;
boolean solved;
int start0;
int start1;
public MazeEngine(char[][] input)
{
maze = input;
}
/* In each recursive call addes check for row and column*/
public void findPath(int row, int col,int R,int C)
{
if (maze[row][col] == '-')
{
solved = true;
return;
}
maze[row][col] = '.';
/*
* If the value on bottom is empty or is an exit,
* we recall the function.
*/
if (row+1 < R && maze[row + 1][col] == ' ' || row+1 < R && maze[row + 1][col] == '-')
{
findPath(row + 1, col,R,C);
}
/*
* If the value on right is empty or is an exit,
*/
else if (col+1 < C && maze[row][col + 1] == ' ' || col+1 < C && maze[row][col + 1] == '-')
{
findPath(row, col + 1,R,C);
}
/*
* If the value on top is empty or is an exit,
*/
else if (row - 1 >= 0 && maze[row - 1][col] == ' ' || row - 1 >= 0 && maze[row - 1][col] == '-')
{
findPath(row -1, col,R,C);
}
/*
* If the value on left is empty or is an exit,
*/
else if (col-1 >=0 && maze[row][col - 1] == ' ' || col-1 >=0 && maze[row][col - 1] == '-')
{
findPath(row, col - 1,R,C);
}
else
{
wall = true;
return;
}
if (wall)
{
wall = false;
findPath(row, col,R,C);
}
if (solved)
{
maze[row][col] = '+';
}
}
public void printMaze(int rows, int cols)
{
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
System.out.print(maze[i][j]);
}
System.out.println();
}
}
public char[][] fileout()
{
return maze;
}
}
//Add additional row in Maze.txt file for Row and column value.
//Maze.txt
90 15
000100000000000
000100001000010
000111111111000
000100000001000
000111110001000
000000010001000
000011110001000
000010010001010
000010010000000
000010000000000
000011111110000
000000000010000
000000000010000
000001000011110
000000000010000
000000000000000
0000000000000A0
000000A00000000
000000000000000
000000000000000
000000000000000
000000000000000
00000000000B0B0
0000000C0000000
000000000000000
0000D0000000000
000000000000000
000000000000000
00000D0000C0000
000000000000000
010000000000000
010000000000000
010000000000000
011100000000000
000111110001000
011100010000000
000100010001000
000000010001000
000000011111100
000000000000100
000000000000100
000000001111100
000000000000100
000000000000100
000000000000100
000000000000000
000000000000000
000000000000000
000000000000000
0000000B000C000
0A0000000000000
000B0000000C000
000000000000000
000000000000000
000000000000000
000000000000000
000000000000000
000000000000000
000000000000000
000000000000A00
000000000000010
000000000000010
000000000000010
000000000000010
000000000011110
000000000010010
000000000010010
000001111110010
000001000000010
000001000000010
000001000000010
000011000000010
000010000000010
011110000000010
010000000000000
000000000000000
000000000000000
000000000000000
000000000000000
000000000000000
0000000000B0000
000000000000000
00000000C000000
0000000000000B0
000000000000000
000000000000000
000000000C00000
000000000000000
0000000000000A0
0A0000000000000
//Output:
000100000000000
000100001000010
000111111111000
000100000001000
000111110001000
000000010001000
000011110001000
000010010001010
000010010000000
000010000000000
000011111110000
000000000010000
000000000010000
000001000011110
000000000010000
000000000000000
0000000000000A0
000000A00000000
000000000000000
000000000000000
000000000000000
000000000000000
00000000000B0B0
0000000C0000000
000000000000000
0000D0000000000
000000000000000
000000000000000
00000D0000C0000
000000000000000
010000000000000
010000000000000
010000000000000
011100000000000
000111110001000
011100010000000
000100010001000
000000010001000
000000011111100
000000000000100
000000000000100
000000001111100
000000000000100
000000000000100
000000000000100
000000000000000
000000000000000
000000000000000
000000000000000
0000000B000C000
0A0000000000000
000B0000000C000
000000000000000
000000000000000
000000000000000
000000000000000
000000000000000
000000000000000
000000000000000
000000000000A00
000000000000010
000000000000010
000000000000010
000000000000010
000000000011110
000000000010010
000000000010010
000001111110010
000001000000010
000001000000010
000001000000010
000011000000010
000010000000010
011110000000010
010000000000000
000000000000000
000000000000000
000000000000000
000000000000000
000000000000000
0000000000B0000
000000000000000
00000000C000000
0000000000000B0
000000000000000
000000000000000
000000000C00000
000000000000000
0000000000000A0
0A0000000000000
.00100000000000
000100001000010
000111111111000
000100000001000
000111110001000
000000010001000
000011110001000
000010010001010
000010010000000
000010000000000
000011111110000
000000000010000
000000000010000
000001000011110
000000000010000
000000000000000
0000000000000A0
000000A00000000
000000000000000
000000000000000
000000000000000
000000000000000
00000000000B0B0
0000000C0000000
000000000000000
0000D0000000000
000000000000000
000000000000000
00000D0000C0000
000000000000000
010000000000000
010000000000000
010000000000000
011100000000000
000111110001000
011100010000000
000100010001000
000000010001000
000000011111100
000000000000100
000000000000100
000000001111100
000000000000100
000000000000100
000000000000100
000000000000000
000000000000000
000000000000000
000000000000000
0000000B000C000
0A0000000000000
000B0000000C000
000000000000000
000000000000000
000000000000000
000000000000000
000000000000000
000000000000000
000000000000000
000000000000A00
000000000000010
000000000000010
000000000000010
000000000000010
000000000011110
000000000010010
000000000010010
000001111110010
000001000000010
000001000000010
000001000000010
000011000000010
000010000000010
011110000000010
010000000000000
000000000000000
000000000000000
000000000000000
000000000000000
000000000000000
0000000000B0000
000000000000000
00000000C000000
0000000000000B0
000000000000000
000000000000000
000000000C00000
000000000000000
0000000000000A0
0A0000000000000
Solution found in: 0 milliseconds