返回
用Java语言编写的迷宫小游戏软件
闲谈
2024-02-05 20:50:57
import java.util.Scanner;
public class Maze {
// 迷宫的二维数组
private char[][] maze;
// 迷宫的宽和高
private int width;
private int height;
// 起始点和终点坐标
private int startX;
private int startY;
private int endX;
private int endY;
// 构造函数
public Maze(char[][] maze) {
this.maze = maze;
this.width = maze[0].length;
this.height = maze.length;
// 查找起始点和终点坐标
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
if (maze[i][j] == 'S') {
startX = i;
startY = j;
} else if (maze[i][j] == 'E') {
endX = i;
endY = j;
}
}
}
}
// 迷宫的宽度
public int getWidth() {
return width;
}
// 迷宫的高度
public int getHeight() {
return height;
}
// 起始点横坐标
public int getStartX() {
return startX;
}
// 起始点纵坐标
public int getStartY() {
return startY;
}
// 终点横坐标
public int getEndX() {
return endX;
}
// 终点纵坐标
public int getEndY() {
return endY;
}
// 判断某个位置是否为墙
public boolean isWall(int x, int y) {
return maze[x][y] == '#';
}
// 判断某个位置是否为终点
public boolean isEnd(int x, int y) {
return maze[x][y] == 'E';
}
// 查找从起始点到终点的路径
public String findPath() {
// 使用广度优先搜索算法
Queue<Point> queue = new LinkedList<>();
queue.offer(new Point(startX, startY));
// 记录每个位置的父节点
Map<Point, Point> parentMap = new HashMap<>();
// 标记已经访问过的位置
Set<Point> visited = new HashSet<>();
// 当队列不为空时
while (!queue.isEmpty()) {
// 取出队列中的第一个元素
Point current = queue.poll();
// 将该元素标记为已访问
visited.add(current);
// 检查该元素是否是终点
if (isEnd(current.x, current.y)) {
// 如果是终点,则返回路径
return getPath(current, parentMap);
}
// 检查该元素的四个方向是否可以通行
for (int i = -1; i <= 1; i++) {
for (int j = -1; j <= 1; j++) {
// 跳过当前元素本身
if (i == 0 && j == 0) {
continue;
}
// 计算相邻位置的坐标
int nextX = current.x + i;
int nextY = current.y + j;
// 检查相邻位置是否合法
if (nextX >= 0 && nextX < height && nextY >= 0 && nextY < width) {
// 检查相邻位置是否不是墙且没有被访问过
if (!isWall(nextX, nextY) && !visited.contains(new Point(nextX, nextY))) {
// 将相邻位置加入队列
queue.offer(new Point(nextX, nextY));
// 记录相邻位置的父节点
parentMap.put(new Point(nextX, nextY), current);
}
}
}
}
}
// 如果没有找到路径,则返回空字符串
return "";
}
// 根据父节点记录获取路径
private String getPath(Point current, Map<Point, Point> parentMap) {
// 将路径存储在栈中
Stack<Point> stack = new Stack<>();
// 从终点开始回溯路径
while (current != null) {
stack.push(current);
current = parentMap.get(current);
}
// 将路径从栈中取出并拼接成字符串
StringBuilder path = new StringBuilder();
while (!stack.isEmpty()) {
Point point = stack.pop();
path.append('(').append(point.x).append(',').append(point.y).append(") ");
}
// 返回路径字符串
return path.toString();
}
// 主函数
public static void main(String[] args) {
// 定义迷宫的二维数组
char[][] maze = {
{'#', '#', '#', '#', '#', '#', '#', '#', '#', '#'},
{'#', '.', '.', '.', '.', '.', '.', '.', '.', '#'},
{'#', '.', '#', '#', '#', '#', '#', '#', '.', '#'},
{'#', '.', '.', '.', '.', '.', '.', '.', '.', '#'},
{'#', '#', '#', '#', '#', '#', '#', '#', '.', '#'},
{'#', '.', '.', '.', '.', '.', '.', '.', '.', '#'},
{'#', '#', '#', '#', '#', '#', '#', '#', '.', '#'},
{'#', '.', '.', '.', '.', '.', '.', '.', 'E', '#'},
{'#', '#', '#', '#', '#', '#', '#', '#', '#', '#'}
};
// 创建迷宫对象
Maze mazeObj = new Maze(maze);
// 查找从起始点到终点的路径
String path = mazeObj.findPath();
// 打印路径
System.out.println("从起始点到终点的路径为:" + path);
}
}
class Point {
int x;
int y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Point point = (Point) o;
return x == point.x && y == point.y;
}
@Override
public int hashCode() {
return Objects.hash(x, y);
}
}
用Java语言编写的迷宫小游戏软件是一款非常有趣的小游戏,玩家可以在游戏中体验到解谜的乐趣。游戏操作简单,玩家只需要控制一个小球,在迷宫中找到出口即可。迷宫中有一些障碍物,玩家需要避开这些障碍物,才能到达出口。游戏提供了多种迷宫关卡,玩家可以根据自己的能力选择不同难度的关卡。游戏还提供了排行榜功能,玩家可以在排行榜上查看自己的成绩,与其他玩家进行比较。
这款迷宫小游戏软件非常适合用来打发时间,玩家可以在游戏中体验到解谜的乐趣。游戏操作简单,玩家只需要控制一个小球,在迷宫中找到出口即可。迷宫中有一些障碍物,玩家需要避开这些障碍物,才能到达出口。游戏提供了多种迷宫关卡,玩家可以根据自己的能力选择不同难度的关卡。游戏还提供了排行榜功能,玩家可以在排行榜上查看自己的成绩,与其他玩家进行比较。
这款迷宫小游戏软件非常适合用来打发时间,玩家可以在游戏中体验到解谜的乐趣。游戏操作简单,玩家只需要控制一个小球,在迷宫中找到出口即可。迷宫中有一些障碍物,玩家需要避开这些障碍物,才能到达出口。游戏提供了多种迷宫关卡,玩家可以根据自己的能力选择不同难度的关卡。游戏还提供了排行榜功能,玩家可以在排行榜上查看自己的成绩,与其他玩家进行比较。