JAVA实现扫雷游戏

后记:经评论区提醒,发现有两个bug没考虑到,感谢大家的提醒

bug1:绘制雷的时候有可能把两个雷随机到同样的位置。解决方法是在绘制雷的for循环内,rRow和rCol生成后做一个检测即可:

 /* 绘制地雷 */
    private void randomBomb() {
        for (int i = 0; i < bombCount; ++i) {
            int rRow = (int) (Math.random() * rows);
            int rCol = (int) (Math.random() * cols);
            if (state[rRow][rCol] == true) --i;
            label[rRow][rCol].setIcon(bomb);
            state[rRow][rCol] = true;/* 有地雷的格子state为真 */
        }
    }

思路是在每次生成随机数之后检测一下这个位置是否有雷,有的话就把这次的计数器归位重新生成。bombCount的作用域太大,能在尽量小的范围内解决的问题就不影响它了。

bug2:插旗的位置左键还能点开,解决方法是在左键点击事件触发前做检测:

   /* 绘制按钮 */
    private void randomBtn() {
        for (int i = 0; i < rows; ++i) {
            for (int j = 0; j < cols; ++j) {
                Btn btn = new Btn();
                btn.i = i;
                btn.j = j;
                btn.setBounds(j * BLOCKWIDTH, i * BLOCKHEIGHT, BLOCKWIDTH, BLOCKHEIGHT);
                this.add(btn);
                btns[i][j] = btn;
                btn.addMouseListener(new MouseAdapter() {
                    public void mouseClicked(MouseEvent e) {
                        /* 左键点击,屏蔽插旗格子的点击事件 */ 
                        if(e.getButton() == MouseEvent.BUTTON1) {
                            if(click[btn.i][btn.j] != 3) open(btn); 
                        } 
                        /* 右键点击 */
                        if(e.getButton() == MouseEvent.BUTTON3) placeFlag(btn);
                    }
 
                }
                );
 
            }
        }
 
    }

思路是禁用插旗格子的左键点击事件。

以下是正文:

学习完java事件处理......老师要求写个乞丐版扫雷......作为一只前端dog果断不能怂阿,于是......

乞丐版扫雷

硬着头皮写出来了,参考了如下三份代码:

乞丐版JAVA扫雷

Java语言实现的扫雷游戏(一)

Java语言实现的扫雷游戏(二)

先把代码贴出来,需要注意的东西都写在注释里了,有空把教程写出来

package sixth;

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

public class MineClearance {
    static private int midtime = 3600,mineNum = 0;/* 倒计时时间以及可用旗子数 */
    private static ImageIcon face = new ImageIcon("E:\\project\\JAVA\\test\\demo\\face.jpg");/* 小黄脸图标 */
    static private JLabel label1,label2;/* 提示文字 */
    static private GamePanel gp;/* 雷区 */

    MineClearance(){
        /* 绘制窗口 */
        JFrame f = new JFrame("扫雷");
        f.setBounds(600,200,500,600);
        f.setDefaultCloseOperation(3);
        f.setLayout(null);
        label1 = new JLabel("剩余时间:" +(midtime / 60 / 60 % 60) + ":"+ (midtime / 60 % 60)+ ":" +(midtime % 60));
        label1.setBounds(10,20,120,20);
        f.add(label1);
        /* 显示旗子数 */
        label2 = new JLabel("剩余:"+mineNum);
        label2.setBounds(400,20,120,20);
        f.add(label2);
        /* 重置按钮 */
        JButton bt = new JButton(face);
        bt.setBounds(230, 15,30,30);
        bt.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                f.dispose();
                midtime = 3600;
                new MineClearance();
            }
        });
        f.add(bt);
        /* 绘制雷区 */
        gp = new GamePanel(20,20);
        gp.setBounds(40,100,400,400);
        f.add(gp);
        /* 显示界面 */
        f.setVisible(true);
    }
    /* 倒计时线程 */
    static class CountDown extends Thread{
        public void run(){
            while (midtime > 0){
                try{
                    -- midtime;
                    label1.setText("剩余时间:" +(midtime / 60 / 60 % 60) + ":"+ (midtime / 60 % 60)+ ":" +(midtime % 60));
                    this.sleep(1000);
                }catch (Exception e){
                    System.out.println("错误:" + e.toString());
                }
            }
            if(midtime == 0) {
                gp.showBomb();
                JOptionPane.showMessageDialog(null,"时间已到","游戏结束",JOptionPane.PLAIN_MESSAGE);
            }
        }

    }
    public static void main(String[] args){
       new MineClearance();
       /* 倒计时 */
       CountDown cd = new CountDown();
       cd.start();
    }
    /* 修改旗子数 */
    public static void setMineNum(int i){
        mineNum = i;
        label2.setText("剩余:"+mineNum);
    }
}

