学完C语言必需会做的一个题“扫雷游戏”(主要知识点是函数)

1、主函数

主要打印出游戏菜单

int main()
{
	int select = 1;
	while (select)
	{
		printf("*******扫雷小游戏*******\n");
		printf("*        1.Play        *\n");
		printf("*        0.Exit        *\n");
		printf("************************\n");
		printf("请输入你的选择:>");
		scanf("%d", &select);
		if (select == 0)
			break;
		if (select != 1)
		{
			printf("输入有误,请重新输入...\n");
			continue;
		}
		StartGame();
	}
	printf("退出游戏,欢迎下次使用...\n");
	return 0;
}

2、设置两个棋牌

show[ROWS][COLS]、mine[ROWS][COLS]分别是一个不埋雷一个埋雷;此函数呢,我们是将该游戏所有的函数写在这个函数里面,通过我们所学的调用函数来一一实现游戏功能。 

void StartGame()
{
	char show[ROWS][COLS];
	char mine[ROWS][COLS];
	
	//初始化棋盘
	InitBoard(mine, ROWS, COLS, '0');//字符0代表无雷,1代表雷
	InitBoard(show, ROWS, COLS, '*');
	
	//埋雷
	SetMine(mine, ROW, COL);
	//显示棋盘
	DisplayBoard(show, ROW, COL);
	//扫雷
	FindMine(mine, show, ROW, COL);
}

3、对两个棋牌进行初始化show[ROW][COL]被初始化为‘*’,mine[ROW][COL]被初始化为‘0’;我们为何不用数字1和0呢?因为show[ROW][COL]用字符'*'进行初始化,我们只用一个类型char,如果用数字0、1需要int类型会给后面带来麻烦。

​
void InitBoard(char board[ROWS][COLS], int row, int col, char set)
{
	for (int i = 0; i < row; i++)
	{
		for (int j = 0; j < col; j++)
		{
			board[i][j] = set;
		}
	}
}

​

 3、对其函数进行埋雷

 count是埋雷的个数,x,y是埋雷大的位置。为了是x,y是随机的,我们需要用到随机函数rand()、srand().

void SetMine(char mine[ROWS][COLS], int row, int col)
{
	int count = EASY_COUNT;
	srand(time(0));
	while (count)
	{
		int x = rand() % row + 1;
		int y = rand() % col + 1;
		if (mine[x][y] == '0')
		{
			mine[x][y] = '1';
			count--;
		}
	}
}

 4、弄出埋好雷的棋牌,并将其打印出来

void DisplayBoard(char board[ROWS][COLS], int row, int col)
{
	for (int i = 0; i <= col; i++)
		printf("%d ", i);
	printf("\n");
	for (int i = 1; i <= row; i++)
	{
		printf("%d ", i);
		for (int j = 1; j <= col; j++)
		{
			printf("%c ", board[i][j]);
		}
		printf("\n");
	}
}

 5、下面是游戏玩家进行排雷的操作

void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
	int win = 0;
	int x, y;
	while (win < col * row - EASY_COUNT)
	{
		printf("请输入排雷的位置:>");
		scanf("%d %d", &x, &y);
		if ((x >= 1 && x <= row) && (y >= 1 && y <= col))
		{
			if (mine[x][y] == '1')
			{
				DisplayBoard(mine, ROW, COL);
				printf("很遗憾,排雷失败,你被炸糊了...\n");
				break;
			}
			int n = GetMineCount(mine, x, y);
			show[x][y] = n + '0';
			system("cls");
			DisplayBoard(show, ROW, COL);
			win++;
		}
		else
			printf("非法输入,请重新输入...\n");
		
	}
	if (win >= col * row - EASY_COUNT)
	{
		printf("恭喜你,排雷成功...\n");
		DisplayBoard(mine, ROW, COL);
	}
}

 6、计算输入坐标(x,y)位置周围8个位置有多少雷。因为有雷我用的字符’1‘设置的,因此将这8个位置的值相加起来,就能知道雷的个数

