服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C/C++ - 基于C语言实现简单的扫雷游戏示例代码

基于C语言实现简单的扫雷游戏示例代码

2022-11-25 13:34爱弹吉他的小奔同学 C/C++

windows自带的游戏《扫雷》是陪伴了无数人的经典游戏,本文将利用C语言实现这一经典的游戏,文中的示例代码讲解详细,感兴趣的可以学习一下

效果展示

开始的界面

基于C语言实现简单的扫雷游戏示例代码

输入0结束程序

基于C语言实现简单的扫雷游戏示例代码

输入1开始游戏

基于C语言实现简单的扫雷游戏示例代码

选择标记地雷或者选择踩坐标

输入0标记地雷模式

基于C语言实现简单的扫雷游戏示例代码

输入坐标

基于C语言实现简单的扫雷游戏示例代码

输入1踩坐标模式

基于C语言实现简单的扫雷游戏示例代码

输入坐标

基于C语言实现简单的扫雷游戏示例代码

在输入坐标处输入0 0结束游戏

基于C语言实现简单的扫雷游戏示例代码

踩到炸弹,出现炸弹位置

(1表示炸弹的位置,0表示没有炸弹的位置)

基于C语言实现简单的扫雷游戏示例代码

输入0结束程序

基于C语言实现简单的扫雷游戏示例代码

输入1重新开始游戏

基于C语言实现简单的扫雷游戏示例代码

胜利

基于C语言实现简单的扫雷游戏示例代码

输入0结束程序

基于C语言实现简单的扫雷游戏示例代码

输入1重新开始游戏

基于C语言实现简单的扫雷游戏示例代码

 

代码

我创建了两个.c源文件,一个.h头文件

test.c

#define _CRT_SECURE_NO_WARNINGS

#include"game.h"

int main()
{
	int exi = 0;
	srand((unsigned int)time(NULL));
	board();
	printf("请输入是否开始游戏:>");
	scanf("%d", &exi);
	do
	{
		switch (exi)
		{
		case 1:
		{
			game();
			printf("是否输入1重新开始游戏:>");
			scanf("%d", &exi);
			if (exi == 0)
			{
				printf("游戏结束");
			}
			break;
		}
		case 0:
		{
			printf("游戏结束");
			break;
		}
		default:
		{
			printf("输入错误,请重新输入:>");
			scanf("%d", &exi);
			if (exi == 0)
			{
				printf("游戏结束\n");
			}
			break;
		}
		}
	} while (exi);



	return 0;
}

game.h

#pragma once

#include<stdio.h>
#include<time.h>
#include<stdlib.h>

#define WID 9
#define LON 9
#define WIDS WID+2
#define LONS LON+2
#define RAN 5

void board();
//打印开始的面板

void game();
//游戏运行的起点

void initialization(char mane[WIDS][LONS], char siz, int x, int y);
//把数组内框初始化为siz

void display(char mane[WIDS][LONS], int x, int y);
//打印数组内框的字符

void random(char mane[WIDS][LONS], int count);
//在数组中随机赋予count个炸弹

int look(char mane[WIDS][LONS], int x, int y);
//计算mane数组x,y位置周围有多少炸弹

void judge(char mane[WIDS][LONS], char show[WIDS][LONS],char include[WIDS][LONS]);
//判断输入是否获得胜利

void xunhuan(char mane[WIDS][LONS], char show[WIDS][LONS], char include[WIDS][LONS], int X, int Y);
//判断周围没有雷,会向外继续推,直到出现雷

void change(char show[WIDS][LONS], int x, int y, char siz);
//改变数组show位置(x,y)为字符siz

void jishu();
//统计选择了几次的位置,包括类推的位置,实现一点出现一大片的功能

game扫雷.c

#define _CRT_SECURE_NO_WARNINGS

#include"game.h"

static int a = 0;


void board()
{
	printf("****************************\n");
	printf("****************************\n");
	printf("********* 1.play  **********\n");
	printf("********* 0.exit  **********\n");
	printf("****************************\n");
	printf("****************************\n");

}

//数组初始化
void initialization(char mane[WIDS][LONS], char siz, int x, int y)
{
	int i = 0;
	for (i = 0; i <= x+1; i++)
	{
		int j = 0;
		for (j = 0; j <= y+1; j++)
		{
			mane[i][j] = siz;
		}

	}
}


//打印第一个面板
void display(char mane[WIDS][LONS], int x,int y)
{
	int i = 0;
	int j = 0;
	printf("-----------扫雷-----------\n");
	printf("0 | ");

	for (j = 1; j <= y; j++)
	{
		printf("%d ",j);
	}
	printf("\n");
	printf("- - -");

	for (j = 1; j <= y; j++)
	{
		printf(" -");
	}


	for (i = 1; i <= x; i++)
	{
		printf("\n");
		printf("%d | ",i);
		for (j = 1; j <= y; j++)
		{
			printf("%c ", mane[i][j]);
		}

	}
	printf("\n-----------扫雷-----------\n");
}



void random(char mane[WIDS][LONS],int count)
{
	int x = 0;
	int y = 0;
	while (count)
	{
		x = rand() % WID + 1;
		y = rand() % LON + 1;
		if (mane[x][y] == '0')
		{
			mane[x][y] = '1';
			count--;
		}

	}

}

int look(char mane[WIDS][LONS],int x,int y)
{
	return mane[x][y + 1] +
		mane[x][y - 1] +
		mane[x - 1][y + 1] +
		mane[x - 1][y - 1] +
		mane[x + 1][y + 1] +
		mane[x + 1][y - 1] +
		mane[x - 1][y] +
		mane[x + 1][y]-8*'0';


}

void jishu()
{
	a++;
}

