C语言--扫雷游戏

先要清楚扫雷游戏是怎么实现的!

步骤:

1.初始化棋盘:一个全是*的棋盘(给玩家看到的画面),另外一个埋雷的棋盘(0代表没有雷)                          C语言--扫雷游戏_第1张图片           C语言--扫雷游戏_第2张图片

 2.埋雷:使用rand,srand,time函数在二维数组中随机生成n个雷(1代表地雷)C语言--扫雷游戏_第3张图片

3.排雷:通过输入二维数组的坐标,来判断是不是有雷,碰到雷,结束游戏,否则继续。

具体步骤:

我先把我的game.h头文件讲解一下:

 ROW代表行,COL代表列 -->用在打印棋盘,埋雷,排雷的数组中(在传参的时候ROW,COL,用小写的row,col接收)C语言--扫雷游戏_第4张图片ROW和COL(数字9)

ROWS,COLS代表实际要布置二维数组的大小(初始化棋盘)(在二维数组的[ ][ ]方括号中使用ROWS,COLS)

set是放字符*和字符0,为了初始化成什么样的棋盘

 C语言--扫雷游戏_第5张图片

 

第一步:初始化棋盘:

1.棋盘的大小:

如果需要9*9的位置来玩扫雷,那需要布置多大的二维数组呢?

在你统计周围有多少个雷的时候,如果是在角落,去统计,会造成越界访问。所以二维数组要设置大一点。对于9*9的棋盘,就需要11*11的二维数组。C语言--扫雷游戏_第6张图片为了防止越界访问!

2.二维数组的类型:

因为需要*的棋盘,那我们统一用char类型。如果一个二维数组是int类型,一个是char类型,那么存放数组的类型太多,容易产生歧义!

(因为我用了game.h头文件,game.c和test.c文件,所以在下述代码有些封装起来,在文章的最后我会把完整的代码发出来)

//封装成函数
void InitBoard(char arr[ROWS][COLS], int rows, int cols,char set)
{
	int i = 0;
	for (i = 0; i <= rows; i++)
	{
		int j = 0;
		for (j = 0; j <= cols; j++)
		{
			arr[i][j] = set;
		}
	}
}

	//初始化(Init)棋盘(Board)
	InitBoard(mine,ROWS,COLS,'0');//'0',埋雷的棋盘
	InitBoard(show,ROWS,COLS,'*');//'*',玩家看到的棋盘

 第二步:打印棋盘

void DisplayBoard(char arr[ROWS][COLS], int row, int col)
{
	for (int i = 0; i <= row; i++)//这个for循环是打印棋盘周围的数字,方便查找坐标
	{
		printf("%d ", i);
	}
	printf("\n");
	for (i = 1; i <= row; i++)
	{
		printf("%d ", i);//也是打印数字
		for (int j = 1; j <= col; j++)
		{
			printf("%c ", arr[i][j]);
		}
		printf("\n");
	}
}

 C语言--扫雷游戏_第7张图片

 第二步:埋雷

需要用到rand,srand,time函数

rand是根据系统中的随机数种子,测试伪随机数。不是人为改变系统的随机数的种子,那就不会变。所以每次rand的数都是同一个数。

srand的作用是改变系统的随机数种子。srand(1)  srand(2)每一次的设置都会改变随机数种子。但是太麻烦了,每次都要改变srand的参数。

time就是为了解决srand的参数问题。time(NULL)每一秒的返回值都不一样。

time函数的返回值是时间戳。

时间戳:  时间戳是指格林威治时间自1970年1月1日(00:00:00 GMT)至当前时间的总秒数。

所以每一秒的返回值都不一样。srand和time函数结合使用就可以随时改变系统的随机数种子。然后在使用rand函数就可以返回伪随机数。

代码的模板:

#include

#include

#include

srand((unsigned int)time(NULL));

int x=rand();

printf("%d",x);

void SetMine(char arr[ROWS][COLS], int row, int col)
{
	//步骤10个雷
	int count = COUNT;
	while (count)
	{
		int x = rand()%row+1;//1-9
		int y = rand()%col+1;//1-9
		if (arr[x][y] == '0')
		{
			arr[x][y] = '1';
			count--;
		}
	}
}

第三步:排查地雷

输入坐标,如果在埋雷的二维数组中等于1,爆炸。否则,统计周围雷的个数,打印棋盘。

//统计周围雷的个数,注意字符0和数字0之间的区别
static int GetMineCount(char mine[ROWS][COLS],int x,int y)
{
	int count = 0;
	for (int i = x - 1; i <= x + 1; i++)
	{
		for (int j = y - 1; j <= y + 1; j++)
		{
			count += (mine[i][j]-'0');
		}
	}
	return count;
}