class GamePanel extends JPanel {
    private int rows, cols, bombCount,flagNum;
    private final int BLOCKWIDTH = 20;
    private final int BLOCKHEIGHT = 20;
    private JLabel[][] label;
    private boolean[][] state;
    private Btn[][] btns;
    private byte[][] click;
    private static ImageIcon flag = new ImageIcon("E:\\project\\JAVA\\test\\demo\\flag.jpg");
    private static ImageIcon bomb = new ImageIcon("E:\\project\\JAVA\\test\\demo\\bomb.jpg");
    private static ImageIcon lucency = new ImageIcon("E:\\project\\JAVA\\test\\demo\\lucency.png");
    /* 构造雷区 */
    public GamePanel(int row, int col) {
        rows = row;/* 行数 */
        cols = col;/* 列数 */
        bombCount = rows * cols / 10; /* 地雷数 */
        flagNum = bombCount;/* 标记数(用于插旗) */
        label = new JLabel[rows][cols];
        state = new boolean[rows][cols];/* 用于存储是否有地雷 */
        btns = new Btn[rows][cols];
        click = new byte[rows][cols];/* 用于存储按钮点击状态(0-未点击,1-已点击,2-未点击但周围有雷,3-插旗) */
        MineClearance.setMineNum(flagNum);
        setLayout(null);
        initLable();
        randomBomb();
        writeNumber();
        randomBtn();
    }

