Java课程设计报告扫雷游戏

上传人:沈*** 文档编号:44410765 上传时间:2021-12-05 格式:DOC 页数:45 大小:242.50KB
收藏 版权申诉 举报 下载
Java课程设计报告扫雷游戏_第1页
第1页 / 共45页
Java课程设计报告扫雷游戏_第2页
第2页 / 共45页
Java课程设计报告扫雷游戏_第3页
第3页 / 共45页
资源描述:

《Java课程设计报告扫雷游戏》由会员分享,可在线阅读,更多相关《Java课程设计报告扫雷游戏(45页珍藏版)》请在装配图网上搜索。

1、Java课程设计课程名称:扫雷姓 名: 学 号: 专 业:电子信息工程 摘要Windows 2000/XP系统提供的扫雷游戏是一个很有趣的游戏。本章的课程设计使用Java语言编写一个与其类似的扫雷游戏。具体要求如下:(1) 扫雷游戏分为初级、中级和高级三个级别,扫雷英雄榜存储每个级别的最好成绩,即挖出全部的地雷且用时最少者。单击游戏菜单可以选择“初级”、“中级”和“高级”或“查看英雄版”。(2) 选择级别后将出现相应级别的扫雷区域,这是用户使用鼠标左键单击雷区中任何一个方块便启动计时器。(3) 用户要揭开某个方块,可单击它。若所揭方块下有泪,用户便输了这一局,若所揭方块下五雷,则显示一个数字,

2、该数字代表方块的周围的8个方块中共有多少颗雷。(4) 如果用户认为某个方块下埋着雷,单击右键可以在方块上标识一个用户认为是雷的图标,即给出一个扫雷标记。用户每标记出一个扫雷标记(无论用户的标记是否正确),程序将显示的剩余雷数减少一个。(5) 扫雷胜利后(用时最少者),程序弹出保存成绩的对话框。(6)用户可以选择标记疑问的方块,用可以勾选游戏菜单下的标记即可,此时双击右键并可出现“?”标记。另有颜色选项,当用户勾选此项时,游戏将以最低图像像素来显示。正文一设计目的: 本次课程设计的主要目的是为了通过具体的程序来加深对Java语言的掌握,提高自己的编程水平。选择的题目来自Java课程设计(第二版)

3、中的扫雷游戏,这是一个综合性的题目,可以对Java中的各项功能有更好的理解和使用,同时也为以后的工作打下一定的基础。二总体设计: (1)用户可以自定义级别并且可以任意输入雷数;(2)具有计时功能,即显示用户完成移动盘子所花费的时间;(3)用户可以选择是否有音效;(4)自动保存扫雷英雄榜。三关键技术:四程序流程: 五 主要源代码:import javax.swing.ImageIcon;public class Block String name; /名字,比如雷或数字 int aroundMineNumber; /周围雷的数目 ImageIcon mineIcon; /雷的图标 boolean

4、 isMine=false; /是否是雷 boolean isMark=false; /是否被标记 boolean isOpen=false; /是否被挖开 public void setName(String name) this.name=name; public void setAroundMineNumber(int n) aroundMineNumber=n; public int getAroundMineNumber() return aroundMineNumber; public String getName() return name; public boolean isM

5、ine() return isMine; public void setIsMine(boolean b) isMine=b; public void setMineIcon(ImageIcon icon) mineIcon=icon; public ImageIcon getMineicon() return mineIcon; public boolean getIsOpen() return isOpen; public void setIsOpen(boolean p) isOpen=p; public boolean getIsMark() return isMark; public

6、 void setIsMark(boolean m) isMark=m; import javax.swing.*;import java.awt.*;public class BlockView extends JPanel JLabel blockNameOrIcon; /用来显示Block对象的name、number和mineIcon属性 JButton blockCover; /用来遮挡blockNameOrIcon. CardLayout card; /卡片式布局 BlockView() card=new CardLayout(); setLayout(card); blockNam

7、eOrIcon=new JLabel(,JLabel.CENTER); blockNameOrIcon.setHorizontalTextPosition(AbstractButton.CENTER); blockNameOrIcon.setVerticalTextPosition(AbstractButton.CENTER); blockCover=new JButton(); add(cover,blockCover); add(view,blockNameOrIcon); public void giveView(Block block) if(block.isMine) blockNa

