服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - java实现简单扫雷小游戏

java实现简单扫雷小游戏

2021-05-12 15:31tf1997 Java教程

这篇文章主要为大家详细介绍了java实现简单扫雷小游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了java实现扫雷游戏的具体代码,供大家参考,具体内容如下

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
import java.awt.borderlayout;
import java.awt.color;
import java.awt.container;
import java.awt.gridlayout;
import java.awt.insets;
import java.awt.label;
import java.awt.event.actionevent;
import java.awt.event.actionlistener;
import java.awt.event.itemevent;
import java.awt.event.itemlistener;
import java.awt.event.mouseevent;
import java.awt.event.mouselistener;
import java.util.random;
 
import javax.swing.jbutton;
import javax.swing.jcombobox;
import javax.swing.jframe;
import javax.swing.joptionpane;
import javax.swing.jpanel;
 
public class saolei implements mouselistener,actionlistener{
 jpanel p=new jpanel();
 jframe frame = new jframe("扫雷");
 @suppresswarnings("rawtypes")
 jcombobox combobox = new jcombobox();
 jbutton reset = new jbutton("重新开始");
 container container = new container();
 
 //游戏数据结构
 saoleiconstant constant = new saoleiconstant();
 jbutton[][] buttons = new jbutton[constant.row][constant.col];//定义按钮
 int[][] counts = new int [constant.row][constant.col];//定义整型数组保存按钮下方雷数
 
 //创建构造方法
 public saolei() {
 //显示窗口
 frame.setsize(600,700);//600*700
 frame.setresizable(false);
 frame.setdefaultcloseoperation(jframe.exit_on_close);
 frame.setlayout(new borderlayout());
 
 //添加重来、选择难度按钮
 addtopbutton();
 
 //添加雷区按钮
 addbuttons();
 
 //埋雷
 addlei();
 
 //添加雷的计数
 calcneibolei();
 
 frame.setvisible(true);
 }
 
 void addtopbutton() {
 p.removeall();
 p.add(reset);
 reset.setbackground(color.green);
 reset.setopaque(true);
 reset.addactionlistener(this);
 //combobox.additem("选择难度");
 combobox.additem("新手难度");
 combobox.additem("初级难度");
 combobox.additem("中级难度");
 combobox.additem("高级难度");
 combobox.additem("大师难度");
 combobox.setbackground(color.green);
 combobox.setopaque(true);
 combobox.additemlistener(new itemlistener(){
 
 @override
 public void itemstatechanged(itemevent e) {
 string item = e.getitem().tostring();
 if(item == "新手难度") {
  constant.leicount = 20;
  resetgame();
 } else if(item == "初级难度") {
  constant.leicount = 43;
  resetgame();
 } else if(item == "中级难度"){
  constant.leicount = 63;
  resetgame();
 } else if(item == "高级难度"){
  constant.leicount = 99;
  resetgame();
 } else if(item == "大师难度") {
  constant.leicount = 119;
  resetgame();
 }
 
 }
 
 });
 p.add(combobox);
 frame.add(p,borderlayout.north);
 //p.add(new label("总雷数:"+constant.leicount,label.center));
 //p.add(new label("总雷数:"+constant.leicount,label.right));
 
 }
 
 
 /*
 void addnandubutton() {
 nandu.setbackground(color.green);
 nandu.setopaque(true);
 nandu.addactionlistener(this);
 frame.add(nandu,borderlayout.west);
 }
 
 void addresetbutton() {
 reset.setbackground(color.green);
 reset.setopaque(true);
 reset.addactionlistener(this);
 //reset.addmouselistener(this);
 frame.add(reset,borderlayout.north);
 }
 */
 
 void addlei() {
 random rand = new random();
 int randrow,randcol;
 for(int i=0; i<constant.leicount; i++) {
 randrow = rand.nextint(constant.row);
 randcol = rand.nextint(constant.col);
 if(counts[randrow][randcol] == constant.leicode) {
 i--;
 } else {
 counts[randrow][randcol] = constant.leicode;
 //buttons[randrow][randcol].settext("x");
 }
 }
 }
 
 void addbuttons() {
 frame.add(container,borderlayout.center);
 container.setlayout(new gridlayout(constant.row,constant.col));
 for(int i=0;i<constant.row;i++) {
 for(int j=0;j<constant.col;j++) {
 jbutton button = new jbutton();
 button.setbackground(color.white);
 button.setopaque(true);
 button.addactionlistener(this);
 button.addmouselistener((mouselistener) this);
 buttons[i][j] = button;
 container.add(button);
 }
 }
 }
 
 void calcneibolei() {
 int count;
 for(int i=0;i<constant.row;i++) {
 for(int j=0;j<constant.col;j++) {
 count =0;
 if(counts[i][j] == constant.leicode) continue;
 if(i>0 && j>0 && counts[i-1][j-1] == constant.leicode) count++;
 if(i>0 && counts[i-1][j] == constant.leicode) count++;
 if(i>0 && j<19 &&counts[i-1][j+1] == constant.leicode) count++;
 if(j>0 && counts[i][j-1] == constant.leicode) count++;
 if(j<19 && counts[i][j+1] == constant.leicode) count++;
 if(i<19 && j>0 && counts[i+1][j-1] == constant.leicode) count++;
 if(i<19 && counts[i+1][j] == constant.leicode) count++;
 if(i<19 && j<19 && counts[i+1][j+1] == constant.leicode) count++;
 counts[i][j] = count;
 buttons[i][j].setmargin(new insets(0,0,0,0));//让按钮随按钮上的图案变化
 //buttons[i][j].settext(counts[i][j] + "");
 }
 }
 }
 
