地雷的代码java
以下是一个简单的地雷游戏的Java代码示例:
import java.util.Random;
import java.util.Scanner;
public class MineSweeper {
private static final int SIZE = 10;
private static final int MINES = 10;
private static char[][] board = new char[SIZE][SIZE];
private static boolean[][] revealed = new boolean[SIZE][SIZE];
private static boolean gameOver = false;
public static void main(String[] args) {
initializeBoard();
placeMines();
playGame();
}
private static void initializeBoard() {
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
board[i][j] = '-';
revealed[i][j] = false;
}
}
}
private static void placeMines() {
Random random = new Random();
int minesPlaced = 0;
while (minesPlaced < MINES) {
int row = random.nextInt(SIZE);
int col = random.nextInt(SIZE);
if (board[row][col] != 'M') {
board[row][col] = 'M';
minesPlaced++;
}
}
}
private static void playGame() {
Scanner scanner = new Scanner(System.in);
while (!gameOver) {
printBoard();
System.out.print("Enter row (0-9): ");
int row = scanner.nextInt();
System.out.print("Enter column (0-9): ");
int col = scanner.nextInt();
if (row < 0 || row >= SIZE || col < 0 || col >= SIZE) {
System.out.println("Invalid input! Please enter valid row and column.");
continue;
}
if (revealed[row][col]) {
System.out.println("This cell has already been revealed!");
continue;
}
revealCell(row, col);
if (board[row][col] == 'M') {
gameOver = true;
System.out.println("Game Over! You hit a mine!");
} else {
if (checkWin()) {
gameOver = true;
System.out.println("Congratulations! You won the game!");
}
}
}
scanner.close();
}
private static void printBoard() {
System.out.println(" 0 1 2 3 4 5 6 7 8 9");
for (int i = 0; i < SIZE; i++) {
System.out.print(i + " |");
for (int j = 0; j < SIZE; j++) {
if (revealed[i][j]) {
System.out.print(board[i][j] + "|");
} else {
System.out.print("-|");
}
}
System.out.println();
}
}
private static void revealCell(int row, int col) {
if (row < 0 || row >= SIZE || col < 0 || col >= SIZE || revealed[row][col]) {
return;
}
revealed[row][col] = true;
if (board[row][col] == 'M') {
return;
}
int count = countNeighboringMines(row, col);
if (count == 0) {
revealCell(row - 1, col - 1);
revealCell(row - 1, col);
revealCell(row - 1, col + 1);
revealCell(row, col - 1);
revealCell(row, col + 1);
revealCell(row + 1, col - 1);
revealCell(row + 1, col);
revealCell(row + 1, col + 1);
} else {
board[row][col] = (char) (count + '0');
}
}
private static int countNeighboringMines(int row, int col) {
int count = 0;
if (isMine(row - 1, col - 1)) count++;
if (isMine(row - 1, col)) count++;
if (isMine(row - 1, col + 1)) count++;
if (isMine(row, col - 1)) count++;
if (isMine(row, col + 1)) count++;
if (isMine(row + 1, col - 1)) count++;
if (isMine(row + 1, col)) count++;
if (isMine(row + 1, col + 1)) count++;
return count;
}
private static boolean isMine(int row, int col) {
return row >= 0 && row < SIZE && col >= 0 && col < SIZE && board[row][col] == 'M';
}
private static boolean checkWin() {
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
if (!revealed[i][j] && board[i][j] != 'M') {
return false;
}
}
}
return true;
}
}
这是一个命令行版本的地雷游戏,游戏板是一个10x10的网格,有10个地雷。你可以通过输入行和列的坐标来选择一个单元格进行揭示。如果揭示到地雷,游戏结束。如果揭示到的单元格没有地雷,将显示周围的地雷数量。当所有没有地雷的单元格都被揭示时,游戏胜利
原文地址: http://www.cveoy.top/t/topic/hIBS 著作权归作者所有。请勿转载和采集!