扫雷(二维数组的应用)

扫雷小游戏

游戏胜利规则:如果翻开所有不是雷的草坪,才是胜利

使用9*9的二维数组来表示地图

1)未翻开
2)已翻开(带一个数字)
3)不是雷
4)是雷

两个数组来表示
第一个二维数组表示地图的展示情况,每个位置是翻开还是未翻开的状态(showMap)
第二个数组表示地雷的布局情况,每个位置表示是地雷或者不是(mineMap)

主要思路:
1.创建地图并初始化
2.打印地图
3.玩家翻开指定位置(输入坐标)
4.对玩家的输入进行校验
5.是否踩雷了,如果踩雷,直接游戏失败
6.如果翻开的是最后一个位置,游戏胜利
7.游戏需要继续,翻开的格子会变成一个数字



1.菜单界面

代码如下(示例):

int menu(){
	printf("==========================\n");
	printf("=========1.游戏开始=======\n");
	printf("=========0.游戏结束=======\n");
	printf("==========================\n");
	int choice = 0;
	scanf_s("%d", &choice);
	return choice;
}

2.初始化游戏界面

*表示未翻开,数字表示翻开
约定:‘1’表示是地雷,‘0’表示不是地雷
1.初始化showMap,把每个位置都设为 *
2.初始化mineMap,先把所有位置都设为’0’,再随机产生若干个’1’
3.判断随机生成的雷是否合法
代码如下(示例):

void Init(char showMap[MAX_ROW][MAX_COL], char mineMap[MAX_ROW][MAX_COL]){
	for (int row = 0; row < MAX_ROW; row++){
		for (int col = 0; col < MAX_COL; col++){
			showMap[row][col] = '*';
		}
	}
	for (int row = 0; row < MAX_ROW; row++){
		for (int col = 0; col < MAX_COL; col++){
			mineMap[row][col] = '0';
		}
	}
	int count = MAX_MINE_COUNT;
	while (count > 0){
		int row = rand() % MAX_ROW;
		int col = rand() % MAX_COL;
		if (mineMap[row][col] == '1'){
			continue;
		}
		mineMap[row][col] = '1';
		count--;
	}
}

3.打印游戏界面

void Print(char showMap[MAX_ROW][MAX_COL]){
	printf("  | ");
	for (int col = 0; col < MAX_COL; col++){
		printf("%d",col);
	}
	printf("\n");
	printf("--+---------------\n");
	for (int row = 0; row < MAX_ROW; row++){
		printf(" %d| ",row);
		for (int col = 0; col < MAX_COL; col++){
			printf("%c", showMap[row][col]);
		}
		printf("\n");
	}
}

4.玩家输入

此时玩家输入想要翻开的位置,一定要判断玩家输入的位置是否合法

void Input(char showMap[MAX_ROW][MAX_COL],int* row, int* col){
	while (1){
		printf("请输入要翻开的位置:");
		scanf_s("%d %d", row, col);
		if (*row<0 || *row>MAX_ROW || *col<0 || *col>MAX_COL){
			printf("您的输入有误\n");
			continue;
		}
		if (showMap[*row][*col] != '*'){
			printf("此位置已经翻开了\n");
			continue;
		}
		break;
	}
}

5.计算雷的个数

如果需要实现连续翻开的话,就在这里判定是否是0
递归调用当前的Update函数,递归的时候传入的row,col,当前位置的相邻位置
void Update(char showMap[MAX_ROW][MAX_COL], char mineMap[MAX_ROW][MAX_COL],int row, int col){
	int count = 0;
	for (int r = row - 1; r <= row + 1; r++){
		for (int c = col - 1; c <= col + 1; c++){
			if (row < 0 || row >= MAX_ROW || col < 0 || col >= MAX_COL){
				continue;
			}
			if (mineMap[r][c] == '1'){
				count++;
			}
		}
	}
	showMap[row][col] = count+'0';
}

计算出当前翻开的位置周围有几个雷,首先要熟悉各个坐标之间的关系
在这里插入图片描述


