views:

573

answers:

2

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");
    }
}
A: 

so far i can get the path to the finish but i cannot delete the unnessassary path that leads to a deadend. also i do not see a way to create a random maze.

JAVA
A: 

Instead of just storing one of four characters for each cell, consider storing an integer representing the distance from that cell to the exit (with a special value such as Integer.MAX_VALUE for walls and areas enclosed by walls.)

Once you have populated the array, you can convert that to a string representation that shows the path taken.


Note: I do know the algorithm and implemented it just a couple of days ago but as this is a homework problem I will not post the whole solution immediately.

finnw
I thought the Integer.MAX_VALUE is a constant varaiable for the maximum value a integer can hold. i dont see how this relates to soring distance from cell to the exit
JAVA