JAVA语言课程设计——扫雷小游戏代码
时间: 2023-10-20 18:08:38 浏览: 202
以下是一个简单的扫雷小游戏的 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 BOMBS = 10;
private static final char COVERED = '-';
private static final char UNCOVERED = ' ';
private static final char BOMB = '*';
private char[][] board;
private boolean[][] bombPositions;
public Minesweeper() {
board = new char[ROWS][COLS];
bombPositions = new boolean[ROWS][COLS];
initBoard();
initBombs();
}
private void initBoard() {
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
board[i][j] = COVERED;
}
}
}
private void initBombs() {
Random random = new Random();
int count = 0;
while (count < BOMBS) {
int row = random.nextInt(ROWS);
int col = random.nextInt(COLS);
if (!bombPositions[row][col]) {
bombPositions[row][col] = true;
count++;
}
}
}
private int countAdjacentBombs(int row, int col) {
int count = 0;
for (int i = row - 1; i <= row + 1; i++) {
for (int j = col - 1; j <= col + 1; j++) {
if (i >= 0 && i < ROWS && j >= 0 && j < COLS && bombPositions[i][j]) {
count++;
}
}
}
return count;
}
public boolean playMove(int row, int col) {
if (bombPositions[row][col]) {
board[row][col] = BOMB;
return false;
}
int adjacentBombs = countAdjacentBombs(row, col);
board[row][col] = (char) ('0' + adjacentBombs);
if (adjacentBombs == 0) {
for (int i = row - 1; i <= row + 1; i++) {
for (int j = col - 1; j <= col + 1; j++) {
if (i >= 0 && i < ROWS && j >= 0 && j < COLS && board[i][j] == COVERED) {
playMove(i, j);
}
}
}
}
return true;
}
public void playGame() {
Scanner scanner = new Scanner(System.in);
boolean gameOver = false;
while (!gameOver) {
printBoard();
System.out.print("Enter row and column (e.g. 1 2): ");
int row = scanner.nextInt() - 1;
int col = scanner.nextInt() - 1;
if (row < 0 || row >= ROWS || col < 0 || col >= COLS) {
System.out.println("Invalid row or column!");
continue;
}
gameOver = !playMove(row, col);
if (checkWin()) {
System.out.println("You win!");
gameOver = true;
}
}
printBoard();
}
private boolean checkWin() {
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
if (board[i][j] == COVERED && !bombPositions[i][j]) {
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++) {
System.out.print(board[i][j] == UNCOVERED && bombPositions[i][j] ? BOMB : board[i][j]);
System.out.print(" ");
}
System.out.println();
}
}
public static void main(String[] args) {
Minesweeper minesweeper = new Minesweeper();
minesweeper.playGame();
}
}
```
代码中定义了游戏的行数,列数和雷数,以及用来表示板块状态的字符常量。在 `Minesweeper` 类中,通过二维数组 `board` 和 `bombPositions` 来表示游戏面板和雷的位置。通过 `initBoard` 和 `initBombs` 方法初始化游戏面板和雷的位置。
在 `countAdjacentBombs` 方法中,根据当前位置的周围八个位置计算雷的个数。
在 `playMove` 方法中,根据当前位置是否有雷计算该位置的状态,并根据周围是否有雷递归更新周围的位置状态。
在 `playGame` 方法中,通过 `Scanner` 类读取玩家的输入,并根据输入更新游戏状态,直到游戏结束。
在 `checkWin` 方法中,检查游戏是否胜利。
在 `printBoard` 方法中,输出游戏面板。
在 `main` 方法中,创建 `Minesweeper` 对象并调用 `playGame` 方法开始游戏。
阅读全文