扫雷游戏Java代码
时间: 2023-12-06 16:01:59 浏览: 80
扫雷游戏Java源代码.doc
以下是一个简单的扫雷游戏的Java代码,它包括三种难度级别,具有图形用户界面(GUI)和计时器功能。你可以根据自己的需要进行修改和扩展。
```java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Minesweeper extends JFrame implements ActionListener, MouseListener {
private static final long serialVersionUID = 1L;
private JPanel panel;
private JButton[][] buttons;
private int[][] counts;
private int size = 10;
private int mines = 10;
private boolean started = false;
private boolean gameOver = false;
private Timer timer;
private int time = 0;
private JLabel timeLabel;
private JLabel mineLabel;
public Minesweeper() {
setTitle("Minesweeper");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel = new JPanel();
panel.setLayout(new GridLayout(size, size));
buttons = new JButton[size][size];
counts = new int[size][size];
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
buttons[i][j] = new JButton();
buttons[i][j].addActionListener(this);
buttons[i][j].addMouseListener(this);
panel.add(buttons[i][j]);
}
}
add(panel, BorderLayout.CENTER);
timeLabel = new JLabel("Time: 0");
add(timeLabel, BorderLayout.NORTH);
mineLabel = new JLabel("Mines: " + mines);
add(mineLabel, BorderLayout.SOUTH);
pack();
setLocationRelativeTo(null);
setVisible(true);
}
private void start() {
started = true;
gameOver = false;
time = 0;
timeLabel.setText("Time: " + time);
mineLabel.setText("Mines: " + mines);
timer = new Timer(1000, new ActionListener() {
public void actionPerformed(ActionEvent e) {
time++;
timeLabel.setText("Time: " + time);
}
});
timer.start();
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
buttons[i][j].setEnabled(true);
buttons[i][j].setText("");
counts[i][j] = 0;
}
}
int planted = 0;
while (planted < mines) {
int x = (int) (Math.random() * size);
int y = (int) (Math.random() * size);
if (counts[x][y] == 0) {
counts[x][y] = -1;
planted++;
}
}
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
if (counts[i][j] == -1) {
for (int k = i - 1; k <= i + 1; k++) {
for (int l = j - 1; l <= j + 1; l++) {
if (k >= 0 && k < size && l >= 0 && l < size && counts[k][l] != -1) {
counts[k][l]++;
}
}
}
}
}
}
}
private void reveal(int x, int y) {
if (gameOver) {
return;
}
if (!started) {
start();
}
if (counts[x][y] == -1) {
gameOver();
} else if (counts[x][y] > 0) {
buttons[x][y].setText("" + counts[x][y]);
buttons[x][y].setEnabled(false);
} else {
buttons[x][y].setEnabled(false);
for (int i = x - 1; i <= x + 1; i++) {
for (int j = y - 1; j <= y + 1; j++) {
if (i >= 0 && i < size && j >= 0 && j < size && buttons[i][j].isEnabled()) {
reveal(i, j);
}
}
}
}
checkWin();
}
private void flag(int x, int y) {
if (gameOver) {
return;
}
if (!started) {
start();
}
if (buttons[x][y].getText().equals("F")) {
buttons[x][y].setText("");
mines++;
mineLabel.setText("Mines: " + mines);
} else if (mines > 0) {
buttons[x][y].setText("F");
mines--;
mineLabel.setText("Mines: " + mines);
}
checkWin();
}
private void gameOver() {
gameOver = true;
timer.stop();
JOptionPane.showMessageDialog(this, "Game Over");
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
if (counts[i][j] == -1) {
buttons[i][j].setText("*");
}
buttons[i][j].setEnabled(false);
}
}
}
private void checkWin() {
boolean win = true;
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
if (counts[i][j] != -1 && buttons[i][j].isEnabled()) {
win = false;
}
}
}
if (win) {
gameOver = true;
timer.stop();
JOptionPane.showMessageDialog(this, "You Win!");
}
}
public void actionPerformed(ActionEvent e) {
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
if (e.getSource() == buttons[i][j]) {
reveal(i, j);
}
}
}
}
public void mousePressed(MouseEvent e) {
if (e.getButton() == MouseEvent.BUTTON3) {
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
if (e.getSource() == buttons[i][j]) {
flag(i, j);
}
}
}
}
}
public void mouseReleased(MouseEvent e) {}
public void mouseClicked(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public static void main(String[] args) {
new Minesweeper();
}
}
```
阅读全文