JAVA用数组实现随机布雷,如果是雷显示字符@如果不是雷显示本格周围8格中数字
时间: 2023-12-10 19:37:00 浏览: 71
以下是JAVA代码实现:
```java
import java.util.Random;
public class MineSweeper {
private static final int ROWS = 10; // 行数
private static final int COLS = 10; // 列数
private static final int MINES_NUM = 10; // 雷数
private static final char MINE_SYMBOL = '@'; // 雷的字符
private static final char UNOPENED_SYMBOL = '?'; // 未打开的格子字符
private static final char FLAG_SYMBOL = 'F'; // 标记的字符
private static final int[][] NEIGHBORS = {
{-1, -1}, {-1, 0}, {-1, 1},
{0, -1}, {0, 1},
{1, -1}, {1, 0}, {1, 1}
}; // 周围8个格子的坐标
private static char[][] board = new char[ROWS][COLS];
private static boolean[][] mineBoard = new boolean[ROWS][COLS];
public static void main(String[] args) {
initBoard();
printBoard();
boolean isWin = false;
boolean isLose = false;
while (!isWin && !isLose) {
isWin = checkWin();
if (isWin) {
System.out.println("恭喜你,扫雷成功!");
break;
}
isLose = checkLose();
if (isLose) {
System.out.println("很遗憾,你踩雷了!");
printMineBoard();
break;
}
System.out.print("请输入操作(格式:x y o/f):");
String[] inputs = new java.util.Scanner(System.in).nextLine().split(" ");
int row = Integer.parseInt(inputs[0]) - 1;
int col = Integer.parseInt(inputs[1]) - 1;
char operation = inputs[2].charAt(0);
if (operation == 'o') {
openCell(row, col);
} else if (operation == 'f') {
flagCell(row, col);
} else {
System.out.println("无效操作!");
}
printBoard();
}
}
// 初始化棋盘,随机布雷
private static void initBoard() {
// 初始化棋盘
for (int row = 0; row < ROWS; row++) {
for (int col = 0; col < COLS; col++) {
board[row][col] = UNOPENED_SYMBOL;
}
}
// 随机布雷
Random random = new Random();
int minesCount = 0;
while (minesCount < MINES_NUM) {
int row = random.nextInt(ROWS);
int col = random.nextInt(COLS);
if (!mineBoard[row][col]) {
mineBoard[row][col] = true;
minesCount++;
}
}
}
// 打印棋盘
private static void printBoard() {
System.out.print(" ");
for (int col = 0; col < COLS; col++) {
System.out.print(col + 1 + " ");
}
System.out.println();
for (int row = 0; row < ROWS; row++) {
System.out.print(row + 1 + " ");
for (int col = 0; col < COLS; col++) {
System.out.print(board[row][col] + " ");
}
System.out.println();
}
}
// 打印雷区(调试用)
private static void printMineBoard() {
System.out.print(" ");
for (int col = 0; col < COLS; col++) {
System.out.print(col + 1 + " ");
}
System.out.println();
for (int row = 0; row < ROWS; row++) {
System.out.print(row + 1 + " ");
for (int col = 0; col < COLS; col++) {
if (mineBoard[row][col]) {
System.out.print(MINE_SYMBOL + " ");
} else {
System.out.print(board[row][col] + " ");
}
}
System.out.println();
}
}
// 检查是否胜利
private static boolean checkWin() {
for (int row = 0; row < ROWS; row++) {
for (int col = 0; col < COLS; col++) {
if (!mineBoard[row][col] && board[row][col] == UNOPENED_SYMBOL) {
return false;
}
}
}
return true;
}
// 检查是否失败
private static boolean checkLose() {
for (int row = 0; row < ROWS; row++) {
for (int col = 0; col < COLS; col++) {
if (mineBoard[row][col] && board[row][col] != FLAG_SYMBOL) {
return true;
}
}
}
return false;
}
// 打开一个格子
private static void openCell(int row, int col) {
if (mineBoard[row][col]) { // 踩到雷
board[row][col] = MINE_SYMBOL;
return;
}
int minesCount = getMinesCount(row, col);
if (minesCount == 0) { // 周围没有雷
board[row][col] = '0';
for (int[] neighbor : NEIGHBORS) {
int neighborRow = row + neighbor[0];
int neighborCol = col + neighbor[1];
if (isValidCell(neighborRow, neighborCol) && board[neighborRow][neighborCol] == UNOPENED_SYMBOL) {
openCell(neighborRow, neighborCol);
}
}
} else { // 周围有雷
board[row][col] = (char) ('0' + minesCount);
}
}
// 标记一个格子
private static void flagCell(int row, int col) {
if (board[row][col] == UNOPENED_SYMBOL) { // 未打开的格子才能标记
board[row][col] = FLAG_SYMBOL;
}
}
// 获取一个格子周围的雷数
private static int getMinesCount(int row, int col) {
int count = 0;
for (int[] neighbor : NEIGHBORS) {
int neighborRow = row + neighbor[0];
int neighborCol = col + neighbor[1];
if (isValidCell(neighborRow, neighborCol) && mineBoard[neighborRow][neighborCol]) {
count++;
}
}
return count;
}
// 判断一个坐标是否在棋盘内
private static boolean isValidCell(int row, int col) {
return row >= 0 && row < ROWS && col >= 0 && col < COLS;
}
}
```
代码示例中使用二维数组实现了扫雷的棋盘,其中 `mineBoard` 数组存储了每个格子是否为雷,`board` 数组存储了每个格子的状态(未打开、已标记、已打开等)。在初始化棋盘时,随机生成若干个雷的位置,并将 `mineBoard` 数组相应位置设置为 `true`。
在 `openCell` 方法中,如果当前格子是雷,直接将 `board` 数组对应位置的字符设置为雷的字符 `MINE_SYMBOL`。否则,获取当前格子周围的雷数,如果周围没有雷,则递归打开周围的格子;如果周围有雷,则将当前格子的字符设置为周围雷数的字符。
在 `flagCell` 方法中,只能对未打开的格子进行标记,将 `board` 数组对应位置的字符设置为标记的字符 `FLAG_SYMBOL`。
在 `getMinesCount` 方法中,遍历当前格子周围的八个格子,统计周围的雷数。
最后,通过 `checkWin` 和 `checkLose` 方法判断游戏是否胜利或失败。
阅读全文