import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.*;
import javax.swing.*;public class saolei extends JFrame implements ActionListener{ /**
 * @param args
 */ lei[][] jb = null;//实例化一个雷的数组的类
GridLayout glo2;//设置一个布局方式
JPanel[][] jp;//设置一组容器等未来布置雷用
JPanel jp1;//设置一个容器把所有的子容器 数组JPanel放进里面
JMenuBar a;//设置一个工具栏
JMenu a1;//设置一个子菜单
JMenu a2;//设置另外一个子菜单
JMenuItem a11;//设置一个子菜单的项,以下类同
JMenuItem a12;
JMenuItem a13;
JMenuItem a14;
JMenuItem a15;
JMenuItem a16;
int row;//一个全局变量,设置雷的数组的行的值
int col;//一个全局变量,设置雷的数组的行的值
int bomdCount;//一个全局变量,存放雷的数量

saolei(int row , int col , int bomdCount){//构造函数
this.setTitle("简易扫雷程序");//设置程序的标题
this.row = row;//把传入的row的值设置成,全部变量的row的值
this.col = col;//把传入的col的值设置成,全部变量的col的值
this.bomdCount = bomdCount;//把传入的bomdCount的值设置成,全部变量的bomdCount的值
jb = new lei[row][col];//实例化雷的数量,按照row行col列来设置

jp = new JPanel[row][col];//实例化容器的数量,按照row行col列来设置,便于装入每个雷
glo2 = new GridLayout(row,col);//设置row行col列的网格布局

a = new JMenuBar();// 菜单条
a1 = new JMenu("游戏(G)");// 菜单1
a2 = new JMenu("帮助(H)");// 菜单1
a11 = new JMenuItem("初级开局(N)");// 菜单1的菜单项 
a11.addActionListener(this);
a11.setAccelerator(KeyStroke.getKeyStroke("F2"));
a12 = new JMenuItem("中级开局");// 菜单1的菜单项 
a12.addActionListener(this);
a13 = new JMenuItem("高级开局");// 菜单1的菜单项 
a13.addActionListener(this);
a14 = new JMenuItem("退出", 'Q');// 菜单1的菜单项
a14.addActionListener(this);
a15 = new JMenuItem("帮助");// 菜单1的菜单项
a15.addActionListener(this);
a16 = new JMenuItem("关于作者");// 菜单1的菜单项
a16.addActionListener(this);//为a16这个菜单项添加监听器
a1.add(a11);//把a11 a12 a13 a14添加进a1中
a1.add(a12);
a1.add(a13);
a1.insertSeparator(3);//添加一条插入符
a1.add(a14);
a2.add(a15);//把a15 a16添加进a2中
a2.add(a16);
a.add(a1);//把a1 a2添加进a这个菜单项中
a.add(a2);
this.setJMenuBar(a);//设置主窗体的菜单设置成a
jp1 = new JPanel();//实例化jp1放在菜单项的下面



jp1.setLayout(glo2);//设置jp1的布局为网格布局


for(int i = 0 ; i < col ; i ++){
for(int j = 0 ; j < row ; j ++){
jp[i][j] = new JPanel(new BorderLayout());//分别把jb放入jp中,并加入监听器
jb[i][j] = new lei("    ");
jb[i][j].addActionListener(this);
jp[i][j].add(jb[i][j]);
}
}

for(int i = 0 ; i < col ; i ++){//把jp再加入jp1中
for(int j = 0 ; j < row ; j ++){
jp1.add(jp[i][j]);
}
}


Random r=new Random();//设置一个随机数
for(int i = 0 ; i <= (bomdCount - 1) ; i ++){//进行随机布雷
int index = r.nextInt(row - 1);
int index2 = r.nextInt(col - 1);
if(jb[index][index2].getFlag() != "1"){//如果已经是雷了 就跳过去
if(index > row){
index = 0;
}
else{
index = index + 1;
}
}
jb[index][index2].setFlag(1);//如果不是雷 就设置成雷
}

this.add(jp1 , BorderLayout.SOUTH);//把jp1这个容器放在整个窗体的下部
this.setLocation(358,135);//设置相对位置
this.pack();//设置刚好包含整个窗体的大小
this.setResizable(false);// 不能用鼠标拉伸窗体
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//可以关闭窗体 
Aroundbomb();//检查周围雷的数量
this.setVisible(true);//设置窗体显示
}



// 找周围的雷的数量
private void Aroundbomb() {//检查周围雷的数量

for(int i = 0 ; i < col ; i ++){
for(int j = 0 ; j < row ; j ++){
//当需要检测的单元格本身无地雷的情况下,统计周围的地雷个数
int count = 0;
if (jb[i][j].flag == 0) {
// 检测左边是否为地雷1
if ( (j - 1) >= 0) {
if (jb[i][j - 1].flag == 1) {
count += 1; 
}
}

// 检测右边是否为地雷2
if ( (j + 1) < col ) {
if (jb[i][j + 1].flag == 1) {
count += 1; //右边
}
}
//检测上方空格是否为地雷3
if ( (i - 1) > 0 ) {
if (jb[i - 1][j].flag == 1) {
count += 1; 
}
} //检测下方空格是否为地雷4
if ( (i + 1) < row) {
if (jb[i + 1][j].flag == 1) {
count += 1; 
}
}
// 检测左上是否为地雷5
if ( (j - 1 >= 0) && (i - 1 >= 0)) {
if (jb[i - 1 ][j - 1].flag == 1) {
count += 1; //
}
}
// 检测左下是否为地雷6
if ( (j - 1 >= 0) && (i + 1 < row)) {
if (jb[i + 1][j - 1].flag == 1) {
count += 1; //
}
} //检测右上方是否为地雷7
if ( (i - 1 >= 0) && (j + 1 < col)) {
if (jb[i - 1][j + 1].flag == 1) {
count += 1; 
}
}
// 检测右下是否为地雷8
if ( (i + 1 > row) && (j + 1 < col)) {
if (jb[i + 1][j + 1].flag == 1) {
count += 1; //
}
}
jb[i][j].countBomb = count;
}
}
}
} private int leaveGrid(){//计算剩下的格子数
int leaveGrid = 0;
for(int i = 0 ; i < col ; i ++){
for(int j = 0 ; j < row ; j ++){
if(jb[i][j].getopen() == false)
leaveGrid += 1;
}
}
return leaveGrid;
}

public static void main(String[] args) {
// TODO 自动生成方法存根
new saolei(4 , 4 , 4);
}

public void showAll(){//显示所有的地雷位置
for(int i = 0 ; i < col ; i ++){
for(int j = 0 ; j < row ; j ++){
if(1 == jb[i][j].flag){
jb[i][j].setEnabled(false);//如果是雷 而且没有显示出来 要自动打开并设置背景为×
jb[i][j].setText("×");
}
/*else if(jb[i][j].getopen() == false){
jb[i][j].setEnabled(false);
jb[i][j].setText(jb[i][j].getFlag());
}*/
}
} }

public void DiaplayAround(int x , int y){//显示周围雷没有雷的方格
if(jb[x][y].getopen() == true || jb[x][y].getFlag() == "1"){
return ;
} if(jb[x][y].countBomb == 0){
jb[x][y].setEnabled(false);
jb[x][y].setopen(true);
jb[x][y].setText(jb[x][y].getcountBomb());
}
}

public void actionPerformed(ActionEvent e) {
// TODO 自动生成方法存根
if(e.getSource() == a11){
new saolei(4 , 4 , 4);
}
else if(e.getSource() == a12){
new saolei(7 , 7 , 8);
}

else if(e.getSource() == a13){
new saolei(10 , 10 , 10);
}

else if(e.getSource() == a14){
System.exit(0);
}

else if(e.getSource() == a15){
JOptionPane.showMessageDialog(this, "暂时无帮助");
return;
}

else if(e.getSource() == a16){
JOptionPane.showMessageDialog(this, "作者***");
return;
}

for(int i = 0 ; i < col ; i ++){
for(int j = 0 ; j < row ; j ++){

if(e.getSource() == jb[i][j]){ //如果点击的是雷的按钮

jb[i][j].setEnabled(false);//设置按钮为不可用
jb[i][j].setopen(true);//设置雷的属性为打开状态

if(1 == jb[i][j].flag){//如果点击的是雷,那么提示

jb[i][j].setText("×");
JOptionPane.showMessageDialog(this, "点击到了雷,程序结束,显示所有");
showAll();
}

else if(bomdCount == leaveGrid()){//如果剩下的没打开的方格数 等于雷的数量 那么赢得胜利
jb[i][j].setText(jb[i][j].getcountBomb());
JOptionPane.showMessageDialog(this, "恭喜你游戏胜利!");
showAll();
}

else if(0 == jb[i][j].flag){
//jb[i][j].setText(jb[i][j].getFlag());//这里显示的应该是周围的雷的数量
jb[i][j].setText(jb[i][j].getcountBomb());
}
diaoyongxianshi(i , j); }
}
}
} private void diaoyongxianshi(int i , int j) {//这个方法是调用DiaplayAround来查询旁边是否是雷,是则打开了
// TODO 自动生成方法存根
if((i - 1) > 0 && (j - 1) > 0){
DiaplayAround(i - 1 , j - 1);
DiaplayAround(i , j - 1);
DiaplayAround(i - 1 , j);
}
if((i +1 ) < row && (j - 1) > 0){
DiaplayAround(i + 1 , j - 1);
DiaplayAround(i + 1 , j);
}
if((i - 1) > 0 && (j + 1) < col){
DiaplayAround(i - 1 , j + 1);
}
if((i + 1) < row && (j + 1) <col){
DiaplayAround(i , j + 1);
DiaplayAround(i + 1 , j + 1);
}
}
}
先说说问题:1.雷区的布雷好像有点问题,不能解决!
2.雷的背景设置有问题,总是明明点到了雷,但是显示的是数字,到最后的时候又把数字改成了雷的标志!
3.窗体只开一个的问题,这个已经解决了。
呵呵 帮忙修改一下吧 算法不太好耶。。

