java扫雷游戏课程设计报告Word文档下载推荐.docx
《java扫雷游戏课程设计报告Word文档下载推荐.docx》由会员分享,可在线阅读,更多相关《java扫雷游戏课程设计报告Word文档下载推荐.docx(33页珍藏版)》请在冰点文库上搜索。
![java扫雷游戏课程设计报告Word文档下载推荐.docx](https://file1.bingdoc.com/fileroot1/2023-5/5/60cdc72f-b153-41cc-92b4-b1041e77a103/60cdc72f-b153-41cc-92b4-b1041e77a1031.gif)
2.需求分析
2.1扫雷棋盘的布局设计
系统的整体布局为:
CardLayout布局,采用了菜单、按钮、面板…等组件,菜单主要包括开始,选择级别,标记,扫雷英雄榜,退出,按钮的功能是重新开始新的游戏。
2.2雷区的设计
MineArea类是javax.swing包中Jpanel容器的子类,实现了ActionListener和MouseListener接口,所创建的对象:
mineArea是MineGame类中最重要的成员之一,作为一个容器添加到MineGame窗口的中心。
标明MineArea类的主要成员变量、方法以及和MineGame类之间组合关系的UML图如图2-1所示。
图2-1MineArea类的UML图
以下是UML图中有关数据和方法的详细说明。
1、成员变量
(1)block是Block类型的数组,用来确定雷区有多少需进行扫雷的方块。
(2)blockView是BlockView类型的数组,负责为block数组中的Block对象提供视图。
(3)lay是LayMines类型的对象,负责设置block数组中的哪些方块是雷或不是雷。
(4)record负责提供保存成绩的界面,是一个对话框,默认不可见。
用户只有扫雷成功后(用时最少),才可以看见对话框。
(5)reStart是一个按钮对象,用户单击它重新开始游戏。
(6)time是计时器对象,负责计算用户的用时。
2、方法
(1)initMineArea(int,int,int,int)方法可根据参数提供的数据设置雷区的宽度、高度、雷的数目以及雷区的级别。
(2)actionPerformed(ActionEvent)是MineArea类实现的ActionListener接口中的方法。
当用户单击blockView中的某个方块时,actionPerformed(ActionEvent)方法负责执行有关算法,例如,当用鼠标左键单击方块上的按钮后,若该方块下有雷,actionPerformed(ActionEvent)方法将使用户输掉本局,若该方块下无雷,actionPerformed(ActionEvent)方法将显示blockView对象中的标签,该标签上是一个数字,该数字代表当前方块的周围的8个方块中共有多少颗雷。
(3)show()方法是一个递归方法。
actionPerformed(ActionEvent)方法执行时将调用show方法进行扫雷。
(4)mousePressed(MouseEvent)方法是MineArea类实现的MouseListener接口中的方法,当用户按下鼠标有件事mousePressed(MouseEvent)方法负责让方块上显示一个探雷标记。
(5)inquireWin()方法用来判断用户是否扫雷成功,如果成功该方法负责让record对话框可见。
所谓扫雷成功是指不仅找到了全部的累而且用时最少。
综上所述,MineArea创建的扫雷区域效果如图2.2.2所示。
2.3音效的设计
music类是java.awt包中Frame的子类,通过构造音效声音方法,在MainFrame主类中,通过ActionListener来监视是否已开始游戏,来决定播放计时声,之后又通过ActionListener来监视是否踩到雷和是否完成来决定,播放相应的音乐。
下面标明music类的主要成员变量、方法以及和MineFrame类之间组合关系的UML图如图2-2所示。
图2-2MineArea类的UML图
2.4排行榜设计
LogDialog是javax.swing包中Jdialog对话框的子类,并实现了ActiongListener接口。
LogDialog创建的对象dialog是MainFrame类(主类)的成员之一,当用户选择查看“英雄榜”菜单项时,该对话框可见,提供了查看扫雷的最好成绩的界面。
标明LogDialog类的主要成员变量、方法以及和MainFrame类之间组合关系的UML图如图2-3所示。
图2-3LogDialog类的UML图
3.总体设计
3.1总体功能图
图2-4总体功能图
3.2总体流程图
图2-5总体流程图
4.主要功能设计流程
4.1雷区设计流程图
图2-6雷区设计流程图
4.2音效设计流程图
图2-7音效设计流程图
4.3排行榜设计流程图
图2-8排行榜设计流程图
5.代码实现
5.1类之间关系图
图2-9类之间关系图
5.2运行效果图
图2-10运行主界面效果图
图2-11游戏失败效果图
图2-12自定义雷区效果图
图2-13游戏胜利效果图
图2-14扫雷英雄榜效果图
5.3程序打包发布过程
可以使用jar.exe命令制作JAR文件来发布编写的软件。
(1)首先用文本编辑器,如Windows下的“记事本”,编写一个清单文件:
Mymoon.mf
Manifest-Version:
1.0
Main-Class:
MineGame
Created-By:
1.5(SunMicrosystemsInc.)
将Mymoon.mf保存到G:
\Java课程设计\扫雷游戏1\扫雷游戏33中,即和应用程序所用的字节码文件保存在相同的目录中。
注意:
清单文件中的“Manifest-Version:
”和1.0、“Main-Class”和主类MineGame之间以及“Created-By:
”和1.5之间必须有且只有一个空格。
(2)生成JAR文件。
G:
\Java课程设计\扫雷游戏1\扫雷游戏33\jarcfmGame.jarMymoon.mf*.class其中,参数c表示要生成一个新的JAR文件,f表示要生成的JAR文件的名字,m表示清单文件的名字。
可以将Hannoi.jar复制到任何一个安装了Java运行环境(版本号需高于1.5的计算机上)的计算机上,用户用鼠标双击该文件的图标就可以运行扫雷游戏。
6.JAVA编程的扫雷小游戏的源代码
importjava.awt.event.*;
importjava.awt.*;
importjavax.swing.*;
importjava.util.*;
importjava.io.*;
publicclassMineSquareextendsJPanelimplementsMouseListener
{
introws=10,cols=10,雷数=10;
JButtonbuttonFace=null;
Blockblock[][];
TimeCounttime;
CountMinecount;
Stringgrade=null;
RandomSetMine随机布雷者;
FindAroundMineNumber确定周围雷数者;
FindSafeArea找出无雷连通区者;
DetermineDigArea成片挖开者;
DetermineMineMarkIsRightOrWrong确定雷标记是否正确者;
FindAroundMineMarkNumber确定周围雷标数目者;
DecideWinner判断是否胜利者;
Record记录对话框=null;
publicMineSquare(Stringgrade,intmineNumber,introws,intcols,JButtonb,JDialogdialog)
this.grade=grade;
this.rows=rows;
this.cols=cols;
雷数=mineNumber;
判断是否胜利者=newDecideWinner();
记录对话框=(Record)dialog;
block=newBlock[rows+1][cols+1];
buttonFace=b;
time=newTimeCount();
count=newCountMine(雷数);
count.textShowMine.setText("
"
+雷数);
setLayout(newGridLayout(rows,cols));
importjavax.swing.ImageIcon;
//程序入口
publicclassBlock{
Stringname;
//名字,比如"
雷"
或数字
intaroundMineNumber;
//周围雷的数目
ImageIconmineIcon;
//雷的图标
booleanisMine=false;
//是否是雷
booleanisMark=false;
//是否被标记
booleanisOpen=false;
//是否被挖开
publicvoidsetName(Stringname){
this.name=name;
}
//设置周围的雷数
publicvoidsetAroundMineNumber(intn){
aroundMineNumber=n;
//获得周围的雷数
publicintgetAroundMineNumber(){
returnaroundMineNumber;
publicStringgetName(){
returnname;
//判断是否是雷
publicbooleanisMine(){
returnisMine;
}
//设置是否为雷
publicvoidsetIsMine(booleanb){
isMine=b;
//设置雷的图标
publicvoidsetMineIcon(ImageIconicon){
mineIcon=icon;
//获得雷的图标
publicImageIcongetMineicon(){
returnmineIcon;
//确定雷是否被挖开
publicbooleangetIsOpen(){
returnisOpen;
//设置为已经被挖开
publicvoidsetIsOpen(booleanp){
isOpen=p;
//返回此处是否已经被标记
publicbooleangetIsMark(){
returnisMark;
//设置此处是否已经被标记
publicvoidsetIsMark(booleanm){
isMark=m;
publicclassBlockViewextendsJPanel{
JLabelblockNameOrIcon;
//用来显示Block对象的name、number和mineIcon属性
JButtonblockCover;
//用来遮挡blockNameOrIcon.
CardLayoutcard;
//卡片式布局
BlockView(){
card=newCardLayout();
setLayout(card);
blockNameOrIcon=newJLabel("
JLabel.CENTER);
blockNameOrIcon.setHorizontalTextPosition(AbstractButton.CENTER);
blockNameOrIcon.setVerticalTextPosition(AbstractButton.CENTER);
blockCover=newJButton();
add("
cover"
blockCover);
view"
blockNameOrIcon);
//给出视觉效果变化
publicvoidgiveView(Blockblock){
//如果是雷,将对应的图标和文字更改
if(block.isMine){
blockNameOrIcon.setText(block.getName());
blockNameOrIcon.setIcon(block.getMineicon());
else{
intn=block.getAroundMineNumber();
if(n>
=1)
blockNameOrIcon.setText("
+n);
else
"
);
publicvoidseeBlockNameOrIcon(){
card.show(this,"
validate();
publicvoidseeBlockCover(){
publicJButtongetBlockCover(){
returnblockCover;
}
/*按扭类*/
classBombextendsJButton
publicintnum_x,num_y;
//第几号方块
publicintBombRoundCount;
//周围雷数
publicbooleanisBomb;
//是否为雷
publicbooleanisClicked;
//是否被点击
publicintBombFlag;
//探雷标记
publicbooleanisRight;
//是否点击右键
publicBomb(intx,inty)
BombFlag=0;
num_x=x;
num_y=y;
BombRoundCount=0;
isBomb=false;
isClicked=false;
isRight=false;
/*窗口及算法实现类*/
classMainBombextendsJFrameimplementsActionListener,MouseListener
publicJTextFieldtext;
publicLabelnowBomb,setBomb;
publicintBlockNum,BombNum;
//当前方块数当前雷数
publicIconicon_bomb=newImageIcon("
Bomb.gif"
//踩雷
publicIconicon_bomb_big=newImageIcon("
bomb_big.gif"
//踩雷标记
publicIconicon_flag=newImageIcon("
flag.gif"
//雷标记
publicIconicon_question=newImageIcon("
question.gif"
//疑惑是否有雷
publicJButtonstart=newJButton("
开始"
publicPanelMenuPamel=newPanel();
publicPanelmainPanel=newPanel();
publicBomb[][]bombButton;
/*界面设计*/
publicMainBomb()
super("
扫雷Aaron2004制作2004.8"
BlockNum=64;
BombNum=10;
Containerc=getContentPane();
c.setBackground(Color.gray);
c.setLayout(newBorderLayout());
text=newJTextField("
10"
3);
nowBomb=newLabel("
当前雷数"
+"
+BombNum+"
setBomb=newLabel("
设置地雷数"
start.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEvente)
BombNum=Integer.parseInt(text.getText().trim());
if(BombNum>
=10&
&
BombNum<
50)
replay();
else
JOptionPanemsg=newJOptionPane();
JOptionPane.showMessageDialog(null,"
你设置的地雷数太多了,请重设!
"
错误"
2);
});
MenuPamel.add(setBomb);
MenuPamel.add(text);
MenuPamel.add(start);
MenuPamel.add(nowBomb);
c.add(MenuPamel,"
North"
mainPanel.setLayout(newGridLayout((int)Math.sqrt(BlockNum),(int)Math.sqrt(BlockNum)));
bombButton=newBomb[(int)Math.sqrt(BlockNum)][];
for(inti=0;
i<
(int)Math.sqrt(BlockNum);
i++)
bombButton[i]=newBomb[(int)Math.sqrt(BlockNum)];
i++)
for(intj=0;
j<
j++)
bombButton[i][j]=newBomb(i,j);
bombButton[i][j].setForeground(Color.gray);
bombButton[i][j].addActionListener(this);
bombButton[i][j].addMouseListener(this);
mainPanel.add(bombButton[i][j]);
c.add(mainPanel,"
Center"
startBomb();
setSize(400,400);
setLocation(350,200);
setResizable(false);
/*布雷*/
publicvoidstartBomb()
for(inti=0;
i<
BombNum;
i++)
intx=(int)(Math.random()*(int)(Math.sqrt(BlockNum)-1));
inty=(int)(Math.random()*(int)(Math.sqrt(BlockNum)-1));
if(bombButton[x][y].isBomb==true)
i--;
bombButton[x][y].isBomb=true;
/*重新开始*/
publicvoidreplay()
nowBomb.setText("
j++)
bombButton[i][j].isBomb=false;
bombButton[i][j].isClicked=false;
bombButton[i][j].setEnabled(true);
bombButton[i][j].