    public void initLable() {
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                JLabel l = new JLabel("", JLabel.CENTER);
                // 设置每个小方格的边界
                l.setBounds(j * BLOCKWIDTH, i * BLOCKHEIGHT, BLOCKWIDTH, BLOCKHEIGHT);
                // 绘制方格边框
                l.setBorder(BorderFactory.createLineBorder(Color.GRAY));
                // 设置方格为透明,便于我们填充颜色
                l.setOpaque(true);
                // 背景填充为黄色
                l.setBackground(Color.lightGray);
                // 将方格加入到容器中(即面板JPanel)
                this.add(l);
                // 将方格存到类变量中,方便公用
                label[i][j] = l;
                label[i][j].setVisible(false);
            }
        }
    }

    /* 绘制地雷 */
    private void randomBomb() {
        for (int i = 0; i < bombCount; i++) {
            int rRow = (int) (Math.random() * rows);
            int rCol = (int) (Math.random() * cols);
            label[rRow][rCol].setIcon(bomb);
            state[rRow][rCol] = true;/* 有地雷的格子state为真 */
        }
    }

    /* 绘制数字 */
    private void writeNumber() {
        for (int i = 0; i < rows; ++i) {
            for (int j = 0; j < cols; ++j) {
                if (state[i][j]) {
                    continue;
                }
                int bombCount = 0;
                /* 寻找以自己为中心的九个格子中的地雷数 */
                for (int r = -1; (r + i < rows) && (r < 2); ++r) {
                    if (r + i < 0) continue;
                    for (int c = -1; (c + j < cols) && (c < 2); ++c) {
                        if (c + j < 0) continue;
                        if (state[r + i][c + j]) ++bombCount;
                    }
                }
                if (bombCount > 0) {
                    click[i][j] = 2;
                    label[i][j].setText(String.valueOf(bombCount));
                }
            }
        }
    }
    /* 绘制按钮 */
    private void randomBtn() {
        for (int i = 0; i < rows; ++i) {
            for (int j = 0; j < cols; ++j) {
                Btn btn = new Btn();
                btn.i = i;
                btn.j = j;
                btn.setBounds(j * BLOCKWIDTH, i * BLOCKHEIGHT, BLOCKWIDTH, BLOCKHEIGHT);
                this.add(btn);
                btns[i][j] = btn;
                btn.addMouseListener(new MouseAdapter() {
                    public void mouseClicked(MouseEvent e) {
                        /* 左键点击 */
                        if(e.getButton() == MouseEvent.BUTTON1) open(btn);
                        /* 右键点击 */
                        if(e.getButton() == MouseEvent.BUTTON3) placeFlag(btn);
                    }

                }
                );

            }
        }

    }
    /* 打开这个雷区 */
    private void open(Btn b){
        /* 踩雷 */
        if(state[b.i][b.j]){
            for (int r = 0;r < rows;++r){
                for(int c = 0;c < cols; ++c){
                    btns[r][c].setVisible(false);/* 隐藏label */
                    label[r][c].setVisible(true);/* 显示按钮(这里只有隐藏了按钮才能显示按钮下面的label) */
                }
            }
            JOptionPane.showMessageDialog(null,"您失败了","游戏结束",JOptionPane.PLAIN_MESSAGE);
        }else /* 没有踩雷 */{
            subopen(b);
        }
    }
    /* 递归打开周边雷区 */
    private void subopen(Btn b){
        /* 有雷,不能打开 */
        if(state[b.i][b.j]) return;
        /* 打开过的和插旗的,不用打开 */
        if(click[b.i][b.j] == 1 || click[b.i][b.j] == 3) return;
        /* 周围有雷的,只打开它 */
        if(click[b.i][b.j] == 2) {
            b.setVisible(false);
            label[b.i][b.j].setVisible(true);
            click[b.i][b.j] = 1;
            return;
        }
        /* 打开当前这个按钮 */
        b.setVisible(false);
        label[b.i][b.j].setVisible(true);
        click[b.i][b.j] = 1;
        /* 递归检测周边八个按钮 */
        for (int r = -1; (r + b.i < rows) && (r < 2); ++r) {
            if (r + b.i < 0) continue;
            for (int c = -1; (c + b.j < cols) && (c < 2); ++c) {
                if (c + b.j < 0) continue;
                if (r==0 && c==0) continue;
                Btn newbtn = btns[r + b.i][c + b.j];
                subopen(newbtn);
            }
        }
    }
    /* 插旗 */
    private void placeFlag(Btn b){
        /* 只能插和地雷数相同数目的旗子 */
        if(flagNum>0){
            /* 插过旗的,再点一次取消 */
            if(click[b.i][b.j] == 3){
                if(label[b.i][b.j].getText() == "[0-9]") click[b.i][b.j] = 2;
                else click[b.i][b.j] = 0;
                b.setIcon(lucency);
                ++ flagNum;
                MineClearance.setMineNum(flagNum);
            }else /* 未插旗的,插旗 */{
                b.setIcon(flag);
                click[b.i][b.j] = 3;
                -- flagNum;
                MineClearance.setMineNum(flagNum);
            }
            /* 把所有旗子插完了,检测是否成功 */
            if(flagNum == 0){
                boolean flagstate = true;
                for(int i = 0;i < rows; ++i){
                    for(int j = 0;j < cols; ++j){
                        if (click[i][j] != 3 && state[i][j]) flagstate = false;
                    }
                }
                if(flagstate) JOptionPane.showMessageDialog(null,"您成功了","游戏结束",JOptionPane.PLAIN_MESSAGE);
            }
        }else /* 旗子用完了,不能插 */{
            JOptionPane.showMessageDialog(null,"标记已用尽","错误操作",JOptionPane.PLAIN_MESSAGE);
        }
    }
    /* 显示雷区 */
    public void showBomb(){
        for (int r = 0;r < rows;++r){
            for(int c = 0;c < cols; ++c){
                btns[r][c].setVisible(false);/* 隐藏label */
                label[r][c].setVisible(true);/* 显示按钮(这里只有隐藏了按钮才能显示按钮下面的label) */
            }
        }
    }
}

class Btn extends JButton{
    public int i,j;
}

需要用到的素材:

bomb.jpg bomb.jpg -- 用于显示炸弹

face.jpg face.jpg -- 顶部中间那个酷酷的重置按钮

flag.jpg flag.jpg -- 插的旗子

luncecy.jpg luncecy.png -- 你什么都看不到?那就对了,这是张纯透明的图片,用于再次右键有旗子的按钮时把旗子替换成透明的

  • 122
    点赞
  • 648
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 67
    评论
