好的,以下是一个简单的扫雷程序的Java代码:
```java
import java.util.Random;
import java.util.Scanner;
public class Minesweeper {
private static final int ROWS = 9;
private static final int COLS = 9;
private static final int MINES = 10;
private static final char COVERED_CELL = '-';
private static final char MINE_CELL = '*';
private static final char EMPTY_CELL = ' ';
private static final char FLAGGED_CELL = 'F';
private char[][] board;
private boolean[][] revealed;
private boolean[][] flagged;
private int remainingMines;
public Minesweeper() {
board = new char[ROWS][COLS];
revealed = new boolean[ROWS][COLS];
flagged = new boolean[ROWS][COLS];
remainingMines = MINES;
// Initialize the board with covered cells
for (int row = 0; row < ROWS; row++) {
for (int col = 0; col < COLS; col++) {
board[row][col] = COVERED_CELL;
}
}
// Place mines randomly on the board
Random random = new Random();
int minesPlaced = 0;
while (minesPlaced < MINES) {
int row = random.nextInt(ROWS);
int col = random.nextInt(COLS);
if (board[row][col] != MINE_CELL) {
board[row][col] = MINE_CELL;
minesPlaced++;
}
}
}
public void play() {
Scanner scanner = new Scanner(System.in);
while (true) {
printBoard();
System.out.print("Enter row and column (e.g. 3 4): ");
int row = scanner.nextInt() - 1;
int col = scanner.nextInt() - 1;
if (row < 0 || row >= ROWS || col < 0 || col >= COLS) {
System.out.println("Invalid row or column. Try again.");
continue;
}
if (flagged[row][col]) {
System.out.println("This cell is already flagged.");
continue;
}
if (revealed[row][col]) {
System.out.println("This cell is already revealed.");
continue;
}
char cellValue = board