扫雷游戏java完整代码
时间: 2023-10-18 13:50:49 浏览: 126
以下是一个简单的Java扫雷游戏的完整代码:
```java
import java.util.Scanner;
public class Minesweeper {
private int[][] board; // 扫雷棋盘
private boolean[][] revealed; // 标记哪些格子已经被翻开
private boolean[][] flagged; // 标记哪些格子被插旗
private int size; // 棋盘大小
private int mines; // 地雷数量
public Minesweeper(int size, int mines) {
this.size = size;
this.mines = mines;
this.board = new int[size][size];
this.revealed = new boolean[size][size];
this.flagged = new boolean[size][size];
this.initializeBoard();
this.initializeMines();
}
// 初始化棋盘
private void initializeBoard() {
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
board[i][j] = 0;
revealed[i][j] = false;
flagged[i][j] = false;
}
}
}
// 在棋盘上随机放置地雷
private void initializeMines() {
int count = 0;
while (count < mines) {
int i = (int) (Math.random() * size);
int j = (int) (Math.random() * size);
if (board[i][j] != -1) {
board[i][j] = -1;
count++;
}
}
// 计算每个格子周围地雷的数量
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
if (board[i][j] == -1) {
for (int x = Math.max(i - 1, 0); x <= Math.min(i + 1, size - 1); x++) {
for (int y = Math.max(j - 1, 0); y <= Math.min(j + 1, size - 1); y++) {
if (board[x][y] != -1) {
board[x][y]++;
}
}
}
}
}
}
}
// 判断某个位置是否在棋盘内
private boolean inBounds(int i, int j) {
return i >= 0 && i < size && j >= 0 && j < size;
}
// 翻开某个格子
private void reveal(int i, int j) {
if (inBounds(i, j) && !revealed[i][j] && !flagged[i][j]) {
revealed[i][j] = true;
if (board[i][j] == 0) {
for (int x = Math.max(i - 1, 0); x <= Math.min(i + 1, size - 1); x++) {
for (int y = Math.max(j - 1, 0); y <= Math.min(j + 1, size - 1); y++) {
reveal(x, y);
}
}
}
}
}
// 插旗某个格子
private void flag(int i, int j) {
if (inBounds(i, j) && !revealed[i][j]) {
flagged[i][j] = !flagged[i][j];
}
}
// 打印棋盘
private void printBoard(boolean showMines) {
System.out.print(" ");
for (int j = 0; j < size; j++) {
System.out.print(j + " ");
}
System.out.println();
for (int i = 0; i < size; i++) {
System.out.print(i + " ");
for (int j = 0; j < size; j++) {
if (showMines && board[i][j] == -1) {
System.out.print("* ");
} else if (flagged[i][j]) {
System.out.print("F ");
} else if (!revealed[i][j]) {
System.out.print(". ");
} else if (board[i][j] == -1) {
System.out.print("X ");
} else {
System.out.print(board[i][j] + " ");
}
}
System.out.println();
}
}
public void play() {
Scanner scanner = new Scanner(System.in);
while (true) {
printBoard(false);
System.out.print("Enter an action (r/f) and a coordinate (row col): ");
String[] input = scanner.nextLine().split(" ");
String action = input[0];
int i = Integer.parseInt(input[1]);
int j = Integer.parseInt(input[2]);
if (action.equals("r")) {
if (board[i][j] == -1) {
printBoard(true);
System.out.println("Game over!");
break;
} else {
reveal(i, j);
}
} else if (action.equals("f")) {
flag(i, j);
}
int count = 0;
for (int x = 0; x < size; x++) {
for (int y = 0; y < size; y++) {
if (revealed[x][y]) {
count++;
}
}
}
if (count == size * size - mines) {
printBoard(true);
System.out.println("You win!");
break;
}
}
scanner.close();
}
public static void main(String[] args) {
Minesweeper game = new Minesweeper(10, 10);
game.play();
}
}
```
这个程序创建了一个大小为10x10,含有10个地雷的扫雷游戏。您可以更改`Minesweeper`构造函数的参数来调整游戏的大小和地雷数量。这个程序只是一个简单的实现,如果您想要更完整的游戏体验,您可以添加计时器、计分等功能。
阅读全文