其实扫雷游戏的实现并不是想象中的那么困难,我在这谈谈自己的方法,虽然不算很好的Idear:
1.定义一个10*10的String数组。
2.定义数组的值:共三个:" "(空格),"雷",第三个是数字,即周围的雷的数目,如"3",表示它的周围8个方块里有3个雷。
3.随机生成雷的位置,如要生成10个雷,则生成10个下标[i][j],注意位置不能重复了,下面是代码:
for(int i=0;i<numLei;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<lei.length;i++)
for(int j=0;j<lei[i].length;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<max&&j+c>=0&&j+c<max)
{
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<max&&j+c>=0&&j+c<max&&b[i+r][j+c].getLabel().equals(" "))
{
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编程有兴趣的小伙子有些许的帮助吧,下面是我的原代码:有些乱,因为那时候我也是初学呢:
扫雷代码:
1package game.sl;
2import java.awt.*;
3import java.util.Random;
4import java.awt.event.*;
5import javax.swing.JOptionPane;
6public class MyShaoLei extends Frame implements ActionListener,MouseListener
7{
8
9 public static void main(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=new MyShaoLei(value);
16 }
17 int max;
18 int numLei;
19 int min;
20 Button[][] b;
21 String[][] lei;
22 Random a=new Random();
23 int numLei1=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 public MyShaoLei(String value)
30 {
31
32 if(value.equals("简单"))
33 {
34 max=9;
35 numLei=10;
36 this.setSize(300,350);
37 }
38 else if(value.equals("一般"))
39 {
40 max=16;
41 numLei=40;
42 this.setSize(400,470);
43 }
44 else if(value.equals("高级"))
45 {
46 max=22;
47 numLei=99;
48 this.setSize(500,570);
49 }
50 this.addWindowListener(new WindowAdapter()
51 {
52 public void windowClosing(WindowEvent e)
53 {
54 dispose();//释放资源
55 System.exit(0);
56 }
57 });
58 this.max=max;
59 this.numLei=numLei;
60 b=new Button[max][max];
61 lei=new String[max][max];
62 this.setLayout(new BorderLayout(20,20));//行,列,行间,列间
63 p=new Panel();
64 p.setLayout(new GridLayout(max,max,0,0));
65 for(int i=0;i<b.length;i++)
66 for(int j=0;j<b[i].length;j++)
67 {
68 b[i][j]=new Button();
69 b[i][j].addActionListener(this);
70 b[i][j].addMouseListener(this);
71 p.add(b[i][j]);
72 }
73 p1=new Panel();
74 p1.setLayout(new GridLayout(1,9,0,0));
75 tf1=new TextField(numLei);
76 tf2=new TextField("2");
77 b1=new Button("简单");
78 b1.addActionListener(this);
79 b2=new Button("一般");
80 b2.addActionListener(this);
81 b3=new Button("困难");
82 b3.addActionListener(this);
83 l1=new Label();
84 l2=new Label();
85 l3=new Label();
86 l4=new Label();
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 int w = getSize().width;
103 int h = getSize().height;
104 int x = (dim.width-w)/2;
105 int y = (dim.height-h)/2;
106 setLocation(x,y);
107
108 begin();
109
110 }
111 public void begin()
112 {
113 this.tf2.setText("0");
114 for(int i=0;i<b.length;i++)
115 for(int j=0;j<b[i].length;j++)
116 {
117 b[i][j].setLabel(" ");
118 b[i][j].setBackground(Color.lightGray);
119 lei[i][j]=new String("");
120 }
121 for(int i=0;i<numLei;i++)
122 {
123 leiX=a.nextInt(max);
124 leiY=a.nextInt(max);
125 if(lei[leiX][leiY].equals("雷"))
126 i--;
127 else
128 lei[leiX][leiY]="雷";
129 }
130 for(int i=0;i<lei.length;i++)
131 for(int j=0;j<lei[i].length;j++)
132 {
133 if(!lei[i][j].equals("雷"))
134 {
135 for(int r=-1;r<=1;r++)
136 for(int c=-1;c<=1;c++)
137 {
138 if(i+r>=0&&i+r<max&&j+c>=0&&j+c<max)
139 {
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 public void death()
153 {
154 for(int i=0;i<max;i++)
155 for(int j=0;j<max;j++)
156 {
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 else
166 System.exit(0);
167 }
168 public void win()
169 {
170 for(int i=0;i<max;i++)
171 for(int j=0;j<max;j++)
172 {
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 else
183 System.exit(0);
184 }
185 else
186 min=0;
187 }
188 public void open(int i,int j)
189 {
190 for(int r=-1;r<=1;r++)
191 for(int c=-1;c<=1;c++)
192 {
193 if(i+r>=0&&i+r<max&&j+c>=0&&j+c<max&&b[i+r][j+c].getLabel().equals(" "))
194 {
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 public void actionPerformed(ActionEvent e)
205 {
206 for(int i=0;i<max;i++)
207 for(int j=0;j<max;j++)
208 if(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 else if(lei[i][j].equals(""))
217 {
218 System.out.println("周围无雷!");
219 open(i,j);
220 }
221 else
222 {
223 b[i][j].setLabel(lei[i][j]);
224 b[i][j].setBackground(Color.white);
225 }
226 win();
227 }
228 }
229 public void mousePressed(MouseEvent e) {}
230 public void mouseReleased(MouseEvent e) {}
231 public void mouseEntered(MouseEvent e) {}
232 public void mouseExited(MouseEvent e) {}
233 public void mouseClicked(MouseEvent e)
234 {
235 switch(e.getModifiers())
236 {
237 case InputEvent.BUTTON1_MASK:
238 {
239 //左键
240 break;
241 }
242 case InputEvent.BUTTON2_MASK:
243 {
244 //中键
245 break;
246 }
247 case InputEvent.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 else if(((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 else if(((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