8、meOrIcon.setText(block.getName(); blockNameOrIcon.setIcon(block.getMineicon(); else int n=block.getAroundMineNumber(); if(n=1) blockNameOrIcon.setText(+n); else blockNameOrIcon.setText( ); public void seeBlockNameOrIcon() card.show(this,view); validate(); public void seeBlockCover() card.show(this,c

9、over); validate(); public JButton getBlockCover() return blockCover; import java.util.*;import javax.swing.*;public class LayMines ImageIcon mineIcon; LayMines() mineIcon=new ImageIcon(mine.gif); public void layMinesForBlock(Block block,int mineCount) int row=block.length; int column=block0.length;

10、LinkedList list=new LinkedList(); for(int i=0;irow;i+) for(int j=0;j0) int size=list.size(); / list返回节点的个数 int randomIndex=(int)(Math.random()*size); Block b=list.get(randomIndex); b.setIsMine(true); b.setName(雷); b.setMineIcon(mineIcon); list.remove(randomIndex); /list删除索引值为randomIndex的节点 mineCount

11、-; for(int i=0;irow;i+) for(int j=0;jcolumn;j+) if(blockij.isMine() blockij.setIsOpen(false); blockij.setIsMark(false); else int mineNumber=0; for(int k=Math.max(i-1,0);k=Math.min(i+1,row-1);k+) for(int t=Math.max(j-1,0);t=Math.min(j+1,column-1);t+) if(blockkt.isMine() mineNumber+; blockij.setIsOpen

12、(false); blockij.setIsMark(false); blockij.setName(+mineNumber); blockij.setAroundMineNumber(mineNumber); import java.awt.*;import java.awt.event.*;import javax.swing.*;public class MineArea extends JPanel implements ActionListener,MouseListener JButton reStart; Block block; BlockView blockView; Lay

13、Mines lay; int row,colum,mineCount,markMount;/雷区的行数、列数以及地雷个数和用户给出的标记数 ImageIcon mark; int grade; JPanel pCenter,pNorth; JTextField showTime,showMarkedMineCount; /显示用时以及标记数 Timer time; /计时器 int spendTime=0; Record record; public MineArea(int row,int colum,int mineCount,int grade) reStart=new JButton(

14、重新开始); mark=new ImageIcon(mark.gif); /探雷标记 time=new Timer(1000,this); showTime=new JTextField(5); showMarkedMineCount=new JTextField(5); showTime.setHorizontalAlignment(JTextField.CENTER); showMarkedMineCount.setHorizontalAlignment(JTextField.CENTER); showMarkedMineCount.setFont(new Font(Arial,Font.

15、BOLD,16); showTime.setFont(new Font(Arial,Font.BOLD,16); pCenter=new JPanel(); pNorth=new JPanel(); lay=new LayMines(); initMineArea(row,colum,mineCount,grade); /初始化雷区,见下面的LayMines() reStart.addActionListener(this); pNorth.add(showMarkedMineCount); pNorth.add(reStart); pNorth.add(showTime); setLayou

16、t(new BorderLayout(); add(pNorth,BorderLayout.NORTH); add(pCenter,BorderLayout.CENTER); public void initMineArea(int row,int colum,int mineCount,int grade) pCenter.removeAll(); spendTime=0; markMount=mineCount; this.row=row; this.colum=colum; this.mineCount=mineCount; this.grade=grade; block=new Blo

17、ckrowcolum; for(int i=0;irow;i+) for(int j=0;jcolum;j+) blockij=new Block(); lay.layMinesForBlock(block,mineCount); blockView=new BlockViewrowcolum; pCenter.setLayout(new GridLayout(row,colum); for(int i=0;irow;i+) for(int j=0;jcolum;j+) blockViewij=new BlockView(); blockViewij.giveView(blockij); /给

18、blockij提供视图 pCenter.add(blockViewij); blockViewij.getBlockCover().addActionListener(this); blockViewij.getBlockCover().addMouseListener(this); blockViewij.seeBlockCover(); blockViewij.getBlockCover().setEnabled(true); blockViewij.getBlockCover().setIcon(null); showMarkedMineCount.setText(+markMount)

19、; validate(); public void setRow(int row) this.row=row; public void setColum(int colum) this.colum=colum; public void setMineCount(int mineCount) this.mineCount=mineCount; public void setGrade(int grade) this.grade=grade; public void actionPerformed(ActionEvent e) if(e.getSource()!=reStart&e.getSour

20、ce()!=time) time.start(); int m=-1,n=-1; for(int i=0;irow;i+) for(int j=0;jcolum;j+) if(e.getSource()=blockViewij.getBlockCover() m=i; n=j; break; if(blockmn.isMine() for(int i=0;irow;i+) for(int j=0;j0&blockmn.getIsOpen()=false) blockViewmn.seeBlockNameOrIcon(); blockmn.setIsOpen(true); return; els

21、e if(blockmn.getAroundMineNumber()=0&blockmn.getIsOpen()=false) blockViewmn.seeBlockNameOrIcon(); blockmn.setIsOpen(true); for(int k=Math.max(m-1,0);k=Math.min(m+1,row-1);k+) for(int t=Math.max(n-1,0);t=Math.min(n+1,colum-1);t+) show(k,t); public void mousePressed(MouseEvent e) JButton source=(JButt

22、on)e.getSource(); for(int i=0;irow;i+) for(int j=0;jcolum;j+) if(e.getModifiers()=InputEvent.BUTTON3_MASK& source=blockViewij.getBlockCover() if(blockij.getIsMark() source.setIcon(null); blockij.setIsMark(false); markMount=markMount+1; showMarkedMineCount.setText(+markMount); else source.setIcon(mar

23、k); blockij.setIsMark(true); markMount=markMount-1; showMarkedMineCount.setText(+markMount); public void inquireWin() int number=0; for(int i=0;irow;i+) for(int j=0;jcolum;j+) if(blockij.getIsOpen()=false) number+; if(number=mineCount) time.stop(); record=new Record(); switch(grade) case 1: record.s

24、etGrade(初级); break; case 2: record.setGrade(中级); break; case 3: record.setGrade(高级); break; record.setTime(spendTime); record.setVisible(true); public void mouseReleased(MouseEvent e) public void mouseEntered(MouseEvent e) public void mouseExited(MouseEvent e) public void mouseClicked(MouseEvent e)i

25、mport java.awt.event.*;import java.awt.*;import javax.swing.*;import javax.swing.border.*;import java.util.*;import java.io.*;public class MineGame extends JFrame implements ActionListener JMenuBar bar; JMenu fileMenu; JMenuItem 初级,中级,高级,扫雷英雄榜; MineArea mineArea=null; File 英雄榜=new File(英雄榜.txt); Has

26、htable hashtable=null; ShowRecord showHeroRecord=null; MineGame() mineArea=new MineArea(16,16,40,1); add(mineArea,BorderLayout.CENTER); bar=new JMenuBar(); fileMenu=new JMenu(游戏); 初级=new JMenuItem(初级); 中级=new JMenuItem(中级); 高级=new JMenuItem(高级); 扫雷英雄榜=new JMenuItem(扫雷英雄榜); fileMenu.add(初级); fileMenu

27、.add(中级); fileMenu.add(高级); fileMenu.add(扫雷英雄榜); bar.add(fileMenu); setJMenuBar(bar); 初级.addActionListener(this); 中级.addActionListener(this); 高级.addActionListener(this); 扫雷英雄榜.addActionListener(this); hashtable=new Hashtable(); hashtable.put(初级,初级#+999+#匿名); hashtable.put(中级,中级#+999+#匿名); hashtable.

28、put(高级,高级#+999+#匿名); if(!英雄榜.exists() try FileOutputStream out=new FileOutputStream(英雄榜); ObjectOutputStream objectOut=new ObjectOutputStream(out); objectOut.writeObject(hashtable); objectOut.close(); out.close(); catch(IOException e) showHeroRecord=new ShowRecord(this,hashtable); setBounds(100,100,

29、280,380); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); validate(); public void actionPerformed(ActionEvent e) if(e.getSource()=初级) mineArea.initMineArea(8,8,10,1); setBounds(100,100,200,280); if(e.getSource()=中级) mineArea.initMineArea(16,16,40,2); setBounds(100,100,280,380); if(

30、e.getSource()=高级) mineArea.initMineArea(22,22,99,3); setBounds(100,100,350,390); if(e.getSource()=扫雷英雄榜) if(showHeroRecord!=null) showHeroRecord.setVisible(true); validate(); public static void main(String args) new MineGame(); import java.io.*;import java.util.*;import javax.swing.*;import java.awt

31、.event.*;import java.awt.*;public class Record extends JDialog implements ActionListener int time=0; String grade=null; String key=null; String message=null; JTextField textName; JLabel label=null; JButton 确定,取消; public Record() setTitle(记录你的成绩); this.time=time; this.grade=grade; setBounds(100,100,2

32、40,160); setResizable(false); setModal(true); 确定=new JButton(确定); 取消=new JButton(取消); textName=new JTextField(8); textName.setText(匿名); 确定.addActionListener(this); 取消.addActionListener(this); setLayout(new GridLayout(2,1); label=new JLabel(您现在是.高手,输入您的大名上榜); add(label); JPanel p=new JPanel(); p.add(

33、textName); p.add(确定); p.add(取消); add(p); setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE); public void setGrade(String grade) this.grade=grade; label.setText(您现在是+grade+高手,输入您的大名上榜); public void setTime(int time) this.time=time; public void actionPerformed(ActionEvent e) if(e.getSource()=确定) message=g

34、rade+#+time+#+ +textName.getText(); key=grade; writeRecord(key,message); setVisible(false); if(e.getSource()=取消) setVisible(false); public void writeRecord(String key,String message) File f=new File(英雄榜.txt); try FileInputStream in=new FileInputStream(f); ObjectInputStream object_in=new ObjectInputS

35、tream(in); Hashtable hashtable=(Hashtable)object_in.readObject(); object_in.close(); in.close(); String temp=(String)hashtable.get(key); StringTokenizer fenxi=new StringTokenizer(temp,#); fenxi.nextToken(); int n=Integer.parseInt(fenxi.nextToken(); if(timen) hashtable.put(key,message); FileOutputStr

36、eam out=new FileOutputStream(f); ObjectOutputStream object_out=new ObjectOutputStream(out); object_out.writeObject(hashtable); object_out.close(); out.close(); catch(Exception e) System.out.println(e); import java.io.*;import java.util.*;import javax.swing.*;import java.awt.event.*;import java.awt.*

37、;public class ShowRecord extends JDialog implements ActionListener File file=new File(英雄榜.txt); String name=null; Hashtable hashtable=null; JButton 显示,重新记分; JLabel label初级,label中级,label高级; public ShowRecord(JFrame frame,Hashtable h) setTitle(扫雷英雄榜); hashtable=h; setBounds(100,100,320,185); setResiza

38、ble(false); setVisible(false); setModal(true); label初级=new JLabel3; label中级=new JLabel3; label高级=new JLabel3; for(int i=0;i3;i+) label初级i=new JLabel(); label初级i.setBorder(null); label中级i=new JLabel(); label中级i.setBorder(null); label高级i=new JLabel(); label高级i.setBorder(null); label初级0.setText(初级); la

39、bel初级1.setText(+999); label初级1.setText(匿名); label中级0.setText(中级); label中级1.setText(+999); label中级1.setText(匿名); label高级0.setText(高级); label高级1.setText(+999); label高级1.setText(匿名); JPanel pCenter=new JPanel(); pCenter.setLayout(new GridLayout(3,3); for(int i=0;i3;i+) pCenter.add(label初级i); for(int i=

40、0;i3;i+) pCenter.add(label中级i); for(int i=0;i3;i+) pCenter.add(label高级i); pCenter.setBorder(BorderFactory.createTitledBorder(扫雷英雄榜); 显示=new JButton(显示成绩); 重新记分=new JButton(重新记分); 显示.addActionListener(this); 重新记分.addActionListener(this); JPanel pSouth=new JPanel(); pSouth.setLayout(new FlowLayout(Flo

41、wLayout.RIGHT); pSouth.add(重新记分); pSouth.add(显示); add(pCenter,BorderLayout.CENTER); add(pSouth,BorderLayout.SOUTH) ; public void readAndShow() try FileInputStream in=new FileInputStream(file); ObjectInputStream object_in=new ObjectInputStream(in); hashtable=(Hashtable)object_in.readObject(); object_

42、in.close(); in.close(); String temp=(String)hashtable.get(初级); StringTokenizer fenxi=new StringTokenizer(temp,#); label初级0.setText(fenxi.nextToken(); label初级1.setText(fenxi.nextToken(); label初级2.setText(fenxi.nextToken(); temp=(String)hashtable.get(中级); fenxi=new StringTokenizer(temp,#); label中级0.se

43、tText(fenxi.nextToken(); label中级1.setText(fenxi.nextToken(); label中级2.setText(fenxi.nextToken(); temp=(String)hashtable.get(高级); fenxi=new StringTokenizer(temp,#); label高级0.setText(fenxi.nextToken(); label高级1.setText(fenxi.nextToken(); label高级2.setText(fenxi.nextToken(); catch(Exception e) public void actionPerformed(ActionEvent e) if(e.getSource()=重新记分)

展开阅读全文
温馨提示:
1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
2: 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
3.本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
关于我们 - 网站声明 - 网站地图 - 资源地图 - 友情链接 - 网站客服 - 联系我们

copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!