好的,下面是一个简单的Java实现扫雷游戏的代码,供参考: ``` import java.util.*; public class Minesweeper { private int[][] board; private boolean[][] revealed; private boolean[][] flagged; private int numMines; private int numRows; private int numCols; private int numRevealed; public Minesweeper(int numRows, int numCols, int numMines) { this.numRows = numRows; this.numCols = numCols; this.numMines = numMines; this.numRevealed = 0; this.board = new int[numRows][numCols]; this.revealed = new boolean[numRows][numCols]; this.flagged = new boolean[numRows][numCols]; initializeBoard(); } private void initializeBoard() { Random rand = new Random(); int minesPlaced = 0; while (minesPlaced < numMines) { int row = rand.nextInt(numRows); int col = rand.nextInt(numCols); if (board[row][col] == 0) { board[row][col] = -1; minesPlaced++; } } for (int row = 0; row < numRows; row++) { for (int col = 0; col < numCols; col++) { if (board[row][col] == -1) { updateAdjacentCells(row, col); } } } } private void updateAdjacentCells(int row, int col) { for (int i = row - 1; i <= row + 1; i++) { for (int j = col - 1; j <= col + 1; j++) { if (i >= 0 && i < numRows && j >= 0 && j < numCols && board[i][j] != -1) { board[i][j]++; } } } } public void printBoard() { System.out.println("\n"); for (int row = 0; row < numRows; row++) { for (int col = 0; col < numCols; col++) { if (revealed[row][col]) { if (board[row][col] == -1) { System.out.print("* "); } else { System.out.print(board[row][col] + " "); } } else if (flagged[row][col]) { System.out.print("F "); } else { System.out.print("- "); } } System.out.println(); } } public boolean isGameWon() { return numRevealed == numRows * numCols - numMines; } public boolean isGameLost() { for (int row = 0; row < numRows; row++) { for (int col = 0; col < numCols; col++) { if (revealed[row][col] && board[row][col] == -1) { return true; } } } return false; } public boolean isCellRevealed(int row, int col) { return revealed[row][col]; } public boolean isCellFlagged(int row, int col) { return flagged[row][col]; } public boolean isCellMine(int row, int col) { return board[row][col] == -1; } public void revealCell(int row, int col) { if (!revealed[row][col] && !flagged[row][col]) { revealed[row][col] = true; numRevealed++; if (board[row][col] == 0) { revealAdjacentCells(row, col); } } } private void revealAdjacentCells(int row, int col) { for (int i = row - 1; i <= row + 1; i++) { for (int j = col - 1; j <= col + 1; j++) { if (i >= 0 && i < numRows && j >= 0 && j < numCols && !revealed[i][j]) { revealCell(i, j); } } } } public void flagCell(int row, int col) { if (!revealed[row][col]) { flagged[row][col] = !flagged[row][col]; } } public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter number of rows: "); int numRows = sc.nextInt(); System.out.print("Enter number of columns: "); int numCols = sc.nextInt(); System.out.print("Enter number of mines: "); int numMines = sc.nextInt(); Minesweeper game = new Minesweeper(numRows, numCols, numMines); while (!game.isGameWon() && !game.isGameLost()) { game.printBoard(); System.out.print("Enter row to reveal: "); int row = sc.nextInt(); System.out.print("Enter column to reveal: "); int col = sc.nextInt(); if (game.isCellMine(row, col)) { System.out.println("Game over!"); game.revealCell(row, col); } else { game.revealCell(row, col); if (game.isGameWon()) { System.out.println("You win!"); } } System.out.print("Do you want to flag a cell? (Y/N) "); String response = sc.next(); if (response.equalsIgnoreCase("Y")) { System.out.print("Enter row to flag: "); row = sc.nextInt(); System.out.print("Enter column to flag: "); col = sc.nextInt(); game.flagCell(row, col); } } game.printBoard(); } } ``` 这个实现中,我们使用了一个二维数组来表示扫雷游戏的棋盘,其中包含了每个格子周围的地雷数量。我们还使用了两个布尔类型的二维数组来表示每个格子是否已经被揭示或标记。在程序的主循环中,玩家可以选择一个格子来揭示它,或者标记一个格子为地雷。如果揭示的格子是地雷,游戏将结束。否则,游戏将继续进行,直到玩家揭示所有非地雷格子或揭示了一个地雷。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

halftion

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

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

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

打赏作者

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

抵扣说明:

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

余额充值