扫雷,扫雷,游戏

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.*;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: cy
 * Date: 2024-02-18
 * Time: 15:01
 */
public class MineSweeping extends JFrame implements ActionListener {
    /**
     * 行
     */
    private int rows = 10;
    /**
     * 列
     */
    private int cols = 10;
    /**
     * 难度系数(雷的数量)
     */
    private int difficulty = 1;
    /**
     * 已排雷的数量
     */
    private int mineCount = 0;
    /**
     * 每个方格的大小
     */
    private int checkSize = 49;
    /**
     * 坐标位置
     */
    private int index = 1;//默认值
    /**
     * 雷所在位置坐标
     */
    private Set<Integer> mineSet = new HashSet<>();
    /**
     * 是否是开始游戏时的首次点击
     */
    private boolean canFirst = true;
    /**
     * 每个坐标对应的button按钮
     */
    List<JButton> buttonList = new ArrayList<>();
    //文字统一样式
    Font font = new Font("宋体", Font.PLAIN, 12);

    @Override
    public void actionPerformed(ActionEvent e) {

    }

    /**
     * 生成雷
     * @param difficulty 生成雷的数量
     */
    public void setMine(int difficulty) {
        if (difficulty > rows * cols - 1 || difficulty < 1) {
            difficulty = 1;
        }
        this.difficulty = difficulty;
        do {
            //生成随机数
            int randomValue = (int) Math.floor(Math.random() * rows * cols + 1);
            mineSet.add(randomValue);
        } while (mineSet.size() < difficulty);
    }

    /**
     * 数据初始化
     */
    public void init() {
        for (int i = 1; i <= buttonList.size(); i++) {
            JButton j2 = buttonList.get(i - 1);
            j2.setEnabled(true);
            j2.setBackground(null);
            j2.setText("");
            mineSet.clear();
            setMine(difficulty);
            canFirst = true;
            mineCount = 0;
        }
    }


    public void init(int rows, int cols) {
        this.rows = rows;
        this.cols = cols;

        setBounds(10, 10, rows * checkSize, cols * checkSize);
        setTitle("扫雷");
        this.setLayout(new BorderLayout());
        JPanel p2 = new JPanel();
        this.add(p2, BorderLayout.CENTER);
        p2.setLayout(new FlowLayout());
        p2.setLayout(new GridLayout(rows, cols));    //添加按钮
        for (int col = 0; col < cols; col++) {
            for (int row = 0; row < rows; row++) {
                JButton j = new JButton();
                p2.add(j);
                buttonList.add(j);
                int currentInex = index++;
                j.addMouseListener(new MouseAdapter() {
                    long timing = 0;

                    //鼠标按下事件
                    @Override
                    public void mousePressed(MouseEvent e) {
                        timing = System.currentTimeMillis();

                    }

                    @Override
                    public void mouseReleased(MouseEvent e) {
                        if (System.currentTimeMillis() - timing > 400) {
                            // 长按事件
                            System.out.println("长按标记为雷");
                            if (mineSet.contains(currentInex)) {
                                mineCount++;
                                j.setBackground(new Color(0xFFFFFF));
                                j.setText("雷");
                                j.setFont(font);
                                j.setForeground(new Color(0xA2A2A2));
                                if (mineSet.size() == mineCount) {
                                    JOptionPane.showConfirmDialog(null, "你取得了胜利,重新开始?", "Confirm", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
                                    init();
                                }
                            } else {
                                JOptionPane.showConfirmDialog(null, "你失败了,重新开始?", "Confirm", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
                                init();
                            }
                        }else{
                            j.setEnabled(false);
                            if (canFirst) {
                                canFirst = false;
                                for (int i = 1; i <= buttonList.size(); i++) {
                                    if (mineSet.contains(i) || hasMine(i, 2)) {
                                    } else if (hasMine(i, 3)) {
                                        JButton j2 = buttonList.get(i - 1);
                                        j2.setEnabled(false);
                                        j2.setText("3");
                                        j2.setFont(font);
                                        j2.setForeground(new Color(0xA2A2A2));
                                        j2.setBackground(new Color(0x10FF00));
                                    } else {
                                        JButton j2 = buttonList.get(i - 1);
                                        j2.setEnabled(false);
                                        j2.setBackground(new Color(0xCCFFCA));
                                    }
                                }

                            }
                            System.out.println(currentInex);
                            if (mineSet.contains(currentInex)) {
                                j.setBackground(new Color(0x000000));
                                JOptionPane.showConfirmDialog(null, "你失败了,重新开始?", "Confirm", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
                                init();
                            } else if (hasMine(currentInex, 1)) {
                                j.setBackground(new Color(0xFF0000));
                                j.setText("1");
                                j.setFont(font);
                                j.setForeground(new Color(0xA2A2A2));
                            } else if (hasMine(currentInex, 2)) {
                                j.setBackground(new Color(0xFFCD00));
                                j.setText("2");
                                j.setFont(font);
                                j.setForeground(new Color(0xA2A2A2));
                            } else if (hasMine(currentInex, 3)) {
                                j.setBackground(new Color(0xFFCD00));
                                j.setText("3");
                                j.setFont(font);
                                j.setForeground(new Color(0x10FF00));
                            } else {
                                j.setBackground(new Color(0xCCFFCA));
                            }
                        }
                    }
                });
            }
        }
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }

    /**
     * @param number 需要判断的数字
     * @param range  需要判定的范围
     * @return
     */
    public boolean hasMine(int number, int range) {
        //不在行头
        if ((number - 1) % rows != 0) {
            if (mineSet.contains(number - 1)) {
                return true;
            } else if (range > 1) {
                if (hasMine(number - 1, range - 1)) {
                    return true;
                }
            }
        }
        //不在行尾
        if ((number + 1) % rows != 1) {
            if (mineSet.contains(number + 1)) {
                return true;
            } else if (range > 1) {
                if (hasMine(number + 1, range - 1)) {
                    return true;
                }
            }
        }
        if (mineSet.contains(number - 1 * rows)) {
            return true;
        } else if (range > 1) {
            if (hasMine(number - 1 * rows, range - 1)) {
                return true;
            }
        }
        if (mineSet.contains(number + 1 * rows)) {
            return true;
        } else if (range > 1) {
            if (hasMine(number + 1 * rows, range - 1)) {
                return true;
            }
        }
        return false;
    }

    public static void main(String[] args) {
        //创建一个指定大小的格子
        MineSweeping mineSweeping = new MineSweeping();
        mineSweeping.setVisible(true);
        mineSweeping.init(15, 15);
        mineSweeping.setMine(30);
    }

}

  • 8
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值