其实扫雷游戏的实现并不是想象中的那么困难,我在这谈谈自己的方法,虽然不算很好的Idear:
1.定义一个10*10的String数组。
2.定义数组的值:共三个:" "(空格),"雷",第三个是数字,即周围的雷的数目,如"3",表示它的周围8个方块里有3个雷。
3.随机生成雷的位置,如要生成10个雷,则生成10个下标[i][j],注意位置不能重复了,下面是代码:
for(int i=0;i
{
leiX=a.nextInt(max);
leiY=a.nextInt(max);
if(lei[leiX][leiY].equals("雷"))
i--;
else
lei[leiX][leiY]="雷";
}
4.初始化除雷外的其它值,其实也就是计算不是雷的格子周围雷的数目,方法也不算难,即遍历数组,如果不是雷则算它的周围是不是雷
(i - 1, y - 1)(i , y - 1)(i + 1, y - 1)
(i - 1, y) (i + 1, y)
(i - 1, y + 1)(i - 1, y - 1)(i + 1, y + 1)
代码如下:
for(int i=0;i
for(int j=0;j
{
if(!lei[i][j].equals("雷"))
{
for(int r=-1;r<=1;r++)
for(int c=-1;c<=1;c++)
{
if(i+r>=0&&i+r=0&&j+c
{
if(lei[i+r][j+c].equals("雷"))
leiNum++;
}
}
if(leiNum!=0)
lei[i][j]=""+leiNum;
leiNum=0;
}
}
如果算出来的雷数量为0,则给赋值为" "一个空格。
5.点击事件,这个应该是整个游戏稍复杂的吧,我这里用10*10 Button表示界面,一开始Lable的Text都为""。
A。点击按钮,程序可以得知该数组位置对应的值是什么:
"雷"则游戏结束
"3"(即雷数目)则对应Button的Lable = "3"
" "则Button的Lable = " ",注意,我们点击按键的时候,如果按钮为数字和" "的话,那我们就不用处理这个点击事件,因为格子已经打开了。
6.处理连续打开,即如果玩家点击的格是" ",即表示周围没雷,则这个应自动打开它周围的格子。
而如果它周围的某个A格的周围也没雷,也要自动打开A格子周围的格子。玩过扫雷的都知道这个动作的,我这里用了递归方法,代码如下:
public void open(int i,int j)
{
for(int r=-1;r<=1;r++)
for(int c=-1;c<=1;c++)
{
if(i+r>=0&&i+r=0&&j+c
{
b[i+r][j+c].setBackground(Color.white);
b[i+r][j+c].setLabel(lei[i+r][j+c]);
if(b[i+r][j+c].getLabel().equals(""))
{
open(i+r,j+c);
}
}
}
}
最后,希望我这篇破文章对Java编程有兴趣的小伙子有些许的帮助吧,下面是我的原代码:有些乱,因为那时候我也是初学呢:
扫雷代码:1
packagegame.sl;2importjava.awt.*;3importjava.util.Random;4importjava.awt.event.*;5importjavax.swing.JOptionPane;6publicclassMyShaoLeiextendsFrameimplementsActionListener,MouseListener7
{89publicstaticvoidmain(String[] args)10
{11
String[] values={"简单","一般","高级"};12 String value=(String) JOptionPane.showInputDialog(null,13"请选择难度","难度选择",JOptionPane.INFORMATION_MESSAGE,null,14 values, values[0]);15 MyShaoLei msl1=newMyShaoLei(value);16 }17intmax;18intnumLei;19intmin;20 Button[][] b;21 String[][] lei;22 Random a=newRandom();23intnumLei1=numLei,count,leiX,leiY,leiNum;//剩余雷,步数,雷的坐标x,雷坐标y,周围雷数24Button b1,b2,b3;25 TextField tf1,tf2;26 Panel p,p1;27 Label l1,l2,l3,l4;28 Image imghq ;29publicMyShaoLei(String value)30
{3132if(value.equals("简单"))33
{34 max=9;35 numLei=10;36this.setSize(300,350);37 }38elseif(value.equals("一般"))39
{40 max=16;41 numLei=40;42this.setSize(400,470);43 }44elseif(value.equals("高级"))45
{46 max=22;47 numLei=99;48this.setSize(500,570);49 }50this.addWindowListener(newWindowAdapter()51
{52publicvoidwindowClosing(WindowEvent e)53
{54 dispose();//释放资源55System.exit(0);56 }57 });58this.max=max;59this.numLei=numLei;60 b=newButton[max][max];61 lei=newString[max][max];62this.setLayout(newBorderLayout(20,20));//行,列,行间,列间63p=newPanel();64 p.setLayout(newGridLayout(max,max,0,0));65for(inti=0;ifor(intj=0;j
{68 b[i][j]=newButton();69 b[i][j].addActionListener(this);70 b[i][j].addMouseListener(this);71 p.add(b[i][j]);72 }73 p1=newPanel();74 p1.setLayout(newGridLayout(1,9,0,0));75 tf1=newTextField(numLei);76 tf2=newTextField("2");77 b1=newButton("简单");78 b1.addActionListener(this);79 b2=newButton("一般");80 b2.addActionListener(this);81 b3=newButton("困难");82 b3.addActionListener(this);83 l1=newLabel();84 l2=newLabel();85 l3=newLabel();86 l4=newLabel();87 p1.add(tf1);88 p1.add(l1);89 p1.add(b1);90 p1.add(l2);91 p1.add(b2);92 p1.add(l3);93 p1.add(b3);94 p1.add(l4);95 p1.add(tf2);96 add(p,BorderLayout.CENTER);97 add(p1,BorderLayout.SOUTH);9899this.setVisible(true);100101 Dimension dim=Toolkit.getDefaultToolkit().getScreenSize();//获取屏幕大小102intw=getSize().width;103inth=getSize().height;104intx=(dim.width-w)/2;105inty=(dim.height-h)/2;106 setLocation(x,y);107108 begin();109110 }111publicvoidbegin()112
{113this.tf2.setText("0");114for(inti=0;ifor(intj=0;j
{117 b[i][j].setLabel("");118 b[i][j].setBackground(Color.lightGray);119 lei[i][j]=newString("");120 }121for(inti=0;i
{123 leiX=a.nextInt(max);124 leiY=a.nextInt(max);125if(lei[leiX][leiY].equals("雷"))126 i--;127else128 lei[leiX][leiY]="雷";129 }130for(inti=0;ifor(intj=0;j
{133if(!lei[i][j].equals("雷"))134
{135for(intr=-1;r<=1;r++)136for(intc=-1;c<=1;c++)137
{138if(i+r>=0&&i+r=0&&j+c
{140if(lei[i+r][j+c].equals("雷"))141 leiNum++;142 }143144 }145if(leiNum!=0)146 lei[i][j]=""+leiNum;147 leiNum=0;148 }149150 }151 }152publicvoiddeath()153
{154for(inti=0;ifor(intj=0;j
{157 b[i][j].setLabel(lei[i][j]);158if(!lei[i][j].equals("雷"))159 b[i][j].setBackground(Color.white);160 }161if(JOptionPane.showConfirmDialog(null,"是否重新开始?","请选择", JOptionPane.YES_NO_OPTION)==0)162
{163 begin();164 }165else166 System.exit(0);167 }168publicvoidwin()169
{170for(inti=0;ifor(intj=0;j
{173if(b[i][j].getLabel().equals(""))174 min++;175 }176if(min==numLei)177
{178if(JOptionPane.showConfirmDialog(null,"请选择","是否重新开始?", JOptionPane.YES_NO_OPTION)==0)179
{180 begin();181 }182else183 System.exit(0);184 }185else186 min=0;187 }188publicvoidopen(inti,intj)189
{190for(intr=-1;r<=1;r++)191for(intc=-1;c<=1;c++)192
{193if(i+r>=0&&i+r=0&&j+c
{195 b[i+r][j+c].setBackground(Color.white);196 b[i+r][j+c].setLabel(lei[i+r][j+c]);197if(b[i+r][j+c].getLabel().equals(""))198
{199 open(i+r,j+c);200 }201 }202 }203 }204publicvoidactionPerformed(ActionEvent e)205
{206for(inti=0;ifor(intj=0;jif(e.getSource()==b[i][j])209
{210if(b[i][j].getLabel().equals(""))211 tf2.setText(""+(++count));212if(lei[i][j].equals("雷"))213
{214 death();215 }216elseif(lei[i][j].equals(""))217
{218 System.out.println("周围无雷!");219 open(i,j);220 }221else222
{223 b[i][j].setLabel(lei[i][j]);224 b[i][j].setBackground(Color.white);225 }226 win();227 }228 }229
publicvoidmousePressed(MouseEvent e){}230
publicvoidmouseReleased(MouseEvent e){}231
publicvoidmouseEntered(MouseEvent e){}232
publicvoidmouseExited(MouseEvent e){}233publicvoidmouseClicked(MouseEvent e)234
{235switch(e.getModifiers())236
{237caseInputEvent.BUTTON1_MASK:238
{239//左键240break;241 }242caseInputEvent.BUTTON2_MASK:243
{244//中键245break;246 }247caseInputEvent.BUTTON3_MASK:248
{249if(((Button)e.getSource()).getLabel()==(""))250
{251 ((Button)e.getSource()).setLabel("!");252 ((Button)e.getSource()).setBackground(Color.red);253 numLei1--;254 tf1.setText(""+numLei1);255 }256elseif(((Button)e.getSource()).getLabel()==("!"))257
{258 ((Button)e.getSource()).setLabel("?");259 ((Button)e.getSource()).setBackground(Color.yellow);260 numLei1++;261 tf1.setText(""+numLei1);262 }263elseif(((Button)e.getSource()).getLabel()==("?"))264
{265 ((Button)e.getSource()).setLabel("");266 ((Button)e.getSource()).setBackground(Color.lightGray);267 }268//((Button)e.getSource()).setBackground(Color.white);269break;270 }271 }272 }273}274