扫雷小游戏

简介

本游戏是拟扫雷游戏用C语言编写,游戏在代码运行窗口进行,由于是想用代码实现点什么,但是刚刚入门,所以好多功能并没有充分实现,希望不要引起不必要的误会。

运行实例

游戏选择界面
在这里插入图片描述
游戏开始界面
在这里插入图片描述
扫雷后
在这里插入图片描述
标记雷点
在这里插入图片描述
在这里插入图片描述
取消雷点
在这里插入图片描述
在这里插入图片描述
游戏获胜
在这里插入图片描述
游戏失败
在这里插入图片描述
代码运行情况有很多种,在这里不一一列举,有兴趣的朋友可以自行运行尝试~

代码部分

驱动交互部分

#define _CRT_SECURE_NO_WARNINGS 1

#include "game.h"

//简单的驱动代码
int main()
{
	int input;
	srand((unsigned int)time(NULL));
	
	do
	{
		system("cls");
		menu();
		printf("请选择:_\b");
		scanf("%d", &input);
		switch (input)
		{
		case 1:
			system("cls");
			game(ROW,COL);
			break;
		case 0:
			printf("好吧,我会想你的...\n");
			break;
		default :
			printf("不好意思哦,输入错误,请重新输入!\n");
			while (getchar() != '\n');
			Sleep(2000);
			continue;
		}
	} while (input);
	return 0;
}

函数部分

#define _CRT_SECURE_NO_WARNINGS 1

#include "game.h"

//打印菜单
void menu(void)
{
	printf("     欢迎来到扫雷!\n");
	printf("***********************\n");
	printf("****** 1:play *********\n");
	printf("****** 0:exit *********\n");
	printf("***********************\n");
}

//开始游戏
void game(int row,int col)
{
	//创建棋盘
	char mine[ROWS][COLS] = { 0 };
	char show[ROWS][COLS] = { 0 };
	//初始化棋盘
	InitBoard(mine, ROWS, COLS, '0');
	InitBoard(show, ROWS, COLS, '*');
	//布置雷
	SetMine(mine, ROW, COL);
	//打印棋盘
	ShowBoard(show, ROW, COL);
	//排查雷
	FindMine(show, mine, ROW, COL);
}

//初始化棋盘
void InitBoard(char a[ROWS][COLS], int rows, int cols, char ch)
{
	for (int i = 0; i < rows; i++)
	{
		for (int j = 0; j < cols; j++)
		{
			a[i][j] = ch;
		}
	}
}

//设置雷区(随机设置)
void SetMine(char a[ROWS][COLS], int row, int col)
{
	int count = EASY_COUNT;
	int x, y;
	while (count)
	{
		x = rand() % row + 1;
		y = rand() % col + 1;
		if (a[x][y] != '1')
		{
			a[x][y] = '1';
			count--;
		}
	}
}

//输出棋盘
void ShowBoard(char a[ROWS][COLS], int row, int col)
{
	printf("----------扫雷--------\n");
	printf(" ");
	for (int i = 0; i <= row; i++)
	{
		printf("%d ", i);//输出第一行棋盘坐标
	}
	printf("\n");

	for (int i = 1; i <= row; i++)
	{
		printf("%2d ", i);//输出第一列棋盘坐标
		for (int j = 1; j <= col; j++)
		{
			if (a[i][j] == '0')
				a[i][j] = ' ';
			printf("%c ", a[i][j]);
		}
		printf("\n");
	}
}

