扫雷小游戏的源代码

实现思路

用二维数组做扫雷盘,埋雷函数,扫雷动作函数,自动覆盖空格函数

二维数组的列和行都分别比扫雷基本盘的多两行;

埋雷函数

void set_mine(char board[][cols], int ROW, int COL)
{
	int i=EAZY_MINE;
	while (i)
	{
		int x = rand() % 10;
		int y = rand() % 10;
		if (board[x + 1][y + 1] != '1')
		{
			board[x + 1][y + 1] = '1';
			i--;
		}
	}
}

扫雷动作函数和自动覆盖周围无雷区域函数(注意多出来的两行全为0时,对自动覆盖函数有影响,需要条件限制无雷区域覆盖在基本扫雷盘之外的两行进行覆盖)

//扫雷动作函数
void move(char show[][cols], char mine[][cols], int ROW, int COL)
{
	int i = ROW, j = COL;
	if (show[ROW][COL] != '*')
	{
		cout << "坐标已被选中过,请重新输入"<<endl;
		return;
	}
	
	if (mine[ROW][COL] == '1')
	{
		show[ROW][COL] = '#';
	}
	else
	{
		show[ROW][COL] = mine[i - 1][j - 1] + mine[i - 1][j] + mine[i - 1][j + 1]
			+ mine[i][j - 1] + mine[i][j + 1] + mine[i + 1][j - 1] + mine[i + 1][j]
			+ mine[i + 1][j + 1] - 8 * '0' + '0';
		
		if (show[ROW][COL]=='0')
		{
			show[ROW][COL] = ' ';
			i = ROW - 1;
			j = COL - 1;
			move1(show, mine, i, j);
			move1(show, mine, i, j+1);
			move1(show, mine, i, j+2);
			move1(show, mine, i+1, j);
			move1(show, mine, i+2, j);
			move1(show, mine, i+1, j+2);
			move1(show, mine, i+2, j+1);
			move1(show, mine, i+2, j+2);

	}
			
	}
	
}
//自动覆盖周围函数
void move1(char show[][cols], char mine[][cols], int ROW, int COL)
{
	int i = ROW, j = COL;
	if (show[ROW][COL] != '*')
		return;
	if (mine[ROW][COL] == '1'|| ROW==0 || ROW==rows-1 ||  COL==0 ||COL==cols-1)
		return;
	else
	{
		show[ROW][COL] = mine[i - 1][j - 1] + mine[i - 1][j] + mine[i - 1][j + 1]
			+ mine[i][j - 1] + mine[i][j + 1] + mine[i + 1][j - 1] + mine[i + 1][j]
			+ mine[i + 1][j + 1] - 8 * '0' + '0';

		if (show[ROW][COL] == '0')
		{
			show[ROW][COL] = ' ';
			i = ROW - 1;
			j = COL - 1;

			//上
			for (;j <= COL + 1;j++)
			{
				move1(show, mine, i, j);
			}
			j--;i++;
			//右
			for (;i <= ROW + 1;i++)
			{
				move1(show, mine, i, j);
			}
			i--;j--;
			//下
			for(;j>=COL-1;j--)
				move1(show, mine, i, j);
			j++;i--;
			//左
			for(;i>=ROW-1;i--)
				move1(show, mine, i, j);
			i++;

		}

	}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是简单的 Java 扫雷小游戏源代码: ``` import java.util.Random; import java.util.Scanner; public class Minesweeper { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); Random random = new Random(); int rows, columns, bombs; System.out.print("Enter number of rows: "); rows = scanner.nextInt(); System.out.print("Enter number of columns: "); columns = scanner.nextInt(); System.out.print("Enter number of bombs: "); bombs = scanner.nextInt(); boolean[][] board = new boolean[rows][columns]; int[][] counts = new int[rows][columns]; // randomly place bombs for (int i = 0; i < bombs; i++) { int row, column; do { row = random.nextInt(rows); column = random.nextInt(columns); } while (board[row][column]); board[row][column] = true; for (int dr = -1; dr <= 1; dr++) { for (int dc = -1; dc <= 1; dc++) { if (dr != 0 || dc != 0) { int r = row + dr; int c = column + dc; if (r >= 0 && r < rows && c >= 0 && c < columns) { counts[r][c]++; } } } } } boolean[][] revealed = new boolean[rows][columns]; int remaining = rows * columns - bombs; while (remaining > 0) { // print the board for (int c = 0; c < columns; c++) { System.out.print(" " + c); } System.out.println(); for (int r = 0; r < rows; r++) { System.out.print(r); for (int c = 0; c < columns; c++) { if (revealed[r][c]) { if (board[r][c]) { System.out.print("* "); } else { System.out.print(counts[r][c] + " "); } } else { System.out.print(". "); } } System.out.println(); } // ask for the next move int row, column; do { System.out.print("Enter row and column: "); row = scanner.nextInt(); column = scanner.nextInt(); } while (row < 0 || row >= rows || column < 0 || column >= columns || revealed[row][column]); revealed[row][column] = true; remaining--; // handle the move if (board[row][column]) { System.out.println("Boom! Game over."); break; } else if (counts[row][column] == 0) { // reveal all neighbors for (int dr = -1; dr <= 1; dr++) { for (int dc = -1; dc <= 1; dc++) { if (dr != 0 || dc != 0) { int r = row + dr; int c = column + dc; if (r >= 0 && r < rows && c >= 0 && c < columns && !revealed[r][c]) { revealed[r][c] = true; remaining--; } } } } } } if (remaining == 0) { System.out.println("Congratulations! You win!"); } scanner.close(); } } ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值