import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.Collections;
import javax.swing.Action;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
/**
*
* @author zhan
*
*N年前写的扫雷了。。有没有BUG也懒得改了。。
*最少是3年前写的了。。如果我现在写肯定不这样写了。。。汗
*/
public class Minesweeper extends JFrame implements ActionListener {
ArrayList
LButton[] bombs = new LButton[81];
boolean finish = false;
boolean win = false;
//设置窗口
public void lanchFrame() {
this.setBounds(0, 0, 500, 500);
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
this.setTitle("Minesweeper0.1");
addBombArea();
setVisible(true);
}
//显示所有炸弹
public void showBombs() {
for (int i = 0; i < 10; i++) {
bombs[random.get(i)].setBackground(Color.BLACK);
}
}
//添加组件
public void addBombArea() {
JPanel bombArea = new JPanel();
bombArea.setLayout(new GridLayout(9, 9, 1, 1));
for (int i = 0; i < 81; i++) {
bombs[i] = new LButton();
bombs[i].setPosition(i);
bombs[i].addActionListener(this);
bombArea.add(bombs[i]);
}
setBombs();
this.add(bombArea, BorderLayout.CENTER);
}
//设置炸弹的方法
public void setBombs() {
randomNo();
for (int i = 0; i < 10; i++) {
bombs[random.get(i)].setBomb();
}
}
//获胜的条件
public boolean youWIn() {
for (int i = 0; i < 10; i++) {
bombs[random.get(i)].setShow(true);
}
win = true;
for (int i = 0; i < 81; i++) {
if (!bombs[i].isShow()) {
win = false;
}
}
for (int i = 0; i < 10; i++) {
bombs[random.get(i)].setShow(false);
}
return win;
}
//取得随机数字的方法
public void randomNo() {
random.clear();
for (int i = 0; i < 81; i++) {
random.add(new Integer(i));
}
Collections.shuffle(random);
}
//扫雷的方法。。。。用的递归
public void sweeping(LButton checkButton) {
int[] girdNum = new int[8];
int sum = 0;
if (checkButton.isBomb()) {
finish = true;
JOptionPane.showMessageDialog(null, "you lose", "System:",
JOptionPane.INFORMATION_MESSAGE, null);
showBombs();
} else {
if (!checkButton.isShow()) {
girdNum[0] = checkButton.getPosition() - 10;
girdNum[0] = (girdNum[0] < 0 || (girdNum[0] % 8) == 0) ? -1
: girdNum[0];
girdNum[1] = checkButton.getPosition() - 9;
girdNum[1] = (girdNum[1] < 0) ? -1 : girdNum[1];
girdNum[2] = checkButton.getPosition() - 8;
girdNum[2] = (girdNum[2] < 0 || (girdNum[2] % 9) == 0) ? -1
: girdNum[2];
girdNum[3] = checkButton.getPosition() - 1;
girdNum[3] = (girdNum[3] % 8 == 0) ? -1 : girdNum[3];
girdNum[4] = checkButton.getPosition() + 1;
girdNum[4] = (girdNum[4] % 9 == 0) ? -1 : girdNum[4];
girdNum[5] = checkButton.getPosition() + 8;
girdNum[5] = (girdNum[5] > 80 || (girdNum[5] % 8) == 0) ? -1
: girdNum[5];
girdNum[6] = checkButton.getPosition() | +9;
girdNum[6] = (girdNum[6] > 80) ? -1 : girdNum[6];
girdNum[7] = checkButton.getPosition() + 10;
girdNum[7] = (girdNum[7] > 80 || (girdNum[7] % 9) == 0) ? -1
: girdNum[7];
for (int i = 0; i < 8; i++) {
if (girdNum[i] != -1) {
if (bombs[girdNum[i]].isBomb()
&& !bombs[girdNum[i]].isShow())
sum++;
}
}
if (sum > 0) {
checkButton.setShow(true);
checkButton.setBackground(Color.gray);
checkButton.setText("" + sum);
sum = 0;
} else if (sum == 0) {
checkButton.setShow(true);
checkButton.setBackground(Color.gray);
for (int i = 0; i < 8; i++) {
if (girdNum[i] != -1 && !bombs[girdNum[i]].isShow()) {
sweeping(bombs[girdNum[i]]);
}
}
}
}
if (youWIn()) {
JOptionPane.showMessageDialog(null, "You win!!", "System: ",
JOptionPane.INFORMATION_MESSAGE, null);
showBombs();
finish = true;
}
}
}
public static void main(String[] args) {
Minesweeper myMinesweeper = new Minesweeper();
myMinesweeper.lanchFrame();
}
public void actionPerformed(ActionEvent e) {
LButton a = (LButton) (e.getSource());
if (!finish) {
sweeping(a);
}
}
//自定义的Jbutton用于更好的显示扫雷界面
public class LButton extends JButton {
private int position;
private boolean bomb;
private boolean show;
public LButton() {
super();
}
public LButton(Action a) {
super(a);
}
public LButton(Icon icon) {
super(icon);
}
public LButton(String text, Icon icon) {
super(text, icon);
}
public LButton(String text) {
super(text);
}
public boolean isShow() {
return show;
}
public void setShow(boolean show) {
this.show = show;
}
public int getPosition() {
return position;
}
public void setPosition(int position) {
this.position = position;
}
public boolean isBomb() {
return bomb;
}
public void setBomb() {
this.bomb = true;
}
}
}
已发送,请查收。z*****5@126.com