private void exitActionPerformed(java.awt.event.ActionEvent evt) { if (size14.isSelected()) { Maze.gridSize = 14; Maze.afterChoose(); this.dispose(); } else if (size19.isSelected()) { Maze.gridSize = 19; Maze.afterChoose(); this.dispose(); } else if (size20.isSelected()) { Maze.gridSize = 20; Maze.afterChoose(); this.dispose(); } else if (size21.isSelected()) { Maze.gridSize = 21; Maze.afterChoose(); this.dispose(); } else if (size27.isSelected()) { Maze.gridSize = 27; Maze.afterChoose(); this.dispose(); } else if (size28.isSelected()) { Maze.gridSize = 28; Maze.afterChoose(); this.dispose(); } else if (size29.isSelected()) { Maze.gridSize = 29; Maze.afterChoose(); this.dispose(); } else if (size30.isSelected()) { Maze.gridSize = 30; Maze.afterChoose(); this.dispose(); } }
public void paint(Graphics g) { board.drawMaze(); pacman.drawMazeMan(true); pacman.start(); }
public void actionPerformed(ActionEvent e) { if (e.getSource() == start2Button) { maze.sol = 2; timer1.start(); } if (e.getSource() == resetButton) { timer1.stop(); timer2.stop(); timer3.stop(); timer4.stop(); maze.sol = 0; this.maze.readFromFile("maze.txt"); maze.repaint(); } if (e.getSource() == start1Button) { maze.sol = 1; timer2.start(); // maze.leastVisitedNeighbour(); } if (e.getSource() == start3Button) { maze.sol = 3; timer3.start(); } if (e.getSource() == start4Button) { maze.sol = 4; maze.dijkstra(); timer4.start(); } if (maze.sol == 2) { if (!maze.cells[maze.currentX][maze.currentY].isEnd()) { maze.stepLeastVistedNeighbour(); maze.repaint(); } else { timer1.stop(); } } if (maze.sol == 1) { if (!maze.cells[maze.currentX][maze.currentY].isEnd()) { maze.randomStep(); maze.repaint(); } else { timer2.stop(); } } if (maze.sol == 3) { if (!maze.cells[maze.currentX][maze.currentY].isEnd()) { maze.stepRightHand(); maze.repaint(); } else { timer3.stop(); } } if (maze.sol == 4) { if (!maze.cells[maze.currentX][maze.currentY].isEnd()) { maze.stepDjikstra(); maze.repaint(); } else { timer4.stop(); } } }
// Recursivly goes through the maze until it finds the finish line public boolean findTheWay(Point p) { boolean northBarrier = false; boolean southBarrier = false; boolean eastBarrier = false; boolean westBarrier = false; int data = myMaze.getMazeData(p.y, p.x); // Returns false if Y position is out of range if (p.y < 0 || p.y > height) return false; // Returns false if X position is out of range if (p.x < 0 || p.x > height) return false; // Finds which paths are blocked for point P if (data > 14) { northBarrier = true; southBarrier = true; eastBarrier = true; westBarrier = true; } if (data <= 14 && data - 8 >= 0) { eastBarrier = true; data -= 8; } if (data <= 7 && data - 4 >= 0) { southBarrier = true; data -= 4; } if (data <= 3 && data - 2 >= 0) { westBarrier = true; } if (data % 2 != 0) { northBarrier = true; } // Returns true if the goal is reached if (p.equals(goal)) return true; // Adds the point to the path of points path.add(p); placesGone.add(p); // Checks for North if (!northBarrier && !placesGone.contains(new Point(p.x, p.y - 1))) { if (p.y - 1 >= 0) { if (findTheWay(new Point(p.x, p.y - 1))) { return true; } } } // Checks for South if (!southBarrier && !placesGone.contains(new Point(p.x, p.y + 1))) { if (p.y + 1 < height) { if (findTheWay(new Point(p.x, p.y + 1))) { return true; } } } // Checks for East if (!eastBarrier && !placesGone.contains(new Point(p.x + 1, p.y))) { if (p.x + 1 < width) { if (findTheWay(new Point(p.x + 1, p.y))) { return true; } } } // Checks for West if (!westBarrier && !placesGone.contains(new Point(p.x - 1, p.y))) { if (p.x - 1 >= 0) { if (findTheWay(new Point(p.x - 1, p.y))) { return true; } } } // Removes the point if its not part of the final solution path.remove(path.size() - 1); return false; }
// Creates a new maze and adds a maze listner to the maze public SolveMaze() { myMaze = new Maze(height, width); MazeListen mListener = new MazeListen(); myMaze.addMazeListener(mListener); }