So I have this code which is a maze generator and i am trying to change it from
ID: 3571145 • Letter: S
Question
So I have this code which is a maze generator and i am trying to change it from printing " + - | " as a result, to just "W" and "G". The letter "W" is going to stand for wall and the letter "G" is going to stand for ground. The code generates the maze randomly each time. I want the code in java.
import java.util.Collections;
import java.util.Arrays;
public class sdfgsd {
private final int x;
private final int y;
private final int[][] maze;
public sdfgsd(int x, int y) {
this.x = x;
this.y = y;
maze = new int[this.x][this.y];
generateMaze(0, 0);
}
public void display() {
for (int i = 0; i < y; i++) {
// draw the north edge
for (int j = 0; j < x; j++)
{System.out.print((maze[j][i] & 1) == 0 ? "+---" : "+ ");}
System.out.println("+");
// draw the west edge
for (int j = 0; j < x; j++)
{System.out.print((maze[j][i] & 8) == 0 ? "| " : " ");}
System.out.println("|");
}
// draw the bottom line
for (int j = 0; j < x; j++)
{ System.out.print("+---"); }
System.out.println("+");
}
private void generateMaze(int cx, int cy) {
DIR[] dirs = DIR.values();
Collections.shuffle(Arrays.asList(dirs));
for (DIR dir : dirs) {
int nx = cx + dir.dx;
int ny = cy + dir.dy;
if (between(nx, x) && between(ny, y)
&& (maze[nx][ny] == 0)) {
maze[cx][cy] |= dir.bit;
maze[nx][ny] |= dir.opposite.bit;
generateMaze(nx, ny);
}
}
}
private static boolean between(int v, int upper) {
return (v >= 0) && (v < upper);
}
private enum DIR {
N(1, 0, -1), S(2, 0, 1), E(4, 1, 0), W(8, -1, 0);
private final int bit;
private final int dx;
private final int dy;
private DIR opposite;
static {
N.opposite = S;
S.opposite = N;
E.opposite = W;
W.opposite = E;
}
private DIR(int bit, int dx, int dy) {
this.bit = bit;
this.dx = dx;
this.dy = dy;
}
};
public static void main(String[] args) {
int x = args.length >= 1 ? (Integer.parseInt(args[0])) : 3;
int y = args.length == 2 ? (Integer.parseInt(args[1])) : 3;
sdfgsd maze = new sdfgsd(x, y);
maze.display();
}
}
I want the output of a 3 by 3 maze to be something similar to this.
wwwwww
wgwggw
wgwggw
wggwgw
wwwwww
THANK YOU
Explanation / Answer
I've tried modifying without changing the existing logic of DIR generation.
You can check the output and let me know if the logic can be changed.
PROGRAM CODE:
package samplepro;
import java.util.Collections;
import java.util.Arrays;
public class sdfgsd {
private final int x;
private final int y;
private final int[][] maze;
public sdfgsd(int x, int y) {
this.x = x;
this.y = y;
maze = new int[this.x][this.y];
generateMaze(0, 0);
}
public void display() {
// draw the top line
for (int j = 0; j < x; j++)
{
System.out.print("ww");
}
System.out.println("");
for (int i = 0; i < y; i++) {
// draw the west edge
for (int j = 0; j < x; j++)
{System.out.print((maze[j][i] & 8) == 0 ? "wg" : "gw");}
System.out.println("");
}
// draw the bottom line
for (int j = 0; j < x; j++)
{
System.out.print("ww");
}
System.out.println("");
}
private void generateMaze(int cx, int cy) {
DIR[] dirs = DIR.values();
Collections.shuffle(Arrays.asList(dirs));
for (DIR dir : dirs) {
int nx = cx + dir.dx;
int ny = cy + dir.dy;
if (between(nx, x) && between(ny, y)
&& (maze[nx][ny] == 0)) {
maze[cx][cy] |= dir.bit;
maze[nx][ny] |= dir.opposite.bit;
generateMaze(nx, ny);
}
}
}
private static boolean between(int v, int upper) {
return (v >= 0) && (v < upper);
}
private enum DIR {
N(1, 0, -1), S(2, 0, 1), E(4, 1, 0), W(8, -1, 0);
private final int bit;
private final int dx;
private final int dy;
private DIR opposite;
static {
N.opposite = S;
S.opposite = N;
E.opposite = W;
W.opposite = E;
}
private DIR(int bit, int dx, int dy) {
this.bit = bit;
this.dx = dx;
this.dy = dy;
}
};
public static void main(String[] args) {
int x = args.length >= 1 ? (Integer.parseInt(args[0])) : 3;
int y = args.length == 2 ? (Integer.parseInt(args[1])) : 3;
sdfgsd maze = new sdfgsd(x, y);
maze.display();
}
}
OUTPUT:
wwwwww
wgwggw
wggwgw
wggwgw
wwwwww
Modified code:
package samplepro;
import java.util.Collections;
import java.util.Arrays;
public class sdfgsd {
private final int x;
private final int y;
private final int[][] maze;
public sdfgsd(int x, int y) {
this.x = x;
this.y = y;
maze = new int[this.x][this.y];
generateMaze(0, 0);
}
public void display() {
for (int i = 0; i < y; i++) {
// draw the north edge
for (int j = 0; j < x; j++)
{System.out.print((maze[j][i] & 1) == 0 ? "ww" : "wg");}
System.out.println("");
// draw the west edge
for (int j = 0; j < x; j++)
{System.out.print((maze[j][i] & 8) == 0 ? "gw" : "wg");}
System.out.println("");
}
// draw the bottom line
for (int j = 0; j < x; j++)
{ System.out.print("ww"); }
//System.out.println("w");
}
private void generateMaze(int cx, int cy) {
DIR[] dirs = DIR.values();
Collections.shuffle(Arrays.asList(dirs));
for (DIR dir : dirs) {
int nx = cx + dir.dx;
int ny = cy + dir.dy;
if (between(nx, x) && between(ny, y)
&& (maze[nx][ny] == 0)) {
maze[cx][cy] |= dir.bit;
maze[nx][ny] |= dir.opposite.bit;
generateMaze(nx, ny);
}
}
}
private static boolean between(int v, int upper) {
return (v >= 0) && (v < upper);
}
private enum DIR {
N(1, 0, -1), S(2, 0, 1), E(4, 1, 0), W(8, -1, 0);
private final int bit;
private final int dx;
private final int dy;
private DIR opposite;
static {
N.opposite = S;
S.opposite = N;
E.opposite = W;
W.opposite = E;
}
private DIR(int bit, int dx, int dy) {
this.bit = bit;
this.dx = dx;
this.dy = dy;
}
};
public static void main(String[] args) {
int x = args.length >= 1 ? (Integer.parseInt(args[0])) : 3;
int y = args.length == 2 ? (Integer.parseInt(args[1])) : 3;
sdfgsd maze = new sdfgsd(x, y);
maze.display();
}
}
OUTPUT:
Run #1:
wwwwww
gwwgwg
wwwwwg
gwwggw
wgwwwg
gwwgwg
wwwwww
Run#2
wwwwww
gwgwwg
wgwgwg
gwgwgw
wgwgwg
gwwggw
wwwwww
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.