Java

基于java扫雷游戏的设计与实现

小亿
81
2024-12-07 09:16:44
栏目: 编程语言

扫雷游戏是一种经典的计算机游戏,它要求玩家在一个网格中清除所有非地雷方块,同时避开地雷。下面是一个简单的Java实现,包括游戏界面、逻辑和用户交互。

1. 游戏设计

1.1 游戏规则

1.2 数据结构

2. 游戏实现

2.1 初始化游戏

import java.util.Random;
import javax.swing.*;

public class Minesweeper {
    private static final int SIZE = 10;
    private static final int MINES = 10;
    private int[][] grid;
    private boolean[][] visited;
    private Random random;
    private JLabel[][] displayGrid;
    private JButton[] buttons;

    public Minesweeper() {
        grid = new int[SIZE][SIZE];
        visited = new boolean[SIZE][SIZE];
        random = new Random();
        displayGrid = new JLabel[SIZE][SIZE];
        buttons = new JButton[SIZE * SIZE];

        initializeGrid();
        initializeDisplay();
        initializeButtons();
    }

    private void initializeGrid() {
        for (int i = 0; i < SIZE; i++) {
            for (int j = 0; j < SIZE; j++) {
                grid[i][j] = random.nextInt(SIZE * SIZE) < MINES ? -1 : 0;
            }
        }
    }

    private void initializeDisplay() {
        JFrame frame = new JFrame("Minesweeper");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new GridLayout(SIZE, SIZE));

        for (int i = 0; i < SIZE; i++) {
            for (int j = 0; j < SIZE; j++) {
                displayGrid[i][j] = new JLabel();
                displayGrid[i][j].setPreferredSize(new Dimension(50, 50));
                displayGrid[i][j].setBorder(BorderFactory.createLineBorder(Color.BLACK));
                frame.add(displayGrid[i][j]);
            }
        }

        frame.pack();
        frame.setVisible(true);
    }

    private void initializeButtons() {
        JPanel buttonPanel = new JPanel();
        buttonPanel.setLayout(new GridLayout(SIZE * SIZE, 1));

        for (int i = 0; i < SIZE * SIZE; i++) {
            buttons[i] = new JButton();
            buttons[i].addActionListener(e -> onClick(i / SIZE, i % SIZE));
            buttonPanel.add(buttons[i]);
        }

        JFrame frame = (JFrame) displayGrid[0][0].getParent();
        frame.add(buttonPanel, BorderLayout.SOUTH);
    }

    private void onClick(int row, int col) {
        if (grid[row][col] == -1) {
            JOptionPane.showMessageDialog(null, "Game Over!");
            return;
        }

        visited[row][col] = true;
        displayGrid[row][col].setText(String.valueOf(countAdjacentMines(row, col)));

        if (countAdjacentMines(row, col) == 0 && grid[row][col] == 0) {
            for (int i = 0; i < SIZE; i++) {
                for (int j = 0; j < SIZE; j++) {
                    if (!visited[i][j] && grid[i][j] == 0) {
                        onClick(i, j);
                    }
                }
            }
        }
    }

    private int countAdjacentMines(int row, int col) {
        int count = 0;
        for (int i = -1; i <= 1; i++) {
            for (int j = -1; j <= 1; j++) {
                if (i == 0 && j == 0) continue;
                int newRow = row + i;
                int newCol = col + j;
                if (newRow >= 0 && newRow < SIZE && newCol >= 0 && newCol < SIZE && grid[newRow][newCol] == -1) {
                    count++;
                }
            }
        }
        return count;
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> new Minesweeper());
    }
}

3. 代码解释

3.1 初始化游戏

3.2 点击事件处理

4. 运行游戏

将上述代码保存为Minesweeper.java,然后使用以下命令编译和运行:

javac Minesweeper.java
java Minesweeper

这将启动一个简单的扫雷游戏窗口,玩家可以通过点击方块来清除它们,并显示周围地雷的数量。

0
看了该问题的人还看了