扫雷代码java_JAVA实现扫雷游戏
时间: 2023-10-29 11:06:54 浏览: 192
课程设计扫雷_java扫雷小游戏_扫雷运行代码_
3星 · 编辑精心推荐
好的,以下是一个简单的 Java 实现扫雷游戏的代码示例:
```java
import java.util.Random;
import java.util.Scanner;
public class Minesweeper {
private static final int BOARD_SIZE = 10;
private static final int NUM_MINES = 10;
private static final char COVERED_CELL = '*';
private static final char MINE_CELL = 'X';
private static final char EMPTY_CELL = '-';
private static final char FLAGGED_CELL = 'F';
private char[][] gameBoard;
private boolean[][] mineLocations;
private int numUncoveredCells;
public Minesweeper() {
gameBoard = new char[BOARD_SIZE][BOARD_SIZE];
mineLocations = new boolean[BOARD_SIZE][BOARD_SIZE];
numUncoveredCells = 0;
initializeBoard();
placeMines();
}
public void playGame() {
System.out.println("Welcome to Minesweeper!");
Scanner scanner = new Scanner(System.in);
while (true) {
printBoard();
System.out.println("Enter a row and column to uncover (e.g. 3 4), or enter -1 to flag/unflag a cell:");
int row = scanner.nextInt() - 1;
int col = scanner.nextInt() - 1;
if (row == -2 && col == -2) {
System.out.println("Quitting game...");
break;
}
if (row == -1 && col == -1) {
System.out.println("Enter a row and column to flag:");
row = scanner.nextInt() - 1;
col = scanner.nextInt() - 1;
flagCell(row, col);
} else {
if (uncoverCell(row, col)) {
System.out.println("You lose!");
printBoard();
break;
} else if (numUncoveredCells == BOARD_SIZE * BOARD_SIZE - NUM_MINES) {
System.out.println("You win!");
printBoard();
break;
}
}
}
}
private void initializeBoard() {
for (int i = 0; i < BOARD_SIZE; i++) {
for (int j = 0; j < BOARD_SIZE; j++) {
gameBoard[i][j] = COVERED_CELL;
}
}
}
private void placeMines() {
Random rand = new Random();
int minesPlaced = 0;
while (minesPlaced < NUM_MINES) {
int row = rand.nextInt(BOARD_SIZE);
int col = rand.nextInt(BOARD_SIZE);
if (!mineLocations[row][col]) {
mineLocations[row][col] = true;
minesPlaced++;
}
}
}
private void printBoard() {
System.out.print(" ");
for (int i = 1; i <= BOARD_SIZE; i++) {
System.out.print(i + " ");
}
System.out.println();
for (int i = 0; i < BOARD_SIZE; i++) {
System.out.print((i + 1) + " ");
for (int j = 0; j < BOARD_SIZE; j++) {
System.out.print(gameBoard[i][j] + " ");
}
System.out.println();
}
}
private boolean uncoverCell(int row, int col) {
if (mineLocations[row][col]) {
gameBoard[row][col] = MINE_CELL;
return true;
} else {
int numAdjacentMines = countAdjacentMines(row, col);
gameBoard[row][col] = Character.forDigit(numAdjacentMines, 10);
numUncoveredCells++;
if (numAdjacentMines == 0) {
uncoverAdjacentCells(row, col);
}
return false;
}
}
private int countAdjacentMines(int row, int col) {
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 < BOARD_SIZE && j >= 0 && j < BOARD_SIZE && mineLocations[i][j]) {
count++;
}
}
}
return count;
}
private void uncoverAdjacentCells(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 < BOARD_SIZE && j >= 0 && j < BOARD_SIZE && gameBoard[i][j] == COVERED_CELL) {
uncoverCell(i, j);
}
}
}
}
private void flagCell(int row, int col) {
if (gameBoard[row][col] == COVERED_CELL) {
gameBoard[row][col] = FLAGGED_CELL;
} else if (gameBoard[row][col] == FLAGGED_CELL) {
gameBoard[row][col] = COVERED_CELL;
}
}
public static void main(String[] args) {
Minesweeper game = new Minesweeper();
game.playGame();
}
}
```
这个简单的实现中,我们使用了一个 10x10 的二维字符数组来表示游戏板。我们还有一个相同大小的二维布尔数组,用于跟踪哪些方格上有地雷。在每个游戏回合中,玩家输入要揭开的方格的行和列号,程序将检查这个位置是否有地雷。如果是,游戏结束,否则程序将显示该位置周围的地雷数量,并揭开与该位置相邻的所有空白方格。玩家还可以标记他们认为有地雷的方格。如果玩家揭开了所有不是地雷的方格,游戏结束,玩家获胜。
阅读全文