扫雷小游戏

  设计思路:我们可以先用一个数组储存游戏刚开始的界面,可以用*表示,然后再用另一个数组储存炸弹的位置和附近雷的数量,雷可以用#表示,当开始玩游戏的时候选择空的位置可以用while循环来不断查找没有雷的位置,直到排除完成,你就可以赢了,如果在过程中点开了雷的位置,那么很遗憾,你就输了。

  接下来我们就先说一下初始化界面和选择进入游戏还是退出的代码:

void game()
{
	srand((unsigned int)time(NULL));
	int ge;
	do
	{
	    system("cls");
	    menu();
	    printf("请输入你的选项: ");
	    scanf("%d", &ge);
	    if (ge == 1)
	    {
		   playing();
	    }
		else if (ge==0)
		{
			printf("你即将退出游戏\n");
			Sleep(500);
		}
		else
		{
			printf("违法输入,请重新输入\n");
			system("pause");
		}
	} while (ge);
}

第一行那个srand函数是为了做了一个随机种子,方便后边布雷用,然后就用了while循环不断选择,使可以重复玩游戏。

那我们再看一下玩游戏的代码吧:

void playing()
{
	char map[Hs][Ls];
	char show[Hs][Ls];
	init(map,H,L,'0');
	init(show, H, L, '*');
	setbomb(map, H, L);
	print(show, H, L);
	look(map,show, H, L);
	system("pause");
}

init这两个函数就是初始化map数组和show数组,这俩个数组一个使给用户看的,另一个是布置的地雷,我们看下代码:

void init(char a[Hs][Ls], int h, int l, char c)
{
	for (int i = 1; i <= h; i++)
	{
		for (int j = 1; j <= l; j++)
		{
			a[i][j] = c;
		}
	}
}

setbomb函数是设置地雷的:

void setbomb(char map[Hs][Ls], int h, int l)
{
	int count = 0;
	while (count!=BOMB)
	{
		int x = rand() % h + 1;
		int y = rand() % l + 1;
		if (map[x][y] == '0')
		{
			map[x][y] = '#';
			count++;
		}
	}
	round(map, h, l);
}

在这个函数中我们又rand函数生成坐标,然后把是空的,就是没有放雷的放上雷,当生成够雷的数量时我们跳出,然后我们再用round函数查找一下周围地雷的数量,我们再看下round()函数的代码:

void round(char map[Hs][Ls], int h, int l)
{
	for (int i = 1; i <= h; i++)
	{
		for (int j = 1; j <= l; j++)
		{
			if (map[i][j]=='0')
			{
				int count = 0;
				if (map[i - 1][j - 1] == '#')
				{
					count++;
				}
				if (map[i - 1][j] == '#')
				{
					count++;
				}
				if (map[i - 1][j + 1] == '#')
				{
					count++;
				}
				if (map[i][j - 1] == '#')
				{
					count++;
				}
				if (map[i][j + 1] == '#')
				{
					count++;
				}
				if (map[i + 1][j - 1] == '#')
				{
					count++;
				}
				if (map[i + 1][j] == '#')
				{
					count++;
				}
				if (map[i + 1][j + 1] == '#')
				{
					count++;
				}
				map[i][j] = '0' + count;
			}
		}
	}
}

我们雷也布置好了那不得开始打印一下我们要给用户展示的界面,我们看一下print()函数

void print(char show[Hs][Ls], int h, int l)
{
	for (int  i = 0; i <= h; i++)
	{
		printf("%d ", i);
	}
	printf("\n");
	for (int i = 1; i <= h; i++)
	{
		printf("%d ", i);
		for (int j = 1; j <= l; j++)
		{
			printf("%c ", show[i][j]);
		}
		printf("\n");
	}
}

那我们现在不得开始把想看的位置看看是不是雷,那么我们就来看一下look()函数

void look(char map[Hs][Ls], char show[Hs][Ls],int h, int l)
{
	int x;
	int y;
	while (1)
	{
		printf("请输入你的坐标位置:");
		scanf("%d %d", &x, &y);
		if ((map[x][y] != '#'))
		{
			if (show[x][y] == '*')
			{
				show[x][y] = map[x][y];
				df(map, show, h, l, x, y);
				system("cls");
				print(show, h, l);
				int a = iswin(show, h, l);
				if (a==BOMB)
				{
					printf("恭喜你,你赢了!\n");
					break;
				}
			}
		}
		else
		{
			show[x][y] = map[x][y];
			system("cls");
			print(show, h, l);
			Sleep(500);
			system("cls");
			printf("很遗憾你输了!\n");
			print(map, h, l);
			break;
		}
	}
}

在这里我又写了一个可以连续翻出来多个空格,我们看下df函数

void df(char map[Hs][Ls], char show[Hs][Ls], int h, int l,int x,int y)
{
	int a[4][2] = {
		{0,1},
		{1,0},
		{0,-1},
		{-1,0}
	};
	static int b[Hs][Ls];
	static int m = 0;
	if (m == 1)
	{
		m = 0;
		return;
	}
	for (int i = 0; i < 4; i++)
	{
		int tx = x + a[i][0];
		int ty = y + a[i][1];
		if (tx<1 || ty<1 || tx>h || ty>l || b[tx][ty]==1)
		{
			continue;
		}
		if (map[tx][ty]!='#')
		{
			show[tx][ty] = map[tx][ty];
			b[tx][ty] = 1;
			if (map[tx][ty]!='0')
			{
				m = 1;
			}
			df(map, show, h, l, tx, ty);
		}
		else
		{
			return;
		}
	}
}

那我们最后如何判断赢呢,那就来我们的iswin函数

int iswin(char show[Hs][Ls], int h, int l)
{
	int count = 0;
	for (int i = 1; i <= h; i++)
	{
		for (int j = 0; j <= l; j++)
		{
			if (show[i][j]=='*')
			{
				count++;
			}
		}
	}
	return count;
}

最后我们可以在某些地方加上system("cls")函数,它可以把屏幕清空,和system("pause"),它可以使你按一下任意键在继续往下执行。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值