rendered paste bodyimport java.io.*;
import java.util.Scanner;
public class Maze
{
private final static int MAXROW = 12;
private final static int MAXCOL = 12;
private final static char BLANK = ' ';
public void loadMaze(char[][] maze)
{
try
{
String dataFileName = "mazeData.txt";
FileReader reader = new FileReader(dataFileName);
Scanner sf = new Scanner(reader);
for (int row=1; row<=MAXROW; row++)
{
String line = sf.nextLine();
for (int col = 1; col<=MAXCOL; col++)
{
maze[row][col] = line.charAt(col-1);
}
}
sf.close();
}
catch(IOException e)
{
System.out.println("File problems.");
}
}
public void printMaze(char[][] maze) throws IOException
{
PrintWriter outfile = new PrintWriter (new FileWriter("outfile.txt"));
System.out.println(" 123456789012");
for (int row=1; row<=MAXROW; row++)
{
System.out.print (row %10 + " ");
for (int col=1; col<=MAXCOL; col++)
{
System.out.print("" + maze[row][col]);
outfile.print("" + maze[row][col]);
}
System.out.println();
outfile.println();
}
outfile.close();
System.out.println();
}
public void traceMaze(char[][] maze, int row, int col) throws IOException
{
char[][] mazeCopy = new char[maze.length][maze[0].length];
for (int r=0; r<mazeCopy.length; r++)
{
for (int c=0; c<mazeCopy[0].length; c++)
{
mazeCopy[r][c]=maze[r][c];
}
}
if (1 <= row && row <= MAXROW && 1 <= col && col <= MAXCOL)
{
if (BLANK == mazeCopy[row][col])
{
mazeCopy[row][col] = '!';
if (1 == row || MAXROW == row || 1 == col || MAXCOL == col)
{
printMaze(mazeCopy);
}
else
{
traceMaze(mazeCopy, row-1, col);
traceMaze(mazeCopy, row, col + 1);
traceMaze(mazeCopy, row+1, col);
traceMaze(mazeCopy, row, col-1);
}
}
}
}
public static void main(String[] args) throws IOException
{
Maze mazeWalker = new Maze();
char[][] maze = new char[MAXROW+1][MAXCOL+1];
mazeWalker.loadMaze(maze);
mazeWalker.traceMaze(maze, 6, 6);
}