Java语言 扫雷游戏完整源代码.docx
- 文档编号:11023225
- 上传时间:2023-02-24
- 格式:DOCX
- 页数:22
- 大小:18.65KB
Java语言 扫雷游戏完整源代码.docx
《Java语言 扫雷游戏完整源代码.docx》由会员分享,可在线阅读,更多相关《Java语言 扫雷游戏完整源代码.docx(22页珍藏版)》请在冰豆网上搜索。
Java语言扫雷游戏完整源代码
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;
}
}
importjava.util.*;
importjavax.swing.*;
publicclassLayMines{
ImageIconmineIcon;
LayMines(){
mineIcon=newImageIcon("mine.gif");
}
publicvoidlayMinesForBlock(Blockblock[][],intmineCount){
introw=block.length;
intcolumn=block[0].length;
LinkedList
for(inti=0;i for(intj=0;j list.add(block[i][j]); } while(mineCount>0){ intsize=list.size();//list返回节点的个数 intrandomIndex=(int)(Math.random()*size); Blockb=list.get(randomIndex); b.setIsMine(true); b.setName("雷"); b.setMineIcon(mineIcon); list.remove(randomIndex);//list删除索引值为randomIndex的节点 mineCount--; } for(inti=0;i for(intj=0;j if(block[i][j].isMine()){ block[i][j].setIsOpen(false); block[i][j].setIsMark(false); } else{ intmineNumber=0; for(intk=Math.max(i-1,0);k<=Math.min(i+1,row-1);k++){ for(intt=Math.max(j-1,0);t<=Math.min(j+1,column-1);t++){ if(block[k][t].isMine()) mineNumber++; } } block[i][j].setIsOpen(false); block[i][j].setIsMark(false); block[i][j].setName(""+mineNumber); block[i][j].setAroundMineNumber(mineNumber); } } } } } importjava.awt.*; importjava.awt.event.*; importjavax.swing.*; publicclassMineAreaextendsJPanelimplementsActionListener,MouseListener{ JButtonreStart; Block[][]block; BlockView[][]blockView; LayMineslay; introw,colum,mineCount,markMount;//雷区的行数、列数以及地雷个数和用户给出的标记数 ImageIconmark; intgrade; JPanelpCenter,pNorth; JTextFieldshowTime,showMarkedMineCount;//显示用时以及标记数 Timertime;//计时器 intspendTime=0; Recordrecord; publicMineArea(introw,intcolum,intmineCount,intgrade){ reStart=newJButton("重新开始"); mark=newImageIcon("mark.gif");//探雷标记 time=newTimer(1000,this); showTime=newJTextField(5); showMarkedMineCount=newJTextField(5); showTime.setHorizontalAlignment(JTextField.CENTER); showMarkedMineCount.setHorizontalAlignment(JTextField.CENTER); showMarkedMineCount.setFont(newFont("Arial",Font.BOLD,16)); showTime.setFont(newFont("Arial",Font.BOLD,16)); pCenter=newJPanel(); pNorth=newJPanel(); lay=newLayMines(); initMineArea(row,colum,mineCount,grade);//初始化雷区,见下面的LayMines() reStart.addActionListener(this); pNorth.add(showMarkedMineCount); pNorth.add(reStart); pNorth.add(showTime); setLayout(newBorderLayout()); add(pNorth,BorderLayout.NORTH); add(pCenter,BorderLayout.CENTER); } publicvoidinitMineArea(introw,intcolum,intmineCount,intgrade){ pCenter.removeAll(); spendTime=0; markMount=mineCount; this.row=row; this.colum=colum; this.mineCount=mineCount; this.grade=grade; block=newBlock[row][colum]; for(inti=0;i for(intj=0;j block[i][j]=newBlock(); } lay.layMinesForBlock(block,mineCount); blockView=newBlockView[row][colum]; pCenter.setLayout(newGridLayout(row,colum)); for(inti=0;i for(intj=0;j blockView[i][j]=newBlockView(); blockView[i][j].giveView(block[i][j]);//给block[i][j]提供视图 pCenter.add(blockView[i][j]); blockView[i][j].getBlockCover().addActionListener(this); blockView[i][j].getBlockCover().addMouseListener(this); blockView[i][j].seeBlockCover(); blockView[i][j].getBlockCover().setEnabled(true); blockView[i][j].getBlockCover().setIcon(null); } } showMarkedMineCount.setText(""+markMount); validate(); } publicvoidsetRow(introw){ this.row=row; } publicvoidsetColum(intcolum){ this.colum=colum; } publicvoidsetMineCount(intmineCount){ this.mineCount=mineCount; } publicvoidsetGrade(intgrade){ this.grade=grade; } publicvoidactionPerformed(ActionEvente){ if(e.getSource()! =reStart&&e.getSource()! =time){ time.start(); intm=-1,n=-1; for(inti=0;i for(intj=0;j if(e.getSource()==blockView[i][j].getBlockCover()){ m=i; n=j; break; } } } if(block[m][n].isMine()){ for(inti=0;i for(intj=0;j blockView[i][j].getBlockCover().setEnabled(false); if(block[i][j].isMine()) blockView[i][j].seeBlockNameOrIcon(); } } time.stop(); spendTime=0; markMount=mineCount; } else{ show(m,n);//见本类后面的show方法 } } if(e.getSource()==reStart){ initMineArea(row,colum,mineCount,grade); } if(e.getSource()==time){ spendTime++; showTime.setText(""+spendTime); } inquireWin(); } publicvoidshow(intm,intn){ if(block[m][n].getAroundMineNumber()>0&&block[m][n].getIsOpen()==false){ blockView[m][n].seeBlockNameOrIcon(); block[m][n].setIsOpen(true); return; } elseif(block[m][n].getAroundMineNumber()==0&&block[m][n].getIsOpen()==false){ blockView[m][n].seeBlockNameOrIcon(); block[m][n].setIsOpen(true); for(intk=Math.max(m-1,0);k<=Math.min(m+1,row-1);k++){ for(intt=Math.max(n-1,0);t<=Math.min(n+1,colum-1);t++) show(k,t); } } } publicvoidmousePressed(MouseEvente){ JButtonsource=(JButton)e.getSource(); for(inti=0;i for(intj=0;j if(e.getModifiers()==InputEvent.BUTTON3_MASK&& source==blockView[i][j].getBlockCover()){ if(block[i][j].getIsMark()){ source.setIcon(null); block[i][j].setIsMark(false); markMount=markMount+1; showMarkedMineCount.setText(""+markMount); } else{ source.setIcon(mark); block[i][j].setIsMark(true); markMount=markMount-1; showMarkedMineCount.setText(""+markMount); } } } } } publicvoidinquireWin(){ intnumber=0; for(inti=0;i for(intj=0;j if(block[i][j].getIsOpen()==false) number++; } } if(number==mineCount){ time.stop(); record=newRecord(); switch(grade){ case1: record.setGrade("初级"); break; case2: record.setGrade("中级"); break; case3: record.setGrade("高级"); break; } record.setTime(spendTime); record.setVisible(true); } } publicvoidmouseReleased(MouseEvente){} publicvoidmouseEntered(MouseEvente){} publicvoidmouseExited(MouseEvente){} publicvoidmouseClicked(MouseEvente){} } importjava.awt.event.*; importjava.awt.*; importjavax.swing.*; importjavax.swing.border.*; importjava.util.*; importjava.io.*; publicclassMineGameextendsJFrameimplementsActionListener{ JMenuBarbar; JMenufileMenu; JMenuItem初级,中级,高级,扫雷英雄榜; MineAreamineArea=null; File英雄榜=newFile("英雄榜.txt"); Hashtablehashtable=null; ShowRecordshowHeroRecord=null; MineGame(){ mineArea=newMineArea(16,16,40,1); add(mineArea,BorderLayout.CENTER); bar=newJMenuBar(); fileMenu=newJMenu("游戏"); 初级=newJMenuItem("初级"); 中级=newJMenuItem("中级"); 高级=newJMenuItem("高级"); 扫雷英雄榜=newJMenuItem("扫雷英雄榜"); fileMenu.add(初级); fileMenu.add(中级); fileMenu.add(高级); fileMenu.add(扫雷英雄榜); bar.add(fileMenu); setJMenuBar(bar); 初级.addActionListener(this); 中级.addActionListener(this); 高级.addActionListener(this); 扫雷英雄榜.addActionListener(this); hashtable=newHashtable(); hashtable.put("初级","初级#"+999+"#匿名"); hashtable.put("中级","中级#"+999+"#匿名"); hashtable.put("高级","高级#"+999+"#匿名"); if(! 英雄榜.exists()){ try{FileOutputStreamout=newFileOutputStream(英雄榜); ObjectOutputStreamobjectOut=newObjectOutputStream(out); objectOut.writeObject(hashtable); objectOut.close(); out.close(); } catch(IOExceptione){} } showHeroRecord=newShowRecord(this,hashtable); setBounds(100,100,280,380); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); validate(); } publicvoidactionPerformed(ActionEvente){ 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(); } publicstaticvoidmain(Stringargs[]){ newMineGame(); } } importjava.io.*; importjava.util.*; importjavax.swing.*; importjava.awt.event.*; importjava.awt.*; publicclassRecordextendsJDialogimplementsActionListener{ inttime=0; Stringgrade=null; Stringkey=null; Stringmessage=null; JTextFieldtextName; JLabellabel=null; JButton确定,取消; publicRecord(){ setTitle("记录你的成绩"); this.time=time; this.grade=grade; setBounds(100,100,240,160); setResizable(false); setModal(true); 确定=newJButton("确定"); 取消=newJButton("取消"); textName=newJTextField(8); textName.setText("匿名"); 确定.addActionListener(this); 取消.addActionListener(this); setLayout(newGridLayout(2,1)); label=newJLabel("您现在是...高手,输入您的大名上榜"); add(label); JPanelp=newJPanel(); p.add(textName); p.add(确定); p.add(取消); add(p); setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE); } publicvoidsetGrade(Stringgrade){ this.grade=grade; label.setText("您现在是"+grade+"高手,输入您的大名上榜");
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Java语言 扫雷游戏完整源代码 Java 语言 扫雷 游戏 完整 源代码
![提示](https://static.bdocx.com/images/bang_tan.gif)