用java编写扫雷程序,结束后显示所有雷
时间: 2024-01-24 13:20:48 浏览: 49
好的,下面是一个简单的Java扫雷程序,可以在控制台中运行,游戏结束后会显示所有雷的位置:
```java
import java.util.Random;
import java.util.Scanner;
public class Minesweeper {
private static final int ROWS = 10;
private static final int COLS = 10;
private static final int MINES = 10;
private static final char BLANK = '-';
private static final char MINE = '*';
private static boolean[][] mines = new boolean[ROWS][COLS];
private static char[][] board = new char[ROWS][COLS];
private static Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
init();
play();
showMines();
}
private static void init() {
// 初始化雷区和显示面板
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
mines[i][j] = false;
board[i][j] = BLANK;
}
}
// 随机放置地雷
Random random = new Random();
int count = 0;
while (count < MINES) {
int row = random.nextInt(ROWS);
int col = random.nextInt(COLS);
if (!mines[row][col]) {
mines[row][col] = true;
count++;
}
}
}
private static void play() {
// 游戏循环
int uncovered = 0;
while (uncovered < ROWS * COLS - MINES) {
// 显示当前状态
showBoard();
// 获取用户输入
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 input!");
continue;
}
// 判断是否点到地雷
if (mines[row][col]) {
System.out.println("You lose!");
return;
}
// 统计周围地雷数量
int count = 0;
for (int i = row - 1; i <= row + 1; i++) {
for (int j = col - 1; j <= col + 1; j++) {
if (i >= 0 && i < ROWS && j >= 0 && j < COLS && mines[i][j]) {
count++;
}
}
}
// 显示周围地雷数量
board[row][col] = (char) ('0' + count);
// 如果周围没有地雷,继续递归展开
if (count == 0) {
expand(row, col);
}
uncovered++;
}
System.out.println("You win!");
}
private static void expand(int row, int col) {
// 递归展开周围的空白格
for (int i = row - 1; i <= row + 1; i++) {
for (int j = col - 1; j <= col + 1; j++) {
if (i >= 0 && i < ROWS && j >= 0 && j < COLS && board[i][j] == BLANK) {
// 统计周围地雷数量
int count = 0;
for (int m = i - 1; m <= i + 1; m++) {
for (int n = j - 1; n <= j + 1; n++) {
if (m >= 0 && m < ROWS && n >= 0 && n < COLS && mines[m][n]) {
count++;
}
}
}
// 显示周围地雷数量
board[i][j] = (char) ('0' + count);
// 如果周围没有地雷,继续递归展开
if (count == 0) {
expand(i, j);
}
}
}
}
}
private static void showBoard() {
// 显示当前状态
System.out.println(" 1 2 3 4 5 6 7 8 9 10");
System.out.println(" ---------------------");
for (int i = 0; i < ROWS; i++) {
System.out.print((i + 1) + "| ");
for (int j = 0; j < COLS; j++) {
System.out.print(board[i][j] + " ");
}
System.out.println("|");
}
System.out.println(" ---------------------");
}
private static void showMines() {
// 显示所有地雷
System.out.println(" 1 2 3 4 5 6 7 8 9 10");
System.out.println(" ---------------------");
for (int i = 0; i < ROWS; i++) {
System.out.print((i + 1) + "| ");
for (int j = 0; j < COLS; j++) {
if (mines[i][j]) {
System.out.print(MINE + " ");
} else {
System.out.print(board[i][j] + " ");
}
}
System.out.println("|");
}
System.out.println(" ---------------------");
}
}
```
这个程序使用二维布尔数组来表示雷区,每个元素为true代表该位置有地雷,false代表没有。另外,还有一个二维字符数组用于在控制台中显示游戏状态。在游戏过程中,用户输入行号和列号来选择一个格子,程序会根据格子周围的地雷数量进行相应的处理,如果周围没有地雷,会继续递归展开周围的空白格。游戏结束后,程序会显示所有地雷的位置。
相关推荐
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)