昨天看的一个ITMM写的扫雷游戏,稍微改了一下

cs02308 2008-06-05 09:40:44

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class Minesweeper2 extends JFrame implements ActionListener {
int NUM = 9;
int random;
JButton[] button = new JButton[NUM];
JButton bomb = new JButton(); // 有雷的按钮
int total = 0; // 计算有几次没踩到雷

public Minesweeper2()
{
super("Bomb");

setLayout(new GridLayout(3, 3));

for (int i = 0; i < NUM; i++)
{
button[i] = new JButton();
button[i].setBackground(Color.gray);
getContentPane().add(button[i]);
} // 布局
setBomb(); // 设置雷

for (int i = 0; i < NUM; i++)
button[i].addActionListener(this);

setSize(700, 300);
setVisible(true);

}

public void setBomb()
{
int randomNum = (int) (Math.random() * 8);
bomb = button[randomNum];
random = randomNum;
// System.out.println(random); // 随机生成雷

}

public void actionPerformed(ActionEvent e)
{
if (e.getSource() == bomb)
{
bomb.setBackground(Color.black);
LoseDialog lose = new LoseDialog();
if (lose.ans == 1)
System.exit(0);
else
{
for (int i = 0; i < NUM; i++)
{
button[i].setBackground(Color.gray);
}
setBomb();
total = 0; // 重新游戏
return;//这个return很重要
}
}

for (int j = 0; j < NUM; j++)
{
if (e.getSource() == button[j] && j != random&&button[j].getBackground().equals(Color.gray))
{
button[j].setBackground(Color.white);
total += 1;
}
if (total == 8)
{
WinDialog win = new WinDialog();
if (win.ans == 1)
System.exit(0);
else
{
for (int i = 0; i < NUM; i++)
{
button[i].setBackground(Color.gray);
}
setBomb();
total = 0;// 重新游戏
return;//同上,return很重要
}

}
}
}

public static void main(String[] args)
{
// TODO Auto-generated method stub

BeginDialog begin = new BeginDialog();

JFrame frame = new Minesweeper2();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}

if (e.getSource() == button[j] && j != random&&button[j].getBackground().equals(Color.gray))这里加了一个条件,因为之前发现如果一直点击同一个button够8次时虽然没有找到boom,但依然会提示赢得了游戏,是否要重玩。
...全文
84 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
cs02308 2008-06-05
  • 打赏
  • 举报
回复
是没有问题,但是原贴是没有那个条件的
我本来是想在昨天的原贴回的,但今天上来就找不见那个帖子了,只好重开一个贴,呵呵,给MM提个意见。
craky 2008-06-05
  • 打赏
  • 举报
回复
呵呵,有参考价值,有空了我也写个跟系统自带的那个一样的,hoho
jason_kou 2008-06-05
  • 打赏
  • 举报
回复
其实楼主代码并没有什么问题,这个扫雷游戏就是让你不踩雷,所以摁了8次没踩到的话就算你赢
这个代码前几天写过,再次贴上,:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;

public class Minesweeper2 extends JFrame implements ActionListener {
int NUM = 9;

int random;

JButton[] button = new JButton[NUM];

JButton bomb = new JButton(); // 有雷的按钮

int total = 0;// 计算有几次没踩到雷

ArrayList<Integer> label = new ArrayList<Integer>();// 放入已经踩过的方格编号

boolean neverRead = false;

public Minesweeper2() {
super("Bomb");

setLayout(new GridLayout(3, 3));

for (int i = 0; i < NUM; i++) {
button[i] = new JButton();
button[i].setBackground(Color.gray);
getContentPane().add(button[i]);
} // 布局
setBomb(); // 设置雷

for (int i = 0; i < NUM; i++)
button[i].addActionListener(this);
}

public void setBomb() {
int randomNum = (int) (Math.random() * 8);
bomb = button[randomNum];
random = randomNum;
System.out.println(" " + random); // 随机生成雷

}

public void actionPerformed(ActionEvent e) {
if (e.getSource() == bomb) {
bomb.setBackground(Color.black);
LoseDialog lose = new LoseDialog();
if (lose.ans == 1)
System.exit(0);
else {
for (int i = 0; i < NUM; i++) {
button[i].setBackground(Color.gray);
}
setBomb();
total = 0; // 重新游戏
neverRead = false;
return;
}
}

for (int j = 0; j < NUM; j++) {
if (e.getSource() == button[j] && j != random) {
if (total == 0) {
label.add(j);
button[j].setBackground(Color.white);
total += 1;
}
if (total != 0) {
for (int k = 0; k < total; k++) {
if (j != label.get(k))
neverRead = true;
}
}
if (neverRead) {
label.add(j);
if (button[j].getBackground().equals(Color.gray))
total += 1; // 这3个if是判断是否出现在同个方格上连续点击,不知道问题出现在哪
button[j].setBackground(Color.white);
System.out.println(" " + total);
}
}
if (total == 8) {
WinDialog win = new WinDialog();
System.out.println("OK,Success!");
if (win.ans == 1)
System.exit(0);
else {
for (int i = 0; i < NUM; i++) {
button[i].setBackground(Color.gray);
}
setBomb();
total = 0;// 重新游戏
neverRead = false;
return;
}

}
}
}

public static void main(String[] args) {
// TODO Auto-generated method stub

BeginDialog begin = new BeginDialog();

JFrame frame = new Minesweeper2();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(700, 300);
frame.setVisible(true);

}
}

cs02308 2008-06-05
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 YeLang2007 的回复:]

这个是什么东西呀?

可以把这几个类也贴过来呀!

LoseDialog
WinDialog
BeginDialog
[/Quote]
不好意思,现在补全。


import javax.swing.JOptionPane;

//开始游戏的对话框
public class BeginDialog {
BeginDialog()
{
JOptionPane.showMessageDialog(null, "Welcome!", "Message",
JOptionPane.INFORMATION_MESSAGE);
}
}



import javax.swing.JOptionPane;

//失败后的对话框
public class LoseDialog {
int ans;

LoseDialog()
{
Object[] options = { "Yes", "No" };
ans = JOptionPane.showOptionDialog(null,
"BOOoom!!! Would you like to play again?", "",
JOptionPane.NO_OPTION, JOptionPane.QUESTION_MESSAGE, null,
options, options[0]);

}
}



import javax.swing.JOptionPane;

//成功踩雷的对话框
public class WinDialog {
int ans;

WinDialog()
{
Object[] options = { "Yes", "No" };
ans = JOptionPane.showOptionDialog(null,
"Win!!! Would you like to play again?", "",
JOptionPane.NO_OPTION, JOptionPane.QUESTION_MESSAGE, null,
options, options[0]);

}

}
YeLang2007 2008-06-05
  • 打赏
  • 举报
回复

这个是什么东西呀?

可以把这几个类也贴过来呀!

LoseDialog
WinDialog
BeginDialog
craky 2008-06-05
  • 打赏
  • 举报
回复
麻烦楼主把程序帖完好不好,少了几个类
YOLO高分设计资源源码,详情请查看资源内容中使用说明 YOLO高分设计资源源码,详情请查看资源内容中使用说明 YOLO高分设计资源源码,详情请查看资源内容中使用说明 YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