import java.awt.*;
import java.text.*;
import javax.swing.*;
import java.awt.font.*;
public class Maze extends JPanel
{
private final String TRIED = "0";
private final String PATH = "-";
private final String SPACE = " ";
private String[][] grid =
{ {" "," ","*","*","*","*","*","*","*"},
{"*"," "," "," "," "," ","*"," ","*"},
{"*"," ","*","*","*","*","*"," ","*"},
{"*"," ","*"," ","*"," "," "," ","*"},
{"*"," ","*"," ","*","*","*"," ","*"},
{"*"," "," "," ","*"," "," "," ","*"},
{"*"," ","*"," ","*"," ","*"," ","*"},
{"*"," "," "," "," "," ","*"," ","*"},
{"*","*","*","*","*","*","*"," "," "}};
public boolean traverse (int row, int column, Graphics g)
{
AttributedString attributedString = new AttributedString(PATH);
attributedString.addAttribute(TextAttribute.FOREGROUND, Color.red);
boolean done = false;
if (valid (row, column))
{
grid[row][column] = TRIED;
if (row == grid.length-1 && column == grid[0].length-1)
done = true;
else
{
done = traverse (row+1, column, g);
if (!done)
done = traverse (row, column+1, g);
if (!done)
done = traverse (row-1, column, g);
if (!done)
done = traverse (row, column-1, g);
}
if (done)
grid[row][column] = PATH;
}
return done;
}
private boolean valid (int row, int column)
{
boolean result = false;
if (row >= 0 && row < grid.length &&
column >= 0 && column < grid[row].length)
if (grid[row][column] == SPACE)
result = true;
return result;
}
public String toString ()
{
String result = "\n";
for (int row=0; row < grid.length; row++)
{
for (int column=0; column < grid[row].length; column++)
result += grid[row][column] + "";
result += "\n";
}
return result;
}
}