Java分享扫雷小游戏源码

package src.am.demo02;

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

public class MineSweeper extends JFrame implements ActionListener, MouseListener {
    private static final long serialVersionUID = 1L;
    private int row = 10; // 行数
    private int col = 10; // 列数
    private int mineNum = 10; // 雷数
    private int[][] mineMap = new int[row][col]; // 雷区地图
    private JButton[][] btns = new JButton[row][col]; // 按钮数组
    private boolean[][] mineFlag = new boolean[row][col]; // 标记数组
    private boolean[][] openFlag = new boolean[row][col]; // 打开标记数组
    private int leftMineNum = mineNum; // 剩余雷数
    private JLabel leftMineLabel = new JLabel("剩余雷数:" + leftMineNum); // 剩余雷数标签
    private JPanel minePanel = new JPanel(new GridLayout(row, col)); // 雷区面板
    private JPanel topPanel = new JPanel(); // 顶部面板
    private boolean gameOver = false; // 游戏是否结束

    public MineSweeper() {
        setTitle("扫雷");
        setSize(600, 600);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        initMineMap();
        initBtns();
        initTopPanel();
        add(minePanel, BorderLayout.CENTER);
        add(topPanel, BorderLayout.NORTH);
        setVisible(true);
    }

    // 初始化雷区地图
    private void initMineMap() {
        int count = 0;
        while (count < mineNum) {
            int x = (int) (Math.random() * row);
            int y = (int) (Math.random() * col);
            if (mineMap[x][y] != -1) {
                mineMap[x][y] = -1;
                count++;
            }
        }
        for (int i = 0; i < row; i++) {
            for (int j = 0; j < col; j++) {
                if (mineMap[i][j] != -1) {
                    int countMine = 0;
                    if (i > 0 && j > 0 && mineMap[i - 1][j - 1] == -1)
                        countMine++;
                    if (i > 0 && mineMap[i - 1][j] == -1)
                        countMine++;
                    if (i > 0 && j < col - 1 && mineMap[i - 1][j + 1] == -1)
                        countMine++;
                    if (j > 0 && mineMap[i][j - 1] == -1)
                        countMine++;
                    if (j < col - 1 && mineMap[i][j + 1] == -1)
                        countMine++;
                    if (i < row - 1 && j > 0 && mineMap[i + 1][j - 1] == -1)
                        countMine++;
                    if (i < row - 1 && mineMap[i + 1][j] == -1)
                        countMine++;
                    if (i < row - 1 && j < col - 1 && mineMap[i + 1][j + 1] == -1)
                        countMine++;
                    mineMap[i][j] = countMine;
                }
            }
        }
    }

    // 初始化按钮数组
    private void initBtns() {
        for (int i = 0; i < row; i++) {
            for (int j = 0; j < col; j++) {
                btns[i][j] = new JButton();
                btns[i][j].addActionListener(this);
                btns[i][j].addMouseListener(this);
                minePanel.add(btns[i][j]);
            }
        }
    }

