欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C语言实现扫雷游戏详细代码实例

程序员文章站 2022-09-26 09:38:48
扫雷游戏思路:先制作一个菜单让玩家选择是玩游戏还是退出游戏,菜单做好了,接着我们开始制作扫雷的棋盘并初始化,初始化弄完了我们下一步开始埋雷,雷埋好了就开始扫雷。大概思路就是这样具体实现看下面:菜单的实...

扫雷游戏

思路:先制作一个菜单让玩家选择是玩游戏还是退出游戏,菜单做好了,接着我们开始制作扫雷的棋盘并初始化,初始化弄完了我们下一步开始埋雷,雷埋好了就开始扫雷。

大概思路就是这样具体实现看下面:

菜单的实现代码:

int main()
{
	int input = 0;
	srand((unsigned int)time(null));

	do
	{
		printf("**************************\n");
		printf("*** 1. play 0. exit ***\n");
		printf("*** 2. clear ***\n");
		printf("**************************\n");
		printf("请选择:>");
		scanf("%d", &input);

		switch (input)
		{
		case 1:
			game();//游戏实现
			break;
		case 2://清屏选项
			system("cls");
			break;
		case 0:
			printf("退出程序!\n");
			break;
		default:
			printf("输入错误,请重新输入!\n");
			sleep(1000);
			system("cls");
			break;
		}
	} while (input);

	return 0;
}

这里我们用了windows库函数清屏,如果屏幕上显示的东西太多了,我们可以选择2来清屏,还有一个睡眠函数,如果输出错误会短暂的提示你一秒,告诉你选择错误了。

效果展示图:

C语言实现扫雷游戏详细代码实例

制作好菜单那我们开始实现整个游戏的逻辑框架了,定义两个二维数组,一个用于显示,一个用于存放地雷。如果这两个东西都只用一个二维数组的话后面的实现逻辑会比较麻烦所以我选择使用两个二维数组。

char mine[rows][cols] = { 0 };//用于埋雷
char show[rows][cols] = { 0 };//用于游戏的画面显示

有这两个二维数组我们开始初始化

//初始化
void initzeboard(char mine[rows][cols], int rows, int cols, char val)
{
	int i = 0;
	int j = 0;

	for (i = 0; i < rows; i++)
	{
		for (j = 0; j < cols; j++)
		{
			mine[i][j] = val;
		}
	}
}

我遍历整个二维数组来初始化,val这个参数是用于接收这两个二维数组的初始化的内容,如果不用这个val参数我就要再写一个函数封装很麻烦。

接下来,实现个打印棋盘函数来看看我们的初始化是否正确的初始化成我想要的内容

打印棋盘代码:

//显示棋盘
void display_board(char mine[rows][cols], int row, int col)
{
	int i = 0;
	int j = 0;

	printf("  扫雷游戏\n");
	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 ", mine[i][j]);
		}

		printf("\n");

	}
}

接下来看看我们初始化的是否正确:

C语言实现扫雷游戏详细代码实例

可以看到它正确的初始化了,全0代表埋雷的二维数组,而*代表的是用户看到的,正常来说,这个埋雷是不需要显示出来的,但是我们需要确认是否正确初始化所以打印出来看看,下一步我们开始埋雷。

//埋雷
void random_mine(char mine[rows][cols], int row, int col)
{
	int x = 0;
	int y = 0;
	int count = easily;

	while (count)
	{
		x = rand() % row + 1;
		y = rand() % col + 1;

		if (mine[x][y] == '0')//用于判断是否正确的埋雷,只有我们这没被埋过的雷我们才自减
		{
			mine[x][y] = '1';
			count--;
		}
	}
}

埋好雷的效果:

C语言实现扫雷游戏详细代码实例

1代表是雷,0代表步是雷,埋好的雷会被赋值成字符1

埋雷我采用随机函数来帮我埋下雷,弄好这步接来的就是扫雷这很关键。

扫雷代码:

//扫雷
void mine_sweeping(char mine[rows][cols], char show[rows][cols], int row, int col)
{
	int x = 0;
	int y = 0;

	while (count < row * col - easily)
	{
		system("cls");
		display_board(show, col, row);
		printf("请输入坐标:>");
		scanf("%d %d", &x, &y);

		if ((1 <= x && x <= row) && (1 <= y && y <= col))
		{
			if (mine[x][y] == '0')
			{
				int leng = statistics_mine(mine, x, y);
				show[x][y] = leng + '0';
				count++;
			}
			else
			{
				printf("很遗憾你被炸死了\n");
				display_board(mine, row, col);
				break;
			}
		}
		else
		{
			printf("请输入有效数字!\n");
		}
	}

	if (count == row * col - easily)
	{
		printf("恭喜你扫雷成功!\n");
		display_board(mine, row, col);
	}
}

