如何用C语言实现小游戏——扫雷

每天进步一点点,坚持带来大改变!

目录

实现思路:

1.代码搭建:

2.游戏框架的搭建:

3.扫雷游戏功能实现:

1.棋盘的设置:

2.初始化棋盘:

3.打印棋盘:

4.布置雷:

5.排查雷:

 6.整体代码实现

7.视频链接

实现思路:

扫雷游戏的实现包含两部分:首先应该是布置雷,其次排查雷。

1.代码搭建:

test.c文件来实现整个游戏的框架,game.h文件实现整个游戏实现需要的声明,game.c文件实现扫雷游戏的功能。

2.游戏框架的搭建:

#include<stdio.h>

void menu()
{
	printf("******************\n");
	printf("****  1.play *****\n");
	printf("****  0.exit  ****\n");
	printf("******************\n");
}
void game()
{
	//布置雷的信息

	//排查出雷的信息

	//初始化数组
	
    
    //打印棋盘

	//布置雷

	//排查雷

}
int main()
{
	int input = 0;
	do
	{
		menu();
		printf("请输入:");
		scanf("%d", &input);
		switch (input)
		{
		case 1:
			game();//扫雷游戏的实现
			break;
		case 0:
			printf("退出游戏!\n");
			break;
		default:
			printf("输入错误,请重新输入!");
			break;
		}
	} while (input);


	return 0;
}

3.扫雷游戏功能实现:

1.棋盘的设置:

因为当排查出雷的信息是需要显示坐标周围八个含雷的个数,所以避免数组越界的问题,9*9的扫雷棋盘实际需要11*11的棋盘。

game.h

#define Row 9
#define Col 9

#define Rows Row+2
#define Cols Col+2

定义棋盘:

    //布置雷的信息
	char mine[Rows][Cols] = { 0 };
	//排查出雷的信息
	char show[Rows][Cols] = { 0 };

2.初始化棋盘:

布置雷的数组:用‘0’初始化

排查雷的数组:用‘*‘初始化

 void Initboard(char board[Rows][Cols], int rows, int cols, int set);


void Initboard(char board[Rows][Cols], int rows, int cols, int set)
{
	int i = 0;
	int j = 0;
	for (i = 0; i < rows; i++)
	{
		for (j = 0; j < cols; j++)
		{
			board[i][j] = set;
		}
	}
}

3.打印棋盘:

   //打印棋盘
    Displayboard(mine, Row, Col);
    Displayboard(show, Row, Col); 

void Displayboard(char board[Rows][Cols], int row, int col)
{
	int i = 0;
	int j = 0;
	printf("*欢迎来到扫雷游戏*\n");
	for (j = 0; j < col; j++)
	{
		printf("%d ", j);
	}
	printf("\n");
	for (i = 1; i < row; i++)
	{
		printf("%d ", i);
		for (j = 1; j < col; j++)
		{
			printf("%c ", board[i][j]);
		}
		printf("\n");
	}
	printf("*欢迎来到扫雷游戏*\n");

}

4.布置雷:

思路:电脑生成随机数对应的x,y坐标,然后在棋盘上布置雷,雷用’1‘表示。

随机数的生成:使用rand()函数———srand()函数———time()函数(时间戳)

                          头文件#include<stdlib.h>                            #include<time.h>

 void SetMine(char board[Rows][Cols], int row, int col);

   //主函数调用

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

void SetMine(char board[Rows][Cols], int row, int col)
{
	int count = Easy_count;
	while (count)
	{
		int x = rand() % row + 1;
		int y = rand() % col + 1;
		if (board[x][y] == '0')
		{
			board[x][y] = '1';
			count--;
		}
	}
}

5.排查雷:

思路实现:

循环查找,每次查找一个坐标,如果是雷,很遗憾,被炸死了,如果不是雷,返回坐标周围八个坐标的含雷的信息赋值给show数组并且递归展开所有非雷的棋子


//用来查找坐标周围含有的雷的个数
int get_mine_count(char board[Rows][Cols], int x, int y)
{
	return (board[x - 1][y] + board[x - 1][y - 1] +
		board[x][y - 1] + board[x + 1][y - 1] +
		board[x + 1][y] + board[x + 1][y + 1] +
		board[x][y + 1] + board[x - 1][y + 1] - 8 * '0');
}
static void Openboard(char mine[Rows][Cols],char show[Rows][Cols] ,int x, int y, int* win)
{
	if (x >= 1 && x <= Row && y >= 1 && y <= Col)
	{
		if (show[x][y] == ' ' || show[x][y] != '*')
		{
			return;
		}
		else if (get_mine_count(mine, x, y) != 0)
		{
			show[x][y] = get_mine_count(mine, x, y) + '0';
			(*win)++;
			return;
		}
		else//如果不是雷,则递归展开调用
		{
			show[x][y] = ' ';//第二次调用的时候不会重复排查
			(*win)++;
			int i = 0;
			int j = 0;
			for (i = -1; i <= 1; i++)
			{
				for (j = -1; j <= 1; j++)
				{
					Openboard(mine, show, x + i, y + j, win);
				}
			}
		}
	}
}
void FindMine(char mine[Rows][Cols], char show[Rows][Cols], int row, int col)
{
	int x = 0;
	int y = 0;
	int win = 0;
	while (win<row*col-Easy_count)
	{
		printf("请输入要查找雷的坐标:");
		scanf("%d%d", &x, &y);
		if (x >= 1 & x <= row && y >= 1 && y <= col)
		{
			if (show[x][y] == '*')
			{
				if (mine[x][y] == '1')
				{
					printf("很遗憾,你被炸死了!\n");
					Displayboard(mine, Row, Col);
					break;
				}
				else//如果不是雷
				{
					Openboard(mine,show, x, y, &win);
					Displayboard(show, Row, Col);
				}
			}
			else
			{
				printf("输入的坐标已被查找,请重新输入:\n");
			}
		}
		else
		{
			printf("输入的坐标不合理,请重新输入:\n");
		}
	}
	if (win == row * col - Easy_count)
	{
		printf("恭喜你,排雷成功!\n");
		Displayboard(show, Row, Col);
    }
}

 

 6.整体代码实现

