《Java语言扫雷游戏源代码》由会员分享,可在线阅读,更多相关《Java语言扫雷游戏源代码(18页珍藏版)》请在金锄头文库上搜索。
1、importjavax.swing.Imagelcon;publicclassBlockStringname;intaroundMineNumber;ImageIconmineIcon;booleanisMine=false;booleanisMark=false;booleanisOpen=false;/名字,比方雷或数字/周围雷的数目/雷的图标/是否是雷/是否被标记/是否被挖开publicvoidsetName(Stringname)this.name=name;publicvoidsetAroundMineNumber(intn)aroundMineNumber=n;publicintg
2、etAroundMineNumber()returnaroundMineNumber;publicStringgetName()returnname;publicbooleanisMine()returnisMine;publicvoidsetIsMine(booleanb)isMine=b;publicvoidsetMineIcon(ImageIconicon)mineIcon=icon;publicImageIcongetMineicon()returnmineIcon;publicbooleangetIsOpen()returnisOpen;publicvoidsetIsOpen(boo
3、leanp)isOpen=p;publicbooleangetIsMark()returnisMark;publicvoidsetIsMark(booleanm)isMark=m;importjava.util.*;importjavax.swing.*;publicclassLayMinesImageIconmineIcon;LayMines()mineIcon=newImageIcon(mine.gi巧;publicvoidlayMinesForBlock(Blockblock叩,intmineCount)introw=block.length;intcolumn=block0.lengt
4、h;LinkedListlist=newLinkedList();for(inti=0;irow;i+)for(intj=0;j0)intsize=list.size();/list返回节点的个数intrandomIndex=(int)(Math.random()*size);Blockb=list.get(randomIndex);b.setlsMine(true);b.setName(雷力b.setMinelcon(minelcon);list.remove(randomlndex);/list删除索弓I值为randomindex的节点mineCount-;for(inti=0;irow;
5、i+)for(intj=0;jcolumn;j+)if(blockij.isMine()blockij.setIsOpen(false);blockij.setIsMark(false);elseintmineNumber=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(blockkt.isMine()mineNumber+;blockij.setIsOpen(false);blockij.setIsMark(false);bl
6、ockij.setName(+mineNumber);blockij.setAroundMineNumber(mineNumber);importjava.awt.*;importjava.awt.event.*;importjavax.swing.*;publicclassMineAreaextendsJPanelimplementsActionListener,MouseListeneruttonreStart;Blockblock;BlockViewblockView;LayMineslay;introw,colum,mineCount,markMount;/雷区的行数、列数以及地雷个数
7、和用户给出的标记数ImageIconmark;intgrade;JPanelpCenter,pNorth;JTextFieldshowTime,showMarkedMineCount;/显示用时以及标记数Timertime;/计时器intspendTime=0;Recordrecord;publicMineArea(introw,intcolum,intmineCount,intgrade)reStart=newutton(重新开场);mark=newImageIcon(mark.gif);/探雷标记time=newTimer(1000,this);showTime=newJTextField
8、(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=ne
9、wLayMines();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,
10、intcolum,intmineCount,intgrade)pCenter.removeAll();spendTime=0;markMount=mineCount;this.row=row;this.colum=colum;this.mineCount=mineCount;this.grade=grade;block=newBlockrowcolum;for(inti=0;irow;i+)for(intj=0;jcolum;j+)blockij=newBlock();lay.layMinesForBlock(block,mineCount);blockView=newBlockViewrow
11、colum;pCenter.setLayout(newGridLayout(row,colum);for(inti=0;irow;i+)for(intj=0;jcolum;j+)blockViewij=newBlockView();blockViewij.giveView(blockij);给blockij提供视图pCenter.add(blockViewij);blockViewiU.getBlockCover().addActionListener(this);blockViewiU.getBlockCover().addMouseListener(this);blockViewij.se
12、eBlockCover();blockViewij.getBlockCover().setEnabled(true);blockViewij.getBlockCover().setIcon(null);showMarkedMineCount.setText(+markMount);validate。;publicvoidsetRow(introw)this.row=row;publicvoidsetColum(intcolum)this.colum=colum;publicvoidsetMineCount(intmineCount)this.mineCount=mineCount;public
13、voidsetGrade(intgrade)this.grade=grade;publicvoidactionPerformed(ActionEvente)if(e.getSource()!=reStart&e.getSource()!=time)time.start();intm=-1,n=-1;for(inti=0;irow;i+)for(intj=0;jcolum;j+)if(e.getSource()=blockViewij.getBlockCover()m=i;n=j;break;if(blockmn.isMine()for(inti=0;irow;i+)for(intj=0;jco
14、lum;j+)blockViewiU.getBlockCover().setEnabled(false);if(blockij.isMine()blockViewiU.seeBlockNameOrIcon();time.stop();spendTime=0;markMount=mineCount;elseshow(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(blockmn.getAround