用java开发一个扫雷游戏
时间: 2023-05-31 15:04:56 浏览: 121
很好,下面是一个扫雷游戏的Java代码示例。
```java
import java.util.Scanner;
public class Minesweeper {
private int[][] board;
private int size;
private int mines;
private int uncovered;
private boolean gameOver;
public Minesweeper(int size, int mines) {
this.size = size;
this.mines = mines;
this.board = new int[size][size];
this.uncovered = 0;
this.gameOver = false;
placeMines();
calculateNumbers();
}
private void placeMines() {
int count = 0;
while (count < mines) {
int row = (int) (Math.random() * size);
int col = (int) (Math.random() * size);
if (board[row][col] == 0) {
board[row][col] = -1;
count++;
}
}
}
private void calculateNumbers() {
for (int row = 0; row < size; row++) {
for (int col = 0; col < size; col++) {
if (board[row][col] == -1) {
continue;
}
int count = 0;
if (row > 0 && col > 0 && board[row - 1][col - 1] == -1) {
count++;
}
if (row > 0 && board[row - 1][col] == -1) {
count++;
}
if (row > 0 && col < size - 1 && board[row - 1][col + 1] == -1) {
count++;
}
if (col > 0 && board[row][col - 1] == -1) {
count++;
}
if (col < size - 1 && board[row][col + 1] == -1) {
count++;
}
if (row < size - 1 && col > 0 && board[row + 1][col - 1] == -1) {
count++;
}
if (row < size - 1 && board[row + 1][col] == -1) {
count++;
}
if (row < size - 1 && col < size - 1 && board[row + 1][col + 1] == -1) {
count++;
}
board[row][col] = count;
}
}
}
public void play() {
Scanner sc = new Scanner(System.in);
while (!gameOver) {
printBoard();
System.out.print("Enter row and column: ");
int row = sc.nextInt();
int col = sc.nextInt();
if (row < 0 || row >= size || col < 0 || col >= size) {
System.out.println("Invalid input.");
continue;
}
if (board[row][col] == -1) {
gameOver = true;
System.out.println("Game over.");
} else if (board[row][col] == 0) {
uncover(row, col);
checkWin();
} else {
board[row][col] = -board[row][col];
checkWin();
}
}
printBoard();
}
private void printBoard() {
System.out.print(" ");
for (int col = 0; col < size; col++) {
System.out.print(col + " ");
}
System.out.println();
for (int row = 0; row < size; row++) {
System.out.print(row + " ");
for (int col = 0; col < size; col++) {
if (board[row][col] >= 0) {
System.out.print(" ");
}
if (board[row][col] == -1 || board[row][col] >= 0 && board[row][col] <= 8) {
System.out.print(board[row][col]);
}
System.out.print(" ");
}
System.out.println();
}
}
private void uncover(int row, int col) {
if (row < 0 || row >= size || col < 0 || col >= size || board[row][col] < 0) {
return;
}
if (board[row][col] == 0) {
board[row][col] = -1;
uncovered++;
uncover(row - 1, col - 1);
uncover(row - 1, col);
uncover(row - 1, col + 1);
uncover(row, col - 1);
uncover(row, col + 1);
uncover(row + 1, col - 1);
uncover(row + 1, col);
uncover(row + 1, col + 1);
} else {
board[row][col] = -board[row][col];
uncovered++;
}
}
private void checkWin() {
if (uncovered == size * size - mines) {
gameOver = true;
System.out.println("You win!");
}
}
public static void main(String[] args) {
Minesweeper game = new Minesweeper(10, 10);
game.play();
}
}
```
该游戏使用一个二维数组来表示扫雷游戏板,其中每个单元格可以是未揭示的、揭示的、揭示的雷或揭示的数字。-1表示雷,0表示空单元格,1到8表示周围雷的数量。在游戏开始时,随机放置指定数量的雷并计算每个单元格周围的雷的数量。然后,游戏开始,玩家输入要揭示的单元格的行和列。如果这是一个雷,则游戏结束,否则如果它是一个空单元格,则递归地揭示周围的空单元格,否则只需将单元格标记为已揭示。如果玩家揭示所有非雷单元格,则游戏胜利。
阅读全文