test.c

#include"game.h"

void menu()
{
	printf("******************\n");
	printf("****  1.play *****\n");
	printf("****  0.exit  ****\n");
	printf("******************\n");
}
void game()
{
	//布置雷的信息
	char mine[Rows][Cols] = { 0 };
	//排查出雷的信息
	char show[Rows][Cols] = { 0 };
	//初始化数组
	Initboard(mine, Rows, Cols, '0');
	Initboard(show, Rows, Cols, '*');
	//布置雷
	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;
		case 0:
			printf("退出游戏!\n");
			break;
		default:
			printf("输入错误,请重新输入!\n");
			break;
		}
	} while (input);


	return 0;
}

game.h 

#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 10
void Initboard(char board[Rows][Cols], int rows, int cols, int set);

void Displayboard(char board[Rows][Cols], int row, int col);

void SetMine(char board[Rows][Cols], int row, int col);

void FindMine(char mine[Rows][Cols], char show[Rows][Cols], int row, int col);

game.c 

#include"game.h"


void Initboard(char board[Rows][Cols], int rows, int cols, int set)
{
	int i = 0;
	int j = 0;
	for (i = 0; i < rows; i++)
	{
		for (j = 0; j < cols; j++)
		{
			board[i][j] = set;
		}
	}
}
void Displayboard(char board[Rows][Cols], int row, int col)
{
	int i = 0;
	int j = 0;
	printf("*欢迎来到扫雷游戏*\n");
	for (j = 0; j < col; j++)
	{
		printf("%d ", j);
	}
	printf("\n");
	for (i = 1; i < row; i++)
	{
		printf("%d ", i);
		for (j = 1; j < col; j++)
		{
			printf("%c ", board[i][j]);
		}
		printf("\n");
	}
	printf("*欢迎来到扫雷游戏*\n");

}

void SetMine(char board[Rows][Cols], int row, int col)
{
	int count = Easy_count;
	while (count)
	{
		int x = rand() % row + 1;
		int y = rand() % col + 1;
		if (board[x][y] == '0')
		{
			board[x][y] = '1';
			count--;
		}
	}
}

//用来查找坐标周围含有的雷的个数
int get_mine_count(char board[Rows][Cols], int x, int y)
{
	return (board[x - 1][y] + board[x - 1][y - 1] +
		board[x][y - 1] + board[x + 1][y - 1] +
		board[x + 1][y] + board[x + 1][y + 1] +
		board[x][y + 1] + board[x - 1][y + 1] - 8 * '0');
}
static void Openboard(char mine[Rows][Cols],char show[Rows][Cols] ,int x, int y, int* win)
{
	if (x >= 1 && x <= Row && y >= 1 && y <= Col)
	{
		if (show[x][y] == ' ' || show[x][y] != '*')
		{
			return;
		}
		else if (get_mine_count(mine, x, y) != 0)
		{
			show[x][y] = get_mine_count(mine, x, y) + '0';
			(*win)++;
			return;
		}
		else//如果不是雷,则递归展开调用
		{
			show[x][y] = ' ';//第二次调用的时候不会重复排查
			(*win)++;
			int i = 0;
			int j = 0;
			for (i = -1; i <= 1; i++)
			{
				for (j = -1; j <= 1; j++)
				{
					Openboard(mine, show, x + i, y + j, win);
				}
			}
		}
	}
}
void FindMine(char mine[Rows][Cols], char show[Rows][Cols], int row, int col)
{
	int x = 0;
	int y = 0;
	int win = 0;
	while (win<row*col-Easy_count)
	{
		printf("请输入要查找雷的坐标:");
		scanf("%d%d", &x, &y);
		if (x >= 1 & x <= row && y >= 1 && y <= col)
		{
			if (show[x][y] == '*')
			{
				if (mine[x][y] == '1')
				{
					printf("很遗憾,你被炸死了!\n");
					Displayboard(mine, Row, Col);
					break;
				}
				else//如果不是雷
				{
					Openboard(mine,show, x, y, &win);
					Displayboard(show, Row, Col);
				}
			}
			else
			{
				printf("输入的坐标已被查找,请重新输入:\n");
			}
		}
		else
		{
			printf("输入的坐标不合理,请重新输入:\n");
		}
	}
	if (win == row * col - Easy_count)
	{
		printf("恭喜你,排雷成功!\n");
		Displayboard(show, Row, Col);
    }
}

7.视频链接

扫雷

评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

linkindly

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

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

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

打赏作者

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

抵扣说明:

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

余额充值