c语言写一个扫雷小游戏

电脑上的扫雷游戏大家都玩过,我们今天就用c语言来写一下这个男女老少皆宜的小游戏。

和三子棋游戏一样,我们首先要有雷盘,不过这次我们需要三个雷盘,一个用来存放我们随机生成的雷,一个用来让玩家排雷,还有一个用来测试我们的展开雷盘。
首先我们来初始化雷盘。

void InitBoard(char arr[ROWS][COLS], int row, int col,char ch)//初始化雷盘
{
	int i = 0;
	int j = 0;
	for (i = 0; i < row; i++)
	{
		for (j = 0; j < col; j++)
		{
			arr[i][j] = ch;
		}

	}
	

}

初始化雷盘之后,我们需要把雷盘打印出来。为了让玩家更好的找到坐标,我们在雷盘外面先打印出坐标。

void DisplayBoard(char arr[ROWS][COLS], int row, int col)//打印雷盘
{
	int i = 0;
	int j = 0;

	for (i = 0; i <= col; i++)
	{
		printf("%d ",i);

	}
	printf("\n");
	for (i = 1; i <= row; i++)
	{
		printf("%d ",i);
		for (j = 1; j <= col; j++)
		{
			printf("%c ",arr[i][j]);
		}
		printf("\n");
	}
}

有了雷盘之后,我们就要开始随机放雷了,我们今天写的是9*9 的雷盘,所以我们大概需要10个雷就够了,多了难度会容易炸死。放雷的个数为了调整,先定义在game.h里。

void SetBoard(char arr[ROWS][COLS], int row, int col)//放雷
{
	int count = SET;//放雷的个数


	while (count)
	{
		int x = rand() % row + 1;
		int y = rand() % col + 1;
		if (arr[x][y] == '0')
		{
			arr[x][y] = '1';
			count--;
		}
	}
}

放完雷后我们来到了核心部分,排雷。

void FindBoard(char mine[ROWS][COLS], char show[ROWS][COLS], char test[ROWS][COLS], int row, int col)//排雷
{
	int x = 0;
	int y = 0;
	int win = row * col - SET;//剩余未点开的雷盘数
	int count = 0;//记录第几次排雷


	while (1)
	{
		DisplayBoard(show, row, col);
		printf("请输入坐标>:");
		scanf("%d%d", &x, &y);


		if (x >= 1 && x <= row && y >= 1 && y <= col)//判断坐标合法
		{
			count++;

			if (mine[x][y] == '1')
			{
				if (count == 1)//如果第一次排雷,不要让玩家炸死,重新设置雷盘
				{
					SetBoard(mine, ROW, COL);
					mine[x][y] = '0';
					OpenBoard(show, mine, test, x, y);
				}
				else
				{
					printf("踩雷了!\n");
					DisplayBoard(mine, row, col);
					break;
				}
			}
			else
			{
				win--;
				OpenBoard(show, mine, test, x, y);

			}
		}
		else
		{
			printf("非法坐标,请重新输入:\n");

		}
		if (win == 0)
		{
			printf("恭喜你赢了!\n");
			DisplayBoard(mine, row, col);
			break;
		}
	}
}

上面代码看到,我们需要统计你点开的地方周围雷数,所以我们还需要一个计数的函数。

void GetCount(char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y)//计算点开坐标周围有几个雷
{
	show[x][y] = mine[x - 1][y] +
		mine[x - 1][y - 1] +
		mine[x][y - 1] +
		mine[x + 1][y - 1] +
		mine[x + 1][y] +
		mine[x + 1][y + 1] +
		mine[x][y + 1] +
		mine[x - 1][y + 1]
		- 7 * '0';
	
}

我们想点开雷盘时可以展开一片来判断周围的雷数。

