Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Model a maze in either Java or C++. Requirements: Maze must be a grid of at leas

ID: 3788272 • Letter: M

Question

Model a maze in either Java or C++.

Requirements: Maze must be a grid of at least 10 x 10 squares.

From each square, an agent (to come later) may or may not be able to move either up, down, left, or right depending on the walls in the maze.

There must be a start cell and a goal cell.

The must must be solvable.

The must must have at least one dead end.

The program must draw the maze (either as text or graphics).

One possible solution that we talked about in class is to store the maze as a 2D array. The coordinates in the array represent the positions of the squares. The values in the array represent the walls. We only need to store the presence of two walls (up and to the right, either present or not, for example) for a total of 4 possible values for each cell.

Separately store the location of the starting cell and goal cell.

Extra Credit if the maze is automatically generated.

What to submit:

A word doc showing your well written and commented code as well as a screen shot from the output of your code (drawing the maze).

(Later, in part 2, we will solve the maze with agents)

Explanation / Answer

//You need to add StdDraw library to run this proogram. Use the following link to get this library - http://introcs.cs.princeton.edu/java/stdlib/

//After that use this link to get how to add this library - http://stackoverflow.com/questions/1051640/correct-way-to-add-external-jars-lib-jar-to-an-intellij-idea-project/32853178#32853178

import stddraw.StdDraw;

public class Maze {

    private int n;                //Maze Dimension

    private boolean[][] north;     //These variables determine if there are walls in any of the direction

    private boolean[][] east;

    private boolean[][] south;

    private boolean[][] west;

    private boolean[][] isisvisited;

    private boolean done = false;

    public Maze(int n) {

        this.n = n;

        StdDraw.setXscale(0, n+2);

        StdDraw.setYscale(0, n+2);

        init();

        generate();

    }

    private void init() {

        //This is to initialize if border cells are already isvisited

        isvisited = new boolean[n+2][n+2];

        for (int x = 0; x < n+2; x++) {

            isvisited[x][0] = true;

            isvisited[x][n+1] = true;

        }

        for (int y = 0; y < n+2; y++) {

            isvisited[0][y] = true;

            isvisited[n+1][y] = true;

        }

        //This will initialize all the walls are present

        north = new boolean[n+2][n+2];

        east = new boolean[n+2][n+2];

        south = new boolean[n+2][n+2];

        west = new boolean[n+2][n+2];

        for (int x = 0; x < n+2; x++) {

            for (int y = 0; y < n+2; y++) {

                north[x][y] = true;

                east[x][y] = true;

                south[x][y] = true;

                west[x][y] = true;

            }

        }

    }

    // This will generate the maze

    private void generate(int x, int y) {

        isvisited[x][y] = true;

        // This loop will check If there are any unisvisited neighbour

        while (!isvisited[x][y+1] || !isvisited[x+1][y] || !isvisited[x][y-1] || !isvisited[x-1][y]) {

            // This will pick random neighbour using Knuth's trick

            while (true) {

                double r = StdRandom.uniform(4);

                if (r == 0 && !isvisited[x][y+1]) {

                    north[x][y] = false;

                    south[x][y+1] = false;

                    generate(x, y + 1);

                    break;

                }

                else if (r == 1 && !isvisited[x+1][y]) {

                    east[x][y] = false;

                    west[x+1][y] = false;

                    generate(x+1, y);

                    break;

                }

                else if (r == 2 && !isvisited[x][y-1]) {

                    south[x][y] = false;

                    north[x][y-1] = false;

                    generate(x, y-1);

                    break;

                }

                else if (r == 3 && !isvisited[x-1][y]) {

                    west[x][y] = false;

                    east[x-1][y] = false;

                    generate(x-1, y);

                    break;

                }

            }

        }

    }

    // This will generate neighbour starting lower left

    private void generate() {

        generate(1, 1);    

    }

    // This will solve the maze using depth first search

    private void solve(int x, int y) {

        if (x == 0 || y == 0 || x == n+1 || y == n+1) return;

        if (done || isvisited[x][y]) return;

        isvisited[x][y] = true;

        StdDraw.setPenColor(StdDraw.BLUE);

        StdDraw.filledCircle(x + 0.5, y + 0.5, 0.25);

        StdDraw.show();

        StdDraw.pause(30);

        // reached middle

        if (x == n/2 && y == n/2) done = true;

        if (!north[x][y]) solve(x, y + 1);

        if (!east[x][y]) solve(x + 1, y);

        if (!south[x][y]) solve(x, y - 1);

        if (!west[x][y]) solve(x - 1, y);

        if (done) return;

        StdDraw.setPenColor(StdDraw.GRAY);

        StdDraw.filledCircle(x + 0.5, y + 0.5, 0.25);

        StdDraw.show();

        StdDraw.pause(30);

    }

    // This will solve the maze from start state

    public void solve() {

        for (int x = 1; x <= n; x++)

            for (int y = 1; y <= n; y++)

                isvisited[x][y] = false;

        done = false;

        solve(1, 1);

    }

    // This will draw your maze

    public void draw() {

        StdDraw.setPenColor(StdDraw.RED);

        StdDraw.filledCircle(n/2.0 + 0.5, n/2.0 + 0.5, 0.375);

        StdDraw.filledCircle(1.5, 1.5, 0.375);

        StdDraw.setPenColor(StdDraw.BLACK);

        for (int x = 1; x <= n; x++) {

            for (int y = 1; y <= n; y++) {

                if (south[x][y]) StdDraw.line(x, y, x+1, y);

               if (north[x][y]) StdDraw.line(x, y+1, x+1, y+1);

                if (west[x][y]) StdDraw.line(x, y, x, y+1);

                if (east[x][y]) StdDraw.line(x+1, y, x+1, y+1);

            }

        }

        StdDraw.show();

        StdDraw.pause(1000);

    }

    // Main function

    public static void main(String[] args) {

        int n = Integer.parseInt(args[0]);

        Maze maze = new Maze(n);

        StdDraw.enableDoubleBuffering();

        maze.draw();

        maze.solve();

    }

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote