Question
Please Answer the question asked below as the previous time I asked this someone copy pasted the answer from ChatGPT public class Cell { public
Please Answer the question asked below as the previous time I asked this someone copy pasted the answer from ChatGPT
public class Cell {
public boolean north=true;
public boolean south=true;
public boolean east=true;
public boolean west=true;
public boolean visited=false;}
class Point {
int x;
int y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
public Point(String point) {
this.x = Integer.parseInt(point.split(",")[0], 10);
this.y = Integer.parseInt(point.split(",")[1], 10);
}
}
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.Random;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Maze {
int width;
int height;
int cellWidth = 15;
Cell[][] cells;
public Maze(int width, int height) {
this.width = width;
this.height = height;
this.cells = new Cell[width][height];
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
this.cells[i][j] = new Cell();
}
}
}
public void generate() {
Random random = new Random();
int x = random.nextInt(width);
int y = random.nextInt(height);
cells[x][y].visited = true;
ArrayList
path.add(new int[] { x, y });
while (!allVisited()) {
x = path.get(path.size() - 1)[0];
y = path.get(path.size() - 1)[1];
ArrayList
if (exists(x, y - 1) && !cells[x][y - 1].visited) {
goodAdjCells.add("north");
}
if (exists(x, y + 1) && !cells[x][y + 1].visited) {
goodAdjCells.add("south");
}
if (exists(x + 1, y) && !cells[x + 1][y].visited) {
goodAdjCells.add("east");
}
if (exists(x - 1, y) && !cells[x - 1][y].visited) {
goodAdjCells.add("west");
}
if (!goodAdjCells.isEmpty()) {
String go = goodAdjCells.get(random.nextInt(goodAdjCells.size()));
if (go.equals("north")) {
cells[x][y].north = false;
cells[x][y - 1].south = false;
cells[x][y - 1].visited = true;
path.add(new int[] { x, y - 1 });
}
if (go.equals("south")) {
cells[x][y].south = false;
cells[x][y + 1].north = false;
cells[x][y + 1].visited = true;
path.add(new int[] { x, y + 1 });
}
if (go.equals("east")) {
cells[x][y].east = false;
cells[x + 1][y].west = false;
cells[x + 1][y].visited = true;
path.add(new int[] { x + 1, y });
}
if (go.equals("west")) {
cells[x][y].west = false;
cells[x - 1][y].east = false;
cells[x - 1][y].visited = true;
path.add(new int[] { x - 1, y });
}
} else {
path.remove(path.size() - 1);
}
}
}
private boolean allVisited() {
for (Cell[] cell : cells) {
for (Cell c : cell) {
if (!c.visited) {
return false;
}
}
}
return true;
}
private boolean exists(int x, int y) {
if (x < 0 || x > this.width - 1 || y < 0 || y > this.height - 1) {
return false;
}
return true;
}
public void draw(Point start, Point goal) {
int canvasWidth = this.cellWidth * this.width + 1;
int canvasHeight = this.cellWidth * this.height + 1;
BufferedImage im = new BufferedImage(canvasWidth, canvasHeight, BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = im.createGraphics();
g2d.setColor(Color.white);
for (int x = 0; x < this.width; x++) {
for (int y = 0; y < this.height; y++) {
if (this.cells[x][y].north) {
g2d.drawLine(x * this.cellWidth, y * this.cellWidth, (x + 1) * this.cellWidth, y * this.cellWidth);
}
if (this.cells[x][y].south) {
g2d.drawLine(x * this.cellWidth, (y + 1) * this.cellWidth, (x + 1) * this.cellWidth,
(y + 1) * this.cellWidth);
}
if (this.cells[x][y].east) {
g2d.drawLine((x + 1) * this.cellWidth, y * this.cellWidth, (x + 1) * this.cellWidth,
(y + 1) * this.cellWidth);
}
if (this.cells[x][y].west) {
g2d.drawLine(x * this.cellWidth, y * this.cellWidth, x * this.cellWidth, (y + 1) * this.cellWidth);
}
}
}
if (start != null) {
drawRect(g2d, start, Color.RED);
}
if (goal != null) {
drawRect(g2d, goal, Color.GREEN);
}
displayImage(im);
}
private void drawRect(Graphics2D g2d, Point point, Color color) {
int x = point.x;
int y = point.y;
int shapeX1 = x * this.cellWidth + 2;
int shapeY1 = y * this.cellWidth + 2;
int shapeX2 = (x + 1) * this.cellWidth - 2;
int shapeY2 = (y + 1) * this.cellWidth - 2;
g2d.setColor(color);
g2d.fillRect(shapeX1, shapeY1, shapeX2 - shapeX1, shapeY2 - shapeY1);
}
private void displayImage(BufferedImage im) {
JFrame jf = new JFrame();
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.getContentPane().add(new JLabel(new ImageIcon(im)));
jf.getContentPane().setBackground(Color.black);
jf.pack();
jf.setVisible(true);
} }
Implement a method to find the shortest path using BFS to go from the start to goal and draw it on the same maze. Alongside that use DFS to find a path from the start to the goal in Java
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started