草庐IT

用C语言做一个简单的扫雷游戏

小奔同学 2023-12-08 原文

🌟小奔最近学了C语言不少的东西,但是想用学到的东西来搞一个小游戏。
这次就来搞一个扫雷游戏,可以实现的功能就是可自定义 1*1 到 9*9 的扫雷范围,自定义雷的数量,可以自己识别胜利的条件,也可以标记雷的地方,现在我们就来演示一下这个游戏吧
(当前的范围是 9*9 的大小,雷的数量是 5 个,简单实现一下就好)
如果对你有帮助,那就给小奔点一个赞吧,谢谢啦

目录

运行的过程


💥开始的界面💥

跳转到目录


📓输入0结束程序

跳转到目录


📓输入1开始游戏

跳转到目录


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


📓输入0标记地雷模式

跳转到目录


📓输入坐标

跳转到目录


📓输入1踩坐标模式

跳转到目录


📓输入坐标

跳转到目录


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

跳转到目录


💥踩到炸弹,出现炸弹位置💥

跳转到目录
(1表示炸弹的位置,0表示没有炸弹的位置)


📓输入0结束程序

跳转到目录


📓输入1重新开始游戏

跳转到目录


💥胜利💥

跳转到目录


📓输入0结束程序

跳转到目录


📓输入1重新开始游戏

跳转到目录


💥代码💥

跳转到目录
我创建了两个.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语言做一个简单的扫雷游戏的更多相关文章

  1. ruby - 如何将脚本文件的末尾读取为数据文件(Perl 或任何其他语言) - 2

    我正在寻找执行以下操作的正确语法(在Perl、Shell或Ruby中):#variabletoaccessthedatalinesappendedasafileEND_OF_SCRIPT_MARKERrawdatastartshereanditcontinues. 最佳答案 Perl用__DATA__做这个:#!/usr/bin/perlusestrict;usewarnings;while(){print;}__DATA__Texttoprintgoeshere 关于ruby-如何将脚

  2. ruby - 使用 Vim Rails,您可以创建一个新的迁移文件并一次性打开它吗? - 2

    使用带有Rails插件的vim,您可以创建一个迁移文件,然后一次性打开该文件吗?textmate也可以这样吗? 最佳答案 你可以使用rails.vim然后做类似的事情::Rgeneratemigratonadd_foo_to_bar插件将打开迁移生成的文件,这正是您想要的。我不能代表textmate。 关于ruby-使用VimRails,您可以创建一个新的迁移文件并一次性打开它吗?,我们在StackOverflow上找到一个类似的问题: https://sta

  3. ruby-on-rails - Rails - 一个 View 中的多个模型 - 2

    我需要从一个View访问多个模型。以前,我的links_controller仅用于提供以不同方式排序的链接资源。现在我想包括一个部分(我假设)显示按分数排序的顶级用户(@users=User.all.sort_by(&:score))我知道我可以将此代码插入每个链接操作并从View访问它,但这似乎不是“ruby方式”,我将需要在不久的将来访问更多模型。这可能会变得很脏,是否有针对这种情况的任何技术?注意事项:我认为我的应用程序正朝着单一格式和动态页面内容的方向发展,本质上是一个典型的网络应用程序。我知道before_filter但考虑到我希望应用程序进入的方向,这似乎很麻烦。最终从任何

  4. ruby-on-rails - 渲染另一个 Controller 的 View - 2

    我想要做的是有2个不同的Controller,client和test_client。客户端Controller已经构建,我想创建一个test_clientController,我可以使用它来玩弄客户端的UI并根据需要进行调整。我主要是想绕过我在客户端中内置的验证及其对加载数据的管理Controller的依赖。所以我希望test_clientController加载示例数据集,然后呈现客户端Controller的索引View,以便我可以调整客户端UI。就是这样。我在test_clients索引方法中试过这个:classTestClientdefindexrender:template=>

  5. ruby-on-rails - 如果 Object::try 被发送到一个 nil 对象,为什么它会起作用? - 2

    如果您尝试在Ruby中的nil对象上调用方法,则会出现NoMethodError异常并显示消息:"undefinedmethod‘...’fornil:NilClass"然而,有一个tryRails中的方法,如果它被发送到一个nil对象,它只返回nil:require'rubygems'require'active_support/all'nil.try(:nonexisting_method)#noNoMethodErrorexceptionanymore那么try如何在内部工作以防止该异常? 最佳答案 像Ruby中的所有其他对象

  6. ruby - 为什么 SecureRandom.uuid 创建一个唯一的字符串? - 2

    关闭。这个问题需要detailsorclarity.它目前不接受答案。想改进这个问题吗?通过editingthispost添加细节并澄清问题.关闭8年前。Improvethisquestion为什么SecureRandom.uuid创建一个唯一的字符串?SecureRandom.uuid#=>"35cb4e30-54e1-49f9-b5ce-4134799eb2c0"SecureRandom.uuid方法创建的字符串从不重复?

  7. ruby - 简单获取法拉第超时 - 2

    有没有办法在这个简单的get方法中添加超时选项?我正在使用法拉第3.3。Faraday.get(url)四处寻找,我只能先发起连接后应用超时选项,然后应用超时选项。或者有什么简单的方法?这就是我现在正在做的:conn=Faraday.newresponse=conn.getdo|req|req.urlurlreq.options.timeout=2#2secondsend 最佳答案 试试这个:conn=Faraday.newdo|conn|conn.options.timeout=20endresponse=conn.get(url

  8. ruby-on-rails - Rails - 从另一个模型中创建一个模型的实例 - 2

    我有一个正在构建的应用程序,我需要一个模型来创建另一个模型的实例。我希望每辆车都有4个轮胎。汽车模型classCar轮胎模型classTire但是,在make_tires内部有一个错误,如果我为Tire尝试它,则没有用于创建或新建的activerecord方法。当我检查轮胎时,它没有这些方法。我该如何补救?错误是这样的:未定义的方法'create'forActiveRecord::AttributeMethods::Serialization::Tire::Module我测试了两个环境:测试和开发,它们都因相同的错误而失败。 最佳答案

  9. ruby - 寻找通过阅读代码确定编程语言的ruby gem? - 2

    几个月前,我读了一篇关于ruby​​gem的博客文章,它可以通过阅读代码本身来确定编程语言。对于我的生活,我不记得博客或gem的名称。谷歌搜索“ruby编程语言猜测”及其变体也无济于事。有人碰巧知道相关gem的名称吗? 最佳答案 是这个吗:http://github.com/chrislo/sourceclassifier/tree/master 关于ruby-寻找通过阅读代码确定编程语言的rubygem?,我们在StackOverflow上找到一个类似的问题:

  10. ruby - 用 Ruby 编写一个简单的网络服务器 - 2

    我想在Ruby中创建一个用于开发目的的极其简单的Web服务器(不,不想使用现成的解决方案)。代码如下:#!/usr/bin/rubyrequire'socket'server=TCPServer.new('127.0.0.1',8080)whileconnection=server.acceptheaders=[]length=0whileline=connection.getsheaders想法是从命令行运行这个脚本,提供另一个脚本,它将在其标准输入上获取请求,并在其标准输出上返回完整的响应。到目前为止一切顺利,但事实证明这真的很脆弱,因为它在第二个请求上中断并出现错误:/usr/b

随机推荐