用java写一个扫雷程序
时间: 2023-11-30 14:04:55 浏览: 97
扫雷小游戏,和window上自带的一模一样。用java编写,可以在各大系统运行.zip
由于扫雷程序比较复杂,需要涉及到图形界面、算法等方面的知识,因此建议您先学习相关的知识再进行编写。
以下是一个简单的扫雷程序的代码,仅供参考:
```java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Minesweeper extends JFrame implements ActionListener, MouseListener {
private final int ROWS = 10;
private final int COLS = 10;
private final int NUM_MINES = 10;
private JButton[][] board;
private boolean[][] mines;
private int[][] counts;
private boolean[][] revealed;
private boolean[][] flagged;
private int numRevealed;
private int numFlagged;
public Minesweeper() {
setTitle("Minesweeper");
setSize(400, 400);
board = new JButton[ROWS][COLS];
mines = new boolean[ROWS][COLS];
counts = new int[ROWS][COLS];
revealed = new boolean[ROWS][COLS];
flagged = new boolean[ROWS][COLS];
numRevealed = 0;
numFlagged = 0;
initBoard();
initMines();
initCounts();
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private void initBoard() {
JPanel panel = new JPanel(new GridLayout(ROWS, COLS));
for(int r = 0; r < ROWS; r++) {
for(int c = 0; c < COLS; c++) {
board[r][c] = new JButton();
board[r][c].addActionListener(this);
board[r][c].addMouseListener(this);
panel.add(board[r][c]);
}
}
add(panel);
}
private void initMines() {
int numMines = 0;
while(numMines < NUM_MINES) {
int r = (int)(Math.random() * ROWS);
int c = (int)(Math.random() * COLS);
if(!mines[r][c]) {
mines[r][c] = true;
numMines++;
}
}
}
private void initCounts() {
for(int r = 0; r < ROWS; r++) {
for(int c = 0; c < COLS; c++) {
for(int i = -1; i <= 1; i++) {
for(int j = -1; j <= 1; j++) {
if(r+i >= 0 && r+i < ROWS && c+j >= 0 && c+j < COLS && mines[r+i][c+j]) {
counts[r][c]++;
}
}
}
}
}
}
private void reveal(int r, int c) {
if(!revealed[r][c]) {
revealed[r][c] = true;
numRevealed++;
board[r][c].setEnabled(false);
if(mines[r][c]) {
board[r][c].setText("*");
JOptionPane.showMessageDialog(this, "Game Over");
System.exit(0);
}
else {
board[r][c].setText(counts[r][c] + "");
if(counts[r][c] == 0) {
for(int i = -1; i <= 1; i++) {
for(int j = -1; j <= 1; j++) {
if(r+i >= 0 && r+i < ROWS && c+j >= 0 && c+j < COLS && !(i == 0 && j == 0)) {
reveal(r+i, c+j);
}
}
}
}
}
if(numRevealed == ROWS*COLS - NUM_MINES) {
JOptionPane.showMessageDialog(this, "You Win!");
System.exit(0);
}
}
}
private void flag(int r, int c) {
if(!revealed[r][c]) {
if(flagged[r][c]) {
board[r][c].setText("");
flagged[r][c] = false;
numFlagged--;
}
else {
board[r][c].setText("F");
flagged[r][c] = true;
numFlagged++;
}
}
}
public void actionPerformed(ActionEvent e) {
for(int r = 0; r < ROWS; r++) {
for(int c = 0; c < COLS; c++) {
if(e.getSource() == board[r][c]) {
reveal(r, c);
}
}
}
}
public void mousePressed(MouseEvent e) {
for(int r = 0; r < ROWS; r++) {
for(int c = 0; c < COLS; c++) {
if(e.getSource() == board[r][c]) {
if(SwingUtilities.isRightMouseButton(e)) {
flag(r, c);
}
}
}
}
}
public void mouseClicked(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public static void main(String[] args) {
new Minesweeper();
}
}
```
阅读全文