int GetMineCount(char mine[ROWS][COLS], int x, int y)
{
	return mine[x - 1][y - 1] + mine[x - 1][y] + mine[x - 1][y + 1]
	+ mine[x][y - 1] + mine[x][y + 1] +
	mine[x + 1][y - 1] + mine[x + 1][y] + mine[x + 1][y + 1] - (8 * '0');
}

7、分享整个源代码

#define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

//用于棋盘的显示
#define ROW 9
#define COL 9

//用于真实的处理
#define ROWS ROW+2
#define COLS COL+2

#define EASY_COUNT 50

void StartGame();

void InitBoard(char board[ROWS][COLS], int row, int col, char set);
void SetMine(char mine[ROWS][COLS], int row, int col);
void DisplayBoard(char board[ROWS][COLS], int row, int col);
void FindMine(char mine[ROWS][COLS],char show[ROWS][COLS], int row, int col);
int GetMineCount(char mine[ROWS][COLS], int x, int y);

int main()
{
	int select = 1;
	while (select)
	{
		printf("*******扫雷小游戏*******\n");
		printf("*        1.Play        *\n");
		printf("*        0.Exit        *\n");
		printf("************************\n");
		printf("请输入你的选择:>");
		scanf("%d", &select);
		if (select == 0)
			break;
		if (select != 1)
		{
			printf("输入有误,请重新输入...\n");
			continue;
		}
		StartGame();
	}
	printf("退出游戏,欢迎下次使用...\n");
	return 0;
}
void StartGame()
{
	char show[ROWS][COLS];
	char mine[ROWS][COLS];
	
	//初始化棋盘
	InitBoard(mine, ROWS, COLS, '0');//字符0代表无雷,1代表雷
	InitBoard(show, ROWS, COLS, '*');
	
	//埋雷
	SetMine(mine, ROW, COL);
	//显示棋盘
	DisplayBoard(show, ROW, COL);
	//扫雷
	FindMine(mine, show, ROW, COL);
}
void InitBoard(char board[ROWS][COLS], int row, int col, char set)
{
	for (int i = 0; i < row; i++)
	{
		for (int j = 0; j < col; j++)
		{
			board[i][j] = set;
		}
	}
}

void SetMine(char mine[ROWS][COLS], int row, int col)
{
	int count = EASY_COUNT;
	srand(time(0));
	while (count)
	{
		int x = rand() % row + 1;
		int y = rand() % col + 1;
		if (mine[x][y] == '0')
		{
			mine[x][y] = '1';
			count--;
		}
	}
}
void DisplayBoard(char board[ROWS][COLS], int row, int col)
{
	for (int i = 0; i <= col; i++)
		printf("%d ", i);
	printf("\n");
	for (int i = 1; i <= row; i++)
	{
		printf("%d ", i);
		for (int j = 1; j <= col; j++)
		{
			printf("%c ", board[i][j]);
		}
		printf("\n");
	}
}
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
	int win = 0;
	int x, y;
	while (win < col * row - EASY_COUNT)
	{
		printf("请输入排雷的位置:>");
		scanf("%d %d", &x, &y);
		if ((x >= 1 && x <= row) && (y >= 1 && y <= col))
		{
			if (mine[x][y] == '1')
			{
				DisplayBoard(mine, ROW, COL);
				printf("很遗憾,排雷失败,你被炸糊了...\n");
				break;
			}
			int n = GetMineCount(mine, x, y);
			show[x][y] = n + '0';
			system("cls");
			DisplayBoard(show, ROW, COL);
			win++;
		}
		else
			printf("非法输入,请重新输入...\n");
		
	}
	if (win >= col * row - EASY_COUNT)
	{
		printf("恭喜你,排雷成功...\n");
		DisplayBoard(mine, ROW, COL);
	}
}
int GetMineCount(char mine[ROWS][COLS], int x, int y)
{
	return mine[x - 1][y - 1] + mine[x - 1][y] + mine[x - 1][y + 1]
	+ mine[x][y - 1] + mine[x][y + 1] +
	mine[x + 1][y - 1] + mine[x + 1][y] + mine[x + 1][y + 1] - (8 * '0');
}

 希望这些能够给大家带来一点点收获!!!!!

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值