如果玩家输入的坐标是合法的我们就统计它周围有多少雷,统计雷的功能我做成了一个内部函数防止重名。

如果全部没有雷的地方都排查完了,我们就停止循环。然后打印下雷的棋盘让玩家知道哪有雷,如果被炸死了,我们也打印下雷的二维数组让玩家死得瞑目。

C语言实现扫雷游戏详细代码实例

C语言实现扫雷游戏详细代码实例

统计雷的函数:

//统计周围雷的个数
static int statistics_mine(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';
}

根据ascii码字符1减去字符0就是整形1,所以根据此方法我们就只用把周围的雷全部加起来减去字符零就可以得到周围雷的个数并返回 。

统计雷效果图:

C语言实现扫雷游戏详细代码实例

周围没有雷就是0,如果有就显示个数。

通关也会打印所有雷的位置并提示你是否通关

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-m4to2zup-1613214039851)(c:\users\86176\appdata\roaming\typora\typora-user-images\image-20210213185535958.png)]

头文件:

#pragma once
#include <stdio.h>
#include <windows.h>
#include <time.h>
#include <stdlib.h>

#define row 9
#define col 9
#define rows row + 2
#define cols col + 2
#define easily 0//雷的数量

//初始化
void initzeboard(char mine[rows][cols], int rows, int cols, char val);

//显示棋盘
void display_board(char mine[rows][cols], int row, int col);

//埋雷
void random_mine(char mine[rows][cols], int row, int col);

//扫雷
void mine_sweeping(char mine[rows][cols], char show[rows][cols], int row, int col);

游戏实现代码:

#define _crt_secure_no_warnings 1
#include "game.h"

int count = 0;//统计还剩多少格子没被扫


void game()//游戏实现
{
	char mine[rows][cols] = { 0 };//用于埋雷
	char show[rows][cols] = { 0 };//用于游戏的画面显示

	//初始化
	initzeboard(show, rows, cols, '*');
	initzeboard(mine, rows, cols, '0');

	//显示棋盘
	display_board(show, row, col);
	//display_board(mine, row, col);

	//埋雷
	random_mine(mine, row, col);
	//display_board(mine, row, col);
	
	//扫雷
	mine_sweeping(mine, show, row, col);
}

//初始化
void initzeboard(char mine[rows][cols], int rows, int cols, char val)
{
	int i = 0;
	int j = 0;

	for (i = 0; i < rows; i++)
	{
		for (j = 0; j < cols; j++)
		{
			mine[i][j] = val;
		}
	}
}

//显示棋盘
void display_board(char mine[rows][cols], int row, int col)
{
	int i = 0;
	int j = 0;

	printf("  扫雷游戏\n");
	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 ", mine[i][j]);
		}

		printf("\n");

	}
}

//埋雷
void random_mine(char mine[rows][cols], int row, int col)
{
	int x = 0;
	int y = 0;
	int count = easily;

	while (count)
	{
		x = rand() % row + 1;
		y = rand() % col + 1;

		if (mine[x][y] == '0')
		{
			mine[x][y] = '1';
			count--;
		}
	}
}

//统计周围雷的个数
static int statistics_mine(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';
}


//扫雷
void mine_sweeping(char mine[rows][cols], char show[rows][cols], int row, int col)
{
	int x = 0;
	int y = 0;

	while (count < row * col - easily)
	{
		system("cls");
		display_board(show, col, row);
		printf("请输入坐标:>");
		scanf("%d %d", &x, &y);

		if ((1 <= x && x <= row) && (1 <= y && y <= col))
		{
			if (mine[x][y] == '0')
			{
				int leng = statistics_mine(mine, x, y);
				show[x][y] = leng + '0';
				count++;
			}
			else
			{
					printf("很遗憾你被炸死了\n");
					display_board(mine, row, col);
					break;
			}
		}
		else
		{   
				printf("请输入有效数字!\n");
		}
	}

	if (count == row * col - easily)
	{
		printf("恭喜你扫雷成功!\n");
		display_board(mine, row, col);
	}
}

主函数代码:

#define _crt_secure_no_warnings 1
#include "game.h"

extern void game();

int main()
{
	int input = 0;
	srand((unsigned int)time(null));

	do
	{
		printf("**************************\n");
		printf("*** 1. play 0. exit ***\n");
		printf("*** 2. clear ***\n");
		printf("**************************\n");
		printf("请选择:>");
		scanf("%d", &input);

		switch (input)
		{
		case 1:
			game();//游戏实现
			break;
		case 2://清屏选项
			system("cls");
			break;
		case 0:
			printf("退出程序!\n");
			break;
		default:
			printf("输入错误,请重新输入!\n");
			sleep(1000);
			system("cls");
			break;
		}
	} while (input);

	return 0;
}

到此这篇关于c语言实现扫雷游戏详细代码实例的文章就介绍到这了,更多相关c语言实现扫雷游戏内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!