收录网站源码建站之星官方网站
其实扫雷游戏的实现并不是想象中的那么困难,我在这谈谈自己的方法,虽然不算很好的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;2
importjava.awt.*;3
importjava.util.Random;4
importjava.awt.event.*;5
importjavax.swing.JOptionPane;6
publicclassMyShaoLeiextendsFrameimplementsActionListener,MouseListener7
{8
9
publicstaticvoidmain(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
}17
intmax;18
intnumLei;19
intmin;20
Button[][] b;21
String[][] lei;22
Random a=newRandom();23
intnumLei1=numLei,count,leiX,leiY,leiNum;//剩余雷,步数,雷的坐标x,雷坐标y,周围雷数24
Button b1,b2,b3;25
TextField tf1,tf2;26
Panel p,p1;27
Label l1,l2,l3,l4;28
Image imghq ;29
publicMyShaoLei(String value)30
{31
32
if(value.equals("简单"))33
{34
max=9;35
numLei=10;36
this.setSize(300,350);37
}38
elseif(value.equals("一般"))39
{40
max=16;41
numLei=40;42
this.setSize(400,470);43
}44
elseif(value.equals("高级"))45
{46
max=22;47
numLei=99;48
this.setSize(500,570);49
}50
this.addWindowListener(newWindowAdapter()51
{52
publicvoidwindowClosing(WindowEvent e)53
{54
dispose();//释放资源55
System.exit(0);56
}57
});58
this.max=max;59
this.numLei=numLei;60
b=newButton[max][max];61
lei=newString[max][max];62
this.setLayout(newBorderLayout(20,20));//行,列,行间,列间63
p=newPanel();64
p.setLayout(newGridLayout(max,max,0,0));65
for(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);98
99
this.setVisible(true);100
101
Dimension dim=Toolkit.getDefaultToolkit().getScreenSize();//获取屏幕大小102
intw=getSize().width;103
inth=getSize().height;104
intx=(dim.width-w)/2;105
inty=(dim.height-h)/2;106
setLocation(x,y);107
108
begin();109
110
}111
publicvoidbegin()112
{113
this.tf2.setText("0");114
for(inti=0;ifor(intj=0;j
{117
b[i][j].setLabel("");118
b[i][j].setBackground(Color.lightGray);119
lei[i][j]=newString("");120
}121
for(inti=0;i
{123
leiX=a.nextInt(max);124
leiY=a.nextInt(max);125
if(lei[leiX][leiY].equals("雷"))126
i--;127
else128
lei[leiX][leiY]="雷";129
}130
for(inti=0;ifor(intj=0;j
{133
if(!lei[i][j].equals("雷"))134
{135
for(intr=-1;r<=1;r++)136
for(intc=-1;c<=1;c++)137
{138
if(i+r>=0&&i+r=0&&j+c
{140
if(lei[i+r][j+c].equals("雷"))141
leiNum++;142
}143
144
}145
if(leiNum!=0)146
lei[i][j]=""+leiNum;147
leiNum=0;148
}149
150
}151
}152
publicvoiddeath()153
{154
for(inti=0;ifor(intj=0;j
{157
b[i][j].setLabel(lei[i][j]);158
if(!lei[i][j].equals("雷"))159
b[i][j].setBackground(Color.white);160
}161
if(JOptionPane.showConfirmDialog(null,"是否重新开始?","请选择", JOptionPane.YES_NO_OPTION)==0)162
{163
begin();164
}165
else166
System.exit(0);167
}168
publicvoidwin()169
{170
for(inti=0;ifor(intj=0;j
{173
if(b[i][j].getLabel().equals(""))174
min++;175
}176
if(min==numLei)177
{178
if(JOptionPane.showConfirmDialog(null,"请选择","是否重新开始?", JOptionPane.YES_NO_OPTION)==0)179
{180
begin();181
}182
else183
System.exit(0);184
}185
else186
min=0;187
}188
publicvoidopen(inti,intj)189
{190
for(intr=-1;r<=1;r++)191
for(intc=-1;c<=1;c++)192
{193
if(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]);197
if(b[i+r][j+c].getLabel().equals(""))198
{199
open(i+r,j+c);200
}201
}202
}203
}204
publicvoidactionPerformed(ActionEvent e)205
{206
for(inti=0;ifor(intj=0;jif(e.getSource()==b[i][j])209
{210
if(b[i][j].getLabel().equals(""))211
tf2.setText(""+(++count));212
if(lei[i][j].equals("雷"))213
{214
death();215
}216
elseif(lei[i][j].equals(""))217
{218
System.out.println("周围无雷!");219
open(i,j);220
}221
else222
{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)
{}233
publicvoidmouseClicked(MouseEvent e)234
{235
switch(e.getModifiers())236
{237
caseInputEvent.BUTTON1_MASK:238
{239
//左键240
break;241
}242
caseInputEvent.BUTTON2_MASK:243
{244
//中键245
break;246
}247
caseInputEvent.BUTTON3_MASK:248
{249
if(((Button)e.getSource()).getLabel()==(""))250
{251
((Button)e.getSource()).setLabel("!");252
((Button)e.getSource()).setBackground(Color.red);253
numLei1--;254
tf1.setText(""+numLei1);255
}256
elseif(((Button)e.getSource()).getLabel()==("!"))257
{258
((Button)e.getSource()).setLabel("?");259
((Button)e.getSource()).setBackground(Color.yellow);260
numLei1++;261
tf1.setText(""+numLei1);262
}263
elseif(((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);269
break;270
}271
}272
}273
}274