使用ChatGPT实现可视化操作扫雷小游戏 【java代码实现】

简介: 这篇文章介绍了使用Java语言和Swing框架实现的扫雷小游戏的详细代码和实现过程。

代码

package com.atguigu.java4;

/**
 * @author zyz
 * @version 1.0
 * @data 2023/2/20 14:55
 * @Description:
 */

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

class MineSweeper extends JFrame { private JButton[][] buttons; private int[][] mines; private int[][] counts; private int row; private int col; private int mineNum; private int count; private boolean isGameOver;

    public MineSweeper(int row, int col, int mineNum) {
        this.row = row;
        this.col = col;
        this.mineNum = mineNum;
        this.count = 0;
        this.isGameOver = false;
        this.buttons = new JButton[row][col];
        this.mines = new int[row][col];
        this.counts = new int[row][col];
        initMines();
        initCounts();
        initFrame();
    }

    private void initMines() {
        Random random = new Random();
        int num = 0;
        while (num < mineNum) {
            int x = random.nextInt(row);
            int y = random.nextInt(col);
            if (mines[x][y] == 0) {
                mines[x][y] = 1;
                num++;
            }
        }
    }

    private void initCounts() {
        for (int i = 0; i < row; i++) {
            for (int j = 0; j < col; j++) {
                if (mines[i][j] == 1) {
                    counts[i][j] = -1;
                } else {
                    int count = 0;
                    if (i > 0 && j > 0 && mines[i - 1][j - 1] == 1) {
                        count++;
                    }
                    if (i > 0 && mines[i - 1][j] == 1) {
                        count++;
                    }
                    if (i > 0 && j < col - 1 && mines[i - 1][j + 1] == 1) {
                        count++;
                    }
                    if (j > 0 && mines[i][j - 1] == 1) {
                        count++;
                    }
                    if (j < col - 1 && mines[i][j + 1] == 1) {
                        count++;
                    }
                    if (i < row - 1 && j > 0 && mines[i + 1][j - 1] == 1) {
                        count++;
                    }
                    if (i < row - 1 && mines[i + 1][j] == 1) {
                        count++;
                    }
                    if (i < row - 1 && j < col - 1 && mines[i + 1][j + 1] == 1) {
                        count++;
                    }
                    counts[i][j] = count;
                }
            }
        }
    }

    private void initFrame() {
        this.setTitle("扫雷");
        this.setSize(col * 50, row * 50);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLayout(new GridLayout(row, col));
        for (int i = 0; i < row; i++) {
            for (int j = 0; j < col; j++) {
                buttons[i][j] = new JButton();
                buttons[i][j].setBackground(Color.GRAY);
                buttons[i][j].addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        JButton button = (JButton) e.getSource();
                        int x = 0;
                        int y = 0;
                        for (int i = 0; i < row; i++) {
                            for (int j = 0; j < col; j++) {
                                if (buttons[i][j] == button) {
                                    x = i;
                                    y = j;
                                    break;
                                }
                            }
                        }
                        if (mines[x][y] == 1) {
                            button.setBackground(Color.RED);
                            isGameOver = true;
                        } else {
                            button.setText(String.valueOf(counts[x][y]));
                            button.setEnabled(false);
                            count++;
                        }
                        if (count == row * col - mineNum) {
                            JOptionPane.showMessageDialog(null, "恭喜你,你赢了!");
                            isGameOver = true;
                        }
                        if (isGameOver) {
                            for (int i = 0; i < row; i++) {
                                for (int j = 0; j < col; j++) {
                                    if (mines[i][j] == 1) {
                                        buttons[i][j].setBackground(Color.RED);
                                    }
                                    buttons[i][j].setEnabled(false);
                                }
                            }
                        }
                    }
                });
                this.add(buttons[i][j]);
            }
        }
        this.setVisible(true);
    }

    public static void main(String[] args) {
        new MineSweeper(10, 10, 10);
    }
}

测试

踩到雷会爆红

在这里插入图片描述

相关文章
|
28天前
|
Java 开发工具
【Azure Storage Account】Java Code访问Storage Account File Share的上传和下载代码示例
本文介绍如何使用Java通过azure-storage-file-share SDK实现Azure文件共享的上传下载。包含依赖引入、客户端创建及完整示例代码,助你快速集成Azure File Share功能。
320 4
|
2月前
|
IDE Java 关系型数据库
Java 初学者学习路线(含代码示例)
本教程为Java初学者设计,涵盖基础语法、面向对象、集合、异常处理、文件操作、多线程、JDBC、Servlet及MyBatis等内容,每阶段配核心代码示例,强调动手实践,助你循序渐进掌握Java编程。
356 3
|
2月前
|
安全 Java 应用服务中间件
Spring Boot + Java 21:内存减少 60%,启动速度提高 30% — 零代码
通过调整三个JVM和Spring Boot配置开关,无需重写代码即可显著优化Java应用性能:内存减少60%,启动速度提升30%。适用于所有在JVM上运行API的生产团队,低成本实现高效能。
250 3
|
2月前
|
Java API 开发工具
【Azure Developer】Java代码实现获取Azure 资源的指标数据却报错 "invalid time interval input"
在使用 Java 调用虚拟机 API 获取指标数据时,因本地时区设置非 UTC,导致时间格式解析错误。解决方法是在代码中手动指定时区为 UTC,使用 `ZoneOffset.ofHours(0)` 并结合 `withOffsetSameInstant` 方法进行时区转换,从而避免因时区差异引发的时间格式问题。
191 3
|
1月前
|
Java 数据处理 API
为什么你的Java代码应该多用Stream?从循环到声明式的思维转变
为什么你的Java代码应该多用Stream?从循环到声明式的思维转变
231 115
|
1月前
|
安全 Java 编译器
为什么你的Java代码需要泛型?类型安全的艺术
为什么你的Java代码需要泛型?类型安全的艺术
169 98
|
2月前
|
Java
java入门代码示例
本文介绍Java入门基础,包含Hello World、变量类型、条件判断、循环及方法定义等核心语法示例,帮助初学者快速掌握Java编程基本结构与逻辑。
377 0
|
3月前
|
人工智能 监控 安全
智慧工地解决方案,java智慧工地程序代码
智慧工地系统融合物联网、AI、大数据等技术,实现对施工现场“人、机、料、法、环”的全面智能监控与管理,提升安全、效率与决策水平。
122 2
|
1月前
|
安全 Java 容器
告别繁琐判空:Optional让你的Java代码更优雅
告别繁琐判空:Optional让你的Java代码更优雅