目录
一 问题描述 1
-
1 课程设计题目 1
-
2 课程设计要求 1
二 需求分析 2
三 概要设计 3 -
1 程序界面设计 3
-
2 总体功能框架 3
四 详细设计 5 -
1 数据结构设计 5
-
2 具体模块设计 6
-
- 1 游戏模块 6
-
- 2 文件操作模块 9
-
- 3 编辑模块 9
-
- 4 路径提示模块 9
-
- 5 帮助模块 11
五 调试运行分析 12
- 5 帮助模块 11
-
1经典迷宫最短路径算法与本程序新算法在时间和空间效率上的对比 12
-
2 程序运行效果演示 13
六 课程设计总结 18
七 参考文献 19
二 需求分析迷宫游戏,作为一种经典的游戏,与之相关的程序可谓琳琅满目,数不胜数。在这种情况下,自己设计的程序要想有点看头,单靠黑洞洞的控制台是绝对不行的,至少要有像样一点的图形界面和简单方便的操作。
上学期刚学过C++之后,我通过自学MFC,完成了拥有图形界面的课程设计。这学期学数据结构的同时,也学习了另外一门重要的高级语言——java,所以,也想牛刀小试,玩一下java的图形界面设计。
为了达到题目中的要求,用java实现的话,显然需要用到它的图形界面、事件驱动、多线程等知识。前两部分内容,教材上讲的已基本上能帮我完成相关设计了,多线程的知识,教材上没有,但查阅相关资料(包括图书馆的纸质书籍和网上的各种论坛信息),也很容易找到设置时间限制的方法。
Java工具的问题解决了,毕竟大神们把这些工具设计的很方便和漂亮,使用起来当然不会太难,所以,剩下的才是核心部分——真正实现程序设计要求的重要算法。
用户只能自己设计迷宫玩是不太方便的,当然也不能没有这个功能,但我想在保留此功能的同时,添加一个生成随机迷宫的功能。当然这种随机不能是简单的调用java提供的random方法,这样所得到的迷宫很难保证有通路。要想设计出随机的,而且至少有一条通路的迷宫,自然要用到特殊的、巧妙的算法。另外,要求出最短路径,也是需要好的算法设计的,算法时间复杂度太高的话,用户也会等的不耐烦的。
//迷宫界面
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.io.RandomAccessFile;
import javax.swing.*;
@SuppressWarnings("serial")
public class map extends JFrame implements ActionListener, KeyListener, Runnable {
static int m, n;
static wrmPane[][] tp = null; //显示动画,同一包内类均可访问
//时间限制
static Thread timeThread; //时间控制线程
static int timelimit, remaintime;
static JPanel timePanel = new JPanel() {
//剩余时间显示面板
public void paintComponent(Graphics g) {
super.paintComponent(g);
String rt;
if (timelimit == 0) {
rt = "无限制";
setForeground(Color.GREEN); //青色表示无时间限制
} else {
rt = remaintime / 3600 + " : " + (remaintime - (remaintime / 3600) * 3600) / 60 + " : " + remaintime % 60;
if (remaintime > 10)
setForeground(Color.BLUE); //剩余时间充足时为绿色
else
setForeground(Color.RED); //剩余时间很少时为红色
}
g.drawString("剩余时间: " + rt, 220, 16);
}
};
// 菜单项
private JMenuItem m_start = new JMenuItem("开始新游戏(S)");
private JMenuItem m_time = new JMenuItem("游戏时间限制(L)");
private JMenuItem m_return = new JMenuItem("返回主界面(R)");
private JMenuItem m_exit = new JMenuItem("退出游戏(Q)");
private JMenuItem m_savefile = new JMenuItem("保存迷宫结构(W)");
private JMenuItem m_importfile = new JMenuItem("导入迷宫结构(I)");
private JMenuItem m_selfconfig = new JMenuItem("编辑当前迷宫(E)");
private JMenuItem m_randommake = new JMenuItem("随机生成迷宫(Z)");
private JMenuItem m_sortpath = new JMenuItem("显示最短路径(T)");
private JMenuItem m_DFSpath = new JMenuItem("随意显示一个路径(K)");
private JMenuItem m_help = new JMenuItem("游戏使用说明(H)");
private JMenuItem m_about = new JMenuItem("关于迷宫游戏(A)");
@SuppressWarnings("deprecation")
map(int x, int y) {
m = x;
n = y;
tp = new wrmPane[m][n];
timelimit = remaintime = 0; //初始化时,时间为0,代表没有时间限制
timeThread = new Thread(this);
timeThread.start();
timeThread.suspend();
//菜单
JMenu game = new JMenu("游戏");
JMenu file = new JMenu("文件");
JMenu edit = new JMenu("编辑");
JMenu tip = new JMenu("提示");
JMenu help = new JMenu("帮助");
game.add(m_start);
game.add(m_time);
game.add(m_return);
game.add(m_exit);
file.add(m_savefile);
file.add(m_importfile);
edit.add(m_selfconfig);
edit.add(m_randommake);
tip.add(m_sortpath);
tip.add(m_DFSpath);
help.add(m_help);
help.add(m_about);
//菜单栏
JMenuBar menu = new JMenuBar();
menu.add(game);
menu.add(file);
menu.add(edit);
menu.add(tip);
menu.add(help);
//初始化迷宫组件,并生成随机路径
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++) {
tp[i][j] = new wrmPane();
}
Operations.creatMaze(); //深度优先遍历生成至少有一条随机通道的迷宫
//迷宫地图
JPanel mazePane = new JPanel();
mazePane.setLayout(new GridLayout(m, n, 0, 0));
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
mazePane.add(tp[i][j]);
}
}
//菜单和时间显示放在同一面板上
JPanel northPanel = new JPanel();
northPanel.setLayout(new GridLayout(1, 1));
northPanel.add(menu);
northPanel.add(timePanel);
timePanel.setBackground(new Color(245, 240, 245));
menu.setBackground(new Color(245, 240, 245));
//添加到框架
setLayout(new BorderLayout());
add(northPanel, BorderLayout.NORTH);
add(mazePane, BorderLayout.CENTER);
add(new JPanel(), BorderLayout.SOUTH);
//注册监听器
m_start.addActionListener(this);
m_time.addActionListener(this);
m_return.addActionListener(this);
m_exit.addActionListener(this);
m_savefile.addActionListener(this);
m_importfile.addActionListener(this);
m_selfconfig.addActionListener(this);
m_randommake.addActionListener(this);
m_sortpath.addActionListener(this);
m_DFSpath.addActionListener(this);
m_help.addActionListener(this);
m_about.addActionListener(this);
addKeyListener(this);
//基本设置
setTitle("迷宫游戏");
setSize(850, 650);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
setResizable(false);
}
map() {
this(25, 25);
}