java实现扫雷小游戏【完整版】

📩很多朋友都问我学完基础知识以后怎样提高编程水平?当然是刷题啦!很多小伙伴都在纠结从哪里开始,今天给大家推荐一个身边朋友都在使用的刷题网站:点击进入牛客网刷题吧! 各大互联网大厂面试真题,成体系的分类题库,从入门到进阶,分类练习哦!


大家好,我是orangemilk_,哈哈,学习Java已经到一个阶段啦,今天我们使用GUI来写一个扫雷小游戏吧!


  • 188
    点赞
  • 437
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 192
    评论
好的,以下是一个简单的 Java 实现扫雷游戏代码示例: ```java import java.util.Random; import java.util.Scanner; public class Minesweeper { private static final int BOARD_SIZE = 10; private static final int NUM_MINES = 10; private static final char COVERED_CELL = '*'; private static final char MINE_CELL = 'X'; private static final char EMPTY_CELL = '-'; private static final char FLAGGED_CELL = 'F'; private char[][] gameBoard; private boolean[][] mineLocations; private int numUncoveredCells; public Minesweeper() { gameBoard = new char[BOARD_SIZE][BOARD_SIZE]; mineLocations = new boolean[BOARD_SIZE][BOARD_SIZE]; numUncoveredCells = 0; initializeBoard(); placeMines(); } public void playGame() { System.out.println("Welcome to Minesweeper!"); Scanner scanner = new Scanner(System.in); while (true) { printBoard(); System.out.println("Enter a row and column to uncover (e.g. 3 4), or enter -1 to flag/unflag a cell:"); int row = scanner.nextInt() - 1; int col = scanner.nextInt() - 1; if (row == -2 && col == -2) { System.out.println("Quitting game..."); break; } if (row == -1 && col == -1) { System.out.println("Enter a row and column to flag:"); row = scanner.nextInt() - 1; col = scanner.nextInt() - 1; flagCell(row, col); } else { if (uncoverCell(row, col)) { System.out.println("You lose!"); printBoard(); break; } else if (numUncoveredCells == BOARD_SIZE * BOARD_SIZE - NUM_MINES) { System.out.println("You win!"); printBoard(); break; } } } } private void initializeBoard() { for (int i = 0; i < BOARD_SIZE; i++) { for (int j = 0; j < BOARD_SIZE; j++) { gameBoard[i][j] = COVERED_CELL; } } } private void placeMines() { Random rand = new Random(); int minesPlaced = 0; while (minesPlaced < NUM_MINES) { int row = rand.nextInt(BOARD_SIZE); int col = rand.nextInt(BOARD_SIZE); if (!mineLocations[row][col]) { mineLocations[row][col] = true; minesPlaced++; } } } private void printBoard() { System.out.print(" "); for (int i = 1; i <= BOARD_SIZE; i++) { System.out.print(i + " "); } System.out.println(); for (int i = 0; i < BOARD_SIZE; i++) { System.out.print((i + 1) + " "); for (int j = 0; j < BOARD_SIZE; j++) { System.out.print(gameBoard[i][j] + " "); } System.out.println(); } } private boolean uncoverCell(int row, int col) { if (mineLocations[row][col]) { gameBoard[row][col] = MINE_CELL; return true; } else { int numAdjacentMines = countAdjacentMines(row, col); gameBoard[row][col] = Character.forDigit(numAdjacentMines, 10); numUncoveredCells++; if (numAdjacentMines == 0) { uncoverAdjacentCells(row, col); } return false; } } private int countAdjacentMines(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 < BOARD_SIZE && j >= 0 && j < BOARD_SIZE && mineLocations[i][j]) { count++; } } } return count; } private void uncoverAdjacentCells(int row, int col) { for (int i = row - 1; i <= row + 1; i++) { for (int j = col - 1; j <= col + 1; j++) { if (i >= 0 && i < BOARD_SIZE && j >= 0 && j < BOARD_SIZE && gameBoard[i][j] == COVERED_CELL) { uncoverCell(i, j); } } } } private void flagCell(int row, int col) { if (gameBoard[row][col] == COVERED_CELL) { gameBoard[row][col] = FLAGGED_CELL; } else if (gameBoard[row][col] == FLAGGED_CELL) { gameBoard[row][col] = COVERED_CELL; } } public static void main(String[] args) { Minesweeper game = new Minesweeper(); game.playGame(); } } ``` 这个简单的实现中,我们使用了一个 10x10 的二维字符数组来表示游戏板。我们还有一个相同大小的二维布尔数组,用于跟踪哪些方格上有地雷。在每个游戏回合中,玩家输入要揭开的方格的行和列号,程序将检查这个位置是否有地雷。如果是,游戏结束,否则程序将显示该位置周围的地雷数量,并揭开与该位置相邻的所有空白方格。玩家还可以标记他们认为有地雷的方格。如果玩家揭开了所有不是地雷的方格,游戏结束,玩家获胜。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 192
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

橙 子_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值