基于java语言扫雷游戏的课设
时间: 2023-12-19 13:06:12 浏览: 29
以下是基于Java语言的扫雷游戏课设的实现步骤:
1. 界面设计
- 使用Swing或Java************将方块、雷区、计时器等封装成对象
- 使用递归算法实现点击方块时的连锁反应
- 使用线程实现计时器
下面是一个简单的Java扫雷游戏实现的代码示例:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class MineSweeper extends JFrame {
private final int ROWS = 10;
private final int COLS = 10;
private final int MINES = 10;
private final int BLOCK_SIZE = 30;
private final int FRAME_WIDTH = COLS * BLOCK_SIZE;
private final int FRAME_HEIGHT = ROWS * BLOCK_SIZE + 50;
private final int[][] grid = new int[ROWS][COLS];
private final JButton[][] buttons = new JButton[ROWS][COLS];
private int minesLeft = MINES;
private boolean gameOver = false; private boolean gameWon = false;
private JLabel statusLabel;
private Timer timer;
private int timeElapsed = 0;
public MineSweeper() {
setTitle("Mine Sweeper");
setSize(FRAME_WIDTH, FRAME_HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
setLayout(new BorderLayout());
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(ROWS, COLS));
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
JButton button = new JButton();
button.setPreferredSize(new Dimension(BLOCK_SIZE, BLOCK_SIZE));
buttonPanel.add(button);
buttons[i][j] = button;
int row = i;
int col = j;
button.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if (gameOver || gameWon) {
return;
}
if (e.getButton() == MouseEvent.BUTTON1) {
if (grid[row][col] == -1) {
gameOver();
} else {
reveal(row, col);
checkWin();
}
} else if (e.getButton() == MouseEvent.BUTTON3) {
if (button.getText().equals("")) {
button.setText("F");
minesLeft--;
} else if (button.getText().equals("F")) {
button.setText("");
minesLeft++;
}
updateStatusLabel();
}
}
});
}
}
add(buttonPanel, BorderLayout.CENTER);
JPanel statusPanel = new JPanel();
statusLabel = new JLabel();
statusPanel.add(statusLabel);
add(statusPanel, BorderLayout.SOUTH);
timer = new Timer(1000, e -> {
timeElapsed++;
updateStatusLabel();
});
initGame();
}
private void initGame() {
minesLeft = MINES;
gameOver = false;
gameWon = false;
timeElapsed = 0;
updateStatusLabel();
timer.stop();
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
grid[i][j] = 0;
buttons[i][j].setText("");
buttons[i][j].setEnabled(true);
}
}
int minesToPlace = MINES;
while (minesToPlace > 0) {
int row = (int) (Math.random() * ROWS);
int col = (int) (Math.random() * COLS);
if (grid[row][col] == 0) {
grid[row][col] = -1;
minesToPlace--;
}
}
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
if (grid[i][j] == -1) {
continue;
}
int count = 0;
if (i > 0 && j > 0 && grid[i - 1][j - 1] == -1) count++;
if (i > 0 && grid[i - 1][j] == -1) count++;
if (i > 0 && j < COLS - 1 && grid[i - 1][j + 1] == -1) count++;
if (j > 0 && grid[i][j - 1] == -1) count++;
if (j < COLS - 1 && grid[i][j + 1] == -1) count++;
if (i < ROWS - 1 && j > 0 && grid[i + 1][j - 1] == -1) count++;
if (i < ROWS - 1 && grid[i + 1][j] == -1) count++;
if (i < ROWS - 1 && j < COLS - 1 && grid[i + 1][j + 1] == -1) count++;
grid[i][j] = count;
}
}
}
private void gameOver() {
gameOver = true;
timer.stop();
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
if (grid[i][j] == -1) {
buttons[i][j].setText("X");
}
buttons[i][j].setEnabled(false);
}
}
updateStatusLabel();
}
private void reveal(int row, int col) {
if (row < 0 || row >= ROWS || col < 0 || col >= COLS || !buttons[row][col].isEnabled()) {
return;
}
buttons[row][col].setEnabled(false);
if (grid[row][col] == -1) {
return;
}
if (grid[row][col] > 0) {
buttons[row][col].setText(Integer.toString(grid[row][col]));
return;
}
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 checkWin() {
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
if (grid[i][j] != -1 && buttons[i][j].isEnabled()) {
return;
}
}
}
gameWon = true;
timer.stop();
updateStatusLabel();
}
private void updateStatusLabel() {
if (gameOver) {
statusLabel.setText("Game Over");
} else if (gameWon) {
statusLabel.setText("You Win!");
} else {
statusLabel.setText("Mines Left: " + minesLeft + " Time: " + timeElapsed);
}
}
public static void main(String[] args) {
MineSweeper game = new MineSweeper();
game.setVisible(true);
}
}
```