1.按钮监听
package com.saolei.contorl;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import com.saolei.model.ComputeModel;
import com.saolei.view.BoomFrame;
import com.saolei.view.StartFrame;
public class BoomLook implements ActionListener {//监听类
private BoomFrame win;// 定义BoomFrame窗口
private int row, col;// 定义行、列
private StartFrame win0;// 定义StartFrame窗口
private long time, htime, s;// 定义时间的存放位置
public BoomLook(BoomFrame win, int row, int col) {
super();
this.win = win; //构造方法重载
this.row = row;
this.col = col;
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO 自动生成的方法存根
setType(row, col);
}
// 给雷界面设置监听动作
public void setType(int row, int col) {
JButton but = win.getBut()[row][col];
but.setBorder(finalBoom.Bor2);// 把按钮的边线设置为单线
but.setBackground(Color.orange);// 把按钮的颜色设置为橙色
htime = System.currentTimeMillis();// 获取每点击一次按钮的时间
s = (htime - win.getTime()) / 1000;// 算出点击按钮时的游戏时间
this.win.getNum2().setText(
ComputeModel.formatNum(Integer.parseInt(s + "")));// 将时间文本设置为三位数显示
if (ComputeModel.isboom(row, col, win.getBoompost(), win.getBwidth())) {// 判断点击的按钮是否为地雷
but.setIcon(finalBoom.boom[0]);// 添加图片
JOptionPane.showMessageDialog(win, "壮志未酬身先死!!!!", "你猜!!",
JOptionPane.ERROR_MESSAGE);// 弹出信息窗口“你输了”
win.setVisible(false);// 关闭弹出窗口时,游戏界面一起隐藏
win0 = new StartFrame();// 重新打开选择难度界面
} else {
// JButton[][] but0 = new JButton[win.getBwidth()][win.getBhight()];
int sum = 0;
// 计算剩余没有点击按钮的个数
for (int i = 0; i < win.getBwidth(); i++) {
for (int j = 0; j < win.getBhight(); j++) {
JButton but0 = win.getBut()[i][j];
// System.out.println(win.getBut()[i][j].getBorder());
// System.out.println((finalBoom.Bor0));
if (win.getBut()[i][j].getBorder() == finalBoom.Bor0) {
sum++;
// System.out.println(sum);
}
}
}
// 如果剩余按钮的个数与地雷数相同,则游戏胜利;并弹出信息“你赢了”
if (sum == win.getBoomnum()) {
JOptionPane.showMessageDialog(win, "恭喜!!你赢了!!", "你猜!!",
JOptionPane.PLAIN_MESSAGE);
win.setVisible(false);
win0 = new StartFrame();
}
int count = ComputeModel.getRoundBoomCount(row, col,
win.getBoompost(), win.getBwidth(), win.getBhight());
//如果按钮不是地雷,给出其周边八个按钮的雷数
if (count > 0) {
but.setText(count + "");
} else {
//如果没有雷,进行递归
int[][] round = ComputeModel.getRoundBox(row, col);
for (int i = 0; i < round.length; i++) {
// 检测格子是否合法
if (ComputeModel.ispost(round[i][0], round[i][1],
win.getBwidth(), win.getBhight())) {
// 检测格子是否已经调用,否则继续递归
if (win.getBut()[round[i][0]][round[i][1]].getBorder() != finalBoom.Bor2) {
setType(round[i][0], round[i][1]);
}
}
}
}
}
}
}
2.主要算法
package com.saolei.model;
import java.awt.Color;
import java.text.DecimalFormat;
import javax.swing.JButton;
import com.saolei.contorl.BoomLook;
import com.saolei.contorl.finalBoom;
import com.saolei.view.*;
public class ComputeModel {// 算法类
// 计算窗口大小
public static int[] getwindowsize(int bwidth, int bhight) {
int w = finalBoom.boomwidth * bhight + 10 + 8;
int h = finalBoom.boomwidth * bwidth + 10 + 8 + finalBoom.boomwidth * 2
+ 5 + 30;
// 获取时间/秒
long time = System.currentTimeMillis();
long htime = System.currentTimeMillis();
long s = (htime - time) / 1000;
return new int[] { w, h };
}
// 随机给定雷的位置
public static int[] getRandomBoom(int bwidth, int bhight, int boomnum) {
int[] post = new int[boomnum];
int maxpost = bwidth * bhight;
post[0] = (int) (Math.random() * maxpost + 1);
for (int i = 1; i < post.length;) {
int temp = (int) (Math.random() * maxpost + 1);
boolean flog = true;
for (int j = 0; j < i; j++) {
if (temp == post[j]) {
flog = false;
break;
}
}
if (flog) {
post[i] = temp;
i++;
}
}
return post;
}
// 计算位置
public static int getpost(int row, int col, int bwidth) {
return row * bwidth + col + 1;
}
// 判断是否为雷
public static boolean isboom(int row, int col, int[] boompost, int bwidth) {
int post1 = getpost(row, col, bwidth);
for (int i = 0; i < boompost.length; i++) {
if (boompost[i] == post1) {
return true;
}
}
return false;
}
// 计算周围雷的数量
public static int getRoundBoomCount(int row, int col, int[] boompost,
int bwidth, int bhight) {
int count = 0;
// 获取格子周围的坐标
int[][] round = getRoundBox(row, col);
for (int i = 0; i < round.length; i++) {
// 判断格子坐标是否在范围内
if (ispost(round[i][0], round[i][1], bwidth, bhight)) {
if (isboom(round[i][0], round[i][1], boompost, bwidth)) {
count++;
}
}
}
return count;
}
// 判断指定格子坐标是否合法
public static boolean ispost(int row, int col, int bwidth, int bhight) {
if (row < 0 || row >= bwidth) {
return false;
}
if (col < 0 || col >= bhight) {
return false;
}
return true;
}
// 计算周围格子坐标
public static int[][] getRoundBox(int row, int col) {
return new int[][] { { row - 1, col - 1 }, { row - 1, col },
{ row - 1, col + 1 }, { row, col + 1 }, { row + 1, col + 1 },
{ row + 1, col }, { row + 1, col - 1 }, { row, col - 1 } };
}
// 格式化三位数
public static String formatNum(int num) {
DecimalFormat df = new DecimalFormat("000");
return df.format(num);
}
}