文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

Java如何实现简单扫雷程序

2023-06-30 18:24

关注

本篇内容介绍了“Java如何实现简单扫雷程序”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

页面设置:框架是borderlayout,在上中下加入外面要加入的组件(左边和右边不加)。扫雷用的布局是gridlayout即网格布局,与扫雷界面天然契合。

组件:使用panel,button,frame,label等组件,最后集中到frame中形成整体。并对其加入适当的监听,监听算法的实现如下。

扫雷算法:一共要考虑三种情况,第一种是点到雷的情况,第二种是点到空的格的情况,第三种是点到有数字的情况。下面我们对这些情况一一进行考虑,完成算法的设计。首先随机生成一个扫雷的图,上面包含有随机生成的雷,雷旁边的数字和空白组成的矩阵matrix,之后所有的监听都依照这个矩阵matrix为参照。

①点到空的时候,调用递归算法寻找旁边空格(周围一圈)的将其显示(这里笔者就是帮他换了一个颜色,即完成了
显示工作,读者也可以将点击的button组件换成label),直到找到是数字的格就停止对这个方向上的显示。
②点到数字的时候,就将格子显示。
③点到雷的时候,显示全部matrix中的值,即结束游戏。

递归算法:当点击空白格时,对其周围一圈的格子进行查找,当它是隔空的时候就将它显示,如果不是就停止对这个格子上的递归算法。

参照矩阵matrix的生成:随机使用random函数随机生成1-size(扫雷的规模)的随机数,形成雷的横纵坐标,并加入matrix中,这里的矩阵是string类型的,雷用“*”表示,根据雷的位置,分别对其周围的格子上的数字加1,即形成参照矩阵。

算法的改进:加入菜单栏,实时计时器,改变扫雷游戏规模,及代码比较冗余(毕竟完成大于完美),这里为读者提供一个模板,读者可以以此参照做进一步修改。

下面是成品的展示图和源码:

Java如何实现简单扫雷程序

