扫雷-完整源码(C语言实现)

云边有个稻草人-CSDN博客

在学完C语言函数之后,我们就有能力去实现简易版扫雷游戏了(成就感满满),下面是扫雷游戏的源码,快试一试效果如何吧!

在test.c里面进行扫雷游戏的测试,game.h和game.c里实现扫雷游戏的实现,后续我会出扫雷游戏代码的详细思路和解析。

目录

效果图

game.h

game.c

test.c


效果图

(哇趣,一下子就踩到雷了,幸运到爆!)

game.h

cpp 复制代码
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<time.h>

#define ROW 9
#define COL 9

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

#define EASY_COUNT 10

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


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

//布置雷
void SetMine(char mine[ROWS][COLS], int row, int col);

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

game.c

cpp 复制代码
#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"

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

//打印棋盘
void DisplayBoard(char show[ROWS][COLS], int row, int col)
{
	printf("--------扫雷--------\n");
	//打印显示列
	int i = 0;
	for ( i = 0; i <= row; i++)
	{
		printf("%d ", i);
	}
	printf("\n");

	for (i = 1; i <= row; i++)
	{
		int j = 0;
		printf("%d ", i);
		for (j = 1; j <= col; j++)
		{
			printf("%c ", show[i][j]);
		}
		printf("\n");
	}

}

//布置雷
void SetMine(char mine[ROWS][COLS], int row, int col)
{
	//产生随机的一个坐标
	int x = 0;
	int y = 0;
	int count = EASY_COUNT;
	while (count)
	{
		x = rand() % row + 1;
		y = rand() % col + 1;
		if (mine[x][y] != '1')
		{
			mine[x][y] = '1';
			count--;
		}
	}
}

//方法一
//int GetMineCount(char mine[ROWS][COLS], int x, int y)
//{
//	return mine[x - 1][y] +
//	mine[x + 1][y] + 
//	mine[x - 1][y - 1] +
//	mine[x][y - 1] + 
//	mine[x + 1][y - 1] + 
//	mine[x - 1][y + 1] + 
//	mine[x][y + 1] + 
//	mine[x + 1][y + 1] - 8 * '0';
//}

//方法二
int GetMineCount(char mine[ROWS][COLS], int x, int y)
{
	int i = 0;
	int count = 0;
	for (i = -1; i <= 1; i++)
	{
		int j = 0;
		for (j = -1; j <= 1; j++)
		{
			count += mine[x + i][y + j] - '0';
		}
	}
	return count;
}


//排查雷
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
	int x = 0;
	int y = 0;
	int win = 0;
	while (win<row*col-EASY_COUNT)
	{
		printf("请输入要排查的坐标:");
		scanf("%d %d", &x, &y);

		//要正确地输入坐标
		if (x >= 1 && x <= row && y >= 1 && y <= col)
		{
			//输入的坐标是雷
			if (mine[x][y] == '1')
			{
				printf("踩雷被炸,游戏结束\n");
				DisplayBoard(mine, ROW, COL);
				break;
			}
			//输入的坐标不是雷
			else
			{
				int count = GetMineCount(mine, x, y);
				show[x][y] = count + '0';
				DisplayBoard(show, ROW, COL);
				win++;
			}
		}
		else
		{
			printf("输入错误,请重新输入:");
		}
	}

	if (win == EASY_COUNT)
	{
		printf("恭喜你,排雷成功!\n");
	}
	
}

test.c

cpp 复制代码
#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"

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

void game()
{
	srand((unsigned int)time(NULL));
	//定义两个字符串数组
	char mine[ROWS][COLS] = { 0 };//存放雷的信息
	char show[ROWS][COLS] = { 0 };//存放排查出的雷的信息

	//初始化棋盘
	InitBoard(mine, ROWS, COLS, '0');
	InitBoard(show, ROWS, COLS, '*');

	//打印棋盘
	DisplayBoard(show, ROW, COL);

	//布置雷
	SetMine(mine, ROW, COL);

	//排查雷
	FindMine(mine, show, ROW, COL);

}

void test()
{
	int input = 0;
	do
	{
		menu();
		printf("请选择:");
		scanf("%d", &input);
		switch (input)
		{
		case 1:
			game();
			break;
		case 0:
			printf("退出游戏\n");
		default :
			printf("输入错误,请重新输入:");
		}
	} while (input);
}

int main()
{
	test();
	return 0;
}

期待我后续扫雷游戏的详细介绍吧

我是云边有个稻草人

期待与你的下一次相遇!Bye~

相关推荐
怕浪猫6 小时前
RAG 面试 6 连问,从原理到优化全部覆盖
python·算法·面试
yyy(十一月限定版)6 小时前
224redfg
c语言
陈卫军老师6 小时前
陈卫军语录全集总结:12句话,一条主线
经验分享·笔记
无线通信科研笔记6 小时前
IEEE TVT 2026 论文精读与完整复现|相位误差如何重塑近场 RIS 的幅相响应
论文阅读·人工智能·python·算法·论文笔记
2601_962885727 小时前
如何用 Python 自动识别股票的支撑位与压力位?
开发语言·python
mmmmath_37 小时前
二叉树の递归遍历
算法
爱吃奥利奥_wen7 小时前
面向对象三板斧:封装、继承、多态,到底在“装“什么、“承“什么、“变“什么?
开发语言·经验分享·c#
All for pursuit.7 小时前
【位运算-1】338.比特位计数
数据结构·c++·算法·leetcode
我可能是个假开发7 小时前
FTP Unicode 文件名导致 `550` 的问题分析与解决方案
java·开发语言·spring
小师兄吃牛肉8 小时前
C语言中的nullptr有必要吗
c语言·c++·算法