java设计界面并实现扫雷游戏的代码
时间: 2023-10-29 07:07:01 浏览: 108
好的,下面是Java设计界面并实现扫雷游戏的代码示例:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Minesweeper extends JFrame {
private final int ROWS = 10;
private final int COLS = 10;
private final int MINES = 10;
private boolean[][] mineField;
private boolean[][] revealed;
private boolean[][] flagged;
public Minesweeper() {
setTitle("Minesweeper");
setSize(500, 500);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mineField = new boolean[ROWS][COLS];
revealed = new boolean[ROWS][COLS];
flagged = new boolean[ROWS][COLS];
initGame();
add(createBoard(), BorderLayout.CENTER);
add(createStatusBar(), BorderLayout.SOUTH);
setVisible(true);
}
private void initGame() {
// Randomly place mines
int count = 0;
while (count < MINES) {
int row = (int) (Math.random() * ROWS);
int col = (int) (Math.random() * COLS);
if (!mineField[row][col]) {
mineField[row][col] = true;
count++;
}
}
// Calculate number of neighbouring mines
for (int row = 0; row < ROWS; row++) {
for (int col = 0; col < COLS; col++) {
if (!mineField[row][col]) {
int countNeighbours = 0;
for (int i = -1; i <= 1; i++) {
for (int j = -1; j <= 1; j++) {
int r = row + i;
int c = col + j;
if (r >= 0 && r < ROWS && c >= 0 && c < COLS && mineField[r][c]) {
countNeighbours++;
}
}
}
mineField[row][col] = countNeighbours > 0;
}
}
}
}
private JPanel createBoard() {
JPanel board = new JPanel(new GridLayout(ROWS, COLS));
for (int row = 0; row < ROWS; row++) {
for (int col = 0; col < COLS; col++) {
JButton button = new JButton();
button.addActionListener(new ButtonListener(row, col));
board.add(button);
}
}
return board;
}
private JPanel createStatusBar() {
JPanel statusBar = new JPanel(new GridLayout(1, 3));
JLabel mineCount = new JLabel("Mines: " + MINES);
JLabel timeElapsed = new JLabel("Time: 0");
JButton restartButton = new JButton("Restart");
restartButton.addActionListener(new RestartListener());
statusBar.add(mineCount);
statusBar.add(timeElapsed);
statusBar.add(restartButton);
return statusBar;
}
private void reveal(int row, int col) {
if (row < 0 || row >= ROWS || col < 0 || col >= COLS || revealed[row][col] || flagged[row][col]) {
return;
}
revealed[row][col] = true;
JButton button = (JButton) ((JPanel) getContentPane().getComponent(0)).getComponent(row * COLS + col);
if (mineField[row][col]) {
button.setText("*");
JOptionPane.showMessageDialog(this, "Game over!");
setVisible(false);
dispose();
} else {
int count = countNeighbours(row, col);
if (count > 0) {
button.setText(String.valueOf(count));
} else {
button.setText("");
for (int i = -1; i <= 1; i++) {
for (int j = -1; j <= 1; j++) {
reveal(row + i, col + j);
}
}
}
}
}
private int countNeighbours(int row, int col) {
int count = 0;
for (int i = -1; i <= 1; i++) {
for (int j = -1; j <= 1; j++) {
int r = row + i;
int c = col + j;
if (r >= 0 && r < ROWS && c >= 0 && c < COLS && mineField[r][c]) {
count++;
}
}
}
return count;
}
private boolean checkWin() {
for (int row = 0; row < ROWS; row++) {
for (int col = 0; col < COLS; col++) {
if (!mineField[row][col] && !revealed[row][col]) {
return false;
}
}
}
return true;
}
private class ButtonListener implements ActionListener {
private int row;
private int col;
public ButtonListener(int row, int col) {
this.row = row;
this.col = col;
}
public void actionPerformed(ActionEvent e) {
reveal(row, col);
if (checkWin()) {
JOptionPane.showMessageDialog(Minesweeper.this, "You win!");
setVisible(false);
dispose();
}
}
}
private class RestartListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
setVisible(false);
dispose();
new Minesweeper();
}
}
public static void main(String[] args) {
new Minesweeper();
}
}
```
这个代码示例实现了一个10x10的扫雷游戏,其中有10个地雷。玩家可以点击每个方格,如果方格中没有地雷,则会显示周围的地雷数量。如果方格中有地雷,则游戏结束,并弹出“Game over”对话框。如果玩家成功揭开了所有没有地雷的方格,则游戏胜利,并弹出“You win”对话框。玩家可以点击“Restart”按钮重新开始游戏。
阅读全文