阅读量:88
扫雷游戏是一种经典的计算机游戏,它要求玩家在一个网格中清除所有非地雷方块,同时避开地雷。下面是一个简单的Java实现,包括游戏界面、逻辑和用户交互。
1. 游戏设计
1.1 游戏规则
- 网格大小:10x10
- 地雷数量:随机生成
- 非地雷方块:显示数字表示周围地雷的数量
- 玩家可以通过点击方块来清除它
- 如果点击地雷,游戏结束
- 如果清除所有非地雷方块且没有点击地雷,游戏胜利
1.2 数据结构
int[][] grid:表示游戏网格boolean[][] visited:表示方块是否被访问过Random random:用于生成随机地雷
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 class="hljs-keyword">for (int j = 0; j < SIZE xss=removed class="hljs-number">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 class="hljs-keyword">for (int j = 0; j < SIZE xss=removed class="hljs-keyword">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 xss=removed class="hljs-keyword">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 class="hljs-keyword">for (int j = 0; j < SIZE class="hljs-keyword">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>= 0 && newCol < SIZE xss=removed class="hljs-number">1) {
count++;
}
}
}
return count;
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> new Minesweeper());
}
}
3. 代码解释
3.1 初始化游戏
initializeGrid():随机生成地雷和非地雷方块。initializeDisplay():创建游戏界面,包括网格和按钮。initializeButtons():初始化按钮并添加点击事件监听器。
3.2 点击事件处理
onClick(int row, int col):处理玩家点击事件,更新显示并递归清除非地雷方块。countAdjacentMines(int row, int col):计算指定位置的相邻地雷数量。
4. 运行游戏
将上述代码保存为Minesweeper.java,然后使用以下命令编译和运行:
javac Minesweeper.java
java Minesweeper
这将启动一个简单的扫雷游戏窗口,玩家可以通过点击方块来清除它们,并显示周围地雷的数量。