java扫雷游戏源代码百度云
时间: 2023-07-04 22:26:13 浏览: 139
很抱歉,我无法提供百度云链接,但我可以提供一个Java扫雷游戏的源代码,您可以在自己的电脑上使用它。
```java
import java.util.Random;
import java.util.Scanner;
public class Minesweeper {
private static final char HIDDEN_CELL = '-';
private static final char MINE_CELL = '*';
private static final char MARKED_CELL = '!';
private static final char[] NUMBERED_CELLS = {'0', '1', '2', '3', '4', '5', '6', '7', '8'};
private static final int[] ROW_NEIGHBORS = {-1, -1, -1, 0, 0, 1, 1, 1};
private static final int[] COL_NEIGHBORS = {-1, 0, 1, -1, 1, -1, 0, 1};
private int numRows;
private int numCols;
private int numMines;
private char[][] board;
private boolean[][] revealed;
private boolean[][] marked;
private Minesweeper(int numRows, int numCols, int numMines) {
this.numRows = numRows;
this.numCols = numCols;
this.numMines = numMines;
board = new char[numRows][numCols];
revealed = new boolean[numRows][numCols];
marked = new boolean[numRows][numCols];
initializeBoard();
}
private void initializeBoard() {
for (int i = 0; i < numRows; i++) {
for (int j = 0; j < numCols; j++) {
board[i][j] = HIDDEN_CELL;
}
}
placeMines();
}
private void placeMines() {
Random random = new Random();
int count = 0;
while (count < numMines) {
int row = random.nextInt(numRows);
int col = random.nextInt(numCols);
if (board[row][col] != MINE_CELL) {
board[row][col] = MINE_CELL;
count++;
}
}
}
private boolean isValid(int row, int col) {
return row >= 0 && row < numRows && col >= 0 && col < numCols;
}
private int countNeighborMines(int row, int col) {
int count = 0;
for (int i = 0; i < ROW_NEIGHBORS.length; i++) {
int r = row + ROW_NEIGHBORS[i];
int c = col + COL_NEIGHBORS[i];
if (isValid(r, c) && board[r][c] == MINE_CELL) {
count++;
}
}
return count;
}
private void revealAllMines() {
for (int i = 0; i < numRows; i++) {
for (int j = 0; j < numCols; j++) {
if (board[i][j] == MINE_CELL) {
revealed[i][j] = true;
}
}
}
}
private void revealEmptyCells(int row, int col) {
if (!isValid(row, col) || revealed[row][col] || marked[row][col]) {
return;
}
revealed[row][col] = true;
if (board[row][col] != HIDDEN_CELL) {
return;
}
int neighborMines = countNeighborMines(row, col);
if (neighborMines > 0) {
board[row][col] = NUMBERED_CELLS[neighborMines];
return;
}
board[row][col] = NUMBERED_CELLS[neighborMines];
for (int i = 0; i < ROW_NEIGHBORS.length; i++) {
int r = row + ROW_NEIGHBORS[i];
int c = col + COL_NEIGHBORS[i];
revealEmptyCells(r, c);
}
}
private boolean isGameWon() {
for (int i = 0; i < numRows; i++) {
for (int j = 0; j < numCols; j++) {
if (!revealed[i][j] && board[i][j] != MINE_CELL) {
return false;
}
}
}
return true;
}
private void printBoard(boolean showMines) {
System.out.print(" ");
for (int j = 0; j < numCols; j++) {
System.out.print(j + " ");
}
System.out.println();
System.out.print(" ");
for (int j = 0; j < numCols; j++) {
System.out.print("--");
}
System.out.println();
for (int i = 0; i < numRows; i++) {
System.out.printf("%2d| ", i);
for (int j = 0; j < numCols; j++) {
if (showMines && board[i][j] == MINE_CELL) {
System.out.print(MINE_CELL + " ");
} else if (marked[i][j]) {
System.out.print(MARKED_CELL + " ");
} else if (revealed[i][j]) {
System.out.print(board[i][j] + " ");
} else {
System.out.print(HIDDEN_CELL + " ");
}
}
System.out.println();
}
}
private void play() {
Scanner scanner = new Scanner(System.in);
while (true) {
printBoard(false);
System.out.print("Enter row and column: ");
int row = scanner.nextInt();
int col = scanner.nextInt();
if (!isValid(row, col)) {
System.out.println("Invalid row or column!");
continue;
}
if (board[row][col] == MINE_CELL) {
System.out.println("Game over!");
revealAllMines();
printBoard(true);
break;
} else if (countNeighborMines(row, col) > 0) {
board[row][col] = NUMBERED_CELLS[countNeighborMines(row, col)];
revealed[row][col] = true;
} else {
revealEmptyCells(row, col);
}
if (isGameWon()) {
System.out.println("Congratulations! You won the game!");
break;
}
}
scanner.close();
}
public static void main(String[] args) {
Minesweeper game = new Minesweeper(8, 8, 10);
game.play();
}
}
```
这个代码实现了一个基本的扫雷游戏,可以在控制台中运行。您可以根据自己的需求进行修改和扩展。
阅读全文