【C语言】扫雷小游戏

【C语言】扫雷

看这篇博客之前,可已先看一下我前面的一篇三子棋的博客,两个都是二维数组的小应用,我在这里就不讲了直接上代码,因为打字讲的话很难讲通透,真的想学会的话可以到B站上看鹏哥的视频。
代码运行起来是这样的:
在这里插入图片描述

还是三个文件:

test.c

#define _CRT_SECURE_NO_WARNINGS

#include"game.h"

void game()
{
	//GameTime();

	char mine[ROWS][COLS];
	char show[ROWS][COLS];

	//初始化
	InitBoard(mine, ROWS, COLS, '0');
	InitBoard(show, ROWS, COLS, '*');
	//生成雷
	SetMine(mine, ROW, COL);

	while (1)
	{
		system("Cls");
		DisplayBoard(show, ROW, COL);
		//选位置
		int ret = find(mine, show, ROW, COL);
		if (ret == -1)
		{
			system("Cls");
			printf("你被炸死了,地雷如下(1为雷)\n");
			DisplayBoard(mine, ROW, COL);
			break;
		}
		else if(ret == NUM_OF_MINE)
		{
			DisplayBoard(show, ROW, COL);
			printf("玩家胜利\n");
			break;
		}
	}

}


int main()
{
	srand((unsigned int)time(NULL));
	int input = 0;
	do
	{
		menu();
		printf("\t\t\t\t\t请选择:");
		scanf("%d", &input);
		switch (input)
		{
		case 1:
			game();
			break;
		case 0:
			printf("退出游戏\n");
			break;
		default:
			printf("选择错误,重新选择\n");
			break;
		}

	} while (input);

	return 0;
}

game.c

#define _CRT_SECURE_NO_WARNINGS

#include"game.h"

//菜单
void menu()
{
	printf("\n\n\n\n\n\n\n\n");
	printf("\t\t\t\t\t********************\n");
	printf("\t\t\t\t\t****** 1.play ******\n");
	printf("\t\t\t\t\t****** 0.exit ******\n");
	printf("\t\t\t\t\t********************\n");
}

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

//打印
void DisplayBoard(char board[ROWS][COLS], int row, int col)
{
	printf("\n\n\n\n\n");

	printf("\t\t\t\t\t");

	for (int i = 0; i <= row; i++)
	{
		printf("%-2d ", i);
	}
	printf("\n");
	for (int i = 1; i <= row; i++)
	{
		printf("\t\t\t\t\t%-2d ", i);
		for (int j = 1; j <= col; j++)
		{
			printf("%-2c ", board[i][j]);
		}
		printf("\n");
	}
	printf("-----------------------------------------------------扫雷---------------------------------------------------------------\n");
}

//选位置排雷
int find(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
	int x = 0;
	int y = 0;

	printf("\t\t\t\t\t请选择坐标:");
	scanf("%d %d", &x, &y);

	//坐标合法
	if (x >= 1 && x <= row && y >= 1 && y <= col)
	{
		//该坐标在展示面板处是否被排查过
		if (show[x][y] == '*')
		{
			//该处是否有雷
			if (mine[x][y] == '1')
			{
				//炸死了
				return -1;
			}
			else
			{
				ShowMove(mine, show, x, y);
			}
		}
		else
		{
			printf("该位置已经排查过了\n");
		}
	}
	else
	{
		printf("坐标非法,重新输入。\n");

	}

	return IsWin(show, row, col);
}

//生成雷
void SetMine(char mine[ROWS][COLS], int row, int col)
{
	int count = NUM_OF_MINE;
	while (count)
	{
		int x = rand() % row + 1;
		int y = rand() % col + 1;
		if (mine[x][y] == '0')
		{
			mine[x][y] = '1';
			count--;
		}
	}
}


