java扫雷游戏的实现

(创作背景:今天下午上java上机课,开始还挺认真地写程序,之后老毛病就又犯了,下意识地“开始”—“程序”—“游戏”—“扫雷”这一熟练地点击,一个扫雷程序呈现在我的面前。可能是最近老研究算法的缘故,恨不得见到的每一样题都用算法实现一遍,自然扫雷这一经典的游戏也不例外。心想这一游戏应该很好实现。想法一旦冒出来就刹不住闸了,立即开始写程序!刚写了几行代码下课铃就响了,走出教室整个人也“魂不守舍”了,思绪全都放在了其实现上。回宿舍带本本到自习室,花了一段时间,最后成功实现了扫雷的游戏。)
(本博客即使是记录个人学习经历,也是抛砖引玉之用,欢迎大牛不吝指教!)


下面是今天(神棍节)写的扫雷代码:

//扫雷游戏的简单实现
//设计思路:遍历“地雷”位置,找到后对其附近的数组进行加1操作

import java.util.Random;
public class SaoLei {
	public static void main(String args[]){
		Random r=new Random();				//产生随机数,为随机产生“地雷”做准备
		int n=15;							//行列数
		int[][] Arr=new int[n][n];
		
		for(int i=0;i<r.nextInt(1000);i++){		//随机产生“地雷”
			Arr[r.nextInt(n)][r.nextInt(n)]=9;	//当数组内的值大于等于9时认为是地雷							
		}
//下面进行判断,思路:遍历“地雷”位置,找到后对其附近的数组进行加1操作
		for(int i=0;i<n;i++){
			for(int j=0;j<Arr[i].length;j++){
				if(Arr[i][j]>8){
					if(j>0){				//保证列不越界
						Arr[i][j-1]++;
					}
					if(j<Arr[i].length-1){			//保证行不越界
						Arr[i][j+1]++;
					}
					if(i>0){				//上一行
						Arr[i-1][j]++;
						if(j>0){
							Arr[i-1][j-1]++;
						}
						if(j<Arr[i].length-1){
							Arr[i-1][j+1]++;
						}
					}
					if(i<n-1){				//下一行
						Arr[i+1][j]++;
						if(j>0){
							Arr[i+1][j-1]++;
						}
						if(j<Arr[i].length-1){
							Arr[i+1][j+1]++;
						}
					}
				}
			}
		}
		for(int i=0;i<n;i++){
			for(int j=0;j<n;j++){
				if(Arr[i][j]>=9)
					System.out.print(" * ");
				else
					System.out.print(" "+Arr[i][j]+" " );
			}
			System.out.println( );
		}
	}
}
//
//输出结果:
//0  0  0  0  0  0  1  *  2  2  *  2  *  *  1 
//1  1  1  1  1  2  2  3  *  2  2  3  4  4  3 
//1  *  1  1  *  2  *  3  2  1  1  *  2  *  * 
//1  1  1  1  1  3  4  *  3  1  3  2  3  2  2 
//1  1  1  0  0  2  *  *  4  *  2  *  1  0  0 
//1  *  1  0  0  2  *  *  4  3  4  2  1  0  0 
//1  1  1  1  1  2  2  2  2  *  *  1  0  1  1 
//1  1  1  1  *  2  1  1  1  2  3  2  2  2  * 
//2  *  1  1  1  2  *  1  0  0  1  *  2  *  2 
//*  2  2  1  2  3  3  3  1  1  1  1  2  1  1 
//1  1  2  *  3  *  *  3  *  2  0  0  1  1  1 
//0  1  3  *  4  3  3  4  *  4  1  0  1  *  2 
//1  2  *  2  2  *  2  3  *  *  2  0  1  3  * 
//*  5  3  2  1  2  3  *  4  *  3  1  1  4  * 
//*  *  *  1  0  1  *  2  2  2  *  1  1  *  * 
//

