




免费预览已结束,剩余40页可下载查看
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
java课程设计课程名称:扫雷姓 名: 学 号: 专 业:电子信息工程 摘要windows 2000/xp系统提供的扫雷游戏是一个很有趣的游戏。本章的课程设计使用java语言编写一个与其类似的扫雷游戏。具体要求如下:(1) 扫雷游戏分为初级、中级和高级三个级别,扫雷英雄榜存储每个级别的最好成绩,即挖出全部的地雷且用时最少者。单击游戏菜单可以选择“初级”、“中级”和“高级”或“查看英雄版”。(2) 选择级别后将出现相应级别的扫雷区域,这是用户使用鼠标左键单击雷区中任何一个方块便启动计时器。(3) 用户要揭开某个方块,可单击它。若所揭方块下有泪,用户便输了这一局,若所揭方块下五雷,则显示一个数字,该数字代表方块的周围的8个方块中共有多少颗雷。(4) 如果用户认为某个方块下埋着雷,单击右键可以在方块上标识一个用户认为是雷的图标,即给出一个扫雷标记。用户每标记出一个扫雷标记(无论用户的标记是否正确),程序将显示的剩余雷数减少一个。(5) 扫雷胜利后(用时最少者),程序弹出保存成绩的对话框。(6)用户可以选择标记疑问的方块,用可以勾选游戏菜单下的标记即可,此时双击右键并可出现“?”标记。另有颜色选项,当用户勾选此项时,游戏将以最低图像像素来显示。正文一设计目的: 本次课程设计的主要目的是为了通过具体的程序来加深对java语言的掌握,提高自己的编程水平。选择的题目来自java课程设计(第二版)中的扫雷游戏,这是一个综合性的题目,可以对java中的各项功能有更好的理解和使用,同时也为以后的工作打下一定的基础。二总体设计: (1)用户可以自定义级别并且可以任意输入雷数;(2)具有计时功能,即显示用户完成移动盘子所花费的时间;(3)用户可以选择是否有音效;(4)自动保存扫雷英雄榜。三关键技术:四程序流程: 五 主要源代码:import javax.swing.imageicon;public class block string name; /名字,比如雷或数字 int aroundminenumber; /周围雷的数目 imageicon mineicon; /雷的图标 boolean ismine=false; /是否是雷 boolean ismark=false; /是否被标记 boolean isopen=false; /是否被挖开 public void setname(string name) =name; public void setaroundminenumber(int n) aroundminenumber=n; public int getaroundminenumber() return aroundminenumber; public string getname() return name; public boolean ismine() 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 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); blocknameoricon=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) blocknameoricon.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,cover); 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; 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-; 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(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; laymines 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(重新开始); 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.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); setlayout(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 blockrowcolum; 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); /给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); 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.getsource()!=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; else 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=(jbutton)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(mark); 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.setgrade(初级); 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)import 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); hashtable 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.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.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,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(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.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,240,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(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=grade+#+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 objectinputstream(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); fileoutputstream 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.*;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); setresizable(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(初级); label初级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
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 社会学概论练习题及答案
- 2025年国家开发投资集团甘肃售电有限公司招聘笔试参考题库附带答案详解
- 2025年广州海珠人防防护设备工程有限公司招聘笔试参考题库含答案解析
- 医学基础知识自主学习考题试题及答案
- 保山学院考试试题及答案
- 2024药师考试入门知识试题及答案
- 健康评估与临床实践试题及答案
- 安检识图考试题及答案
- 初级会计师对市场走向的分析能力试题及答案
- 用户界面设计考察内容试题及答案
- 医院改造工程设计服务方案
- 中职英语 高教版(2021)基础模块3 Unit 7 Natural Disasters Part 1-2教案
- 光伏电表过户协议书模板
- 《记念刘和珍君》高中语文选择性必修中册
- 职业本科《大学英语》课程标准
- 人教版高中数学选择性必修第三册8-1-1变量的相关关系【课件】
- 《认识面积》(教学设计)-2023-2024学年三年级下册数学苏教版
- 《应用文写作》高职全套教学课件
- 晚期产后出血 课件
- 绿城地产集团标准化运营手册地产客服项目交付项目运营手册之交付工作操作指引V1
- 茶农和公司合作协议
评论
0/150
提交评论