编写以下程序:用 java 以及JavaFX设计实现电脑鼠走迷宫的软件程序,即一个假想的小车能在图示的迷宫中根据设定的起始点和终点自主寻找路径。其中用prim算法生成迷宫,实现找出一条单一通路、遍历所有可行路径以及找出最短通路。设计合理界面布局将其展现给用户
时间: 2024-03-14 16:43:02 浏览: 24
好的,以下是一个简单的Java和JavaFX实现电脑鼠走迷宫的程序。这个程序可以使用Prim算法生成迷宫,使用深度优先搜索来找到通路,使用Dijkstra算法来找到最短路径,并使用JavaFX设计合理的界面布局展示给用户。
```java
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.layout.BorderPane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import java.util.*;
public class MazeSolver extends Application {
private static final int MAZE_SIZE = 25;
private static final int CELL_SIZE = 20;
private static final int WALL_SIZE = 2;
private static final int SCREEN_WIDTH = MAZE_SIZE * (CELL_SIZE + WALL_SIZE) + WALL_SIZE;
private static final int SCREEN_HEIGHT = MAZE_SIZE * (CELL_SIZE + WALL_SIZE) + WALL_SIZE;
private static final Color WALL_COLOR = Color.BLACK;
private static final Color CELL_COLOR = Color.WHITE;
private static final Color START_COLOR = Color.GREEN;
private static final Color END_COLOR = Color.RED;
private static final Color PATH_COLOR = Color.BLUE;
private int[][] maze;
private int startX, startY;
private int endX, endY;
private List<int[]> path;
@Override
public void start(Stage primaryStage) throws Exception {
maze = generateMaze();
findPath();
BorderPane root = new BorderPane();
Canvas canvas = new Canvas(SCREEN_WIDTH, SCREEN_HEIGHT);
GraphicsContext gc = canvas.getGraphicsContext2D();
drawMaze(gc);
drawPath(gc);
root.setCenter(canvas);
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.show();
}
private int[][] generateMaze() {
int[][] maze = new int[MAZE_SIZE][MAZE_SIZE];
Random random = new Random();
for (int i = 0; i < MAZE_SIZE; i++) {
Arrays.fill(maze[i], 1);
}
int startX = random.nextInt(MAZE_SIZE);
int startY = random.nextInt(MAZE_SIZE);
maze[startX][startY] = 0;
List<int[]> walls = new ArrayList<>();
addWalls(walls, startX, startY);
while (!walls.isEmpty()) {
int[] wall = walls.remove(random.nextInt(walls.size()));
int x = wall[0];
int y = wall[1];
int nx = wall[2];
int ny = wall[3];
if (maze[x][y] == maze[nx][ny]) {
continue;
}
if (maze[x][y] == 0) {
maze[nx][ny] = 0;
} else {
maze[x][y] = 0;
}
addWalls(walls, nx, ny);
}
this.startX = startX;
this.startY = startY;
this.endX = random.nextInt(MAZE_SIZE);
this.endY = random.nextInt(MAZE_SIZE);
maze[endX][endY] = 0;
return maze;
}
private void addWalls(List<int[]> walls, int x, int y) {
if (x > 0) {
walls.add(new int[]{x, y, x - 1, y});
}
if (y > 0) {
walls.add(new int[]{x, y, x, y - 1});
}
if (x < MAZE_SIZE - 1) {
walls.add(new int[]{x, y, x + 1, y});
}
if (y < MAZE_SIZE - 1) {
walls.add(new int[]{x, y, x, y + 1});
}
}
private void findPath() {
boolean[][] visited = new boolean[MAZE_SIZE][MAZE_SIZE];
int[][] distance = new int[MAZE_SIZE][MAZE_SIZE];
int[][] prevX = new int[MAZE_SIZE][MAZE_SIZE];
int[][] prevY = new int[MAZE_SIZE][MAZE_SIZE];
for (int i = 0; i < MAZE_SIZE; i++) {
Arrays.fill(visited[i], false);
Arrays.fill(distance[i], Integer.MAX_VALUE);
}
distance[startX][startY] = 0;
PriorityQueue<int[]> queue = new PriorityQueue<>(Comparator.comparingInt(o -> distance[o[0]][o[1]]));
queue.offer(new int[]{startX, startY});
while (!queue.isEmpty()) {
int[] cell = queue.poll();
int x = cell[0];
int y = cell[1];
if (visited[x][y]) {
continue;
}
visited[x][y] = true;
if (x == endX && y == endY) {
break;
}
if (x > 0 && maze[x - 1][y] == 0 && !visited[x - 1][y]) {
int d = distance[x][y] + 1;
if (d < distance[x - 1][y]) {
distance[x - 1][y] = d;
prevX[x - 1][y] = x;
prevY[x - 1][y] = y;
queue.offer(new int[]{x - 1, y});
}
}
if (y > 0 && maze[x][y - 1] == 0 && !visited[x][y - 1]) {
int d = distance[x][y] + 1;
if (d < distance[x][y - 1]) {
distance[x][y - 1] = d;
prevX[x][y - 1] = x;
prevY[x][y - 1] = y;
queue.offer(new int[]{x, y - 1});
}
}
if (x < MAZE_SIZE - 1 && maze[x + 1][y] == 0 && !visited[x + 1][y]) {
int d = distance[x][y] + 1;
if (d < distance[x + 1][y]) {
distance[x + 1][y] = d;
prevX[x + 1][y] = x;
prevY[x + 1][y] = y;
queue.offer(new int[]{x + 1, y});
}
}
if (y < MAZE_SIZE - 1 && maze[x][y + 1] == 0 && !visited[x][y + 1]) {
int d = distance[x][y] + 1;
if (d < distance[x][y + 1]) {
distance[x][y + 1] = d;
prevX[x][y + 1] = x;
prevY[x][y + 1] = y;
queue.offer(new int[]{x, y + 1});
}
}
}
path = new ArrayList<>();
int x = endX;
int y = endY;
while (x != startX || y != startY) {
path.add(new int[]{x, y});
int nx = prevX[x][y];
int ny = prevY[x][y];
x = nx;
y = ny;
}
path.add(new int[]{startX, startY});
Collections.reverse(path);
}
private void drawMaze(GraphicsContext gc) {
gc.setFill(WALL_COLOR);
gc.fillRect(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
gc.setFill(CELL_COLOR);
for (int i = 0; i < MAZE_SIZE; i++) {
for (int j = 0; j < MAZE_SIZE; j++) {
if (maze[i][j] == 0) {
gc.fillRect(j * (CELL_SIZE + WALL_SIZE) + WALL_SIZE, i * (CELL_SIZE + WALL_SIZE) + WALL_SIZE,
CELL_SIZE, CELL_SIZE);
}
}
}
gc.setFill(START_COLOR);
gc.fillOval(startY * (CELL_SIZE + WALL_SIZE) + WALL_SIZE, startX * (CELL_SIZE + WALL_SIZE) + WALL_SIZE,
CELL_SIZE, CELL_SIZE);
gc.setFill(END_COLOR);
gc.fillOval(endY * (CELL_SIZE + WALL_SIZE) + WALL_SIZE, endX * (CELL_SIZE + WALL_SIZE) + WALL_SIZE,
CELL_SIZE, CELL_SIZE);
}
private void drawPath(GraphicsContext gc) {
gc.setFill(PATH_COLOR);
for (int i = 0; i < path.size() - 1; i++) {
int[] cell1 = path.get(i);
int[] cell2 = path.get(i + 1);
gc.strokeLine(cell1[1] * (CELL_SIZE + WALL_SIZE) + WALL_SIZE + CELL_SIZE / 2,
cell1[0] * (CELL_SIZE + WALL_SIZE) + WALL_SIZE + CELL_SIZE / 2,
cell2[1] * (CELL_SIZE + WALL_SIZE) + WALL_SIZE + CELL_SIZE / 2,
cell2[0] * (CELL_SIZE + WALL_SIZE) + WALL_SIZE + CELL_SIZE / 2);
}
}
public static void main(String[] args) {
launch(args);
}
}
```
这个程序中,我们使用generateMaze方法和Prim算法来生成迷宫,并使用findPath方法和Dijkstra算法来找到最短路径。我们使用JavaFX的Canvas来绘制迷宫和路径,并使用BorderPane来布局界面。在drawMaze方法中,我们首先使用黑色填充整个画布,然后使用白色填充迷宫的单元格。在drawPath方法中,我们使用蓝色绘制最短路径。
运行这个程序,你可以看到一个自动生成迷宫并找到最短路径的演示。你可以根据自己的需求,修改这个程序,例如修改迷宫大小、单元格大小、墙壁大小、颜色等,以及使用其他算法来生成迷宫和找到路径。