经验总结:( 人们把自己经历过的失败叫做经验
1.可以借助java对数组默认初始化为0的特点,不必像C/C++那样分别初始化。
2.先写出大框架,再逐步细化。例如,先写出上面4个if语句的雏形,然后在内部将其完善。(分析设计真的很重要!)



  • 6
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
下面是一个简单的 Java 扫雷游戏实现,你可以参考一下: ```java import java.util.Random; import java.util.Scanner; public class MineSweeperGame { private static final int ROWS = 10; // 行数 private static final int COLS = 10; // 列数 private static final int MINES = 10; // 雷数 private static final int MINE = -1; // 雷的标记 private static final int COVERED = 0; // 覆盖的标记 private static final int UNCOVERED = 1; // 已经被翻开的标记 private int[][] board; // 存储游戏状态的二维数组 private boolean gameOver; // 游戏是否结束 private int remaining; // 剩余未翻开的格子数 public MineSweeperGame() { board = new int[ROWS][COLS]; gameOver = false; remaining = ROWS * COLS - MINES; initBoard(); placeMines(); } // 初始化游戏面板 private void initBoard() { for (int i = 0; i < ROWS; i++) { for (int j = 0; j < COLS; j++) { board[i][j] = COVERED; } } } // 随机放置雷 private void placeMines() { Random random = new Random(); int count = 0; while (count < MINES) { int row = random.nextInt(ROWS); int col = random.nextInt(COLS); if (board[row][col] != MINE) { board[row][col] = MINE; count++; } } } // 显示游戏面板 private void displayBoard() { System.out.println(" 0 1 2 3 4 5 6 7 8 9"); System.out.println(" ---------------------"); for (int i = 0; i < ROWS; i++) { System.out.print(i + "| "); for (int j = 0; j < COLS; j++) { if (board[i][j] == COVERED) { System.out.print(". "); } else if (board[i][j] == UNCOVERED) { // 如果是已经被翻开的格子,显示数字 int count = countAdjacentMines(i, j); if (count > 0) { System.out.print(count + " "); } else { System.out.print(" "); } } else { System.out.print("* "); } } System.out.println("|" + i); } System.out.println(" ---------------------"); System.out.println(" 0 1 2 3 4 5 6 7 8 9"); } // 获取相邻的格子中雷的数量 private int countAdjacentMines(int row, int col) { int count = 0; for (int i = Math.max(0, row - 1); i <= Math.min(ROWS - 1, row + 1); i++) { for (int j = Math.max(0, col - 1); j <= Math.min(COLS - 1, col + 1); j++) { if (board[i][j] == MINE) { count++; } } } return count; } // 翻开格子 private void uncover(int row, int col) { if (board[row][col] == UNCOVERED) { return; } if (board[row][col] == MINE) { gameOver = true; return; } board[row][col] = UNCOVERED; remaining--; if (countAdjacentMines(row, col) == 0) { // 如果相邻的格子中没有雷,就翻开它们 for (int i = Math.max(0, row - 1); i <= Math.min(ROWS - 1, row + 1); i++) { for (int j = Math.max(0, col - 1); j <= Math.min(COLS - 1, col + 1); j++) { if (board[i][j] == COVERED) { uncover(i, j); } } } } } // 运行游戏 public void run() { Scanner scanner = new Scanner(System.in); while (!gameOver) { displayBoard(); System.out.print("Enter row and column (separated by a space): "); int row = scanner.nextInt(); int col = scanner.nextInt(); if (row < 0 || row >= ROWS || col < 0 || col >= COLS) { System.out.println("Invalid row or column!"); continue; } if (board[row][col] == UNCOVERED) { System.out.println("The cell has already been uncovered!"); continue; } uncover(row, col); if (remaining == 0) { System.out.println("Congratulations! You have won!"); break; } } displayBoard(); System.out.println("Game over!"); } public static void main(String[] args) { MineSweeperGame game = new MineSweeperGame(); game.run(); } } ``` 这个实现比较简单,只有基本的游戏逻辑,有些细节可能还需要完善。你可以根据自己的需求进行修改和扩展。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值