用Java语言生成扫雷游戏的代码
时间: 2023-10-29 19:06:57 浏览: 36
以下是用Java语言生成扫雷游戏的示例代码:
```java
import java.util.Random;
import java.util.Scanner;
public class Minesweeper {
private static int ROWS = 10;
private static int COLS = 10;
private static int MINES = 10;
private static int[][] board = new int[ROWS][COLS];
private static boolean[][] revealed = new boolean[ROWS][COLS];
private static boolean gameOver = false;
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Welcome to Minesweeper!");
System.out.print("Enter number of rows (default 10): ");
ROWS = scanner.nextInt();
System.out.print("Enter number of columns (default 10): ");
COLS = scanner.nextInt();
System.out.print("Enter number of mines (default 10): ");
MINES = scanner.nextInt();
initBoard();
while (!gameOver) {
printBoard();
int row = getRow(scanner);
int col = getCol(scanner);
if (board[row][col] == -1) {
System.out.println("Game over! You hit a mine.");
gameOver = true;
} else {
reveal(row, col);
if (checkWin()) {
System.out.println("Congratulations! You won!");
gameOver = true;
}
}
}
}
private static void initBoard() {
Random random = new Random();
int count = 0;
while (count < MINES) {
int row = random.nextInt(ROWS);
int col = random.nextInt(COLS);
if (board[row][col] != -1) {
board[row][col] = -1;
count++;
}
}
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
if (board[i][j] != -1) {
int countMines = 0;
if (i > 0 && j > 0 && board[i - 1][j - 1] == -1) countMines++;
if (i > 0 && board[i - 1][j] == -1) countMines++;
if (i > 0 && j < COLS - 1 && board[i - 1][j + 1] == -1) countMines++;
if (j > 0 && board[i][j - 1] == -1) countMines++;
if (j < COLS - 1 && board[i][j + 1] == -1) countMines++;
if (i < ROWS - 1 && j > 0 && board[i + 1][j - 1] == -1) countMines++;
if (i < ROWS - 1 && board[i + 1][j] == -1) countMines++;
if (i < ROWS - 1 && j < COLS - 1 && board[i + 1][j + 1] == -1) countMines++;
board[i][j] = countMines;
}
}
}
}
private static void printBoard() {
System.out.print(" ");
for (int j = 0; j < COLS; j++) {
System.out.print(j + " ");
}
System.out.println();
for (int i = 0; i < ROWS; i++) {
System.out.print(i + " ");
for (int j = 0; j < COLS; j++) {
if (revealed[i][j]) {
if (board[i][j] == -1) System.out.print("* ");
else System.out.print(board[i][j] + " ");
} else {
System.out.print(". ");
}
}
System.out.println();
}
}
private static int getRow(Scanner scanner) {
int row;
do {
System.out.print("Enter row: ");
row = scanner.nextInt();
} while (row < 0 || row >= ROWS);
return row;
}
private static int getCol(Scanner scanner) {
int col;
do {
System.out.print("Enter column: ");
col = scanner.nextInt();
} while (col < 0 || col >= COLS);
return col;
}
private static void reveal(int row, int col) {
if (revealed[row][col]) return;
revealed[row][col] = true;
if (board[row][col] == 0) {
if (row > 0 && col > 0) reveal(row - 1, col - 1);
if (row > 0) reveal(row - 1, col);
if (row > 0 && col < COLS - 1) reveal(row - 1, col + 1);
if (col > 0) reveal(row, col - 1);
if (col < COLS - 1) reveal(row, col + 1);
if (row < ROWS - 1 && col > 0) reveal(row + 1, col - 1);
if (row < ROWS - 1) reveal(row + 1, col);
if (row < ROWS - 1 && col < COLS - 1) reveal(row + 1, col + 1);
}
}
private static boolean checkWin() {
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
if (!revealed[i][j] && board[i][j] != -1) return false;
}
}
return true;
}
}
```
该代码基于控制台实现,可根据需要进行修改和优化。