源代码

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define MAX_ROW 9
#define MAX_COL 9
#define MAX_MINE_COUNT 10
int menu(){
	printf("==========================\n");
	printf("=========1.游戏开始=======\n");
	printf("=========0.游戏结束=======\n");
	printf("==========================\n");
	int choice = 0;
	scanf_s("%d", &choice);
	return choice;
}
//*表示未翻开,数字表示翻开
//约定:'1'表示是地雷,'0'表示不是地雷
void Init(char showMap[MAX_ROW][MAX_COL], char mineMap[MAX_ROW][MAX_COL]){
	//初始化showMap,把每个位置都设为*
	for (int row = 0; row < MAX_ROW; row++){
		for (int col = 0; col < MAX_COL; col++){
			showMap[row][col] = '*';
		}
	}
	//2.初始化mineMap,先把所有位置都设为'0'
	//再随机产生若干个'1'
	for (int row = 0; row < MAX_ROW; row++){
		for (int col = 0; col < MAX_COL; col++){
			mineMap[row][col] = '0';
		}
	}
	int count = MAX_MINE_COUNT;
	while (count > 0){
		int row = rand() % MAX_ROW;
		int col = rand() % MAX_COL;
		//判断随机生成的雷是否合法
		if (mineMap[row][col] == '1'){
			continue;
		}
		mineMap[row][col] = '1';
		count--;
	}
}
void Print(char showMap[MAX_ROW][MAX_COL]){
	printf("  | ");
	for (int col = 0; col < MAX_COL; col++){
		printf("%d",col);
	}
	printf("\n");
	printf("--+---------------\n");
	for (int row = 0; row < MAX_ROW; row++){
		printf(" %d| ",row);
		for (int col = 0; col < MAX_COL; col++){
			printf("%c", showMap[row][col]);
		}
		printf("\n");
	}
}
void Input(char showMap[MAX_ROW][MAX_COL],int* row, int* col){
	while (1){
		printf("请输入要翻开的位置:");
		scanf_s("%d %d", row, col);
		if (*row<0 || *row>MAX_ROW || *col<0 || *col>MAX_COL){
			printf("您的输入有误\n");
			continue;
		}
		if (showMap[*row][*col] != '*'){
			printf("此位置已经翻开了\n");
			continue;
		}
		break;
	}
}
void Update(char showMap[MAX_ROW][MAX_COL], char mineMap[MAX_ROW][MAX_COL],int row, int col){
	int count = 0;
	for (int r = row - 1; r <= row + 1; r++){
		for (int c = col - 1; c <= col + 1; c++){
			if (row < 0 || row >= MAX_ROW || col < 0 || col >= MAX_COL){
				continue;
			}
			if (mineMap[r][c] == '1'){
				count++;
			}
		}
	}
	showMap[row][col] = count+'0';
	//如果需要实现连续翻开的话,就在这里判定是否是0
	//递归调用当前的Update函数,递归的时候传入的row,col
	//当前位置的相邻位置
}
void game(){
	//1.创建地图并初始化	
	char showMap[MAX_ROW][MAX_COL];
	char mineMap[MAX_ROW][MAX_COL];
	Init(showMap, mineMap);
	int notMineCount = 0;
	while (1){
		Print(mineMap);
		printf("\n");
		//2.打印地图
		Print(showMap);
        //3.玩家翻开指定位置(输入坐标)
		//4.对玩家的输入进行校验
		int row = 0;
		int col = 0;
		Input(showMap,&row, &col);
		//5.是否踩雷了,如果踩雷,直接游戏失败
		if (mineMap[row][col] =='1'){
			Print(mineMap);
			printf("游戏失败\n");
			break;
		}
		notMineCount++;
		//6.如果翻开的是最后一个位置,游戏胜利
		if (notMineCount == MAX_ROW*MAX_COL - MAX_MINE_COUNT){
			Print(mineMap);
			printf("游戏胜利\n");
			break;
		}
		//7.游戏需要继续,翻开的格子会变成一个数字
		Update(showMap, mineMap, row, col);
	}

}
int main(){
	srand((unsigned int)time(0));
	while (1){
		int choice = menu();
		if (choice == 1){
			game();
		}
		else if (choice == 0){
			printf("goodbye\n");
			break;
		}
		else{
			printf("您的输入有误!\n");
		}
	}
	system("pause");
	return 0;
}
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
下面是一个简单的 C++ 二维数组实现扫雷游戏的例子: ```c++ #include <iostream> #include <cstdlib> #include <ctime> using namespace std; const int ROWS = 10; const int COLS = 10; void initBoard(bool board[][COLS], int mines); void printBoard(bool board[][COLS], bool reveal); int main() { srand(time(nullptr)); // 设置随机数种子 bool board[ROWS][COLS]; int mines = 10; initBoard(board, mines); printBoard(board, false); // 初始状态,不显示地雷 return 0; } void initBoard(bool board[][COLS], int mines) { // 初始化棋盘 for (int i = 0; i < ROWS; ++i) { for (int j = 0; j < COLS; ++j) { board[i][j] = false; } } // 布置地雷 int count = 0; while (count < mines) { int row = rand() % ROWS; int col = rand() % COLS; if (!board[row][col]) { board[row][col] = true; count++; } } } void printBoard(bool board[][COLS], bool reveal) { cout << " "; for (int i = 0; i < COLS; ++i) { cout << i << " "; } cout << endl; for (int i = 0; i < ROWS; ++i) { cout << i << " "; for (int j = 0; j < COLS; ++j) { if (board[i][j]) { if (reveal) { cout << "* "; } else { cout << ". "; } } else { cout << "- "; } } cout << endl; } } ``` 在这个例子中,我们定义了一个 `bool` 类型的二维数组 `board`,表示扫雷游戏的棋盘。数组的每个元素代表一个方格,值为 `true` 表示这个方格上有地雷,否则没有地雷。 `initBoard()` 函数用来初始化棋盘,随机布置指定数量的地雷。 `printBoard()` 函数用来打印棋盘。如果 `reveal` 参数为 `true`,表示要显示所有的地雷,否则只显示未被翻开的方格。 在 `main()` 函数中,我们设置随机数种子,然后调用 `initBoard()` 函数初始化棋盘,并调用 `printBoard()` 函数打印初始状态的棋盘。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

李憨憨_

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

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

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

打赏作者

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

抵扣说明:

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

余额充值