//排查雷
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
	int x, y;
	int win = 0;
	while (win= 1 && x <= row && x >= 1 && x <= col)
		{
			if (mine[x][y] == '1')
			{
				printf("很遗憾,你被炸死了!\n");
				DisplayBoard(mine, ROW, COL);
				break;
			}
			else
			{
				int n =GetMineCount(mine, x, y);
				show[x][y] = n+'0';//把周围雷的个数给那个坐标的位置,取代*
				DisplayBoard(show, ROW, COL);//打印给玩家看的那个棋盘
			}
		}
		else
		{
			printf("坐标非法,请重新输入坐标!\n");
		}
	}
	if (win == row * col - COUNT)//COUNT是我在头文件中define的埋雷的个数
	{
		printf("恭喜你,排雷成功\n");
		DisplayBoard(mine, ROW, COL);
	}
}

完整的代码:

//在game.h文件中
#pragma once
#include
#include
#include
#define ROW 9
#define COL 9

#define ROWS ROW+2
#define COLS COL+2

#define COUNT 10

//初始化棋盘
void InitBoard(char arr[ROWS][COLS],int rows,int cols,char set);

//打印棋盘
void DisplayBoard(char arr[ROWS][COLS],int row,int col );

//布置雷
void SetMine(char arr[ROWS][COLS], int row, int col);

//排除雷
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);
//在game.c文件中
#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"
void InitBoard(char arr[ROWS][COLS], int rows, int cols,char set)
{
	int i = 0;
	for (i = 0; i <= rows; i++)
	{
		int j = 0;
		for (j = 0; j <= cols; j++)
		{
			arr[i][j] = set;
		}
	}
}
void DisplayBoard(char arr[ROWS][COLS], int row, int col)
{
	int i = 1;
	for (i = 0; i <= row; i++)
	{
		printf("%d ", i);
	}
	printf("\n");
	for (i = 1; i <= row; i++)
	{
		int j = 1;
		printf("%d ", i);
		for (j = 1; j <= col; j++)
		{
			printf("%c ", arr[i][j]);
		}
		printf("\n");
	}
}


void SetMine(char arr[ROWS][COLS], int row, int col)
{
	//步骤10个雷
	int count = COUNT;
	while (count)
	{
		int x = rand()%row+1;//1-9
		int y = rand()%col+1;//1-9
		if (arr[x][y] == '0')
		{
			arr[x][y] = '1';
			count--;
		}
	}
}

static int GetMineCount(char mine[ROWS][COLS],int x,int y)
{
	int count = 0;
	for (int i = x - 1; i <= x + 1; i++)
	{
		for (int j = y - 1; j <= y + 1; j++)
		{
			count += (mine[i][j]-'0');
		}
	}
	return count;
}
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
	int x, y;
	int win = 0;
	while (win= 1 && x <= row && x >= 1 && x <= col)
		{
			if (mine[x][y] == '1')
			{
				printf("很遗憾,你被炸死了!\n");
				DisplayBoard(mine, ROW, COL);
				break;
			}
			else
			{
				int n =GetMineCount(mine, x, y);
				show[x][y] = n+'0';
				DisplayBoard(show, ROW, COL);
			}
		}
		else
		{
			printf("坐标非法,请重新输入坐标!\n");
		}
	}
	if (win == row * col - COUNT)
	{
		printf("恭喜你,排雷成功\n");
		DisplayBoard(mine, ROW, COL);
	}
}

 

 

//在test.c文件中
#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"
menu()
{
	printf("*****************************\n");
	printf("*******  1.play    **********\n");
	printf("*******  0.exit    **********\n");
	printf("*****************************\n");
}
void game()
{
	char mine[ROWS][COLS];
	char show[ROWS][COLS];
	//初始化(Init)棋盘(Board)
	InitBoard(mine,ROWS,COLS,'0');//'0'
	InitBoard(show,ROWS,COLS,'*');//'*'
	//打印棋盘
	DisplayBoard(show,ROW,COL);
	DisplayBoard(mine,ROW,COL);
	
	//布置雷
	SetMine(mine, ROW, COL);
	DisplayBoard(show, ROW, COL);
	//排查雷
	FindMine(mine,show,ROW,COL);
}
int main()
{
	srand((unsigned int)time(NULL));
	int input = 0;  
	do
	{
		menu();
		printf("请输入:");
		scanf("%d", &input);
		switch (input)
		{
		case 1:
			game();
			break;//break是跳出switch,而不是do...while
		case 0:
			printf("退出游戏\n");
			break;
		default:
			printf("选择错误,请重新选择\n");
			break;
		}
	}while(input);
	return 0;
}

你可能感兴趣的:(游戏)