```java
import java.util.Random;
import java.util.Scanner;
public class Minesweeper {
private static final int BOARD_SIZE = 10;
private static final int MINE_COUNT = 10;
private static final char MINE = '*';
private static final char UNOPENED = '-';
private static final char OPENED = ' ';
private static final char FLAG = 'F';
private char[][] board;
private boolean[][] isMine;
public Minesweeper() {
board = new char[BOARD_SIZE][BOARD_SIZE];
isMine = new boolean[BOARD_SIZE][BOARD_SIZE];
initializeBoard();
placeMines();
}
private void initializeBoard() {
for (int i = 0; i < BOARD_SIZE; i++) {
for (int j = 0; j < BOARD_SIZE; j++) {
board[i][j] = UNOPENED;
}
}
}
private void placeMines() {
Random random = new Random();
int minesPlaced = 0;
while (minesPlaced < MINE_COUNT) {
int x = random.nextInt(BOARD_SIZE);
int y = random.nextInt(BOARD_SIZE);
if (!isMine[x][y]) {
isMine[x][y] = true;
minesPlaced++;
}
}
}
public void playGame() {
Scanner scanner = new Scanner(System.in);
while (true) {
printBoard();
System.out.print("Enter row and column (e.g., '1 2'): ");
int row = scanner.nextInt() - 1;
int col = scanner.nextInt() - 1;
if (isValidMove(row, col)) {
if (isMine[row][col]) {
gameOver();
break;
} else {
int mineCount = countAdjacentMines(row, col);
board[row][col] = (char) (mineCount + '0');
if (mineCount == 0) {
openAdjacentCells(row, col);
}
}
} else {
System.out.println("Invalid move. Try again.");
}
if (isWin()) {
printBoard();
System.out.println("Congratulations! You won!");
break;
}
}
}
private boolean isValidMove(int row, int col) {
return row >= 0 && row < BOARD_SIZE && col >= 0 && col < BOARD_SIZE && board[row][col] == UNOPENED;
}
private int countAdjacentMines(int row, int col) {
int count = 0;
for (int dr = -1; dr <= 1; dr++) {
for (int dc = -1; dc <= 1; dc++) {
int r = row + dr;
int c = col + dc;
if (r >= 0 && r < BOARD_SIZE && c >= 0 && c < BOARD_SIZE && isMine[r][c]) {
count++;
}
}
}
return count;
}
private void openAdjacentCells(int row, int col) {
for (int dr = -1; dr <= 1; dr++) {
for (int dc = -1; dc <= 1; dc++) {
int r = row + dr;
int c = col + dc;
if (r >= 0 && r < BOARD_SIZE && c >= 0 && c < BOARD_SIZE && board[r][c] == UNOPENED) {
int mineCount = countAdjacentMines(r, c);
board[r][c] = (char) (mineCount + '0');
if (mineCount == 0) {
openAdjacentCells(r, c);
}
}
}
}
}
private void printBoard() {
System.out.println("Minesweeper Board:");
for (int i = 0; i < BOARD_SIZE; i++) {
for (int j = 0; j < BOARD_SIZE; j++) {
System.out.print(board[i][j] + " ");
}
System.out.println();
}
}
private void gameOver() {
System.out.println("Game Over! You hit a mine.");
for (int i = 0; i < BOARD_SIZE; i++) {
for (int j = 0; j < BOARD_SIZE; j++) {
if (isMine[i][
j]) {
board[i][j] = MINE;
}
}
}
printBoard();
}
private boolean isWin() {
for (int i = 0; i < BOARD_SIZE; i++) {
for (int j = 0; j < BOARD_SIZE; j++) {
if (board[i][j] == UNOPENED && !isMine[i][j]) {
return false;
}
}
}
return true;
}
public static void main(String[] args) {
Minesweeper game = new Minesweeper();
game.playGame();
}
}
```
运行结果