JAVA扫雷代码

目录

1、前言

2、效果展示

 3、代码

3.1 Main

3.2 MineJFrame 主界面和菜单

 3.3 GridJLabel 数字和炸弹方格实现

 3.4 BasicGridButton 基本方格的点击事件的监听相关逻辑判断

3.5 笑脸按钮和计数计时功能实现

3.6 Win  弹出获胜窗口

3.7 DataClass 数据类

4、图片资源在github上。


1、前言

        去年土木大四寒假时,不想做毕设,学Java玩,很想做一个小游戏,但那时Java基础语法都还没学完,就找了个简单的来做,网上找了找,刚好找到了扫雷的图片资源,就做了个全图片的扫雷。菜得要命。还有本人英语一言难尽。虽然这注释少,我一年后(现在)还是看的懂,就不在添加注释了。有问题可以加讨论吹水群:332566785,也可以发邮件2823891694@qq.com。

2、效果展示

中级模式

高级模式

胜利

失败

 3、代码

3.1 Main

public class Main {
    public static  void main(String[] args)
    {
        new MineJFrame();
    }
}

3.2 MineJFrame 主界面和菜单

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

public  class MineJFrame extends JFrame {

    ImageIcon icon = new ImageIcon("image/icon.gif");
    Image imageIcon = icon.getImage();

    DataClass dataClass = new DataClass();
    FaceJLabel faceJLabel = new FaceJLabel(this);
    GridJLabel gridJLabel = new GridJLabel(faceJLabel);

    public MineJFrame() {
        setFrame();
        mineMenu();
        initframe();
        setVisible(true);
    }

    /*
     * 加载GridJPanel,设置JFrame尺寸
     */
    public void initframe(){
        //dataClass.reStart();
        add(faceJLabel);
        add(gridJLabel);
        setMinimumSize(new Dimension(DataClass.getMineRow()*16+16, DataClass.getMineRand()*16+104));
        repaint();
        pack();
    }
    /*
     * 重置
     */
    public void reStart(){
        dataClass.reStart();
        remove(gridJLabel);
        gridJLabel = new GridJLabel(faceJLabel);
        add(gridJLabel);
        faceJLabel.reSet();
    }
/*
*实现菜单
*/
    protected void mineMenu(){
        JMenuBar menuBar=new JMenuBar();
        JMenu menu = new JMenu("菜单");
        menu.setMargin(new Insets(0,0,0,0));//设置字边距;
        menu.addSeparator();//添加分割线;
        JMenu difficulty = new JMenu("难度");
        JMenuItem primary = new JMenuItem("初级");
        JMenuItem intermediate = new JMenuItem("中级");
        JMenuItem senior = new JMenuItem("高级");
        difficulty.addSeparator();//添加分割线;
        JMenu cheat = new JMenu("作弊模式");
        JMenuItem cheatOpen = new JMenuItem("开");
        JMenuItem cheatClose = new JMenuItem("关");

        primary.addActionListener(e -> {
            DataClass.setMineGrid(9,9,10);
            initframe();
            reStart();
            revalidate();

        });

        intermediate.addActionListener(e -> {
            DataClass.setMineGrid(16,16,40);
            initframe();
            reStart();

            validate();
        });

        senior.addActionListener(e -> {
            DataClass.setMineGrid(32,16,99);
            initframe();
            reStart();

            validate();
        });

        cheatOpen.addActionListener(e -> {
            DataClass.isCheat=true;
            initframe();
            reStart();
            validate();
        });

        cheatClose.addActionListener(e -> {
            DataClass.isCheat=false;
            initframe();
            reStart();
            validate();
        });

        difficulty.add(primary);
        difficulty.add(intermediate);
        difficulty.add(senior);

        cheat.add(cheatOpen);
        cheat.add(cheatClose);

        menu.add(difficulty);
        menu.add(cheat);
        menuBar.add(menu);
        setJMenuBar(menuBar);
    }

    public void setFrame(){
        setTitle("扫雷");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setIconImage(imageIcon);
        getContentPane().setBackground(new Color(191, 191, 191, 255));
        setResizable(false);
        setLocationRelativeTo(null);

        
  • 5
    点赞
  • 35
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
以下是一个简单的 Java 扫雷游戏代码示例: ```java import java.util.Random; import java.util.Scanner; public class Minesweeper { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); Random random = new Random(); System.out.println("欢迎来到扫雷游戏!"); // 设置雷区大小 System.out.print("请输入雷区的行数:"); int rows = scanner.nextInt(); System.out.print("请输入雷区的列数:"); int cols = scanner.nextInt(); // 设置雷数 System.out.print("请输入雷数:"); int mines = scanner.nextInt(); // 初始化雷区 int[][] board = new int[rows][cols]; for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { board[i][j] = 0; } } // 随机布雷 int count = 0; while (count < mines) { int x = random.nextInt(rows); int y = random.nextInt(cols); if (board[x][y] == 0) { board[x][y] = -1; count++; } } // 计算每个格子周围的雷数 for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { if (board[i][j] == -1) { continue; } int numMines = 0; for (int k = i - 1; k <= i + 1; k++) { for (int l = j - 1; l <= j + 1; l++) { if (k < 0 || k >= rows || l < 0 || l >= cols) { continue; } if (board[k][l] == -1) { numMines++; } } } board[i][j] = numMines; } } // 开始游戏 int[][] revealed = new int[rows][cols]; boolean gameover = false; while (!gameover) { // 打印当前状态 System.out.println("当前状态:"); for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { if (revealed[i][j] == 0) { System.out.print("# "); } else if (revealed[i][j] == 1) { System.out.print(board[i][j] + " "); } else if (revealed[i][j] == 2) { System.out.print("* "); } } System.out.println(); } // 输入下一步操作 System.out.print("请输入操作(格式:x y r/l):"); int x = scanner.nextInt(); int y = scanner.nextInt(); String op = scanner.next(); // 执行操作 if (op.equals("r")) { if (board[x][y] == -1) { System.out.println("你输了!"); gameover = true; } else { revealed[x][y] = 1; } } else if (op.equals("l")) { if (board[x][y] == -1) { revealed[x][y] = 2; } else { System.out.println("你输了!"); gameover = true; } } else { System.out.println("无效操作!"); } // 判断是否胜利 int countRevealed = 0; for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { if (revealed[i][j] == 1) { countRevealed++; } } } if (countRevealed == rows * cols - mines) { System.out.println("你赢了!"); gameover = true; } } scanner.close(); } } ``` 该代码实现了一个简单的扫雷游戏,包括设置雷区大小和雷数、随机布雷、计算每个格子周围的雷数、开始游戏、执行操作、判断胜负等功能。其中,用二维数组 `board` 存储雷区,-1 表示有雷,其他数字表示周围的雷数;用二维数组 `revealed` 存储已经揭开的格子,0 表示未揭开,1 表示已揭开,2 表示已标记为雷。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

哈士奇快秃了

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值