Java扫雷游戏程序代码
时间: 2023-07-01 08:18:42 浏览: 109
以下是一个简单的Java扫雷游戏程序代码,供参考:
```java
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
import javax.swing.*;
public class Minesweeper extends JFrame implements ActionListener, MouseListener {
private final int WIDTH = 300;
private final int HEIGHT = 400;
private final int ROWS = 10;
private final int COLS = 10;
private final int MINES = 15;
private JButton[][] buttons;
private JLabel[][] labels;
private int[][] grid;
private boolean[][] revealed;
private boolean[][] flagged;
private boolean gameOver;
private int remaining;
private int seconds;
private Timer timer;
public Minesweeper() {
setTitle("Minesweeper");
setSize(WIDTH, HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setResizable(false);
JPanel panel = new JPanel(new BorderLayout());
JPanel buttonPanel = new JPanel(new GridLayout(ROWS, COLS));
buttons = new JButton[ROWS][COLS];
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
buttons[i][j] = new JButton();
buttons[i][j].addActionListener(this);
buttons[i][j].addMouseListener(this);
buttonPanel.add(buttons[i][j]);
}
}
JPanel labelPanel = new JPanel(new GridLayout(ROWS, COLS));
labels = new JLabel[ROWS][COLS];
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
labels[i][j] = new JLabel();
labelPanel.add(labels[i][j]);
}
}
panel.add(buttonPanel, BorderLayout.CENTER);
panel.add(labelPanel, BorderLayout.SOUTH);
remaining = ROWS * COLS - MINES;
seconds = 0;
timer = new Timer(1000, new ActionListener() {
public void actionPerformed(ActionEvent e) {
seconds++;
setTitle("Minesweeper - " + seconds + " seconds");
}
});
timer.start();
initGrid();
add(panel);
setVisible(true);
}
private void initGrid() {
grid = new int[ROWS][COLS];
revealed = new boolean[ROWS][COLS];
flagged = new boolean[ROWS][COLS];
gameOver = false;
remaining = ROWS * COLS - MINES;
seconds = 0;
timer.restart();
Random rand = new Random();
for (int i = 0; i < MINES; i++) {
int row, col;
do {
row = rand.nextInt(ROWS);
col = rand.nextInt(COLS);
} while (grid[row][col] == -1);
grid[row][col] = -1;
}
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
if (grid[i][j] == -1) {
continue;
}
int count = 0;
for (int k = Math.max(0, i - 1); k <= Math.min(ROWS - 1, i + 1); k++) {
for (int l = Math.max(0, j - 1); l <= Math.min(COLS - 1, j + 1); l++) {
if (grid[k][l] == -1) {
count++;
}
}
}
grid[i][j] = count;
}
}
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
labels[i][j].setText("");
revealed[i][j] = false;
flagged[i][j] = false;
buttons[i][j].setEnabled(true);
buttons[i][j].setBackground(null);
}
}
}
private void reveal(int row, int col) {
if (row < 0 || row >= ROWS || col < 0 || col >= COLS || revealed[row][col] || flagged[row][col] || gameOver) {
return;
}
revealed[row][col] = true;
remaining--;
if (grid[row][col] == -1) {
gameOver = true;
timer.stop();
JOptionPane.showMessageDialog(this, "Game over!", "Minesweeper", JOptionPane.INFORMATION_MESSAGE);
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
if (grid[i][j] == -1) {
buttons[i][j].setText("*");
buttons[i][j].setEnabled(false);
}
}
}
} else if (remaining == 0) {
gameOver = true;
timer.stop();
JOptionPane.showMessageDialog(this, "You win!", "Minesweeper", JOptionPane.INFORMATION_MESSAGE);
} else {
buttons[row][col].setEnabled(false);
buttons[row][col].setBackground(Color.lightGray);
if (grid[row][col] > 0) {
labels[row][col].setText(Integer.toString(grid[row][col]));
} else {
reveal(row - 1, col - 1);
reveal(row - 1, col);
reveal(row - 1, col + 1);
reveal(row, col - 1);
reveal(row, col + 1);
reveal(row + 1, col - 1);
reveal(row + 1, col);
reveal(row + 1, col + 1);
}
}
}
private void flag(int row, int col) {
if (row < 0 || row >= ROWS || col < 0 || col >= COLS || revealed[row][col] || gameOver) {
return;
}
flagged[row][col] = !flagged[row][col];
if (flagged[row][col]) {
buttons[row][col].setText("F");
} else {
buttons[row][col].setText("");
}
}
public void actionPerformed(ActionEvent e) {
if (gameOver) {
return;
}
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
if (e.getSource() == buttons[i][j]) {
reveal(i, j);
return;
}
}
}
}
public void mousePressed(MouseEvent e) {
if (gameOver) {
return;
}
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
if (e.getSource() == buttons[i][j] && e.getButton() == MouseEvent.BUTTON3) {
flag(i, j);
return;
}
}
}
}
public void mouseClicked(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
public static void main(String[] args) {
new Minesweeper();
}
}
```
这个程序使用了Java Swing库来创建界面和组件,实现了一个简单的扫雷游戏。在这个程序中,我们定义了一个JFrame作为游戏窗口,并且使用了JPanel、JButton、JLabel等组件来实现游戏界面。我们还使用了Java的事件处理机制,来处理按钮的点击事件和鼠标的按键事件。在游戏的实现中,我们使用了二维数组来表示雷区,以及一个计时器来记录游戏用时。我们还使用了递归算法来实现自动揭示周围的方格。
阅读全文