Question: Hello. Can somebody help me finish this maze and stack on java? My assignment is to create a basic maze solving algorithm to find its
Hello. Can somebody help me finish this maze and stack on java? My assignment is to create a basic maze solving algorithm to find its way from the top left corner of the maze to the bottom right.
import java.awt.Color; import java.awt.Dimension; import java.awt.GridLayout; import java.util.LinkedList; import java.util.Queue; import java.util.Stack; import javax.swing.JPanel; public class MazeGridPanel extends JPanel{ private int rows; private int cols; private Cell[][] maze; // extra credit public void genDFSMaze() { boolean[][] visited; Stack stack = new Stack(); Cell start = maze[0][0]; stack.push(start); } //homework public void solveMaze() { Stack stack = new Stack(); Cell start = maze[0][0]; start.setBackground(Color.GREEN); Cell finish = maze[rows-1][cols-1]; finish.setBackground(Color.RED); stack.push(start); } public void solveMazeQueue() { Queue toVisitNext = new LinkedList| (); Cell start = maze[0][0]; start.setBackground(Color.CYAN); Cell finish = maze[rows -5][cols - 5]; finish.setBackground(Color.RED); toVisitNext.offer(start); boolean done = false; while(!done && !toVisitNext.isEmpty()) { Cell current = toVisitNext.poll(); current.setBackground(Color.BLUE); if(current == finish) { done = true; } else { if( !current.northWall && !this.visited(current.row-1, current.col) ) { toVisitNext.offer(maze[current.row-1][current.col]); maze[current.row-1][current.col].setBackground(Color.GREEN); } if( !current.southWall && !this.visited(current.row+1, current.col) ) { toVisitNext.offer(maze[current.row+1][current.col]); maze[current.row+1][current.col].setBackground(Color.GREEN); } if( !current.eastWall && !this.visited(current.row, current.col+1) ) { toVisitNext.offer(maze[current.row][current.col+1]); maze[current.row][current.col+1].setBackground(Color.GREEN); } if( !current.westWall && !this.visited(current.row, current.col-1) ) { toVisitNext.offer(maze[current.row][current.col-1]); maze[current.row][current.col-1].setBackground(Color.GREEN); } } } } public boolean visited(int row, int col) { Cell c = maze[row][col]; Color status = c.getBackground(); if(status.equals(Color.WHITE) || status.equals(Color.RED) ) { return false; } return true; } public void genNWMaze() { for(int row = 0; row < rows; row++) { for(int col = 0; col < cols; col++) { if(row == 0 && col ==0) { continue; } else if(row ==0) { maze[row][col].westWall = false; maze[row][col-1].eastWall = false; } else if(col == 0) { maze[row][col].northWall = false; maze[row-1][col].southWall = false; }else { boolean north = Math.random() < 0.5; if(north ) { maze[row][col].northWall = false; maze[row-1][col].southWall = false; } else { // remove west maze[row][col].westWall = false; maze[row][col-1].eastWall = false; } maze[row][col].repaint(); } } } this.repaint(); } public MazeGridPanel(int rows, int cols) { this.setPreferredSize(new Dimension(800,800)); this.rows = rows; this.cols = cols; this.setLayout(new GridLayout(rows,cols)); this.maze = new Cell[rows][cols]; for(int row = 0 ; row < rows ; row++) { for(int col = 0; col < cols; col++) { maze[row][col] = new Cell(row,col); this.add(maze[row][col]); } } this.genNWMaze(); this.solveMaze(); } } | | | | | |
import java.awt.GridLayout;
import javax.swing.JFrame; import javax.swing.JPanel;
public class Maze extends JFrame {
public Maze() { this.add(new MazeGridPanel(5,5)); this.setSize(800, 800); this.setDefaultCloseOperation(EXIT_ON_CLOSE); this.pack(); this.setVisible(true); } public static void main(String[] args) { new Maze(); } }
import java.awt.*;
import javax.swing.*;
public class Cell extends JPanel{
boolean northWall = true; boolean southWall = true; boolean eastWall = true; boolean westWall = true; int row; int col; public void paintComponent(Graphics g) { super.paintComponent(g); g.setColor(Color.BLACK); //g.drawString(row +"," + col, 10, 10); int y = this.getHeight(); int x = this.getWidth(); g.setColor(Color.BLACK); if(northWall) {
g.drawLine(0,0,x, 0); }if(southWall) { g.drawLine(0,y,x, y); }if(eastWall) { g.drawLine(x,0,x, y); }if(westWall) { g.drawLine(0,0,0, y); } } public Cell(int row, int col ) { this.setBackground(Color.WHITE); this.row = row; this.col = col; northWall = true; southWall = true; eastWall = true; westWall = true;
} }