用Java编写一个扫雷游戏程序
时间: 2024-06-09 19:07:21 浏览: 193
好的,下面是一个简单的扫雷游戏程序,使用 Java 编写:
```java
import java.util.Random;
import java.util.Scanner;
public class Minesweeper {
private static final int MINE = 9;
private static final int ROWS = 9;
private static final int COLS = 9;
private static final int NUM_MINES = 10;
private static final int[][] OFFSETS = {{-1, -1}, {-1, 0}, {-1, 1}, {0, -1}, {0, 1}, {1, -1}, {1, 0}, {1, 1}};
private int[][] board;
private int[][] visible;
public Minesweeper() {
board = new int[ROWS][COLS];
visible = new int[ROWS][COLS];
initBoard();
initMines();
}
private void initBoard() {
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
board[i][j] = 0;
visible[i][j] = 0;
}
}
}
private void initMines() {
int count = 0;
Random rand = new Random();
while (count < NUM_MINES) {
int row = rand.nextInt(ROWS);
int col = rand.nextInt(COLS);
if (board[row][col] != MINE) {
board[row][col] = MINE;
count++;
}
}
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
if (board[i][j] != MINE) {
int numMines = 0;
for (int[] offset : OFFSETS) {
int row = i + offset[0];
int col = j + offset[1];
if (row >= 0 && row < ROWS && col >= 0 && col < COLS && board[row][col] == MINE) {
numMines++;
}
}
board[i][j] = numMines;
}
}
}
}
private void printBoard() {
System.out.print(" ");
for (int i = 0; i < COLS; i++) {
System.out.print(i + " ");
}
System.out.println();
System.out.print(" ");
for (int i = 0; i < COLS; i++) {
System.out.print("--");
}
System.out.println();
for (int i = 0; i < ROWS; i++) {
System.out.print(i + "| ");
for (int j = 0; j < COLS; j++) {
if (visible[i][j] == 0) {
System.out.print(". ");
} else if (visible[i][j] == 1) {
System.out.print(board[i][j] + " ");
} else if (visible[i][j] == 2) {
System.out.print("F ");
}
}
System.out.println();
}
}
private boolean isWin() {
int numCovered = 0;
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
if (visible[i][j] == 0) {
numCovered++;
}
}
}
return numCovered == NUM_MINES;
}
private boolean isLose(int row, int col) {
return board[row][col] == MINE;
}
private void reveal(int row, int col) {
if (visible[row][col] != 0) {
return;
}
visible[row][col] = 1;
if (board[row][col] == 0) {
for (int[] offset : OFFSETS) {
int r = row + offset[0];
int c = col + offset[1];
if (r >= 0 && r < ROWS && c >= 0 && c < COLS) {
reveal(r, c);
}
}
}
}
private void flag(int row, int col) {
if (visible[row][col] == 0) {
visible[row][col] = 2;
} else if (visible[row][col] == 2) {
visible[row][col] = 0;
}
}
public void play() {
Scanner scanner = new Scanner(System.in);
while (true) {
printBoard();
System.out.print("Enter row and column (e.g. 0 1) or f to flag: ");
String input = scanner.nextLine();
if (input.equalsIgnoreCase("f")) {
System.out.print("Enter row and column to flag (e.g. 0 1): ");
input = scanner.nextLine();
String[] parts = input.split(" ");
int row = Integer.parseInt(parts[0]);
int col = Integer.parseInt(parts[1]);
flag(row, col);
} else {
String[] parts = input.split(" ");
int row = Integer.parseInt(parts[0]);
int col = Integer.parseInt(parts[1]);
if (isLose(row, col)) {
System.out.println("You lose!");
printBoard();
return;
}
reveal(row, col);
if (isWin()) {
System.out.println("You win!");
printBoard();
return;
}
}
}
}
public static void main(String[] args) {
Minesweeper game = new Minesweeper();
game.play();
}
}
```
这个程序创建了一个 9x9 的棋盘,其中包含了 10 个地雷。玩家可以通过输入坐标来翻开一个方格,如果这个方格中有地雷,游戏就结束了。如果没有地雷,程序会显示这个方格周围的地雷数量。玩家可以通过输入 "f" 和坐标来标记一个方格,表示这个方格中肯定有地雷。当标记的地雷数量和实际地雷数量相同时,玩家胜利。
阅读全文