用Java实现一个迷宫游戏,并且在游戏运行期间可以计时
时间: 2024-03-06 08:50:08 浏览: 17
好的,以下是一个Java实现的迷宫游戏,其中包含计时器功能:
```java
import java.util.*;
import java.text.*;
public class Maze {
private int[][] maze;
private int row;
private int col;
private int startX;
private int startY;
private int endX;
private int endY;
private long startTime;
private long endTime;
public Maze(int row, int col) {
this.row = row;
this.col = col;
maze = new int[row][col];
}
public void generateMaze() {
Random rand = new Random();
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
if (i == 0 || j == 0 || i == row - 1 || j == col - 1) {
maze[i][j] = 1; // 设置边界为墙壁
} else {
maze[i][j] = rand.nextInt(2); // 随机生成空地或墙壁
}
}
}
startX = rand.nextInt(row - 2) + 1;
startY = rand.nextInt(col - 2) + 1;
endX = rand.nextInt(row - 2) + 1;
endY = rand.nextInt(col - 2) + 1;
maze[startX][startY] = 2; // 设置起点
maze[endX][endY] = 3; // 设置终点
}
public void printMaze() {
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
if (maze[i][j] == 0) {
System.out.print(" "); // 空地
} else if (maze[i][j] == 1) {
System.out.print("██"); // 墙壁
} else if (maze[i][j] == 2) {
System.out.print("S "); // 起点
} else if (maze[i][j] == 3) {
System.out.print("E "); // 终点
}
}
System.out.println();
}
}
public void findPath() {
Queue<int[]> queue = new LinkedList<>();
boolean[][] visited = new boolean[row][col];
int[][] directions = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
queue.offer(new int[]{startX, startY, 0});
visited[startX][startY] = true;
while (!queue.isEmpty()) {
int[] pos = queue.poll();
int x = pos[0];
int y = pos[1];
int step = pos[2];
if (x == endX && y == endY) {
System.out.println("最佳路径长度为:" + step);
return;
}
for (int[] direction : directions) {
int newX = x + direction[0];
int newY = y + direction[1];
if (newX >= 0 && newX < row && newY >= 0 && newY < col && !visited[newX][newY] && maze[newX][newY] != 1) {
queue.offer(new int[]{newX, newY, step + 1});
visited[newX][newY] = true;
}
}
}
System.out.println("没有找到最佳路径!");
}
public void play() {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入迷宫的行数:");
int row = scanner.nextInt();
System.out.print("请输入迷宫的列数:");
int col = scanner.nextInt();
Maze maze = new Maze(row, col);
maze.generateMaze();
maze.printMaze();
startTime = System.currentTimeMillis();
while (true) {
System.out.print("请输入移动方向(上:w,下:s,左:a,右:d):");
String direction = scanner.next();
int newX = startX;
int newY = startY;
if (direction.equals("w")) {
newX--;
} else if (direction.equals("s")) {
newX++;
} else if (direction.equals("a")) {
newY--;
} else if (direction.equals("d")) {
newY++;
}
if (newX >= 0 && newX < row && newY >= 0 && newY < col && maze.maze[newX][newY] != 1) {
maze.maze[startX][startY] = 0;
maze.maze[newX][newY] = 2;
startX = newX;
startY = newY;
maze.printMaze();
if (startX == endX && startY == endY) {
endTime = System.currentTimeMillis();
long duration = endTime - startTime;
SimpleDateFormat sdf = new SimpleDateFormat("mm:ss");
sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
System.out.println("恭喜你,成功走出迷宫!");
System.out.println("用时:" + sdf.format(duration));
break;
}
} else {
System.out.println("不能移动到该位置!");
}
}
}
public static void main(String[] args) {
Maze maze = new Maze(10, 10);
maze.generateMaze();
maze.printMaze();
maze.findPath();
maze.play();
}
}
```
在这个迷宫游戏中,我们使用了Java的Scanner类来读取用户输入,使用了Java的Queue接口来实现广度优先搜索算法,使用了Java的System.currentTimeMillis()方法来计时,使用了Java的SimpleDateFormat类来格式化时间。您可以根据需要修改代码以满足您的需求。