void OpenBoard(char show[ROWS][COLS], char mine[ROWS][COLS], char test[ROWS][COLS], int x, int y)
{
	test[x][y] = '1';
	if (x > 0 && x < ROWS - 1 && y>0 && y < COLS - 1)
	{
		GetCount(mine,show,x, y);
		if (show[x][y] == '0')
		{
			GetCount(mine, show, x - 1, y);
			GetCount(mine, show, x - 1, y - 1);
			GetCount(mine, show, x - 1, y + 1);
			GetCount(mine, show, x, y - 1);
			GetCount(mine, show, x, y + 1);
			GetCount(mine, show, x + 1, y);
			GetCount( mine, show, x + 1, y - 1);
			GetCount(mine, show, x + 1, y + 1);
			if (show[x - 1][y] == '0')
			{
				if (test[x - 1][y] == '0')
				{
					OpenBoard(show, mine, test, x - 1, y);
				}
			}
			if (show[x - 1][y - 1] == '0')
			{
				if (test[x - 1][y - 1] == '0')
				{
					OpenBoard(show, mine, test, x - 1, y - 1);
				}
			}
			if (show[x - 1][y + 1] == '0')
			{
				if (test[x - 1][y + 1] == '0')
				{
					OpenBoard(show, mine, test, x - 1, y + 1);
				}
			}
			if (show[x][y - 1] == '0')
			{
				if (test[x][y - 1] == '0')
				{
					OpenBoard(show, mine, test, x, y - 1);
				}
			}
			if (show[x][y + 1] == '0')
			{
				if (test[x][y + 1] == '0')
				{
					OpenBoard(show, mine, test, x, y + 1);
				}
			}
			if (show[x + 1][y] == '0')
			{
				if (test[x + 1][y] == '0')
				{
					OpenBoard(show, mine, test, x + 1, y);
				}
			}
			if (show[x + 1][y - 1] == '0')
			{
				if (test[x + 1][y - 1] == '0')
				{
					OpenBoard(show, mine, test, x + 1, y - 1);
				}
			}
			if (show[x + 1][y + 1] == '0')
			{
				if (test[x + 1][y + 1] == '0')
				{
					OpenBoard(show, mine, test, x + 1, y + 1);
				}
			}
		}
	}
}

接下来给出我们此次的头文件部分以及测试部分。

game.h
#ifndef __GMAE__H__
#define __GAME__H__
#include <stdio.h>
#include <time.h>
#include <stdlib.h>

#define ROW 9
#define COL 9

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

#define SET 10

void InitBoard(char arr[ROWS][COLS], int row, int col, char ch);
void DisplayBoard(char arr[ROWS][COLS], int row, int col);
void SetBoard(char arr[ROWS][COLS], int row, int col);
void FindBoard(char mine[ROWS][COLS], char show[ROWS][COLS], char test[ROWS][COLS], int row, int col);
void GetCount(char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y);
void OpenBoard(char show[ROWS][COLS], char mine[ROWS][COLS], char test[ROWS][COLS], int x, int y);



#endif // __GMAE__H__

test.c
#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"


void menu()
{
	printf("************************************************\n");
	printf("*****╭─────────────────╮*****\n");
	printf("*****│        欢迎进入扫雷游戏          ┃*****\n");
	printf("*****╰━━━━━━━━━━━━━━━━━╯*****\n");
	printf("************************************************\n");
	printf("*****╭─────────────────╮*****\n");
	printf("*****│           1.开始游戏             ┃*****\n");
	printf("*****╰━━━━━━━━━━━━━━━━━╯*****\n");
	printf("************************************************\n");
	printf("*****╭─────────────────╮*****\n");
	printf("*****│           0.退出游戏             ┃*****\n");
	printf("*****╰━━━━━━━━━━━━━━━━━╯*****\n");
	printf("************************************************\n");
}

void game()
{
	char mine[ROWS][COLS] = {0};//放雷
	char show[ROWS][COLS] = {0};//排雷
	char test[ROWS][COLS] = {0};//测试是否展开的雷盘
	InitBoard(test, ROWS, COLS, '0');
	InitBoard(mine, ROWS, COLS,'0');
    InitBoard(show, ROWS, COLS,'*');
	
    SetBoard(mine, ROW, COL);//布置雷
	FindBoard(mine,show,test, ROW, COL);//排雷
	
}
int main()
{
	int input = 0;
	srand((unsigned int)time(NULL));
	do
	{
		menu();
		printf("请选择:");
		scanf("%d",&input);
		switch (input)
		{
		case 1:
			game();
			break;
		case 0:
			break;
		default:
			printf("请重新选择;\n");
		}

	} while (input);


	system("pause");
	return 0;
}

快来一起玩扫雷吧!

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值