当前位置:网站首页>C语言实现简易扫雷(附带源码)
C语言实现简易扫雷(附带源码)
2022-08-11 05:25:00 【Rserendipity】
学习C语言
源码来源:b站课程以及文件来源
在b站上听这位老师讲的课,自己跟着打了一边代码,分享给大家
需要在同一个工程下创建两个.c 文件,一个.h文件
头文件/函数声明(.h文件)
#define _CRT_SECURE_NO_WARNINGS
#define ROW 9
#define COL 9
#define ROWS ROW + 2
#define COLS COL + 2
#define Easy_Count 10
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void InitBoard(char board[ROWS][COLS], int rows, int cols, char set);
void DisplayBoard(char board[ROWS][COLS], int row, int col);
void Setmap(char board[ROWS][COLS], int row, int col);
void FindMap(char map[ROWS][COLS],char show[ROWS][COLS],int row,int col);
int get_map_count(char map[ROWS][COLS], int x, int y);
Game.c
#include "Game.h"
void InitBoard(char board[ROWS][COLS], int rows, int cols, char set)
{
int i, j;
for (i = 0; i < rows; i++)
{
for (j = 0; j < cols; j++)
{
board[i][j] = set;
}
}
}
void DisplayBoard(char board[ROWS][COLS], int row, int col)
{
int i, j;
for (i = 0; i <= row; i++)
{
printf("%d ", i);
}
printf("\n");
for (i = 1; i <= row; i++)
{
printf("%d ", i);
for (j = 1; j <= col; j++)
{
printf("%c ", board[i][j]);
}
printf("\n");
}
}
void Setmap(char board[ROWS][COLS], int row, int col)
{
int count = Easy_Count;
while (count)
{
int x = rand() % ROW + 1;//1~9之间的数
int y = rand() % COL + 1;
if (board[x][y] == '0')
{
board[x][y] = '1';
count--;
}
}
}
void FindMap(char map[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
int x, y;
int chenggong = 0;
while (chenggong < row*col - Easy_Count)
{
printf("请输入排查雷的坐标:>");
scanf("%d%d", &x, &y);
if (x >= 1 && x <= row && y >= 1 && y <= col)
{
if (map[x][y] == '1')
{
printf("很遗憾,你被炸死了\n");
DisplayBoard(map, row, col);
break;
}
else
{
int count = get_map_count(map, x, y);
show[x][y] = count + '0';
DisplayBoard(show, row, col);
chenggong++;
}
}
else
{
printf("请输入合法坐标!\n");
}
if (chenggong == row * col - Easy_Count)
{
printf("恭喜排雷成功!\n");
DisplayBoard(map, ROW, COL);
}
}
}
int get_map_count(char map[ROWS][COLS], int x, int y)
{
return map[x - 1][y] +
map[x - 1][y - 1] +
map[x][y - 1] +
map[x + 1][y - 1] +
map[x + 1][y] +
map[x + 1][y + 1] +
map[x][y + 1] +
map[x - 1][y + 1] - 8 * '0';
}
Test.c
#include "Game.h"
void menu()
{
printf("\n");
printf("****************************\n");
printf("***** 扫雷 ****\n");
printf("***** 请输入1或者0 ****\n");
printf("***** 1.play 0.exit ****\n");
printf("****************************\n");
printf("\n");
}
void Game()
{
//创建空白数组
char map[ROWS][COLS] = {
0 };
char show[ROWS][COLS] = {
0 };
//初始化
InitBoard(map, ROWS, COLS, '0');
InitBoard(show, ROWS, COLS, '*');
//打印棋盘
//DisplayBoard(map, ROW, COL);
DisplayBoard(show, ROW, COL);
//随机生成雷
Setmap(map, ROW, COL);
//DisplayBoard(map, ROW, COL);
//扫雷
FindMap(map, show, ROW, COL);
}
void test()
{
int input = 0;
srand((unsigned int)time(NULL));
do
{
menu();
printf("请选择:>");
scanf("%d", &input);
switch (input)
{
case 1:
Game();
break;
case 0:
printf("退出游戏\n");
default:
printf("选择错误!\n");
break;
}
} while (input);
}
int main()
{
test();
return 0;
}
最后再次说明:原创作者为b站的鹏老师,个人仅为听课程后搬运代码,如有侵权,取得联系后将尽快删除
边栏推荐
- 实时姿态估计--基于空洞卷积的人体姿态估计网络
- Node-2.垃圾回收机制
- Realize data exchange between kernel and userspace through character device virtual file system (passed based on kernel 5.8 test)
- 电商机会:私域
- Zhejiang University School of Software 2020 Guarantee Research Computer Real Question Practice
- 物联网IOT 固件升级
- 弱监督语义分割CLIMS(CVPR2022)
- 产品经理人物推荐
- net6的Web MVC项目实现限流功能
- 防盗链——防止其他页面通过url直接访问本站资源
猜你喜欢
SWOT分析法
OpenPCDet installs the latest version: spconv in one step
pip安装报错:is not a supported wheel on this platform
Introduction of safety helmet wearing recognition system
华为IOT平台温度过高时自动关闭设备场景试用
LAGRANGIAN FLUID SIMULATION WITH CONTINUOUS CONVOLUTIONS
目标检测思维导图
Safety helmet recognition - construction safety "regulator"
Kotlin 增量编译的新方式 | 技术解析
关于mmdetection框架实用小工具说明
随机推荐
解决jupyter中import torch出错问题
Hardhat Recognition System - Solving Regulatory Conundrums
JVM调优整理
关于mmdetection框架实用小工具说明
Diagnostic Log and Trace——为应用程序和上下文设置日志级别的方法
从概念认识AI
CMT2380F32模块开发6-flash例程
Realize data exchange between kernel and userspace through character device virtual file system (passed based on kernel 5.8 test)
红外线应用-红外遥控
vscode插件开发——懒人专用markdown插件开发
Mei cole studios - fifth training DjangoWeb application framework + MySQL database
aPaaS和iPaaS的区别
SWOT分析法
产品版本号是如何确定的
umi约定式路由规则修改
Diagnostic Log and Trace——DLT 离线日志存储
Argparse模块 学习
STM32学习笔记(白话文理解版)—小灯的点亮、闪烁、呼吸
The kernel communicates with user space through character devices
Asp doNet Mvc4绑定js脚本用法