怎样用Java语言在Eclipse中写个和windows自带的那样的“扫雷”游戏,一模一样的

写好后发447000864@qq.com,注释要非常详细加100分... 写好后发447000864@qq.com,注释要非常详细加100分 展开
 我来答
flymomo123
2012-01-04 · TA获得超过819个赞
知道小有建树答主
回答量:301
采纳率:0%
帮助的人:370万
展开全部
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<Integer> random = new ArrayList<Integer>();
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;
}

}

}
xinfei0804
2012-01-04 · TA获得超过1637个赞
知道小有建树答主
回答量:547
采纳率:0%
帮助的人:318万
展开全部
已发送,请查收。z*****5@126.com
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式