 @override
 public void actionperformed(actionevent e) {
 
 jbutton button = (jbutton)e.getsource();
 if(button.equals(reset)) {
 resetgame();//重新开始游戏
 } else {
 int count = 0;
 for(int i=0;i<constant.row;i++) {
 for(int j=0;j<constant.col;j++) {
  if(button.equals(buttons[i][j])) {
  count = counts[i][j];
  if(count == constant.leicode) {
  losegame();
  } else {
  opencell(i,j);
  checkwin();
  }
  return;
  }
 }
 }
 }
 }
 
 public void mouseclicked(mouseevent e) {
 jbutton button = (jbutton)e.getsource();
 if (e.getbutton() == mouseevent.button3) {//判断鼠标右击动作
 for(int i=0;i<constant.row;i++) {
  for(int j=0;j<constant.col;j++) {
  if(button.equals(buttons[i][j])) {
  if((buttons[i][j].isenabled() == true)) {
  //buttons[i][j].setenabled(false);
  buttons[i][j].setmargin(new insets(0,0,0,0));//让按钮随按钮上的图案变化
  buttons[i][j].settext("?");
  return;
  }
  }
  }
 }
 }
 }
 
 void resetgame() {
 for(int i=0;i<constant.row;i++) {
 for(int j=0;j<constant.col;j++) {
 buttons[i][j].settext("");
 buttons[i][j].setenabled(true);
 buttons[i][j].setbackground(color.white);
 counts[i][j] = 0;
 }
 }
 addlei();
 calcneibolei();
 }
 
 void checkwin() {
 for(int i=0;i<constant.row;i++) {
 for(int j=0;j<constant.col;j++) {
 if(buttons[i][j].isenabled() == true && counts[i][j] != constant.leicode ) return;
 }
 }
 joptionpane.showmessagedialog(frame,"yeah,你赢了!");
 }
 
 //使用递归方法打开格子
 void opencell(int i, int j) {
 if(buttons[i][j].isenabled() == false) return;
 buttons[i][j].setbackground(color.yellow);
 buttons[i][j].setopaque(true);
 buttons[i][j].setenabled(false);
 if(counts[i][j] == 0) {
 if(i>0 && j>0 && counts[i-1][j-1] != constant.leicode) opencell(i-1,j-1);
 if(i>0 && j<19 && counts[i-1][j] != constant.leicode) opencell(i-1,j);
 if(i>0 && j<19 &&counts[i-1][j+1] != constant.leicode) opencell(i-1,j+1);
 if(j>0 && counts[i][j-1] != constant.leicode) opencell(i,j-1);
 if(j<19 && counts[i][j+1] != constant.leicode) opencell(i,j+1);
 if(i<19 && j>0 && counts[i+1][j-1] != constant.leicode) opencell(i+1,j-1);
 if(i<19 && counts[i+1][j] != constant.leicode) opencell(i+1,j);
 if(i<19 && j<19 && counts[i+1][j+1] != constant.leicode) opencell(i+1,j+1);
 } else {
 buttons[i][j].setmargin(new insets(0,0,0,0));
 buttons[i][j].settext(counts[i][j] + "");
 }
 }
 
 void losegame() {
 for(int i=0;i<constant.row;i++) {
 for(int j=0;j<constant.col;j++) {
 int count = counts[i][j];
 if(count == constant.leicode) {
  buttons[i][j].setmargin(new insets(0,0,0,0));
  buttons[i][j].settext("雷");
  buttons[i][j].setbackground(color.red);
  buttons[i][j].setenabled(false);
 } else {
  buttons[i][j].setmargin(new insets(0,0,0,0));
  buttons[i][j].settext(count + "");
  buttons[i][j].setenabled(false);
  
 }
 }
 }
 joptionpane.showmessagedialog(frame,"error,你输了!");
 }
 
 public static void main(string[] args) {
 new saolei();
 }
 
 @override
 public void mousepressed(mouseevent e) {
 // todo auto-generated method stub
 
 }
 
 @override
 public void mousereleased(mouseevent e) {
 // todo auto-generated method stub
 
 }
 
 @override
 public void mouseentered(mouseevent e) {
 // todo auto-generated method stub
 
 }
 
 @override
 public void mouseexited(mouseevent e) {
 // todo auto-generated method stub
 
 }
}

常量类

?
1
2
3
4
5
6
7
public class saoleiconstant {
 final int row = 20;//行数30
 final int col = 20;//列数30
 final int leicode = 10;//定义雷下方的数字
 protected int temp = 20;
 protected int leicount = temp;//雷数30
}

效果图

java实现简单扫雷小游戏

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/tf1997/article/details/80786358

延伸 · 阅读

精彩推荐