void xunhuan(char mane[WIDS][LONS], char show[WIDS][LONS], char include[WIDS][LONS],int X,int Y)
{
	if (include[X][Y] != '1')
	{
		int count = 0;
		count = look(mane, X, Y);
		show[X][Y] = count + '0';
		include[X][Y] = '1';
		jishu();
		if (count == 0)
		{
			
			xunhuan(mane, show, include, X + 1, Y + 1);
			xunhuan(mane, show, include, X - 1, Y - 1);
			xunhuan(mane, show, include, X + 1, Y);
			xunhuan(mane, show, include, X - 1, Y);
			xunhuan(mane, show, include, X, Y + 1);
			xunhuan(mane, show, include, X, Y - 1);
			xunhuan(mane, show, include, X + 1, Y - 1);
			xunhuan(mane, show, include, X - 1, Y + 1);
		}
	
	}

}

void change(char show[WIDS][LONS], int x, int y,char siz)
{
	show[x][y] = siz;

}

void judge(char mane[WIDS][LONS], char show[WIDS][LONS], char include[WIDS][LONS])
{
	int X = 0;
	int Y = 0;
	display(show, WID, LON);


	do
	{
		int num = a;

		if (num == WID * LON - RAN)
		{
			printf("恭喜你获得胜利!\n\n");
			display(mane, WID, LON);

			break;
		}


		printf("想要标记地雷就输入0,想要选择就输入1):>");
		int choose = 0;
		scanf("%d", &choose);
		printf("\n");

		if (choose==1)
		{
			printf("输入0 0结束游戏\n");

			printf("请输入你选择的坐标:>");

			scanf("%d%d", &X, &Y);

			if (X == 0 && Y == 0)
			{
				printf("\n结束此次游戏\n\n");
				break;
			}

			if (X >= 1 && X <= 9 && Y >= 1 && Y <= 9)
			{
				if (mane[X][Y] == '1')
				{
					printf("\n你吃到炸弹啦,死翘翘了\n\n");
					display(mane, WID, LON);
					break;
				}
				else
				{
					xunhuan(mane, show, include, X, Y);
					display(show, WID, LON);

					//display(mane, WID, LON);
				}
			}
			else
			{
				printf("\n你输的超过范围啦,");
			}
		}
		else
		{

			printf("\n输入0 0结束游戏\n");

			printf("请输入你选择的坐标:>");

			scanf("%d%d", &X, &Y);

			if (X == 0 && Y == 0)
			{
				printf("\n结束此次游戏\n\n");
				break;
			}
			change(show,X,Y,'F');
			display(show, WID, LON);

		}
	} while (1);


}

void chu(char mane[WIDS][LONS], char siz,int x, int y)
{
	int i = 0;
	for (i = 1; i <= x ; i++)
	{
		int j = 0;
		for (j = 1; j <= y ; j++)
		{
			mane[i][j] = siz;
		}

	}

}

void game()
{
	char mane[WIDS][LONS];
	char show[WIDS][LONS];
	char include[WIDS][LONS];

	initialization(mane, '0', WID, LON);
	initialization(show, '*', WID, LON);
	initialization(include, '1', WID, LON);

	chu(include, '0', WID, LON);

	random(mane,RAN);

	//display(mane, WID, LON);
	//display(show, WID, LON);
	
	judge(mane,show,include);
}

我写的这个小游戏还很粗糙,不过才开始学,进步空间还是很大的,代码就上传到gitee

以上就是基于C语言实现简单的扫雷游戏的详细内容,更多关于C语言 扫雷的资料请关注服务器之家其它相关文章!

原文链接:https://blog.csdn.net/iqrmshrt/article/details/124638620

延伸 · 阅读

精彩推荐
  • C/C++Qt数据库相关应用开发总结

    Qt数据库相关应用开发总结

    这篇文章主要为大家介绍了在Qt数据库应用开发中的一些经验总结,以及一些组件的使用介绍。文中的示例代码讲解详细,需要的可以参考一下...

    feiyangqingyun11852022-09-27
  • C/C++C语言实现合并字符串

    C语言实现合并字符串

    今天小编就为大家分享一篇C语言实现合并字符串,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...

    grey_csdn9632021-08-10
  • C/C++解析四方定理的应用

    解析四方定理的应用

    “四方定理”是数论中著名的一个定理,指所有自然数至多只要用四个数的平方和就可以表示。...

    C++教程网3532020-11-26
  • C/C++C语言单向链表的表示与实现实例详解

    C语言单向链表的表示与实现实例详解

    这篇文章主要介绍了C语言单向链表的表示与实现,需要的朋友可以参考下...

    C语言程序设计10832021-01-21
  • C/C++C++实现动态顺序表

    C++实现动态顺序表

    这篇文章主要为大家详细介绍了C++实现动态顺序表,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    adorable_5782021-09-07
  • C/C++如何用C写一个web服务器之GCC项目编译

    如何用C写一个web服务器之GCC项目编译

    本文主要介绍了,如何用C写一个web服务器,Linux下用GCC进行项目编译,对此感兴趣的同学,可以参考下。...

    枕边书3932021-11-09
  • C/C++C语言边角料2:用纯软件来代替Mutex互斥锁

    C语言边角料2:用纯软件来代替Mutex互斥锁

    在 Linux 系统中,当多个线程并行执行时,如果需要访问同一个资源,那么在访问资源的地方,需要使用操作系统为我们提供的同步原语来进行保护。同步原...

    IOT物联网小镇7032021-03-23
  • C/C++c++运算符重载基础知识详解

    c++运算符重载基础知识详解

    运算符重载是一种形式的C++多态。运算符重载将重载的概念扩展到运算符上,允许赋予C++运算符多种含义...

    C++教程网6242021-01-17