用Java写一个扫雷游戏
时间: 2023-05-11 10:06:45 浏览: 77
基于java开发的扫雷小游戏
好的,以下是一个简单的Java扫雷游戏的代码示例:
import java.util.Random;
import java.util.Scanner;
public class Minesweeper {
private static final int ROWS = 10;
private static final int COLS = 10;
private static final int MINES = 10;
private static final int[][] NEIGHBORS = {{-1, -1}, {-1, 0}, {-1, 1}, {0, -1}, {0, 1}, {1, -1}, {1, 0}, {1, 1}};
private int[][] board;
private boolean[][] revealed;
private boolean[][] flagged;
private boolean gameOver;
public Minesweeper() {
board = new int[ROWS][COLS];
revealed = new boolean[ROWS][COLS];
flagged = new boolean[ROWS][COLS];
gameOver = false;
initializeBoard();
}
private void initializeBoard() {
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 numMines = 0;
for (int[] neighbor : NEIGHBORS) {
int row = i + neighbor[0];
int col = j + neighbor[1];
if (row >= 0 && row < ROWS && col >= 0 && col < COLS && board[row][col] == -1) {
numMines++;
}
}
board[i][j] = numMines;
}
}
}
}
public void play() {
Scanner scanner = new Scanner(System.in);
while (!gameOver) {
printBoard();
System.out.print("Enter row and column (e.g. 3 4): ");
int row = scanner.nextInt() - 1;
int col = scanner.nextInt() - 1;
if (row < 0 || row >= ROWS || col < 0 || col >= COLS) {
System.out.println("Invalid input!");
continue;
}
if (flagged[row][col]) {
System.out.println("This cell is flagged!");
continue;
}
if (revealed[row][col]) {
System.out.println("This cell is already revealed!");
continue;
}
if (board[row][col] == -1) {
gameOver = true;
System.out.println("Game over! You hit a mine!");
} else {
revealCell(row, col);
if (checkWin()) {
gameOver = true;
System.out.println("Congratulations! You win!");
}
}
}
printBoard();
}
private void revealCell(int row, int col) {
revealed[row][col] = true;
if (board[row][col] == 0) {
for (int[] neighbor : NEIGHBORS) {
int newRow = row + neighbor[0];
int newCol = col + neighbor[1];
if (newRow >= 0 && newRow < ROWS && newCol >= 0 && newCol < COLS && !revealed[newRow][newCol]) {
revealCell(newRow, newCol);
}
}
}
}
private 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;
}
private void printBoard() {
System.out.print(" ");
for (int j = 0; j < COLS; j++) {
System.out.print(j + 1 + " ");
}
System.out.println();
for (int i = 0; i < ROWS; i++) {
System.out.print(i + 1 + " ");
for (int j = 0; j < COLS; j++) {
if (flagged[i][j]) {
System.out.print("F ");
} else if (!revealed[i][j]) {
System.out.print(". ");
} else if (board[i][j] == -1) {
System.out.print("* ");
} else {
System.out.print(board[i][j] + " ");
}
}
System.out.println();
}
}
public static void main(String[] args) {
Minesweeper game = new Minesweeper();
game.play();
}
}
希望这个代码示例能够帮助你!
阅读全文