import java.awt.BorderLayout;import java.awt.Color;import java.awt.GridLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JPanel; class newJButton extends JButton {    // 新建组合JButton类    int axisx;    int axisy;    newJButton(String s, int axisx, int axisy) {        super(s);        this.axisx = axisx;        this.axisy = axisy;    }    public int getaxisx() {        return axisx;    }    public int getaxisy() {        return axisy;    }}public class MineTest implements ActionListener {    // 组件定义    static final int size = 10; // 扫雷游戏规模    int time; // 计算雷的个数    JFrame frame = new JFrame("扫雷");    JPanel panel1 = new JPanel(); // 计时器    JPanel panel2 = new JPanel(); // 笑脸    JPanel panel3 = new JPanel(); // 初级扫雷    newJButton t;    newJButton t2;    long programStart = System.currentTimeMillis();    long programOver;    String[][] matrix = new String[size][size]; // 扫雷信息记录矩阵    newJButton[][] buttons = new newJButton[size][size]; // 创建扫雷按钮矩阵    int[][] tip = new int[size][size]; // 判断是否可以显示按钮    // 提示    public void inittime() {        t = new newJButton("进行中", 101, 101);        t2 = new newJButton("/", 102, 102);        panel2.add(t);        panel2.add(t2);    }    // 笑脸    public void initsmile() {        newJButton b = new newJButton("O", 100, 100); // 将归零按键"O"的坐标设置为(100,100)        b.addActionListener(this);        panel1.add(b);    }    // 初级扫雷    public void initmine() {        panel3.setLayout(new GridLayout(10, 10, 1, 1));        // panel3.setSize(250,250);        for (int i = 0; i < size; i++) {            for (int j = 0; j < size; j++) {                // button.setSize(25,25);                panel3.add(buttons[i][j]);            }        }    }    // 总界面的组合    public void initcombine() {        frame.setLayout(new BorderLayout());        frame.add(panel1, BorderLayout.NORTH);        frame.add(panel2, BorderLayout.CENTER);        frame.add(panel3, BorderLayout.SOUTH);        // frame.add(panel4);    }    // 扫雷页面初始化    MineTest() {        // 产生按钮        for (int i = 0; i < size; i++) {            for (int j = 0; j < size; j++) {                newJButton jb = new newJButton(" ", i, j);                jb.setBackground(Color.WHITE);                jb.addActionListener(this);                buttons[i][j] = jb;            }        }        inittime();        initsmile();        initmine();        initcombine();        frame.setVisible(true);        frame.setSize(500, 500);        frame.setLocation(700, 300);        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 可以关闭        // frame.pack();        // 产生size个雷并加入到矩阵中        do {            time = 0;            pmatrix(); // 初始化原始矩阵            boom(); // 加入雷            for (int i = 0; i < size; i++) {                for (int j = 0; j < size; j++) {                    if (matrix[i][j] == "*") {                        time = time + 1;                    }                }            }        } while (time != 10);        caculate(); // 计算扫雷矩阵中的距离        // 形成改变颜色的状态矩阵        for (int i = 0; i < size; i++) {            for (int j = 0; j < size; j++) {                if (matrix[i][j] == "0") {                    tip[i][j] = 0;                } else {                    tip[i][j] = 1;                }            }        }        // 打印        for (int i = 0; i < size; i++) {            for (int j = 0; j < size; j++) {                System.out.print(matrix[i][j] + " ");            }            System.out.println("\n");        }    }    private void pmatrix() {        // 扫雷矩阵初始化        for (int i = 0; i < size; i++) {            for (int j = 0; j < size; j++) {                matrix[i][j] = "0";            }        }    }    private void boom() {        // 产生炸弹;        int num = 0;        while (num < size) {            int i = (int) (Math.random() * size);            int j = (int) (Math.random() * size);            matrix[i][j] = "*";            num = num + 1;        }    }    @Override    public void actionPerformed(ActionEvent e) {        // 按钮响应事件        newJButton temp = (newJButton) e.getSource();        if (temp.getText() == "O") {            // 产生size个雷并加入到矩阵中            do {                time = 0;                pmatrix(); // 初始化原始矩阵                boom(); // 加入雷                for (int i = 0; i < size; i++) {                    for (int j = 0; j < size; j++) {                        if (matrix[i][j] == "*") {                            time = time + 1;                        }                    }                }            } while (time != 10);            caculate(); // 计算扫雷矩阵中的距离            // 形成改变颜色的状态矩阵            for (int i = 0; i < size; i++) {                for (int j = 0; j < size; j++) {                    if (matrix[i][j] == "0") {                        tip[i][j] = 0;                    } else {                        tip[i][j] = 1;                    }                }            }            // 打印            for (int i = 0; i < size; i++) {                for (int j = 0; j < size; j++) {                    System.out.print(matrix[i][j] + " ");                }                System.out.println("\n");            }            for (int i = 0; i < size; i++) {                for (int j = 0; j < size; j++) {                    buttons[i][j].setText(" ");                    buttons[i][j].setBackground(Color.WHITE);                }            }            t.setLabel("进行中");            t2.setLabel("/");            programStart = System.currentTimeMillis();            // System.out.println("smile");        } else if (matrix[temp.getaxisx()][temp.getaxisy()] == "*") {            for (int i = 0; i < size; i++) {                for (int j = 0; j < size; j++) {                    if (matrix[i][j] != "0") {                        buttons[i][j].setText(matrix[i][j]);                        if (matrix[i][j] == "*") {                            buttons[i][j].setBackground(Color.RED);                        }                    }                }            }            t.setLabel("失败");            programOver = System.currentTimeMillis();            t2.setLabel("" + (programOver - programStart) / 1000 + "s");        } else if (matrix[temp.getaxisx()][temp.getaxisy()] != "0") {            buttons[temp.getaxisx()][temp.getaxisy()].setText(matrix[temp.getaxisx()][temp.getaxisy()]);            buttons[temp.getaxisx()][temp.getaxisy()].setBackground(Color.gray);        } else if (matrix[temp.getaxisx()][temp.getaxisy()] == "0") {            digui(temp.getaxisx(), temp.getaxisy());        }    }    private void digui(int i, int j) {        // 递归调用该函数,处理按钮是“0”的情况        if (i == 0 && j == 0) {            if (tip[i][j] == 0) {                buttons[i][j].setBackground(Color.gray);                tip[i][j] = 1;                digui(i, j + 1);                digui(i + 1, j + 1);                digui(i + 1, j);            } else {                if (matrix[i][j] == "0") {                    buttons[i][j].setBackground(Color.gray);                    tip[i][j] = 1;                } else {                    buttons[i][j].setText(matrix[i][j]);                    buttons[i][j].setBackground(Color.gray);                    tip[i][j] = 1;                }            }        } else if (i == 0 && j == size-1) {            if (tip[i][j] == 0) {                buttons[i][j].setBackground(Color.gray);                tip[i][j] = 1;                digui(i, j - 1);                digui(i + 1, j - 1);                digui(i + 1, j);            } else {                if (matrix[i][j] == "0") {                    buttons[i][j].setBackground(Color.gray);                    tip[i][j] = 1;                } else {                    buttons[i][j].setText(matrix[i][j]);                    buttons[i][j].setBackground(Color.gray);                    tip[i][j] = 1;                }            }        } else if (i == size-1 && j == 0) {            if (tip[i][j] == 0) {                buttons[i][j].setBackground(Color.gray);                tip[i][j] = 1;                digui(i, j + 1);                digui(i - 1, j + 1);                digui(i - 1, j);            } else {                if (matrix[i][j] == "0") {                    buttons[i][j].setBackground(Color.gray);                    tip[i][j] = 1;                } else {                    buttons[i][j].setText(matrix[i][j]);                    buttons[i][j].setBackground(Color.gray);                    tip[i][j] = 1;                }            }        } else if (i == size-1 && j == size-1) {            if (tip[i][j] == 0) {                buttons[i][j].setBackground(Color.gray);                tip[i][j] = 1;                digui(i, j - 1);                digui(i - 1, j - 1);                digui(i - 1, j);            } else {                if (matrix[i][j] == "0") {                    buttons[i][j].setBackground(Color.gray);                    tip[i][j] = 1;                } else {                    buttons[i][j].setText(matrix[i][j]);                    buttons[i][j].setBackground(Color.gray);                    tip[i][j] = 1;                }            }        } else if (i == 0) {            if (tip[i][j] == 0) {                buttons[i][j].setBackground(Color.gray);                tip[i][j] = 1;                digui(i, j - 1);                digui(i, j + 1);                digui(i + 1, j - 1);                digui(i + 1, j);                digui(i + 1, j + 1);            } else {                if (matrix[i][j] == "0") {                    buttons[i][j].setBackground(Color.gray);                    tip[i][j] = 1;                } else {                    buttons[i][j].setText(matrix[i][j]);                    buttons[i][j].setBackground(Color.gray);                    tip[i][j] = 1;                }            }        } else if (j == 0) {            if (tip[i][j] == 0) {                buttons[i][j].setBackground(Color.gray);                tip[i][j] = 1;                digui(i - 1, j);                digui(i + 1, j);                digui(i - 1, j + 1);                digui(i, j + 1);                digui(i + 1, j + 1);            } else {                if (matrix[i][j] == "0") {                    buttons[i][j].setBackground(Color.gray);                    tip[i][j] = 1;                } else {                    buttons[i][j].setText(matrix[i][j]);                    buttons[i][j].setBackground(Color.gray);                    tip[i][j] = 1;                }            }        } else if (i == size-1) {            if (tip[i][j] == 0) {                buttons[i][j].setBackground(Color.gray);                tip[i][j] = 1;                digui(i - 1, j - 1);                digui(i - 1, j);                digui(i - 1, j + 1);                digui(i, j - 1);                digui(i, j + 1);            } else {                if (matrix[i][j] == "0") {                    buttons[i][j].setBackground(Color.gray);                    tip[i][j] = 1;                } else {                    buttons[i][j].setText(matrix[i][j]);                    buttons[i][j].setBackground(Color.gray);                    tip[i][j] = 1;                }            }        } else if (j == size-1) {            if (tip[i][j] == 0) {                buttons[i][j].setBackground(Color.gray);                tip[i][j] = 1;                digui(i - 1, j);                digui(i + 1, j);                digui(i - 1, j - 1);                digui(i - 1, j);                digui(i - 1, j);            } else {                if (matrix[i][j] == "0") {                    buttons[i][j].setBackground(Color.gray);                    tip[i][j] = 1;                } else {                    buttons[i][j].setText(matrix[i][j]);                    buttons[i][j].setBackground(Color.gray);                    tip[i][j] = 1;                }            }        } else {            if (tip[i][j] == 0) {                buttons[i][j].setBackground(Color.gray);                tip[i][j] = 1;                digui(i - 1, j - 1);                digui(i - 1, j);                digui(i - 1, j + 1);                digui(i, j - 1);                digui(i, j + 1);                digui(i + 1, j - 1);                digui(i + 1, j);                digui(i + 1, j + 1);            } else {                if (matrix[i][j] == "0") {                    buttons[i][j].setBackground(Color.gray);                    tip[i][j] = 1;                } else {                    buttons[i][j].setText(matrix[i][j]);                    buttons[i][j].setBackground(Color.gray);                    tip[i][j] = 1;                }            }        }    }    public static void main(String[] args) {        new MineTest();    }    private void caculate() {        // 计算扫雷矩阵中的距离        for (int i = 0; i < size; i++) {            for (int j = 0; j < size; j++) {                if (matrix[i][j] == "*") {                    if (i == 0 && j == 0) {                        if (matrix[i + 1][j] != "*") {                            matrix[i + 1][j] = String.valueOf(Integer.parseInt(matrix[i + 1][j]) + 1);                        }                        if (matrix[i + 1][j + 1] != "*") {                            matrix[i + 1][j + 1] = String.valueOf(Integer.parseInt(matrix[i + 1][j + 1]) + 1);                        }                        if (matrix[i][j + 1] != "*") {                            matrix[i][j + 1] = String.valueOf(Integer.parseInt(matrix[i][j + 1]));                        }                    } else if (i == 9 && j == 0) {                        if (matrix[i - 1][j] != "*") {                            matrix[i - 1][j] = String.valueOf(Integer.parseInt(matrix[i - 1][j]) + 1);                        }                        if (matrix[i - 1][j + 1] != "*") {                            matrix[i - 1][j + 1] = String.valueOf(Integer.parseInt(matrix[i - 1][j + 1]) + 1);                        }                        if (matrix[i][j + 1] != "*") {                            matrix[i][j + 1] = String.valueOf(Integer.parseInt(matrix[i][j + 1]) + 1);                        }                    } else if (i == 0 && j == 9) {                        if (matrix[i + 1][j] != "*") {                            matrix[i + 1][j] = String.valueOf(Integer.parseInt(matrix[i + 1][j]) + 1);                        }                        if (matrix[i + 1][j - 1] != "*") {                            matrix[i + 1][j - 1] = String.valueOf(Integer.parseInt(matrix[i + 1][j - 1]) + 1);                        }                        if (matrix[i][j - 1] != "*") {                            matrix[i][j - 1] = String.valueOf(Integer.parseInt(matrix[i][j - 1]) + 1);                        }                    } else if (i == 9 && j == 9) {                        if (matrix[i][j - 1] != "*") {                            matrix[i][j - 1] = String.valueOf(Integer.parseInt(matrix[i][j - 1]) + 1);                        }                        if (matrix[i - 1][j] != "*") {                            matrix[i - 1][j] = String.valueOf(Integer.parseInt(matrix[i - 1][j]) + 1);                        }                        if (matrix[i - 1][j - 1] != "*") {                            matrix[i - 1][j - 1] = String.valueOf(Integer.parseInt(matrix[i - 1][j - 1]) + 1);                        }                    } else if (i == 0) {                        if (matrix[i][j - 1] != "*") {                            matrix[i][j - 1] = String.valueOf(Integer.parseInt(matrix[i][j - 1]) + 1);                        }                        if (matrix[i][j + 1] != "*") {                            matrix[i][j + 1] = String.valueOf(Integer.parseInt(matrix[i][j + 1]) + 1);                        }                        if (matrix[i + 1][j - 1] != "*") {                            matrix[i + 1][j - 1] = String.valueOf(Integer.parseInt(matrix[i + 1][j - 1]) + 1);                        }                        if (matrix[i + 1][j] != "*") {                            matrix[i + 1][j] = String.valueOf(Integer.parseInt(matrix[i + 1][j]) + 1);                        }                        if (matrix[i + 1][j + 1] != "*") {                            matrix[i + 1][j + 1] = String.valueOf(Integer.parseInt(matrix[i + 1][j + 1]) + 1);                        }                    } else if (j == 0) {                        if (matrix[i - 1][j] != "*") {                            matrix[i - 1][j] = String.valueOf(Integer.parseInt(matrix[i - 1][j]) + 1);                        }                        if (matrix[i - 1][j + 1] != "*") {                            matrix[i - 1][j + 1] = String.valueOf(Integer.parseInt(matrix[i - 1][j + 1]) + 1);                        }                        if (matrix[i][j + 1] != "*") {                            matrix[i][j + 1] = String.valueOf(Integer.parseInt(matrix[i][j + 1]) + 1);                        }                        if (matrix[i + 1][j] != "*") {                            matrix[i + 1][j] = String.valueOf(Integer.parseInt(matrix[i + 1][j]) + 1);                        }                        if (matrix[i + 1][j + 1] != "*") {                            matrix[i + 1][j + 1] = String.valueOf(Integer.parseInt(matrix[i + 1][j + 1]) + 1);                        }                    } else if (i == 9) {                        if (matrix[i][j - 1] != "*") {                            matrix[i][j - 1] = String.valueOf(Integer.parseInt(matrix[i][j - 1]) + 1);                        }                        if (matrix[i][j + 1] != "*") {                            matrix[i][j + 1] = String.valueOf(Integer.parseInt(matrix[i][j + 1]) + 1);                        }                        if (matrix[i - 1][j - 1] != "*") {                            matrix[i - 1][j - 1] = String.valueOf(Integer.parseInt(matrix[i - 1][j - 1]) + 1);                        }                        if (matrix[i - 1][j] != "*") {                            matrix[i - 1][j] = String.valueOf(Integer.parseInt(matrix[i - 1][j]) + 1);                        }                        if (matrix[i - 1][j + 1] != "*") {                            matrix[i - 1][j + 1] = String.valueOf(Integer.parseInt(matrix[i - 1][j + 1]) + 1);                        }                    } else if (j == 9) {                        if (matrix[i - 1][j] != "*") {                            matrix[i - 1][j] = String.valueOf(Integer.parseInt(matrix[i - 1][j]) + 1);                        }                        if (matrix[i + 1][j] != "*") {                            matrix[i + 1][j] = String.valueOf(Integer.parseInt(matrix[i + 1][j]) + 1);                        }                        if (matrix[i - 1][j - 1] != "*") {                            matrix[i - 1][j - 1] = String.valueOf(Integer.parseInt(matrix[i - 1][j - 1]) + 1);                        }                        if (matrix[i][j - 1] != "*") {                            matrix[i][j - 1] = String.valueOf(Integer.parseInt(matrix[i][j - 1]) + 1);                        }                        if (matrix[i + 1][j - 1] != "*") {                            matrix[i + 1][j - 1] = String.valueOf(Integer.parseInt(matrix[i + 1][j - 1]) + 1);                        }                    } else {                        if (matrix[i - 1][j - 1] != "*") {                            matrix[i - 1][j - 1] = String.valueOf(Integer.parseInt(matrix[i - 1][j - 1]) + 1);                        }                        if (matrix[i - 1][j] != "*") {                            matrix[i - 1][j] = String.valueOf(Integer.parseInt(matrix[i - 1][j]) + 1);                        }                        if (matrix[i - 1][j + 1] != "*") {                            matrix[i - 1][j + 1] = String.valueOf(Integer.parseInt(matrix[i - 1][j + 1]) + 1);                        }                        if (matrix[i][j - 1] != "*") {                            matrix[i][j - 1] = String.valueOf(Integer.parseInt(matrix[i][j - 1]) + 1);                        }                        if (matrix[i][j + 1] != "*") {                            matrix[i][j + 1] = String.valueOf(Integer.parseInt(matrix[i][j + 1]) + 1);                        }                        if (matrix[i + 1][j - 1] != "*") {                            matrix[i + 1][j - 1] = String.valueOf(Integer.parseInt(matrix[i + 1][j - 1]) + 1);                        }                        if (matrix[i + 1][j] != "*") {                            matrix[i + 1][j] = String.valueOf(Integer.parseInt(matrix[i + 1][j]) + 1);                        }                        if (matrix[i + 1][j + 1] != "*") {                            matrix[i + 1][j + 1] = String.valueOf(Integer.parseInt(matrix[i + 1][j + 1]) + 1);                        }                    }                }            }        }    }}

“Java如何实现简单扫雷程序”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注编程网网站,小编将为大家输出更多高质量的实用文章!

阅读原文内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     813人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     354人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     318人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     435人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     224人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-后端开发
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