用java+swing写一个扫雷代码
时间: 2023-12-27 09:03:59 浏览: 30
以下是一个简单的扫雷游戏的 Java Swing 代码示例:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
public class Minesweeper extends JFrame implements ActionListener {
private static final int ROWS = 10;
private static final int COLS = 10;
private static final int MINES = 10;
private static final int CELL_SIZE = 30;
private JButton[][] cells;
private boolean[][] mineLocations;
private boolean[][] revealed;
private int remainingCells;
public Minesweeper() {
setTitle("Minesweeper");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
cells = new JButton[ROWS][COLS];
mineLocations = new boolean[ROWS][COLS];
revealed = new boolean[ROWS][COLS];
remainingCells = ROWS * COLS - MINES;
JPanel board = new JPanel(new GridLayout(ROWS, COLS, 1, 1));
board.setPreferredSize(new Dimension(COLS * CELL_SIZE, ROWS * CELL_SIZE));
for (int row = 0; row < ROWS; row++) {
for (int col = 0; col < COLS; col++) {
cells[row][col] = new JButton();
cells[row][col].setPreferredSize(new Dimension(CELL_SIZE, CELL_SIZE));
cells[row][col].addActionListener(this);
board.add(cells[row][col]);
}
}
add(board, BorderLayout.CENTER);
pack();
initializeMines();
setVisible(true);
}
private void initializeMines() {
Random random = new Random();
int minesPlaced = 0;
while (minesPlaced < MINES) {
int row = random.nextInt(ROWS);
int col = random.nextInt(COLS);
if (!mineLocations[row][col]) {
mineLocations[row][col] = true;
minesPlaced++;
}
}
}
private void revealCell(int row, int col) {
if (!revealed[row][col]) {
revealed[row][col] = true;
cells[row][col].setEnabled(false);
remainingCells--;
if (mineLocations[row][col]) {
cells[row][col].setText("*");
gameOver(false);
} else {
int adjacentMines = countAdjacentMines(row, col);
if (adjacentMines > 0) {
cells[row][col].setText(Integer.toString(adjacentMines));
} else {
cells[row][col].setText("");
for (int dRow = -1; dRow <= 1; dRow++) {
for (int dCol = -1; dCol <= 1; dCol++) {
int newRow = row + dRow;
int newCol = col + dCol;
if (newRow >= 0 && newRow < ROWS && newCol >= 0 && newCol < COLS) {
revealCell(newRow, newCol);
}
}
}
}
if (remainingCells == 0) {
gameOver(true);
}
}
}
}
private int countAdjacentMines(int row, int col) {
int count = 0;
for (int dRow = -1; dRow <= 1; dRow++) {
for (int dCol = -1; dCol <= 1; dCol++) {
int newRow = row + dRow;
int newCol = col + dCol;
if (newRow >= 0 && newRow < ROWS && newCol >= 0 && newCol < COLS && mineLocations[newRow][newCol]) {
count++;
}
}
}
return count;
}
private void gameOver(boolean win) {
for (int row = 0; row < ROWS; row++) {
for (int col = 0; col < COLS; col++) {
if (mineLocations[row][col]) {
cells[row][col].setText("*");
}
cells[row][col].setEnabled(false);
}
}
String message = win ? "You win!" : "You lose!";
JOptionPane.showMessageDialog(this, message, "Game Over", JOptionPane.INFORMATION_MESSAGE);
}
@Override
public void actionPerformed(ActionEvent e) {
for (int row = 0; row < ROWS; row++) {
for (int col = 0; col < COLS; col++) {
if (e.getSource() == cells[row][col]) {
revealCell(row, col);
return;
}
}
}
}
public static void main(String[] args) {
new Minesweeper();
}
}
```
这个示例代码实现了一个简单的 10x10 的扫雷游戏,其中包含 10 个地雷。游戏界面使用了 Java Swing 中的 JButton 组件,每个按钮代表一个方格。当玩家点击一个方格时,程序会判断该方格是否包含地雷,如果是,游戏结束;如果不是,程序会计算该方格周围的地雷数量,并将数字显示在按钮上。如果该方格周围没有地雷,则程序会递归地展开周围的方格。
注意,这个示例代码仅供参考,可能存在一些不足之处,需要根据实际需求进行修改和完善。