    // 初始化顶部面板
    private void initTopPanel() {
        JButton restartBtn = new JButton("重新开始");
        restartBtn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                restart();
            }
        });
        topPanel.add(leftMineLabel);
        topPanel.add(restartBtn);
    }

    // 重新开始
    private void restart() {
        gameOver = false;
        leftMineNum = mineNum;
        leftMineLabel.setText("剩余雷数:" + leftMineNum);
        for (int i = 0; i < row; i++) {
            for (int j = 0; j < col; j++) {
                mineMap[i][j] = 0;
                mineFlag[i][j] = false;
                openFlag[i][j] = false;
                btns[i][j].setText("");
                btns[i][j].setEnabled(true);
                btns[i][j].setBackground(null);
            }
        }
        initMineMap();
    }

    // 点击按钮
    public void actionPerformed(ActionEvent e) {
        if (gameOver)
            return;
        JButton btn = (JButton) e.getSource();
        int x = -1, y = -1;
        for (int i = 0; i < row; i++) {
            for (int j = 0; j < col; j++) {
                if (btn == btns[i][j]) {
                    x = i;
                    y = j;
                    break;
                }
            }
        }
        if (mineFlag[x][y])
            return;
        if (mineMap[x][y] == -1) {
            gameOver();
        } else if (mineMap[x][y] == 0) {
            open(x, y);
        } else {
            btn.setText(mineMap[x][y] + "");
            btn.setEnabled(false);
            openFlag[x][y] = true;
            checkWin();
        }
    }

    // 右键标记
    public void mouseClicked(MouseEvent e) {
        if (gameOver)
            return;
        JButton btn = (JButton) e.getSource();
        int x = -1, y = -1;
        for (int i = 0; i < row; i++) {
            for (int j = 0; j < col; j++) {
                if (btn == btns[i][j]) {
                    x = i;
                    y = j;
                    break;
                }
            }
        }
        if (e.getButton() == MouseEvent.BUTTON3) {
            if (btn.getText().equals("")) {
                btn.setText("▲");
                mineFlag[x][y] = true;
                leftMineNum--;
            } else {
                btn.setText("");
                mineFlag[x][y] = false;
                leftMineNum++;
            }
            leftMineLabel.setText("剩余雷数:" + leftMineNum);
        }
    }

    public void mousePressed(MouseEvent e) {
    }

    public void mouseReleased(MouseEvent e) {
    }

    public void mouseEntered(MouseEvent e) {
    }

    public void mouseExited(MouseEvent e) {
    }

    // 打开空白区域
    private void open(int x, int y) {
        if (x < 0 || x >= row || y < 0 || y >= col || openFlag[x][y])
            return;
        JButton btn = btns[x][y];
        if (mineFlag[x][y])
            return;
        if (mineMap[x][y] == -1) {
            gameOver();
        } else if (mineMap[x][y] == 0) {
            btn.setEnabled(false);
            openFlag[x][y] = true;
            open(x - 1, y - 1);
            open(x - 1, y);
            open(x - 1, y + 1);
            open(x, y - 1);
            open(x, y + 1);
            open(x + 1, y - 1);
            open(x + 1, y);
            open(x + 1, y + 1);
        } else {
            btn.setText(mineMap[x][y] + "");
            btn.setEnabled(false);
            openFlag[x][y] = true;
            checkWin();
        }
    }

    // 游戏结束
    private void gameOver() {
        gameOver = true;
        for (int i = 0; i < row; i++) {
            for (int j = 0; j < col; j++) {
                JButton btn = btns[i][j];
                if (mineMap[i][j] == -1) {
                    btn.setText("●");
                    btn.setBackground(Color.RED);
                }
                btn.setEnabled(false);
            }
        }
        JOptionPane.showMessageDialog(this, "游戏结束!");
    }

    // 检查是否胜利
    private void checkWin() {
        int count = 0;
        for (int i = 0; i < row; i++) {
            for (int j = 0; j < col; j++) {
                if (openFlag[i][j])
                    count++;
            }
        }
        if (count == row * col - mineNum) {
            gameOver = true;
            JOptionPane.showMessageDialog(this, "恭喜你,游戏胜利!");
        }
    }

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


  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是简单的 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(); int rows, columns, bombs; System.out.print("Enter number of rows: "); rows = scanner.nextInt(); System.out.print("Enter number of columns: "); columns = scanner.nextInt(); System.out.print("Enter number of bombs: "); bombs = scanner.nextInt(); boolean[][] board = new boolean[rows][columns]; int[][] counts = new int[rows][columns]; // randomly place bombs for (int i = 0; i < bombs; i++) { int row, column; do { row = random.nextInt(rows); column = random.nextInt(columns); } while (board[row][column]); board[row][column] = true; for (int dr = -1; dr <= 1; dr++) { for (int dc = -1; dc <= 1; dc++) { if (dr != 0 || dc != 0) { int r = row + dr; int c = column + dc; if (r >= 0 && r < rows && c >= 0 && c < columns) { counts[r][c]++; } } } } } boolean[][] revealed = new boolean[rows][columns]; int remaining = rows * columns - bombs; while (remaining > 0) { // print the board for (int c = 0; c < columns; c++) { System.out.print(" " + c); } System.out.println(); for (int r = 0; r < rows; r++) { System.out.print(r); for (int c = 0; c < columns; c++) { if (revealed[r][c]) { if (board[r][c]) { System.out.print("* "); } else { System.out.print(counts[r][c] + " "); } } else { System.out.print(". "); } } System.out.println(); } // ask for the next move int row, column; do { System.out.print("Enter row and column: "); row = scanner.nextInt(); column = scanner.nextInt(); } while (row < 0 || row >= rows || column < 0 || column >= columns || revealed[row][column]); revealed[row][column] = true; remaining--; // handle the move if (board[row][column]) { System.out.println("Boom! Game over."); break; } else if (counts[row][column] == 0) { // reveal all neighbors for (int dr = -1; dr <= 1; dr++) { for (int dc = -1; dc <= 1; dc++) { if (dr != 0 || dc != 0) { int r = row + dr; int c = column + dc; if (r >= 0 && r < rows && c >= 0 && c < columns && !revealed[r][c]) { revealed[r][c] = true; remaining--; } } } } } } if (remaining == 0) { System.out.println("Congratulations! You win!"); } scanner.close(); } } ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值