本文實(shí)例講述了java swing實(shí)現(xiàn)的掃雷游戲及改進(jìn)版。分享給大家供大家參考,具體如下:
版本1:
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
|
package awtdemo; import java.awt.event.actionevent; import java.awt.event.actionlistener; import java.awt.event.mouseevent; import java.awt.event.mouselistener; import javax.swing.jbutton; import javax.swing.jframe; import javax.swing.jlabel; /** * 這個(gè)是一個(gè)簡(jiǎn)單的掃雷例子,剛接觸swing編寫的,適合新手練習(xí) * 該程序使用setbounds(x,y,w,h)對(duì)控件布局 * 做法參考win xp自帶的掃雷,當(dāng)然還寫功能沒做出來, * 另外做出來的功能有些還存在bug * * @author ping_qc */ public class test extends jframe implements actionlistener, runnable, mouselistener { private static final long serialversionuid = -2417758397965039613l; private final int empty = 0 ; private final int mine = 1 ; private final int checked = 2 ; private final int mine_count = 10 ; // 雷的個(gè)數(shù) private final int button_border = 50 ; // 每個(gè)點(diǎn)的尺寸 private final int mine_size = 10 ; // 界面規(guī)格, 20x20 private final int start_x = 20 ; // 起始位置x private final int start_y = 50 ; // 起始位置y private boolean flag; private jbutton[][] jb; private jlabel jl; private jlabel showtime; private int [][] map; /** * 檢測(cè)某點(diǎn)周圍是否有雷,周圍點(diǎn)的坐標(biāo)可由該數(shù)組計(jì)算得到 */ private int [][] mv = { { - 1 , 0 }, { - 1 , 1 }, { 0 , 1 }, { 1 , 1 }, { 1 , 0 }, { 1 , - 1 }, { 0 , - 1 }, { - 1 , - 1 } }; /** * 隨機(jī)產(chǎn)生設(shè)定個(gè)數(shù)的雷 */ public void makemine() { int i = 0 , tx, ty; for (; i < mine_count;) { tx = ( int ) (math.random() * mine_size); ty = ( int ) (math.random() * mine_size); if (map[tx][ty] == empty) { map[tx][ty] = mine; i++; // 不記重復(fù)產(chǎn)生的雷 } } } /** * 將button數(shù)組放到frame上,與map[][]數(shù)組對(duì)應(yīng) */ public void makebutton() { for ( int i = 0 ; i < mine_size; i++) { for ( int j = 0 ; j < mine_size; j++) { jb[i][j] = new jbutton(); // if (map[i][j] == mine) // jb[i][j].settext(i+","+j); // listener add jb[i][j].addactionlistener( this ); jb[i][j].addmouselistener( this ); jb[i][j].setname(i + "_" + j); // 方便點(diǎn)擊是判斷是點(diǎn)擊了哪個(gè)按鈕 // font font = new font(font.serif, font.bold, 10); // jb[i][j].setfont(font); // jb[i][j].settext(i+","+j); jb[i][j].setbounds(j * button_border + start_x, i * button_border + start_y, button_border, button_border); this .add(jb[i][j]); } } } public void init() { flag = false ; jl.settext( "歡迎測(cè)試,一共有" + mine_count + "個(gè)雷" ); jl.setvisible( true ); jl.setbounds( 20 , 20 , 500 , 30 ); this .add(jl); showtime.settext( "已用時(shí):0 秒" ); showtime.setbounds( 400 , 20 , 100 , 30 ); this .add(showtime); makemine(); makebutton(); this .setsize( 550 , 600 ); this .setlocation( 700 , 100 ); this .setresizable( false ); this .setdefaultcloseoperation(exit_on_close); this .setvisible( true ); } public test(string title) { super (title); this .setlayout( null ); //不使用布局管理器,每個(gè)控件的位置用setbounds設(shè)定 jb = new jbutton[mine_size][mine_size]; jl = new jlabel(); showtime = new jlabel(); map = new int [mine_size][mine_size]; // 將按鈕映射到數(shù)組中 } public static void main(string[] args) { test test = new test( "服務(wù)器之家 - 掃雷游戲測(cè)試1" ); test.init(); test.run(); } @override public void actionperformed(actionevent e) { object obj = e.getsource(); int x, y; if ((obj instanceof jbutton) == false ) { showmessage( "錯(cuò)誤" , "內(nèi)部錯(cuò)誤" ); return ; } string[] tmp_str = ((jbutton) obj).getname().split( "_" ); x = integer.parseint(tmp_str[ 0 ]); y = integer.parseint(tmp_str[ 1 ]); if (map[x][y] == mine) { showmessage( "死亡" , "你踩到地雷啦~~~" ); flag = true ; showmine(); return ; } dfs(x, y, 0 ); checksuccess(); } /** * 每次點(diǎn)擊完后,判斷有沒有把全部雷都找到 通過計(jì)算狀態(tài)為enable的按鈕的個(gè)數(shù)來判斷 */ private void checksuccess() { int cnt = 0 ; for ( int i = 0 ; i < mine_size; i++) { for ( int j = 0 ; j < mine_size; j++) { if (jb[i][j].isenabled()) { cnt++; } } } if (cnt == mine_count) { string tmp_str = showtime.gettext(); tmp_str = tmp_str.replaceall( "[^0-9]" , "" ); showmessage( "勝利" , "本次掃雷共用時(shí):" + tmp_str + "秒" ); flag = true ; showmine(); } } private int dfs( int x, int y, int d) { map[x][y] = checked; int i, tx, ty, cnt = 0 ; for (i = 0 ; i < 8 ; i++) { tx = x + mv[i][ 0 ]; ty = y + mv[i][ 1 ]; if (tx >= 0 && tx < mine_size && ty >= 0 && ty < mine_size) { if (map[tx][ty] == mine) { cnt++; // 該點(diǎn)附近雷數(shù)統(tǒng)計(jì) } else if (map[tx][ty] == empty) { ; } else if (map[tx][ty] == checked) { ; } } } if (cnt == 0 ) { for (i = 0 ; i < 8 ; i++) { tx = x + mv[i][ 0 ]; ty = y + mv[i][ 1 ]; if (tx >= 0 && tx < mine_size && ty >= 0 && ty < mine_size && map[tx][ty] != checked) { dfs(tx, ty, d + 1 ); } } } else { jb[x][y].settext(cnt + "" ); } jb[x][y].setenabled( false ); return cnt; } /** * 在jl標(biāo)簽上顯示一些信息 * * @param title * @param info */ private void showmessage(string title, string info) { jl.settext(info); system.out.println( "in functino showmessage() : " + info); } public void run() { int t = 0 ; while ( true ) { if (flag) { break ; } try { thread.sleep( 1000 ); } catch (interruptedexception e) { e.printstacktrace(); } t++; showtime.settext( "已用時(shí):" + t + " 秒" ); } // showmine(); } private void showmine() { // icon iconmine = new imageicon("e:/mine.jpg"); for ( int i = 0 ; i < mine_size; i++) { for ( int j = 0 ; j < mine_size; j++) { if (map[i][j] == mine) { jb[i][j].settext( "#" ); // jb[i][j].seticon(iconmine); } } } } @override public void mouseclicked(mouseevent e) { if (e.getbutton() == 3 ) { object obj = e.getsource(); if ((obj instanceof jbutton) == false ) { showmessage( "錯(cuò)誤" , "內(nèi)部錯(cuò)誤" ); return ; } string[] tmp_str = ((jbutton) obj).getname().split( "_" ); int x = integer.parseint(tmp_str[ 0 ]); int y = integer.parseint(tmp_str[ 1 ]); if ( "{1}" .equals(jb[x][y].gettext())) { jb[x][y].settext( "" ); } else { jb[x][y].settext( "{1}" ); } /* if(jb[x][y].geticon() == null){ jb[x][y].seticon(new imageicon("e:/flag.jpg")); }else{ jb[x][y].seticon(null); }*/ } } @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 } } |
運(yùn)行效果:
版本2是對(duì)上面版本1程序的改進(jìn),在基礎(chǔ)不變的基礎(chǔ)上增加了右鍵標(biāo)記功能以及自主選擇難度功能。
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
|
package awtdemo; import java.awt.event.actionevent; import java.awt.event.actionlistener; import java.awt.event.mouseevent; import java.awt.event.mouselistener; import javax.swing.jbutton; import javax.swing.jframe; import javax.swing.jlabel; @suppresswarnings ( "serial" ) public class saolei extends jframe implements actionlistener, runnable, mouselistener { private final int loempty = 0 ; private final int lomine = 1 ; private final int lochecked = 2 ; private final int lomine_count = 10 ; private final int lobutton_border = 50 ; private final int lomine_size = 10 ; private final int lostart_x = 20 ; private final int lostart_y = 50 ; private boolean flag; private jbutton[][] jb; private jlabel jl; private jlabel showtime; private int [][] map; private int [][] mv = { { - 1 , 0 }, { - 1 , 1 }, { 0 , 1 }, { 1 , 1 }, { 1 , 0 }, { 1 , - 1 }, { 0 , - 1 }, { - 1 , - 1 } }; public void makelomine() { int i = 0 , tx, ty; for (; i < lomine_count;) { tx = ( int ) (math.random() * lomine_size); ty = ( int ) (math.random() * lomine_size); if (map[tx][ty] == loempty) { map[tx][ty] = lomine; i++; } } } public void makebutton() { for ( int i = 0 ; i < lomine_size; i++) { for ( int j = 0 ; j < lomine_size; j++) { jb[i][j] = new jbutton(); jb[i][j].addactionlistener( this ); jb[i][j].addmouselistener( this ); jb[i][j].setname(i + "_" + j); jb[i][j].setbounds(j * lobutton_border + lostart_x, i * lobutton_border + lostart_y, lobutton_border, lobutton_border); this .add(jb[i][j]); } } } public void init() { flag = false ; jl.settext( "歡迎測(cè)試,一共有" + lomine_count + "個(gè)雷" ); jl.setvisible( true ); jl.setbounds( 20 , 20 , 500 , 30 ); this .add(jl); showtime.settext( "已用時(shí):0 秒" ); showtime.setbounds( 400 , 20 , 100 , 30 ); this .add(showtime); makelomine(); makebutton(); this .setsize( 550 , 600 ); this .setlocation( 700 , 100 ); this .setresizable( false ); this .setdefaultcloseoperation(exit_on_close); this .setvisible( true ); } public saolei(string title) { super (title); this .setlayout( null ); //不使用布局管理器,每個(gè)控件的位置用setbounds設(shè)定 jb = new jbutton[lomine_size][lomine_size]; jl = new jlabel(); showtime = new jlabel(); map = new int [lomine_size][lomine_size]; // 將按鈕映射到數(shù)組中 } public static void main(string[] args) { saolei test = new saolei( "服務(wù)器之家 - 掃雷游戲測(cè)試2" ); test.init(); test.run(); } @override public void actionperformed(actionevent e) { object obj = e.getsource(); int x, y; if ((obj instanceof jbutton) == false ) { showmessage( "錯(cuò)誤" , "內(nèi)部錯(cuò)誤" ); return ; } string[] tmp_str = ((jbutton) obj).getname().split( "_" ); x = integer.parseint(tmp_str[ 0 ]); y = integer.parseint(tmp_str[ 1 ]); if (map[x][y] == lomine) { showmessage( "死亡" , "你踩到地雷啦~~~" ); flag = true ; showlomine(); return ; } dfs(x, y, 0 ); checksuccess(); } private void checksuccess() { int cnt = 0 ; for ( int i = 0 ; i < lomine_size; i++) { for ( int j = 0 ; j < lomine_size; j++) { if (jb[i][j].isenabled()) { cnt++; } } } if (cnt == lomine_count) { string tmp_str = showtime.gettext(); tmp_str = tmp_str.replaceall( "[^0-9]" , "" ); showmessage( "勝利" , "本次掃雷共用時(shí):" + tmp_str + "秒" ); flag = true ; showlomine(); } } private int dfs( int x, int y, int d) { map[x][y] = lochecked; int i, tx, ty, cnt = 0 ; for (i = 0 ; i < 8 ; i++) { tx = x + mv[i][ 0 ]; ty = y + mv[i][ 1 ]; if (tx >= 0 && tx < lomine_size && ty >= 0 && ty < lomine_size) { if (map[tx][ty] == lomine) { cnt++; } else if (map[tx][ty] == loempty) { ; } else if (map[tx][ty] == lochecked) { ; } } } if (cnt == 0 ) { for (i = 0 ; i < 8 ; i++) { tx = x + mv[i][ 0 ]; ty = y + mv[i][ 1 ]; if (tx >= 0 && tx < lomine_size && ty >= 0 && ty < lomine_size && map[tx][ty] != lochecked) { dfs(tx, ty, d + 1 ); } } } else { jb[x][y].settext(cnt + "" ); } jb[x][y].setenabled( false ); return cnt; } private void showmessage(string title, string info) { jl.settext(info); system.out.println( "in functino showmessage() : " + info); } public void run() { int t = 0 ; while ( true ) { if (flag) { break ; } try { thread.sleep( 1000 ); } catch (interruptedexception e) { e.printstacktrace(); } t++; showtime.settext( "已用時(shí):" + t + " 秒" ); } } private void showlomine() { for ( int i = 0 ; i < lomine_size; i++) { for ( int j = 0 ; j < lomine_size; j++) { if (map[i][j] == lomine) { jb[i][j].settext( "雷" ); } } } } public void mouseclicked(mouseevent e) { if (e.getbutton() == 3 ) { object obj = e.getsource(); if ((obj instanceof jbutton) == false ) { showmessage( "錯(cuò)誤" , "內(nèi)部錯(cuò)誤" ); return ; } string[] tmp_str = ((jbutton) obj).getname().split( "_" ); int x = integer.parseint(tmp_str[ 0 ]); int y = integer.parseint(tmp_str[ 1 ]); if ( "{1}quot" .equals(jb[x][y].gettext())) { jb[x][y].settext( "" ); } else { jb[x][y].settext( "{1}quot" ); } } } public void mousepressed(mouseevent e) { } @override public void mousereleased(mouseevent e) { } public void mouseentered(mouseevent e) { } @override public void mouseexited(mouseevent e) { } } |
運(yùn)行效果:
希望本文所述對(duì)大家java程序設(shè)計(jì)有所幫助。
原文鏈接:http://blog.csdn.net/Limbos/article/details/47701413