java大作业扫雷代码
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
import java.awt.*;
import java.awt.event.*;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
/*按扭类*/
class Bomb extends JButton
{
public int num_x, num_y; // 第几号方块
public int BombRoundCount; // 周围雷数
public boolean isBomb; // 是否为雷
public boolean isClicked; // 是否被点击
public int BombFlag; // 探雷标记
public boolean isRight; // 是否点击右键
public Bomb(int x, int y)
{
BombFlag = 0;
num_x = x;
num_y = y;
BombRoundCount = 0;
isBomb = false;
isClicked = false;
isRight = false;
}
}
/* 窗口及算法实现类*/
class Main extends JFrame implements ActionListener, MouseListener {
public JTextField text;
public Label nowBomb, setBomb;
public int BlockNum, BombNum; // 当前方块数当前雷数
public Icon icon_bomb = new ImageIcon("Bomb.gif"); // 踩雷
public Icon icon_bomb_big = new ImageIcon("bomb_big.gif"); // 踩雷标记
public Icon icon_flag = new ImageIcon("flag.gif"); // 雷标记
public Icon icon_question = new ImageIcon("question.gif"); // 疑惑是否有雷
public JButton start = new JButton(" 开始");
public Panel MenuPamel = new Panel();
public Panel mainPanel = new Panel();
public Bomb[][] bombButton;
/* 界面设计*/
public Main()
{
super("MineSweeping:");
BlockNum = 64;
BombNum = 10;
Container c = getContentPane();
c.setBackground(Color.gray);
c.setLayout(new BorderLayout());
text = new JTextField("10 ", 3);
nowBomb = new Label("当前雷数" + " " + BombNum + "");
setBomb = new Label("设置地雷数");
start.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
BombNum = Integer.parseInt(text.getText().trim());
if (BombNum >= 10 && BombNum < 50)
replay();
else
{
JOptionPane msg = new JOptionPane();
JOptionPane.showMessageDialog(null, "您设置的地雷数不合理了,请重设!",
"错误", 2);
}
}
});
MenuPamel.add(setBomb);
MenuPamel.add(text);
MenuPamel.add(start);
MenuPamel.add(nowBomb);
c.add(MenuPamel, "North");
mainPanel.setLayout(new GridLayout((int) Math.sqrt(BlockNum), (int) Math.sqrt(BlockNum)));
bombButton = new Bomb[(int) Math.sqrt(BlockNum)][];
for (int i = 0; i < (int) Math.sqrt(BlockNum); i++)
{
bombButton[i] = new Bomb[(int) Math.sqrt(BlockNum)];
}
for (int i = 0; i < (int) Math.sqrt(BlockNum); i++)
for (int j = 0; j < (int) Math.sqrt(BlockNum); j++)
{
bombButton[i][j] = new Bomb(i, j);
bombButton[i][j].setForeground(Color.gray);
bombButton[i][j].addActionListener(this);
bombButton[i][j].addMouseListener(this);
}
for (int i = 0; i < (int) Math.sqrt(BlockNum); i++)
for (int j = 0; j < (int) Math.sqrt(BlockNum); j++)
mainPanel.add(bombButton[i][j]);
c.add(mainPanel, "Center");
startBomb();
setSize(400, 400);
setLocation(350, 200);
setResizable(false);
}
/* 布雷*/
public void startBomb()
{
for (int i = 0; i < BombNum; i++)
{
int x = (int) (Math.random() * (int) (Math.sqrt(BlockNum) - 1));
int y = (int) (Math.random() * (int) (Math.sqrt(BlockNum) - 1));
if (bombButton[x][y].isBomb == true)
i--;
else
bombButton[x][y].isBomb = true;
}
}
/* 重新开始*/