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

The maze game is a game in which the purpose of the game is for the player to ma

ID: 3777108 • Letter: T

Question

The maze game is a game in which the purpose of the game is for the player to make their way from the start point to the end point. The layout of the maze is such that the player must follow a series of narrow winding corridors with many false turns and dead-ends until they eventually reach the finish point.

An alternate version of the Maze game involves collecting objects while avoiding computer-controlled creatures (i.e. PacMan-style games). In such games, instead of reaching a finish point, the goal is to collect all the objects.

HELP CREATE A MAZE GAME IN JAVA PLEASE

Explanation / Answer

Answer

Code for the Maze game

public class MazeGame {
private int size; // size of maze
private boolean[][] up; // is there a wall to up of cell i, j
private boolean[][] right;   // is there a wall to right of cell i, j
private boolean[][] down;   // is there a wall to down of cell i, j
private boolean[][] left;   // is there a wall to left of cell i, j
private boolean[][] seen;
private boolean done = false;

public MazeGame(int size) {
this.size = size;
StdDraw.setXscale(0, n+2);
StdDraw.setYscale(0, n+2);
init();
generate_maze();
}

private void init() {
// initialize border cells as already seen
seen = new boolean[size+2][size+2];
for (int x = 0; x < size+2; x++) {
seen[x][0] = true;
seen[x][n+1] = true;
}
for (int y = 0; y < size+2; y++) {
seen[0][y] = true;
seen[n+1][y] = true;
}


// initialze all walls as present
up = new boolean[size+2][size+2];
right = new boolean[size+2][size+2];
down = new boolean[size+2][size+2];
left = new boolean[size+2][size+2];
for (int x = 0; x < size+2; x++) {
for (int y = 0; y < size+2; y++) {
up[x][y] = true;
right[x][y] = true;
down[x][y] = true;
left[x][y] = true;
}
}
}


// generate the maze for the maze game
private void generate_maze(int x, int y) {
seen[x][y] = true;

// while there is an unseen neighbor
while (!seen[x][y+1] || !seen[x+1][y] || !seen[x][y-1] || !seen[x-1][y]) {

// pick random neighbor
while (true) {
double r = StdRandom.uniform(4);
if (r == 0 && !seen[x][y+1]) {
up[x][y] = false;
down[x][y+1] = false;
generate_maze(x, y + 1);
break;
}
else if (r == 1 && !seen[x+1][y]) {
right[x][y] = false;
left[x+1][y] = false;
generate_maze(x+1, y);
break;
}
else if (r == 2 && !seen[x][y-1]) {
down[x][y] = false;
up[x][y-1] = false;
generate_maze(x, y-1);
break;
}
else if (r == 3 && !seen[x-1][y]) {
left[x][y] = false;
right[x-1][y] = false;
generate_maze(x-1, y);
break;
}
}
}
}

// generate the maze starting from lower left
private void generate_maze() {
generate_maze(1, 1);

/*
// delete some random walls
for (int i = 0; i < size; i++) {
int x = 1 + StdRandom.uniform(size-1);
int y = 1 + StdRandom.uniform(size-1);
up[x][y] = down[x][y+1] = false;
}

// add some random walls
for (int i = 0; i < 10; i++) {
int x = n/2 + StdRandom.uniform(size/2);
int y = n/2 + StdRandom.uniform(size/2);
right[x][y] = left[x+1][y] = true;
}
*/

}

// solve the maze using depth-first search
private void solve(int x, int y) {
if (x == 0 || y == 0 || x == size+1 || y == size+1) return;
if (done || seen[x][y]) return;
seen[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 == size/2 && y == size/2) done = true;

if (!up[x][y]) solve(x, y + 1);
if (!right[x][y]) solve(x + 1, y);
if (!down[x][y]) solve(x, y - 1);
if (!left[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);
}

// solve the maze starting from the start state
public void solve() {
for (int x = 1; x <= size; x++)
for (int y = 1; y <= size; y++)
seen[x][y] = false;
done = false;
solve(1, 1);
}

// draw the maze
public void draw() {
StdDraw.setPenColor(StdDraw.RED);
StdDraw.filledCircle(size/2.0 + 0.5, size/2.0 + 0.5, 0.375);
StdDraw.filledCircle(1.5, 1.5, 0.375);

StdDraw.setPenColor(StdDraw.BLACK);
for (int x = 1; x <= size; x++) {
for (int y = 1; y <= size; y++) {
if (down[x][y]) StdDraw.line(x, y, x+1, y);
if (up[x][y]) StdDraw.line(x, y+1, x+1, y+1);
if (left[x][y]) StdDraw.line(x, y, x, y+1);
if (right[x][y]) StdDraw.line(x+1, y, x+1, y+1);
}
}
StdDraw.show();
StdDraw.pause(1000);
}

// a test client
public static void main(String[] args) {
int n = Integer.parseInt(args[0]);
MazeGame maze = new MazeGame(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