java扫雷游戏代码
时间: 2023-07-03 17:15:25 浏览: 141
这是一个简单的 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 MINE_SYMBOL = '*';
private static final char HIDDEN_SYMBOL = '-';
private static final char FLAG_SYMBOL = 'F';
private static char[][] board = new char[BOARD_SIZE][BOARD_SIZE];
private static boolean[][] mineLocations = new boolean[BOARD_SIZE][BOARD_SIZE];
private static int numUncovered;
public static void main(String[] args) {
initializeBoard();
addMines();
playGame();
}
private static void initializeBoard() {
for (int i = 0; i < BOARD_SIZE; i++) {
for (int j = 0; j < BOARD_SIZE; j++) {
board[i][j] = HIDDEN_SYMBOL;
}
}
numUncovered = 0;
}
private static void addMines() {
Random rand = new Random();
int count = 0;
while (count < NUM_MINES) {
int x = rand.nextInt(BOARD_SIZE);
int y = rand.nextInt(BOARD_SIZE);
if (!mineLocations[x][y]) {
mineLocations[x][y] = true;
count++;
}
}
}
private static void playGame() {
boolean gameOver = false;
while (!gameOver) {
printBoard();
int[] move = getMove();
int x = move[0];
int y = move[1];
char action = (char) move[2];
if (action == 'u') {
if (mineLocations[x][y]) {
System.out.println("Game over!");
gameOver = true;
} else {
uncoverCell(x, y);
if (numUncovered == (BOARD_SIZE * BOARD_SIZE - NUM_MINES)) {
System.out.println("You win!");
gameOver = true;
}
}
} else if (action == 'f') {
flagCell(x, y);
}
}
}
private static void printBoard() {
System.out.print(" ");
for (int i = 0; i < BOARD_SIZE; i++) {
System.out.print(i + " ");
}
System.out.println();
for (int i = 0; i < BOARD_SIZE; i++) {
System.out.print(i + " ");
for (int j = 0; j < BOARD_SIZE; j++) {
System.out.print(board[i][j] + " ");
}
System.out.println();
}
}
private static int[] getMove() {
Scanner scanner = new Scanner(System.in);
int[] move = new int[3];
System.out.print("Enter row: ");
move[0] = scanner.nextInt();
System.out.print("Enter column: ");
move[1] = scanner.nextInt();
System.out.print("Enter action (u for uncover, f for flag): ");
move[2] = scanner.next().charAt(0);
return move;
}
private static void uncoverCell(int x, int y) {
if (board[x][y] != HIDDEN_SYMBOL) {
return;
}
int numMines = countAdjacentMines(x, y);
board[x][y] = (numMines == 0) ? ' ' : (char) ('0' + numMines);
numUncovered++;
if (numMines == 0) {
if (x > 0) {
uncoverCell(x - 1, y);
}
if (x < BOARD_SIZE - 1) {
uncoverCell(x + 1, y);
}
if (y > 0) {
uncoverCell(x, y - 1);
}
if (y < BOARD_SIZE - 1) {
uncoverCell(x, y + 1);
}
}
}
private static int countAdjacentMines(int x, int y) {
int count = 0;
if (x > 0 && y > 0 && mineLocations[x - 1][y - 1]) {
count++;
}
if (x > 0 && mineLocations[x - 1][y]) {
count++;
}
if (x > 0 && y < BOARD_SIZE - 1 && mineLocations[x - 1][y + 1]) {
count++;
}
if (y > 0 && mineLocations[x][y - 1]) {
count++;
}
if (y < BOARD_SIZE - 1 && mineLocations[x][y + 1]) {
count++;
}
if (x < BOARD_SIZE - 1 && y > 0 && mineLocations[x + 1][y - 1]) {
count++;
}
if (x < BOARD_SIZE - 1 && mineLocations[x + 1][y]) {
count++;
}
if (x < BOARD_SIZE - 1 && y < BOARD_SIZE - 1 && mineLocations[x + 1][y + 1]) {
count++;
}
return count;
}
private static void flagCell(int x, int y) {
if (board[x][y] == HIDDEN_SYMBOL) {
board[x][y] = FLAG_SYMBOL;
} else if (board[x][y] == FLAG_SYMBOL) {
board[x][y] = HIDDEN_SYMBOL;
}
}
}
```
这个代码是一个基本的扫雷游戏实现,你可以根据需要进行修改和改进。
阅读全文