first assignment was a find the solution of a 12*12 maze.
i am supposed to use a recursive method to solve them.
secondly, i have to make a method to create a randomly generated maze with same dimensions.
(# represents a wall, * represents a untraveled path, T represents a traveled path, X represents a path that leads to a dead end) im stuck on how to get rid of the unnessassry path that leads to a deadend.
so far i got up to here:
package maze;
public class Maze {
private String[][] maze= new String[12][12];
private int starty, startx, finishy, finishx;
public Maze (String x) {
for (int i = 0; i < 12; i++) {
for (int j = 0; j < 12; j++) {
maze[i][j] = x.substring((i*12 + j), (i*12 + j + 1));
}
}
for (int i = 0; i < 12; i++) {
if (startx == 0 && starty == 0) {
if (maze[i][0].equals("*")) {
startx = i;
starty = 0;
}
else if (maze[i][11].equals("*")) {
startx = i;
starty = 11;
}
else if (maze[0][i].equals("*")) {
startx = 0;
starty = i;
}
else if (maze[11][i].equals("*")) {
startx = 11;
starty = i;
}
}
else {
if (maze[i][0].equals("*")) {
finishx = i;
finishy = 0;
}
else if (maze[i][11].equals("*")) {
finishx = i;
finishy = 11;
}
else if (maze[0][i].equals("*")) {
finishx = 0;
finishy = i;
}
else if (maze[11][i].equals("*")) {
finishx = 11;
finishy = i;
}
}
}
System.out.println("start " + startx + "," + starty);
System.out.println("finish " + finishx + "," + finishy);
this.findPath(startx, starty);
}
public void print() {
for (int i = 0; i < 12; i++) {
for (int j = 0; j < 12; j++) {
System.out.print(maze[i][j]);
}
System.out.println();
}
System.out.println();
}
public boolean findPath(int x, int y) {
maze[x][y] = "T";
print();
if ( x == finishx && y == finishy) {
return true;
}
else {
boolean solved = false;
for (int dx=-1; dx<=1; dx++) {
for (int dy=-1; dy<=1; dy++) {
if (dx*dy==0 && dx!=dy && isAvailable(x+dx, y+dy) && !solved) {
solved = findPath(x+dx, y+dy);
}
}
}
return solved;
}
}
public boolean isAvailable (int x, int y) {
if (x < 0 || x > 11 || y < 0 || y > 11)
return false;
else
return !maze[x][y].equals("#") && !maze[x][y].equals("T") && !maze[x][y].equals("X");
}
public boolean isTraveled (int x, int y) {
if (x < 0 || x > 11 || y < 0 || y > 11)
return false;
else
return maze[x][y].equals("T");
}
}