java简单实现扫雷游戏
时间: 2023-10-18 16:06:08 浏览: 89
下面是一个简单的 Java 扫雷游戏实现,你可以参考一下:
```java
import java.util.Random;
import java.util.Scanner;
public class MineSweeperGame {
private static final int ROWS = 10; // 行数
private static final int COLS = 10; // 列数
private static final int MINES = 10; // 雷数
private static final int MINE = -1; // 雷的标记
private static final int COVERED = 0; // 覆盖的标记
private static final int UNCOVERED = 1; // 已经被翻开的标记
private int[][] board; // 存储游戏状态的二维数组
private boolean gameOver; // 游戏是否结束
private int remaining; // 剩余未翻开的格子数
public MineSweeperGame() {
board = new int[ROWS][COLS];
gameOver = false;
remaining = ROWS * COLS - MINES;
initBoard();
placeMines();
}
// 初始化游戏面板
private void initBoard() {
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
board[i][j] = COVERED;
}
}
}
// 随机放置雷
private void placeMines() {
Random random = new Random();
int count = 0;
while (count < MINES) {
int row = random.nextInt(ROWS);
int col = random.nextInt(COLS);
if (board[row][col] != MINE) {
board[row][col] = MINE;
count++;
}
}
}
// 显示游戏面板
private void displayBoard() {
System.out.println(" 0 1 2 3 4 5 6 7 8 9");
System.out.println(" ---------------------");
for (int i = 0; i < ROWS; i++) {
System.out.print(i + "| ");
for (int j = 0; j < COLS; j++) {
if (board[i][j] == COVERED) {
System.out.print(". ");
} else if (board[i][j] == UNCOVERED) {
// 如果是已经被翻开的格子,显示数字
int count = countAdjacentMines(i, j);
if (count > 0) {
System.out.print(count + " ");
} else {
System.out.print(" ");
}
} else {
System.out.print("* ");
}
}
System.out.println("|" + i);
}
System.out.println(" ---------------------");
System.out.println(" 0 1 2 3 4 5 6 7 8 9");
}
// 获取相邻的格子中雷的数量
private int countAdjacentMines(int row, int col) {
int count = 0;
for (int i = Math.max(0, row - 1); i <= Math.min(ROWS - 1, row + 1); i++) {
for (int j = Math.max(0, col - 1); j <= Math.min(COLS - 1, col + 1); j++) {
if (board[i][j] == MINE) {
count++;
}
}
}
return count;
}
// 翻开格子
private void uncover(int row, int col) {
if (board[row][col] == UNCOVERED) {
return;
}
if (board[row][col] == MINE) {
gameOver = true;
return;
}
board[row][col] = UNCOVERED;
remaining--;
if (countAdjacentMines(row, col) == 0) {
// 如果相邻的格子中没有雷,就翻开它们
for (int i = Math.max(0, row - 1); i <= Math.min(ROWS - 1, row + 1); i++) {
for (int j = Math.max(0, col - 1); j <= Math.min(COLS - 1, col + 1); j++) {
if (board[i][j] == COVERED) {
uncover(i, j);
}
}
}
}
}
// 运行游戏
public void run() {
Scanner scanner = new Scanner(System.in);
while (!gameOver) {
displayBoard();
System.out.print("Enter row and column (separated by a space): ");
int row = scanner.nextInt();
int col = scanner.nextInt();
if (row < 0 || row >= ROWS || col < 0 || col >= COLS) {
System.out.println("Invalid row or column!");
continue;
}
if (board[row][col] == UNCOVERED) {
System.out.println("The cell has already been uncovered!");
continue;
}
uncover(row, col);
if (remaining == 0) {
System.out.println("Congratulations! You have won!");
break;
}
}
displayBoard();
System.out.println("Game over!");
}
public static void main(String[] args) {
MineSweeperGame game = new MineSweeperGame();
game.run();
}
}
```
这个实现比较简单,只有基本的游戏逻辑,有些细节可能还需要完善。你可以根据自己的需求进行修改和扩展。
阅读全文