用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类来格式化时间。您可以根据需要修改代码以满足您的需求。

相关推荐

最新推荐

recommend-type

Java 实现倒计时功能(由秒计算天、小时、分钟、秒)

最近做项目遇到这样的需求,天、小时、分钟、秒的数值都是隔开的,服务器端只返回一个时间戳长度,怎么实现这样的功能呢?下面小编给大家带来了Java 实现倒计时功能的方案,需要的朋友参考下吧
recommend-type

Java 获取当前时间及实现时间倒计时功能【推荐】

主要介绍了Java 获取当前时间及实现时间倒计时功能 ,本文通过实例代码给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
recommend-type

Java计时新姿势StopWatch详解

主要介绍了Java计时新姿势StopWatch,最近公司来了个大佬,从他那里学到不少东西,其中一个就是计时的新姿势「StopWatch」,需要的朋友可以参考下
recommend-type

用软件延时实现00-59秒单片机计时器

用软件延时实现00-59秒单片机计时器:在AT89S51单片机的P0和P2端口分别接有两个共阴数码管,P0口驱动显示秒时间的十位,而P2口驱动显示秒时间的个位。
recommend-type

组成原理课程实验:MIPS 流水线CPU、实现36条指令、转发、冒险检测-内含源码和说明书.zip

组成原理课程实验:MIPS 流水线CPU、实现36条指令、转发、冒险检测-内含源码和说明书.zip
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

用 Python 画一个可以动的爱心

好的,我可以帮助您使用Python来绘制一个动态的爱心。您可以使用turtle库来实现。以下是实现代码: ```python import turtle import math # 设置画布和画笔 canvas = turtle.Screen() canvas.bgcolor("black") pencil = turtle.Turtle() pencil.speed(0) pencil.color("red", "pink") pencil.pensize(3) # 定义爱心函数 def draw_love(heart_size, x_offset=0, y_offset=0):
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。