java swing实现的扫雷游戏及改进版完整示例
程序员文章站
2024-02-21 17:08:28
本文实例讲述了java swing实现的扫雷游戏及改进版。分享给大家供大家参考,具体如下:
版本1:
package awtdemo;
import java...
本文实例讲述了java swing实现的扫雷游戏及改进版。分享给大家供大家参考,具体如下:
版本1:
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程序的改进,在基础不变的基础上增加了右键标记功能以及自主选择难度功能。
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算法相关内容感兴趣的读者可查看本站专题:《java数据结构与算法教程》、《java操作dom节点技巧总结》、《java文件与目录操作技巧汇总》和《java缓存操作技巧汇总》
希望本文所述对大家java程序设计有所帮助。