java扫雷源代码思路_java实现扫雷游戏

初学Java,写了一个扫雷代码来锻炼一下自己的代码能力。

一、代码思路

代码思路很重要,如果事先就想好了代码思路,那么写这一个代码肯定是事半功倍,比在哪里瞎打要强不知道多少。

经过思考,觉得可以创建一个二维数组来记录情况

未翻开的牌:(统一显示 █ )

数组的值 代表

-1 雷

0 旁边没有雷

1 旁边有一个雷

以此类推

翻开的牌则:

if(a[x][y] == 9) System.out.print("?");

if(a[x][y] == 10) System.out.print("?");

if(a[x][y] == 11) System.out.print("①");

if(a[x][y] == 12) System.out.print("②");

if(a[x][y] == 13) System.out.print("③");

if(a[x][y] == 14) System.out.print("④");

if(a[x][y] == 15) System.out.print("⑤");

if(a[x][y] == 16) System.out.print("⑥");

if(a[x][y] == 17) System.out.print("⑦");

if(a[x][y] == 18) System.out.print("⑧");

二、代码主题部分

注意不要越界和不要重复打开

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

int x,y;

int a[][]=new int[10][20];

produce(a);

show(a);

while(true){

x=scanner.nextInt();y=scanner.nextInt();

if(x<=0||y<=0||x>10||y>20) {System.out.println("越界!!");continue;} //防止越界

if(a[x-1][y-1]>=10) {System.out.println("已开!!!");continue;} //防止打开重复

if(bomb(a,x,y)) break;

draw(a,x,y);

show(a);

if(All(a)){ System.out.println("你避过了所有地雷!!!");break; }

}

}

三、函数部分

1.显示函数

打一个方格

public static void show(int a[][]) {

int lie = 0,x =0,y=0;

System.out.print(" ┃1 ");

for (short i = 2; i <= 20; i++){

if(i<9)System.out.print("┃"+i+" ");

else System.out.print("┃"+i);

}

System.out.println();

System.out.print(" ");

for (short i = 0; i <= 20; i++) { //输出第一行

if (i == 0) System.out.print("┏─");

else if (i == 20) System.out.println("┓");

else System.out.print("┳─");

}

for (short i = 1; i < 2 * 10; i++) {

if (i % 2 == 0) {

System.out.print(" ");

for (short j = 0; j <= 20; j++) {

if (j == 0) System.out.print("┣─");

else if (j == 20) System.out.println("┫");

else System.out.print("╋─");

}

}

if (i % 2 == 1) {

if(lie+1 >= 10) System.out.print(lie+1);else System.out.print(" " + (lie+1));lie++;

for (short j = 0; j <= 2*20; j++) {

if (j % 2 == 0) System.out.print("┃");

else {

if(a[x][y] <= 8) System.out.print("█&

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个简单的 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 的二维字符数组来表示游戏板。我们还有一个相同大小的二维布尔数组,用于跟踪哪些方格上有地雷。在每个游戏回合中,玩家输入要揭开的方格的行和列号,程序将检查这个位置是否有地雷。如果是,游戏结束,否则程序将显示该位置周围的地雷数量,并揭开与该位置相邻的所有空白方格。玩家还可以标记他们认为有地雷的方格。如果玩家揭开了所有不是地雷的方格,游戏结束,玩家获胜。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值