本文实例讲述了java swing实现的扫雷游戏及改进版。分享给大家供大家参考,具体如下:
版本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;
/**
* 这个是一个简单的扫雷例子,刚接触swing编写的,适合新手练习
* 该程序使用setbounds(x,y,w,h)对控件布局
* 做法参考win xp自带的扫雷,当然还写功能没做出来,
* 另外做出来的功能有些还存在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 ; // 雷的个数
private final int button_border = 50 ; // 每个点的尺寸
private final int mine_size = 10 ; // 界面规格, 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;
/**
* 检测某点周围是否有雷,周围点的坐标可由该数组计算得到
*/
private int [][] mv = { { - 1 , 0 }, { - 1 , 1 }, { 0 , 1 }, { 1 , 1 }, { 1 , 0 },
{ 1 , - 1 }, { 0 , - 1 }, { - 1 , - 1 } };
/**
* 随机产生设定个数的雷
*/
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++; // 不记重复产生的雷
}
}
}
/**
* 将button数组放到frame上,与map[][]数组对应
*/
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); // 方便点击是判断是点击了哪个按钮
// 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( "欢迎测试,一共有" + mine_count + "个雷" );
jl.setvisible( true );
jl.setbounds( 20 , 20 , 500 , 30 );
this .add(jl);
showtime.settext( "已用时: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 ); //不使用布局管理器,每个控件的位置用setbounds设定
jb = new jbutton[mine_size][mine_size];
jl = new jlabel();
showtime = new jlabel();
map = new int [mine_size][mine_size]; // 将按钮映射到数组中
}
public static void main(string[] args) {
test test = new test( "服务器之家 - 扫雷游戏测试1" );
test.init();
test.run();
}
@override
public void actionperformed(actionevent e) {
object obj = e.getsource();
int x, y;
if ((obj instanceof jbutton) == false ) {
showmessage( "错误" , "内部错误" );
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();
}
/**
* 每次点击完后,判断有没有把全部雷都找到 通过计算状态为enable的按钮的个数来判断
*/
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( "胜利" , "本次扫雷共用时:" + 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++; // 该点附近雷数统计
} 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标签上显示一些信息
*
* @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( "已用时:" + 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( "错误" , "内部错误" );
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
}
}
|
运行效果:
版本2是对上面版本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
|
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( "欢迎测试,一共有" + lomine_count + "个雷" );
jl.setvisible( true );
jl.setbounds( 20 , 20 , 500 , 30 );
this .add(jl);
showtime.settext( "已用时: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 ); //不使用布局管理器,每个控件的位置用setbounds设定
jb = new jbutton[lomine_size][lomine_size];
jl = new jlabel();
showtime = new jlabel();
map = new int [lomine_size][lomine_size]; // 将按钮映射到数组中
}
public static void main(string[] args) {
saolei test = new saolei( "服务器之家 - 扫雷游戏测试2" );
test.init();
test.run();
}
@override
public void actionperformed(actionevent e) {
object obj = e.getsource();
int x, y;
if ((obj instanceof jbutton) == false ) {
showmessage( "错误" , "内部错误" );
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( "胜利" , "本次扫雷共用时:" + 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( "已用时:" + 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( "错误" , "内部错误" );
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) {
}
}
|
运行效果:
希望本文所述对大家java程序设计有所帮助。
原文链接:http://blog.csdn.net/Limbos/article/details/47701413