//周围雷数
int SurroundMine(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 ShowMove(char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y)
{
	if (SurroundMine(mine, x, y) == 0)
	{//没有雷就进去
		show[x][y] = ' ';
		//在边上就不递归
		if ((x == 1 && y >= 1 && y <= COL) ||
			(x == ROW && y >= 1 && y <= COL) ||
			(y == 1 && x >= 1 && x <= ROW) ||
			(y == COL && x >= 1 && x <= ROW))
		{
			//上方
			if (x == 1 && y >= 1 && y <= COL)
			{
				if (y == 1)
				{
					ShowMove(mine, show, x, y + 1);
					ShowMove(mine, show, x + 1, y);
					ShowMove(mine, show, x + 1, y + 1);
				}
				else if (y == COL)
				{
					ShowMove(mine, show, x, y + 1);
					ShowMove(mine, show, x - 1, y);
					ShowMove(mine, show, x - 1, y + 1);
				}
				else
				{
					ShowMove(mine, show, x, y + 1);
					ShowMove(mine, show, x + 1, y);
					ShowMove(mine, show, x + 1, y + 1);
					ShowMove(mine, show, x - 1, y);
					ShowMove(mine, show, x - 1, y + 1);
				}
			}
			//下方
			else if (x == ROW && y >= 1 && y <= COL)
			{
				if (y == 1)
				{
					ShowMove(mine, show, x, y - 1);
					ShowMove(mine, show, x + 1, y);
					ShowMove(mine, show, x + 1, y - 1);
				}
				else if (y == COL)
				{
					ShowMove(mine, show, x - 1, y - 1);
					ShowMove(mine, show, x - 1, y);
					ShowMove(mine, show, x, y - 1);
				}
				else
				{
					ShowMove(mine, show, x + 1, y);
					ShowMove(mine, show, x + 1, y - 1);
					ShowMove(mine, show, x - 1, y - 1);
					ShowMove(mine, show, x - 1, y);
					ShowMove(mine, show, x, y - 1);
				}
			}
			//左方
			else if (y == 1 && x >= 1 && x <= ROW)
			{
				if (x == 1)
				{
					ShowMove(mine, show, x + 1, y);
					ShowMove(mine, show, x + 1, y + 1);
					ShowMove(mine, show, x, y + 1);
				}
				else if (x == ROW)
				{
					ShowMove(mine, show, x + 1, y);
					ShowMove(mine, show, x + 1, y - 1);
					ShowMove(mine, show, x, y - 1);
				}
				else
				{
					ShowMove(mine, show, x + 1, y);
					ShowMove(mine, show, x + 1, y + 1);
					ShowMove(mine, show, x + 1, y - 1);
					ShowMove(mine, show, x, y + 1);
					ShowMove(mine, show, x, y - 1);	
				}
			}
			//右方
			else if (y == COL && x >= 1 && x <= ROW)
			{
				if (x == 1)
				{
					ShowMove(mine, show, x - 1, y);
					ShowMove(mine, show, x - 1, y + 1);
					ShowMove(mine, show, x, y + 1);

				}
				else if (x == ROW)
				{
					ShowMove(mine, show, x - 1, y - 1);
					ShowMove(mine, show, x - 1, y);
					ShowMove(mine, show, x, y - 1);
				}
				else
				{
					ShowMove(mine, show, x - 1, y);
					ShowMove(mine, show, x - 1, y + 1);
					ShowMove(mine, show, x - 1, y - 1);
					ShowMove(mine, show, x, y + 1);
					ShowMove(mine, show, x, y - 1);
				}
			}

		}
		else
		{
			for (int i = -1; i <= 1; i++)
			{
				for (int j = -1; j <= 1; j++)
				{
					if (show[x + i][y + j] == '*')
					{
						ShowMove(mine, show, x + i, y + j);
					}
				}
			}
		}
	}
	else
	{
		show[x][y] = '0' + SurroundMine(mine, x, y);
	}
}

//判断是否排完雷
int IsWin(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] == '*')
			{
				count++;
			}
		}
	}
	return count;
}


void GameTime()
{
	int hour = 0, min = 0, sec = 0;
	int cnt = 0;

	while (1)            //任意键退出循环(结束计时)
	{
		hour = cnt / 3600;      //获取计时小时数
		min = cnt / 60;          //获取计时分钟数
		sec = cnt % 60;         //获取计时秒数
		printf("  %02d:%02d:%02d\r", hour, min, sec);
		Sleep(1000);            //1s延时
		cnt++;
	}
}

game.h

#pragma once

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

#define ROW 9
#define COL 9

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

#define NUM_OF_MINE 10


//菜单
void menu();

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

//打印
void DisplayBoard(char board[ROWS][COLS], int row, int col);

//选位置排雷
int find(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);

//生成雷
void SetMine(char board[ROWS][COLS], int row, int col);

//周围雷数
int SurroundMine(char board[ROWS][COLS], int row, int col);

//递归排雷
void ShowMove(char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y);

//判断是否排完雷
int IsWin(char board[ROWS][COLS], int row, int col);

//时间
void GameTime();

到此结束。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

先搞面包再谈爱

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

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

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

打赏作者

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

抵扣说明:

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

余额充值