void FindMine(char show[ROWS][COLS], char mine[ROWS][COLS], int row, int col)
{
	int x, y;
	int win=0, lose=0;
	while (CheckBoard(show,row,col))
	{
		system("cls");
		ShowBoard(show, ROW, COL);
		printf("请输入排查坐标:_ _\b\b\b");
		scanf("%d %d", &x, &y);
		if (x > 0 && x <= row && y > 0 && y <= col && show[x][y]=='*')//判断坐标是否合法
		{
			if (mine[x][y] == '1')//如果踩雷了
			{
				system("cls");
				show[x][y] = '#';
				ShowBoard(show, ROW, COL);
				printf("很遗憾,你被炸死了!\n");
				lose = 1;
				Sleep(2000);
				printf("这是雷盘,请查收!\n");//输出布置的雷盘
				ShowBoard(mine, ROW, COL);
				Sleep(3000);
				break;
			}
			else//没踩雷
			{
				int count = GetMineCount(mine, x, y);//计算坐标值
				show[x][y] = count + '0';
				if (show[x][y] == '0')
					RecursiveSearch(show, mine, x, y);//如果坐标值为0,递归计算坐标旁边的8个格子
				system("cls");
				ShowBoard(show, ROW, COL);
			}
			if (!win && !lose && CheckBoard(show,row,col))//如果游戏没结束,进行游戏标记
			{
				while (1)
				{
					system("cls");
					ShowBoard(show, ROW, COL);
					printf("是否标记雷点?(1/0)\n");
					int a;
					scanf("%d", &a);
					if (a == 1)
					{
						SginBoard(show);
						system("cls");
						ShowBoard(show, ROW, COL);
					}
					else if (a == 0)
						break;
					else
					{
						while (getchar() != EOF);
						printf("输入错误,请重新输入:");
					}
				}
			}
			if (!win && !lose && CheckBoard(show, row, col))//如果游戏没结束,询问用户是否取消标记
			{
				while (1)
				{
					system("cls");
					ShowBoard(show, ROW, COL);
					printf("是否取消标记?(1/0)\n");
					int a;
					scanf("%d", &a);
					if (a == 1)
					{
						NSginBoard(show);
						system("cls");
						ShowBoard(show, ROW, COL);
					}
					else if (a == 0)
						break;
					else
					{
						while (getchar() != EOF);
						printf("输入错误,请重新输入:");
					}
				}
			}
		}
		else
		{
			printf("坐标错误,请重新输入:\n");
			continue;
		}
	}
	if (!CheckBoard(show, row, col)&&!lose)//如果成功扫雷
	{
		system("cls");
		ShowBoard(show, ROW, COL);
		printf("恭喜你!排雷成功!\n");
		win = 1;
		printf("这是雷盘,请查收!\n");
		ShowBoard(mine, ROW, COL);
		Sleep(5000);
		system("cls");
	}
}

//计算坐标旁边的雷数
int GetMineCount(char mine[ROWS][COLS], int x, int y)
{
	return 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] - 8 * '0';
}

//递归查找0值坐标旁边的值
void RecursiveSearch(char show[ROWS][COLS], char mine[ROWS][COLS], int x, int y)
{
	for (int i = x - 1; i <= x + 1; i++)
	{
		for (int j = y - 1; j <= y + 1; j++)
		{
			if (i > 0 && i <= ROW && j > 0 && j <= COL)
			{
				int count = GetMineCount(mine, i, j);
				if (count == 0)
				{
					if ((show[i][j] == '*'||show[i][j]=='?') && mine[i][j] == '0')
					{
						show[i][j] = count + '0';
						RecursiveSearch(show, mine, i, j);
					}
				}
				else
				{
					show[i][j] = count + '0';
				}
			}
		}
	}
}


//检查棋盘状态
int CheckBoard(char show[ROWS][COLS], int row, int col)
{
	int count = 0;
	for (int i = 1; i <= row; i++)
	{
		for (int j = 1; j <= col; j++)
		{
			if (show[i][j] == '*'||show[i][j]=='?')
				count++;
		}
	}
	if (count == 10)
		return 0;
	else
		return 1;
}

//标记棋盘
void SginBoard(char show[ROWS][COLS])
{
	printf("请输入标记点:_ _\b\b\b");
	int x, y;
	scanf("%d %d", &x, &y);
	if (x > 0 && x <= ROW && y > 0 && y <= COL && show[x][y] == '*')
	{
		show[x][y] = '?';
	}
	else
	{
		printf("坐标错误,请重新输入:\n");
	}
}


//取消标记
void NSginBoard(char show[ROWS][COLS])
{
	printf("请输入取消标记点:_ _\b\b\b");
	int x, y;
	scanf("%d %d", &x, &y);
	if (x > 0 && x <= ROW && y > 0 && y <= COL && show[x][y] == '?')
	{
		show[x][y] = '*';
	}
	else
	{
		printf("坐标错误,请重新输入:\n");
	}
}

头文件部分

#define _CRT_SECURE_NO_WARNINGS 1

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


#define ROW 10
#define COL 10

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

#define EASY_COUNT 10

//打印菜单
void menu(void);

//开始游戏
void game(int, int);

//初始化棋盘
void InitBoard(char a[ROWS][COLS], int rows, int cols, char ch);

//设置雷区
void SetMine(char a[ROWS][COLS], int row, int col);

//输出棋盘
void ShowBoard(char a[ROWS][COLS], int row, int col);

//排查雷区
void FindMine(char show[ROWS][COLS], char mine[ROWS][COLS], int row, int col);

//计算坐标旁边的雷数
int GetMineCount(char mine[ROWS][COLS], int x, int y);

//递归0个雷坐标的周围
void RecursiveSearch(char show[ROWS][COLS], char mine[ROWS][COLS], int x, int y);

//检查是否扫雷成功
int CheckBoard(char show[ROWS][COLS], int row, int col);

//标记棋盘
void SginBoard(char show[ROWS][COLS]);

//取消标记
void NSginBoard(char show[ROWS][COLS]);

写到最后

如果有好的的建议的话,很希望接受各位指正,谢谢啦!
同时也欢迎大家和我一起学习!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值