解决方案 »

  1.   


                int index = r.nextInt(row - 1);//[0,row - 1)如果row = 4,则index = [0,3) = 0,1,2 少一行。
                int index2 = r.nextInt(col - 1);
      

  2.   


    //你的flag是数字还是字符串?,字符串判断记得用equals,数字判断直接用 ==,后面的判断自己改改
    if(!jb[index][index2].getFlag().equals("1")){  //如果已经是雷了 就跳过去
                    if(index >= row){  // >   这里改为>=
                        index = 0;
                    }
                    else{
                        index = index + 1;
                    }
                }
         //检测上方空格是否为地雷3
                        if ( (i - 1) >= 0 ) {// >改为>=
                            if (jb[i - 1][j].flag == 1) {
                                count += 1; 
                            }
                        }
      

  3.   

    楼主你代码太多我就不仔细看了,贴下我以前写的扫雷里面的布雷和点击事件的代码,供参考哈:// 初始化炸弹数组
    public void setBombs(int num) {
    int x = bombs.length;
    int y = bombs[0].length;
    for (int i = 0; i < x; i++) {
    for (int j = 0; j < y; j++) {
    bombs[i][j] = "";
    }
    }
    HashSet<Integer> set = new HashSet<Integer>();
    while (set.size() < num) {
    int random = (int) (Math.random() * x * y);
    int i = random / y;
    int j = random % y;
    bombs[i][j] = "*";
    set.add(random);
    }
    for (int i = 0; i < x; i++) {
    for (int j = 0; j < y; j++) {
    if (!bombs[i][j].equals("*")) {
    int count = 0;
    if (i - 1 >= 0 && bombs[i - 1][j].equals("*")) {
    count++;
    }
    if (i - 1 >= 0 && j - 1 >= 0
    && bombs[i - 1][j - 1].equals("*")) {
    count++;
    }
    if (i - 1 >= 0 && j + 1 < y
    && bombs[i - 1][j + 1].equals("*")) {
    count++;
    }
    if (j - 1 >= 0 && bombs[i][j - 1].equals("*")) {
    count++;
    }
    if (j + 1 < y && bombs[i][j + 1].equals("*")) {
    count++;
    }
    if (i + 1 < x && j - 1 >= 0
    && bombs[i + 1][j - 1].equals("*")) {
    count++;
    }
    if (i + 1 < x && bombs[i + 1][j].equals("*")) {
    count++;
    }
    if (i + 1 < x && j + 1 < y
    && bombs[i + 1][j + 1].equals("*")) {
    count++;
    }
    bombs[i][j] = (count == 0) ? "" : "" + count;
    }
    }
    }
    }// 鼠标左键点击事件
    private void click(JButton b) {
    int x = buttons.length;
    int y = buttons[0].length;
    String str = "";
    for (int i = 0; i < x; i++) {
    for (int j = 0; j < y; j++) {
    if (b == buttons[i][j]) {
    str = bombs[i][j];
    }
    }
    }
    if (str.equals("*")) {
    for (int i = 0; i < x; i++) {
    for (int j = 0; j < y; j++) {
    buttons[i][j].setBackground(Color.CYAN);
    buttons[i][j].setForeground(Color.BLACK);
    buttons[i][j].setText(bombs[i][j]);
    buttons[i][j]
    .setBorder(new BevelBorder(BevelBorder.LOWERED));
    timer.stop();
    }
    }
    b.setBackground(Color.RED);
    } else if (str.matches("[1-9]")) {
    int i = Integer.parseInt(str);
    b.setBackground(Color.CYAN);
    switch (i) {
    case (1):
    b.setForeground(Color.BLACK);
    break;
    case (2):
    b.setForeground(Color.BLUE);
    break;
    case (3):
    b.setForeground(Color.RED);
    break;
    case (4):
    b.setForeground(Color.ORANGE);
    break;
    case (5):
    b.setForeground(Color.DARK_GRAY);
    break;
    case (6):
    b.setForeground(Color.YELLOW);
    break;
    }
    b.setText(str);
    b.setBorder(new BevelBorder(BevelBorder.LOWERED));
    isWin();
    } else if (str.matches("")) {
    b.setBackground(Color.CYAN);
    b.setForeground(Color.BLACK);
    b.setText("");
    b.setBorder(new BevelBorder(BevelBorder.LOWERED));
    for (int i = 0; i < x; i++) {
    for (int j = 0; j < y; j++) {
    if (b == buttons[i][j]) {
    if (i - 1 >= 0
    && buttons[i - 1][j].getBackground() != Color.CYAN) {
    click(buttons[i - 1][j]);
    }
    if (i - 1 >= 0
    && j - 1 >= 0
    && buttons[i - 1][j - 1].getBackground() != Color.CYAN) {
    click(buttons[i - 1][j - 1]);
    }
    if (i - 1 >= 0
    && j + 1 < y
    && buttons[i - 1][j + 1].getBackground() != Color.CYAN) {
    click(buttons[i - 1][j + 1]);
    }
    if (j - 1 >= 0
    && buttons[i][j - 1].getBackground() != Color.CYAN) {
    click(buttons[i][j - 1]);
    }
    if (j + 1 < y
    && buttons[i][j + 1].getBackground() != Color.CYAN) {
    click(buttons[i][j + 1]);
    }
    if (i + 1 < x
    && j - 1 >= 0
    && buttons[i + 1][j - 1].getBackground() != Color.CYAN) {
    click(buttons[i + 1][j - 1]);
    }
    if (i + 1 < x
    && buttons[i + 1][j].getBackground() != Color.CYAN) {
    click(buttons[i + 1][j]);
    }
    if (i + 1 < x
    && j + 1 < y
    && buttons[i + 1][j + 1].getBackground() != Color.CYAN) {
    click(buttons[i + 1][j + 1]);
    }
    }
    }
    }
    }
    }