Java扫雷简易小游戏
时间: 2023-12-13 18:32:49 浏览: 32
以下是Java实现的简易扫雷小游戏的步骤:
1. 创建一个窗口,包含一个菜单栏和一个游戏区域。
2. 菜单栏包含难度选择和重新开始游戏的选项。
3. 游戏区域包含一个二维*********```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class Minesweeper extends JFrame {
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 static final int FRAME_WIDTH = COLS * CELL_SIZE;
private static final int FRAME_HEIGHT = ROWS * CELL_SIZE + 22;
private Cell[][] cells = new Cell[ROWS][COLS];
private int minesLeft = MINES;
private JLabel statusbar;
public Minesweeper() {
initUI();
}
private void initUI() {
JPanel panel = new JPanel(); panel.setLayout(new GridLayout(ROWS, COLS));
for (int row = 0; row < ROWS; row++) {
for (int col = 0; col < COLS; col++) {
cells[row][col] = new Cell(row, col);
panel.add(cells[row][col]);
}
}
statusbar = new JLabel("Mines left: " + minesLeft);
add(panel, BorderLayout.CENTER);
add(statusbar, BorderLayout.SOUTH);
setResizable(false);
pack();
setTitle("Minesweeper");
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private void newGame() {
for (int row = 0; row < ROWS; row++) {
for (int col = 0; col < COLS; col++) {
cells[row][col].reset();
}
}
minesLeft = MINES;
statusbar.setText("Mines left: " + minesLeft);
}
private void gameOver() {
for (int row = 0; row < ROWS; row++) {
for (int col = 0; col < COLS; col++) {
cells[row][col].reveal();
}
}
JOptionPane.showMessageDialog(this, "Game over", "Minesweeper", JOptionPane.INFORMATION_MESSAGE);
newGame();
}
private void checkWin() {
boolean win = true;
for (int row = 0; row < ROWS; row++) {
for (int col = 0; col < COLS; col++) {
if (!cells[row][col].isMine() && !cells[row][col].isRevealed()) {
win = false;
}
}
}
if (win) {
JOptionPane.showMessageDialog(this, "You win!", "Minesweeper", JOptionPane.INFORMATION_MESSAGE);
newGame();
}
}
private class Cell extends JPanel {
private int row;
private int col;
private boolean mine;
private boolean revealed;
private boolean marked;
public Cell(int row, int col) {
this.row = row;
this.col = col;
this.mine = Math.random() < (double) MINES / (ROWS * COLS);
this.revealed = false;
this.marked = false;
setPreferredSize(new Dimension(CELL_SIZE, CELL_SIZE));
setBackground(Color.LIGHT_GRAY);
setBorder(BorderFactory.createLineBorder(Color.GRAY));
addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
if (SwingUtilities.isLeftMouseButton(e)) {
if (mine) {
setBackground(Color.RED);
gameOver();
} else {
reveal();
checkWin();
}
} else if (SwingUtilities.isRightMouseButton(e)) {
mark();
}
}
});
}
public void reset() {
this.mine = Math.random() < (double) MINES / (ROWS * COLS);
this.revealed = false;
this.marked = false;
setBackground(Color.LIGHT_GRAY);
}
public boolean isMine() {
return mine;
}
public boolean isRevealed() {
return revealed;
}
public void reveal() {
if (revealed || marked) {
return;
}
revealed = true;
if (mine) {
setBackground(Color.RED);
return;
}
int count = 0;
for (int r = Math.max(0, row - 1); r <= Math.min(ROWS - 1, row + 1); r++) {
for (int c = Math.max(0, col - 1); c <= Math.min(COLS - 1, col + 1); c++) {
if (cells[r][c].isMine()) {
count++;
}
}
}
if (count > 0) {
setText(Integer.toString(count));
} else {
setBackground(Color.WHITE);
for (int r = Math.max(0, row - 1); r <= Math.min(ROWS - 1, row + 1); r++) {
for (int c = Math.max(0, col - 1); c <= Math.min(COLS - 1, col + 1); c++) {
cells[r][c].reveal();
}
}
}
}
public void mark() {
if (revealed) {
return;
}
marked = !marked;
if (marked) {
setBackground(Color.YELLOW);
minesLeft--;
} else {
setBackground(Color.LIGHT_GRAY);
minesLeft++;
}
statusbar.setText("Mines left: " + minesLeft);
}
}
public static void main(String[] args) {
EventQueue.invokeLater(() -> {
Minesweeper game = new Minesweeper();
game.setVisible(true);
});
}
}
```