用Java写一个扫雷小游戏
时间: 2023-12-08 12:04:52 浏览: 153
Java做的扫雷小游戏.zip
好,下面是一个简单的 Java 扫雷小游戏的实现:
```java
import java.util.*;
public class Minesweeper {
private int[][] board;
private boolean[][] revealed;
private int rows;
private int cols;
private int numMines;
public Minesweeper(int rows, int cols, int numMines) {
this.board = new int[rows][cols];
this.revealed = new boolean[rows][cols];
this.rows = rows;
this.cols = cols;
this.numMines = numMines;
// initialize board with mines
Random rand = new Random();
int count = 0;
while (count < numMines) {
int randRow = rand.nextInt(rows);
int randCol = rand.nextInt(cols);
if (board[randRow][randCol] != -1) {
board[randRow][randCol] = -1;
count++;
}
}
// fill in numbers for non-mine cells
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;
}
}
}
}
public 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();
}
}
public boolean click(int row, int col) {
if (board[row][col] == -1) {
System.out.println("Game over!");
return true;
}
revealed[row][col] = true;
if (board[row][col] == 0) {
if (row > 0 && col > 0 && !revealed[row-1][col-1]) click(row-1, col-1);
if (row > 0 && !revealed[row-1][col]) click(row-1, col);
if (row > 0 && col < cols-1 && !revealed[row-1][col+1]) click(row-1, col+1);
if (col > 0 && !revealed[row][col-1]) click(row, col-1);
if (col < cols-1 && !revealed[row][col+1]) click(row, col+1);
if (row < rows-1 && col > 0 && !revealed[row+1][col-1]) click(row+1, col-1);
if (row < rows-1 && !revealed[row+1][col]) click(row+1, col);
if (row < rows-1 && col < cols-1 && !revealed[row+1][col+1]) click(row+1, col+1);
}
return false;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter number of rows: ");
int rows = scanner.nextInt();
System.out.print("Enter number of columns: ");
int cols = scanner.nextInt();
System.out.print("Enter number of mines: ");
int numMines = scanner.nextInt();
Minesweeper game = new Minesweeper(rows, cols, numMines);
game.printBoard();
boolean gameOver = false;
while (!gameOver) {
System.out.print("Enter row to click: ");
int row = scanner.nextInt();
System.out.print("Enter column to click: ");
int col = scanner.nextInt();
gameOver = game.click(row, col);
game.printBoard();
}
scanner.close();
}
}
```
这个扫雷小游戏用的是命令行界面,玩家需要输入行号和列号来选择要点击的方块。游戏会在命令行界面中显示,并且会在游戏结束时显示“Game over!”。
阅读全文