您的位置:首页 > 其它

模拟实现扫雷游戏

2007-12-06 10:36 579 查看
//模拟实现扫雷游戏

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.LineBorder;

public class MySwingTest05 {

JFrame jf;

Container con;

JPanel jp;

// 按钮数组
JButton jb[];

// 标签数组
JLabel jl[];

// 中间容器数组
JPanel jpt[];

// 产生标签的内容的数组
char ch[];

// 定义地雷的个数和位置的数组
int num[];

// 计算地雷的个数 并写到标签中
int temp;

// 产生图盘的行数
int int01 = 9;

// 地雷的个数为int02
int int02 = 10;

// 标签和按钮的大小
int xy = 15;

public MySwingTest05() {

// TODO 自动生成构造函数存根

// 生成随机数组

ch = new char[int01 * int01];
num = new int[int02];
temp = 0;

for (int i = 0; i < num.length;) {
temp = (int) (Math.random() * (int01 * int01));
for (int j = 0; j < i; j++) {
if (temp == num[j] && i != 0) {
temp = 0;
break;
}
}
if (temp != 0) {
num[i] = temp;
// System.out.println(num[i]);
i++;
}

}

/*
* //保证每行都有地雷 for (int i = 0; i < num.length; i++) { temp = int01 * i;
* num[i] = (int) (Math.random() * int01 + temp); //
* System.out.println(num[i]); }
*/

for (int i = 0; i < num.length; i++) {

ch[num[i]] = '⊙';

}
for (int i = 0; i < ch.length; i++) {

if (ch[i] != '⊙') {
if (i == 0) {
temp = 0;
temp += ch[i + 1] == '⊙' ? 1 : 0;// 右边
temp += ch[i + int01] == '⊙' ? 1 : 0;// 下边
temp += ch[i + int01 + 1] == '⊙' ? 1 : 0;// 右下边
if (temp == 0) {
ch[i] = ' ';
} else {
ch[i] = getchar(temp);

}
} else if (i == (int01 - 1)) {
temp = 0;
temp += ch[i - 1] == '⊙' ? 1 : 0;// 左边
temp += ch[i + int01] == '⊙' ? 1 : 0;// 下边
temp += ch[i + int01 - 1] == '⊙' ? 1 : 0;// 左下边
if (temp == 0) {
ch[i] = ' ';
} else {
ch[i] = getchar(temp);
}
}

else if (i == int01 * (int01 - 1)) {
temp = 0;
temp += ch[i + 1] == '⊙' ? 1 : 0;// 右边
temp += ch[i - int01] == '⊙' ? 1 : 0;// 上边
temp += ch[i - int01 + 1] == '⊙' ? 1 : 0;// 右上边
if (temp == 0) {
ch[i] = ' ';
} else {
ch[i] = getchar(temp);
}
} else if (i == (int01 * int01 - 1)) {
temp = 0;
temp += ch[i - 1] == '⊙' ? 1 : 0;// 左边
temp += ch[i - int01] == '⊙' ? 1 : 0;// 上边
temp += ch[i - int01 - 1] == '⊙' ? 1 : 0;// 左上边
if (temp == 0) {
ch[i] = ' ';
} else {
ch[i] = getchar(temp);
}
} else if (i > 0 && i < int01 - 1) {
temp = 0;
temp += ch[i + 1] == '⊙' ? 1 : 0;// 右边
temp += ch[i - 1] == '⊙' ? 1 : 0;// 左边
temp += ch[i + int01] == '⊙' ? 1 : 0;// 下边
temp += ch[i + int01 - 1] == '⊙' ? 1 : 0;// 左下边
temp += ch[i + int01 + 1] == '⊙' ? 1 : 0;// 右下边
if (temp == 0) {
ch[i] = ' ';
} else {
ch[i] = getchar(temp);
}
} else if (i > int01 * (int01 - 1) && i < int01 * int01 - 1) {
temp = 0;
temp += ch[i + 1] == '⊙' ? 1 : 0;// 右边
temp += ch[i - 1] == '⊙' ? 1 : 0;// 左边
temp += ch[i - int01] == '⊙' ? 1 : 0;// 上边
temp += ch[i - int01 - 1] == '⊙' ? 1 : 0;// 左上边
temp += ch[i - int01 + 1] == '⊙' ? 1 : 0;// 右上边
if (temp == 0) {
ch[i] = ' ';
} else {
ch[i] = getchar(temp);
}
} else if (i % int01 == 0) {
temp = 0;
temp += ch[i + 1] == '⊙' ? 1 : 0;// 右边
temp += ch[i - int01] == '⊙' ? 1 : 0;// 上边
temp += ch[i + int01] == '⊙' ? 1 : 0;// 下边
temp += ch[i - int01 + 1] == '⊙' ? 1 : 0;// 右上边
temp += ch[i + int01 + 1] == '⊙' ? 1 : 0;// 右下边
if (temp == 0) {
ch[i] = ' ';
} else {
ch[i] = getchar(temp);
}
} else if (i % int01 == int01 - 1) {
temp = 0;
temp += ch[i - 1] == '⊙' ? 1 : 0;// 左边
temp += ch[i - int01] == '⊙' ? 1 : 0;// 上边
temp += ch[i + int01] == '⊙' ? 1 : 0;// 下边
temp += ch[i - int01 - 1] == '⊙' ? 1 : 0;// 左上边
temp += ch[i + int01 - 1] == '⊙' ? 1 : 0;// 左下边
if (temp == 0) {
ch[i] = ' ';
} else {
ch[i] = getchar(temp);
}
} else {
temp = 0;
temp += ch[i + 1] == '⊙' ? 1 : 0;// 右边
temp += ch[i - 1] == '⊙' ? 1 : 0;// 左边
temp += ch[i - int01] == '⊙' ? 1 : 0;// 上边
temp += ch[i + int01] == '⊙' ? 1 : 0;// 下边
temp += ch[i - int01 - 1] == '⊙' ? 1 : 0;// 左上边
temp += ch[i - int01 + 1] == '⊙' ? 1 : 0;// 右上边
temp += ch[i + int01 - 1] == '⊙' ? 1 : 0;// 左下边
temp += ch[i + int01 + 1] == '⊙' ? 1 : 0;// 右下边
if (temp == 0) {
ch[i] = ' ';
} else {
ch[i] = getchar(temp);
}
}
}
// System.out.println(ch[i]);

}// 生成图形数组完毕

// 生成图形
jf = new JFrame("扫雷");
con = jf.getContentPane();

jp = new JPanel(new GridLayout(int01, int01, 0, 0));

jb = new JButton[int01 * int01];
jl = new JLabel[int01 * int01];
jpt = new JPanel[int01 * int01];

for (int i = 0; i < jpt.length; i++) {
jpt[i] = new JPanel();
jpt[i].setLayout(new BorderLayout());
// getchar();
jl[i] = new JLabel("" + ch[i] + "");
jl[i].setPreferredSize(new Dimension(xy, xy));// 设置图形大小
jl[i].setHorizontalAlignment(JLabel.CENTER);// 中间显示
jb[i] = new JButton();
jb[i].setPreferredSize(new Dimension(xy, xy));// 设置图形大小
jb[i].addActionListener(new actionL01(this));
jb[i].addMouseListener(new mouseL01(this));
jb[i].setActionCommand("" + i);

// jpt[i].add(jl[i]);
jpt[i].setBorder(new LineBorder(Color.GRAY, 1));
jpt[i].add(jb[i]);
jp.add(jpt[i]);
}

con.add(jp);

Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setLocation((int) ((dim.getWidth() - jf.getWidth()) / 2),
(int) ((dim.getHeight() - jf.getHeight()) / 2));
jf.pack();
jf.setResizable(false);
jf.setVisible(true);

}

public static void main(String[] args) {

new MySwingTest05();

}

// 根据int生成char
public static char getchar(int i) {
switch (i) {
case 1:
return '1';
case 2:
return '2';
case 3:
return '3';
case 4:
return '4';
case 5:
return '5';
case 6:
return '6';
case 7:
return '7';
case 8:
return '8';
default:
return ' ';
}
}

// 实现接口 ActionListener

class actionL01 implements ActionListener {
MySwingTest05 mm;

// JButton jbtemp;

public actionL01() {

}

public actionL01(MySwingTest05 mm) {

this.mm = mm;
}

public void actionPerformed(ActionEvent e) {

// this.jbtemp=(JButton) e.getSource();

this.action1(Integer.parseInt((e.getActionCommand())));
this.action5();

}

// 根据不同的按钮对象打开相应的标签
public void action1(int i) {

// 因为地雷是被数字包围着的 所以不用担心打开地雷
// 如果打开的是地雷就直接报告
// 如果打开的是数字 就只打开自己本身
// 如果打开的空 就递归调用 循环打开 直到数字包围着地雷的第一层

// 首先判断按钮的颜色 pink则不打开
if (mm.jb[i].getBackground() != Color.PINK
&& mm.jpt[i].getComponent(0).getClass().toString().equals(
"class javax.swing.JButton")) {
if (mm.ch[i] == '⊙') { // 判断是地雷
action3(i);
mm.jf.setTitle("中雷!");
action4();
} else if (Character.isDigit(mm.ch[i])) { // 判断是数字
action3(i);
} else // 判断是空
{
action2(i);
}

}
}

// 踩雷时触发
public void action4() {
for (int i = 0; i < mm.jb.length; i++) {
// 判断标示的地方是否为地雷

// 如果错误就用表示
if (mm.ch[i] != '⊙' && (mm.jb[i].getBackground() == Color.PINK)) {
mm.jl[i].setText("※");
}
// 如果没有表示就设置为空
else if (mm.ch[i] != '⊙'
&& mm.jpt[i].getComponent(0).getClass().toString().equals(
"class javax.swing.JButton")) {
mm.jl[i].setText("");// 将没有打开且不是地雷的按钮设置为空
}
// 如果正确就用表示
else if (mm.ch[i] == '⊙'
&& (mm.jb[i].getBackground() == Color.PINK)) {
mm.jl[i].setText("√");
}
action3(i);

}
}

// 根据相应的按钮对象打开相应的标签
public void action3(int i) {

mm.jpt[i].remove(mm.jb[i]);
// 根据不同的数字设置颜色
if (Character.isDigit(mm.ch[i])) {
switch (mm.ch[i]) {

case '1':
mm.jl[i].setForeground(Color.BLUE);
break;
case '2':
mm.jl[i].setForeground(Color.GREEN);
break;
case '3':
mm.jl[i].setForeground(Color.RED);
break;
case '4':
mm.jl[i].setForeground(Color.MAGENTA);
break;
case '5':
mm.jl[i].setForeground(Color.CYAN);
break;
case '6':
mm.jl[i].setForeground(Color.ORANGE);
break;
case '7':
mm.jl[i].setForeground(Color.PINK);
break;
case '8':
mm.jl[i].setForeground(Color.DARK_GRAY);
break;

}
}
mm.jpt[i].add(mm.jl[i]);
mm.jf.validate();

}

// 判断是否胜利
public void action5() {
int sum = 0;
for (int i = 0; i < mm.jb.length; i++) {

if (mm.ch[i] == '⊙' && (mm.jb[i].getBackground() == Color.PINK)) {
// sum++;
// System.out.println(sum);
if (++sum == mm.num.length) {
mm.jf.setTitle("胜利!");
for (int j = 0; j < mm.jb.length; j++) {
action3(j);
}
break;

}
}
}
}

// 只判断上下左右 采用递归调用
public void action2(int ind) {
int int01 = mm.int01;

action3(ind);

// 不选择action2() 而选择action1() 是因为需要判断按钮的颜色
// if (Character.isDigit(mm.ch[ind]) == false) {
if (ind == 0) {
/*
* if (mm.ch[ind + 1] == ' ')// 右边 {
*/
action1(ind + 1);// 右边
// 选择action1() 是因为action1()能够判断按钮的颜色
// action2(ind + 1);//不选择action2() 是因为action2()不能判断按钮的颜色 以下同理
/*
* } else { action1(ind + 1); } if (mm.ch[ind + int01] == ' ')// 下边 {
*/
action1(ind + int01);// 下边
/*
* } else { action1(ind + int01); } if (mm.ch[ind + int01 + 1] == ' ' &&
* mm.jpt[ind + int01 + 1].getComponent(0).getClass()
* .toString().equals("class javax.swing.JButton"))// 右下边 {
*/
action1(ind + int01 + 1);// 右下边
/*
* } else { action1(ind + int01 + 1); }
*/

} else if (ind == int01 - 1) {
/*
* if (mm.ch[ind - 1] == ' ')// 左边 {
*/
action1(ind - 1);// 左边
/*
* } else { action1(ind - 1); } if (mm.ch[ind + int01] == ' ') // 下边 {
*/
action1(ind + int01);// 下边
/*
* } else { action1(ind + int01); } if (mm.ch[ind + int01 - 1] == ' ' &&
* mm.jpt[ind + int01 - 1].getComponent(0).getClass()
* .toString().equals("class javax.swing.JButton"))// 左下边 {
*/
action1(ind + int01 - 1);// 左下边
/*
* } else { action1(ind + int01 - 1); }
*/

} else if (ind == int01 * (int01 - 1)) {
/*
* if (mm.ch[ind + 1] == ' ')// 右边 {
*/
action1(ind + 1);// 右边
/*
* } else { action1(ind + 1); } if (mm.ch[ind - int01] == ' ')// 上边 {
*/
action1(ind - int01);// 上边
/*
* } else { action1(ind - int01); }
*
* if (mm.ch[ind - int01 + 1] == ' ' && mm.jpt[ind - int01 +
* 1].getComponent(0).getClass() .toString().equals("class
* javax.swing.JButton"))// 右上边 {
*/
action1(ind - int01 + 1);// 右上边
/*
* } else { action1(ind - int01 + 1); }
*/

} else if (ind == int01 * int01 - 1) {
/*
* if (mm.ch[ind - 1] == ' ')// 左边 {
*/
action1(ind - 1);// 左边
/*
* } else { action1(ind - 1); } if (mm.ch[ind - int01] == ' ')// 上边 {
*/
action1(ind - int01);// 上边
/*
* } else { action1(ind - int01); } if (mm.ch[ind - int01 - 1] == ' ' &&
* mm.jpt[ind - int01 - 1].getComponent(0).getClass()
* .toString().equals("class javax.swing.JButton"))// 左上边 {
*/
action1(ind - int01 - 1);// 左上边
/*
* } else { action1(ind - int01 - 1); }
*/

} else if (ind > 0 && ind < int01 - 1) {
/*
* if (mm.ch[ind + int01] == ' ' && mm.jpt[ind +
* int01].getComponent(0).getClass() .toString().equals("class
* javax.swing.JButton"))// 下边 {
*/
action1(ind + int01);// 下边
/*
* } else { action1(ind + int01); } if (mm.ch[ind - 1] == ' ' &&
* mm.jpt[ind - 1].getComponent(0).getClass()
* .toString().equals("class javax.swing.JButton"))// 左边 {
*/
action1(ind - 1);// 左边
/*
* } else { action1(ind - 1); } if (mm.ch[ind + 1] == ' ' &&
* mm.jpt[ind + 1].getComponent(0).getClass()
* .toString().equals("class javax.swing.JButton"))// 右边 {
*/
action1(ind + 1);// 右边
/*
* } else { action1(ind + 1); } if (mm.ch[ind + int01 + 1] == ' ' &&
* mm.jpt[ind + int01 + 1].getComponent(0).getClass()
* .toString().equals("class javax.swing.JButton"))// 右下边 {
*/
action1(ind + int01 + 1);// 右下边
/*
* } else { action1(ind + int01 + 1); } if (mm.ch[ind + int01 - 1] == ' ' &&
* mm.jpt[ind + int01 - 1].getComponent(0).getClass()
* .toString().equals("class javax.swing.JButton"))// 左下边 {
*/
action1(ind + int01 - 1);// 左下边
/*
* } else { action1(ind + int01 - 1); }
*/
} else if (ind > int01 * (int01 - 1) && ind < int01 * int01 - 1) {
/*
* if (mm.ch[ind - int01] == ' ' && mm.jpt[ind -
* int01].getComponent(0).getClass() .toString().equals("class
* javax.swing.JButton"))// 上边 {
*/
action1(ind - int01);// 上边
/*
* } else { action1(ind - int01); } if (mm.ch[ind - 1] == ' ' &&
* mm.jpt[ind - 1].getComponent(0).getClass()
* .toString().equals("class javax.swing.JButton"))// 左边 {
*/
action1(ind - 1);// 左边
/*
* } else { action1(ind - 1); } if (mm.ch[ind + 1] == ' ' &&
* mm.jpt[ind + 1].getComponent(0).getClass()
* .toString().equals("class javax.swing.JButton"))// 右边 {
*/
action1(ind + 1);// 右边
/*
* } else { action1(ind + 1); } if (mm.ch[ind - int01 - 1] == ' ' &&
* mm.jpt[ind - int01 - 1].getComponent(0).getClass()
* .toString().equals("class javax.swing.JButton"))// 左上边 {
*/
action1(ind - int01 - 1);// 左上边
/*
* } else { action1(ind - int01 - 1); } if (mm.ch[ind - int01 + 1] == ' ' &&
* mm.jpt[ind - int01 + 1].getComponent(0).getClass()
* .toString().equals("class javax.swing.JButton"))// 右上边 {
*/
action1(ind - int01 + 1);// 右上边
/*
* } else { action1(ind - int01 + 1); }
*/
} else if (ind % int01 == 0) {
/*
* if (mm.ch[ind - int01] == ' ' && mm.jpt[ind -
* int01].getComponent(0).getClass() .toString().equals("class
* javax.swing.JButton"))// 上边 {
*/
action1(ind - int01);// 上边
/*
* } else { action1(ind - int01); } if (mm.ch[ind + int01] == ' ' &&
* mm.jpt[ind + int01].getComponent(0).getClass()
* .toString().equals("class javax.swing.JButton"))// 下边 {
*/
action1(ind + int01);// 下边
/*
* } else { action1(ind + int01); } if (mm.ch[ind + 1] == ' ' &&
* mm.jpt[ind + 1].getComponent(0).getClass()
* .toString().equals("class javax.swing.JButton"))// 右边 {
*/
action1(ind + 1);// 右边
/*
* } else { action1(ind + 1); } if (mm.ch[ind + int01 + 1] == ' ' &&
* mm.jpt[ind + int01 + 1].getComponent(0).getClass()
* .toString().equals("class javax.swing.JButton"))// 右下边 {
*/
action1(ind + int01 + 1);// 右下边
/*
* } else { action1(ind + int01 + 1); } if (mm.ch[ind - int01 + 1] == ' ' &&
* mm.jpt[ind - int01 + 1].getComponent(0).getClass()
* .toString().equals("class javax.swing.JButton"))// 右上边 {
*/
action1(ind - int01 + 1);// 右上边
/*
* } else { action1(ind - int01 + 1); }
*/
} else if (ind % int01 == int01 - 1) {
/*
* if (mm.ch[ind - int01] == ' ' && mm.jpt[ind -
* int01].getComponent(0).getClass() .toString().equals("class
* javax.swing.JButton"))// 上边 {
*/
action1(ind - int01);// 上边
/*
* } else { action1(ind - int01); } if (mm.ch[ind + int01] == ' ' &&
* mm.jpt[ind + int01].getComponent(0).getClass()
* .toString().equals("class javax.swing.JButton"))// 下边 {
*/
action1(ind + int01);// 下边
/*
* } else { action1(ind + int01); } if (mm.ch[ind - 1] == ' ' &&
* mm.jpt[ind - 1].getComponent(0).getClass()
* .toString().equals("class javax.swing.JButton"))// 左边 {
*/
action1(ind - 1);// 左边
/*
* } else { action1(ind - 1); }
*
* if (mm.ch[ind + int01 - 1] == ' ' && mm.jpt[ind + int01 -
* 1].getComponent(0).getClass() .toString().equals("class
* javax.swing.JButton"))// 左下边 {
*/
action1(ind + int01 - 1);// 左下边
/*
* } else { action1(ind + int01 - 1); } if (mm.ch[ind - int01 - 1] == ' ' &&
* mm.jpt[ind - int01 - 1].getComponent(0).getClass()
* .toString().equals("class javax.swing.JButton"))// 左上边 {
*/
action1(ind - int01 - 1);// 左上边
/*
* } else { action1(ind - int01 - 1); }
*/

} else {
/*
* if (mm.ch[ind - int01] == ' ' && mm.jpt[ind -
* int01].getComponent(0).getClass() .toString().equals("class
* javax.swing.JButton"))// 上边 {
*/
action1(ind - int01);// 上边
/*
* } else { action1(ind - int01); } if (mm.ch[ind + int01] == ' ' &&
* mm.jpt[ind + int01].getComponent(0).getClass()
* .toString().equals("class javax.swing.JButton"))// 下边 {
*/
action1(ind + int01);// 下边
/*
* } else { action1(ind + int01); } if (mm.ch[ind - 1] == ' ' &&
* mm.jpt[ind - 1].getComponent(0).getClass()
* .toString().equals("class javax.swing.JButton"))// 左边 {
*/
action1(ind - 1);// 左边
/*
* } else { action1(ind - 1); } if (mm.ch[ind + 1] == ' ' &&
* mm.jpt[ind + 1].getComponent(0).getClass()
* .toString().equals("class javax.swing.JButton"))// 右边 {
*/
action1(ind + 1);// 右边
/*
* } else { action1(ind + 1); }
*
* if (mm.ch[ind + int01 + 1] == ' ' && mm.jpt[ind + int01 +
* 1].getComponent(0).getClass() .toString().equals("class
* javax.swing.JButton"))// 右下边 {
*/
action1(ind + int01 + 1);// 右下边
/*
* } else { action1(ind + int01 + 1); } if (mm.ch[ind + int01 - 1] == ' ' &&
* mm.jpt[ind + int01 - 1].getComponent(0).getClass()
* .toString().equals("class javax.swing.JButton"))// 左下边 {
*/
action1(ind + int01 - 1);// 左下边
/*
* } else { action1(ind + int01 - 1); } if (mm.ch[ind - int01 - 1] == ' ' &&
* mm.jpt[ind - int01 - 1].getComponent(0).getClass()
* .toString().equals("class javax.swing.JButton"))// 左上边 {
*/
action1(ind - int01 - 1);// 左上边
/*
* } else { action1(ind - int01 - 1); } if (mm.ch[ind - int01 + 1] == ' ' &&
* mm.jpt[ind - int01 + 1].getComponent(0).getClass()
* .toString().equals("class javax.swing.JButton"))// 右上边 {
*/
action1(ind - int01 + 1);// 右上边
/*
* } else { action1(ind - int01 + 1); }
*/

}
}
}

// 实现接口 MouseListenner
// 继承类 MouseAdapter 不用实现接口 MouseListener 中的所有方法 只要实现需要实现的方法 默认实现为null
class mouseL01 extends MouseAdapter {
MySwingTest05 mm;

actionL01 aa;

public mouseL01() {
aa = new actionL01();
}

public mouseL01(MySwingTest05 mm) {

this.mm = mm;
aa = new actionL01(mm);// 注意要传递对象mm 保证对象为同一对象
}

// 鼠标的右键标示地雷 yellow表示提示但不标示
public void mouseClicked(MouseEvent e) {

if (e.getButton() == MouseEvent.BUTTON3) {
JButton jb = (JButton) e.getSource();
// jb.setText("⊙");
if (jb.getBackground() == Color.PINK) {
jb.setBackground(Color.YELLOW);
} else if (jb.getBackground() == Color.YELLOW) {
jb.setBackground(null);
} else {
jb.setBackground(Color.PINK);
}
aa.action5();
}
}
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: