用javafx生成随即迷宫prim算法
时间: 2023-12-02 13:30:31 浏览: 57
随机生成迷宫算法及其 Java 实现
好的,我可以为您提供一个实现Prim算法生成随机迷宫的JavaFX程序。以下是程序代码:
```java
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import javafx.animation.Animation;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.util.Duration;
public class RandomMaze extends Application {
private static final int WIDTH = 600;
private static final int HEIGHT = 600;
private static final int CELL_SIZE = 20;
private static final int NUM_ROWS = HEIGHT / CELL_SIZE;
private static final int NUM_COLS = WIDTH / CELL_SIZE;
private static final int FRAME_RATE = 30;
private static final Random random = new Random();
private static final int WALL = 1;
private static final int EMPTY = 0;
private int[][] maze = new int[NUM_ROWS][NUM_COLS];
private Timeline timeline;
private boolean generatingMaze = false;
@Override
public void start(Stage primaryStage) throws Exception {
Canvas canvas = new Canvas(WIDTH, HEIGHT);
GraphicsContext gc = canvas.getGraphicsContext2D();
gc.setFill(Color.WHITE);
gc.fillRect(0, 0, WIDTH, HEIGHT);
primaryStage.setScene(new Scene(canvas));
primaryStage.show();
canvas.setOnMouseClicked(event -> {
if (!generatingMaze) {
generateMaze();
}
});
}
private void generateMaze() {
generatingMaze = true;
for (int i = 0; i < NUM_ROWS; i++) {
for (int j = 0; j < NUM_COLS; j++) {
maze[i][j] = WALL;
}
}
int startRow = random.nextInt(NUM_ROWS);
int startCol = random.nextInt(NUM_COLS);
List<int[]> walls = getWalls(startRow, startCol);
Collections.shuffle(walls);
maze[startRow][startCol] = EMPTY;
timeline = new Timeline(new KeyFrame(Duration.millis(1000 / FRAME_RATE), event -> {
if (!walls.isEmpty()) {
int[] wall = walls.remove(walls.size() - 1);
int row = wall[0];
int col = wall[1];
if (isWall(row, col) && hasEmptyNeighbor(row, col)) {
maze[row][col] = EMPTY;
drawCell(row, col, Color.WHITE);
List<int[]> newWalls = getWalls(row, col);
walls.addAll(newWalls);
Collections.shuffle(walls);
}
} else {
timeline.stop();
generatingMaze = false;
}
}));
timeline.setCycleCount(Animation.INDEFINITE);
timeline.play();
}
private boolean isWall(int row, int col) {
return maze[row][col] == WALL;
}
private boolean hasEmptyNeighbor(int row, int col) {
for (int i = -1; i <= 1; i++) {
for (int j = -1; j <= 1; j++) {
if (i == 0 && j == 0) {
continue;
}
int neighborRow = row + i;
int neighborCol = col + j;
if (neighborRow < 0 || neighborRow >= NUM_ROWS || neighborCol < 0 || neighborCol >= NUM_COLS) {
continue;
}
if (maze[neighborRow][neighborCol] == EMPTY) {
return true;
}
}
}
return false;
}
private List<int[]> getWalls(int row, int col) {
List<int[]> walls = new ArrayList<>();
if (row > 0) {
walls.add(new int[] {row - 1, col});
}
if (row < NUM_ROWS - 1) {
walls.add(new int[] {row + 1, col});
}
if (col > 0) {
walls.add(new int[] {row, col - 1});
}
if (col < NUM_COLS - 1) {
walls.add(new int[] {row, col + 1});
}
return walls;
}
private void drawCell(int row, int col, Color color) {
GraphicsContext gc = ((Canvas) timeline.getNode()).getGraphicsContext2D();
gc.setFill(color);
gc.fillRect(col * CELL_SIZE, row * CELL_SIZE, CELL_SIZE, CELL_SIZE);
}
public static void main(String[] args) {
launch(args);
}
}
```
该程序使用JavaFX绘制随机迷宫,通过Prim算法生成迷宫。程序会在单击窗口时随机生成一个起点,并从该点开始生成迷宫。
程序使用一个二维数组`maze`表示迷宫,数组中的元素为0表示空白,为1表示墙。在`generateMaze()`方法中,程序先将整个迷宫初始化为墙,然后随机选择一个起点,并将其设为0(空白)。
接着,程序从起点开始,获取所有与之相邻的墙,并将这些墙随机打乱顺序。然后程序从墙的列表中取出一个墙,如果该墙旁边有空白格子,则将该墙设为0(空白),并将新生成的墙加入墙的列表中。程序不断重复上述过程,直到墙的列表为空。
程序使用JavaFX的动画来演示迷宫生成的过程,每秒钟刷新30次。在每一次刷新时,程序从墙的列表中取出一个墙,如果该墙旁边有空白格子,则将该墙设为0(空白),并将新生成的墙加入墙的列表中。程序在完成所有操作后停止动画演示,并将`generatingMaze`变量设为false,表示迷宫已经生成完毕。
如果您希望了解更多关于JavaFX的内容,可以查看以下链接:
- JavaFX官方文档:https://openjfx.io/javadoc/16/
- JavaFX教程:https://docs.oracle.com/javafx/2/get_started/jfxpub-get_started.htm
阅读全文