当前位置:网站首页>扫雷简单版
扫雷简单版
2022-07-29 02:46:00 【风起、风落】
前言
代码解读在代码中
1.test.c
#include"game.h"
void menu()
{
printf("***********************\n");
printf("*******1.play 0.exit***\n");
printf("***********************\n");
}
void game()
{
char mine[rows][cols] = {
0 };
char slow[rows][cols] = {
0 };//初始化 多了两行两列 就是为了不越界
initboard(mine, row+2, col+2,'0');//假设为9*9的雷盘 变成11*11是为了防止在最边上的坐标寻找周围8个坐标时 越界
initboard(slow, row+2, col+2, '*');//初始化 我们要看到的雷盘
displayboard(slow, row, col);//打印雷盘
setmine(mine, col, row);//布置雷
findmine(mine,slow, row, col);//排雷
}
int main()
{
srand((unsigned int)time(NULL));
int input = 0;
do
{
menu();
printf("输入数字:>");
scanf("%d", &input);
switch(input)
{
case 1:
game();
break;
case 0:
printf("退出游戏\n");
break;
default:
printf("输入错误\n");
break;
}
} while (input);
return 0;
}
2.game.h
#define _CRT_SECURE_NO_WARNINGS
#define row 9
#define col 9
#define rows row+2
#define cols col+2
#define easycount 10
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
void initboard(char board[rows][cols], int Rows, int Cols);
void displayboard(char board[row][col], int Row, int Col);
void setmine(char mine[row][col], int Row, int Col);
void findmine(char mine[row][col],char slow[row][col], int Row, int Col);
3.game.c
#include"game.h"
void initboard(char board[rows][cols], int Rows, int Cols,char set)
{
int i = 0;
int j = 0;
for (i = 0; i < Rows; i++)
{
for (j = 0; j < Cols; j++)
{
board[i][j] = set;// mine雷盘是用来埋雷的 要传过来'0'
} //slow雷盘是用来排查雷的 要传过来'*'
}
}
void displayboard(char board[row][col], int Row, int Col)//打印雷盘
{
int i = 0;
int j = 0;//实际看到的9*9的雷盘 所以传过来 行 9 列 9
for (i = 1; i <= Row; i++)
{
for (j = 1; j <= Col; j++)
{
printf("%c ", board[i][j]);
}
printf("\n");
}
}
void setmine(char mine[row][col], int Row, int Col)//布置雷
{
int count = 10;//设置雷数为10
while (count)
{
int x = rand() % row + 1;//将x y的坐标设为随机数
int y = rand() % col + 1;
if (mine[x][y] == '0')
{
mine[x][y] = '1';
count--;
}
}
}
int nofind(char mine[row][col], int x, int y)//查看坐标的周围8个坐标是否有雷
{
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] + mine[x + 1][y + 1] + mine[x + 1][y - 1]) - 8 * '0';
}//因为是字符数字 所以需要减去字符0 变成数字
void findmine(char mine[row][col], char slow[row][col],int Row, int Col)//排查雷
{
int x = 0;
int y = 0;
int count = 0;
int win = 0;
while (win<row*col-easycount)//雷盘为9*9 -10个雷 若将不是雷的都找到了 则跳出循环
{
printf("输入要排查雷的坐标:>");
scanf("%d%d", &x, &y);
int s1 = x;
int s2 = y;
if(x>=1&&y<=Row&&y>=1&&y<=Col)//雷的范围
{
if (mine[x][y] == '1')
{
printf("被炸死了\n");
displayboard(mine, row, col);
break;
}
count = nofind(mine, x, y);
slow[x][y] = count+'0';//将周围有没有雷的数字传到slow雷盘中
displayboard(slow, row, col); //因为里面存放字符 所以要转化到字符
win++;
}
else
{
printf("坐标非法\n");
}
}
if (win == row * col - easycount)//easycount 为设置雷数
{
printf("排雷成功\n");
displayboard(mine, row, col);//将雷的分布打印
}
}
边栏推荐
- 明明开发薪资高,为什么我还是选了测试?
- 创客教育的起源和内涵的基本理念
- C#从网址异步获得json格式的数据
- Multithreading realizes concurrent reading and execution of multi use case files +selenium grid4 realizes distributed deployment of test framework
- Notes on the sixth day
- 11.书写规则-伪目标
- [opencv] use OpenCV to call mobile camera
- 金山云回港上市:中国TO B云厂商的港股奔袭
- 并发模式之异步回调Future模式
- HTB-Blue
猜你喜欢
Cloud development pocket toolbox wechat applet source code
Flink生产环境经典问题汇总
PHP lucky draw system with background source code
Add a row to a specific location in the dataframe
Notes on the seventh day
Interpretation of ue4.25 slate source code
Implementation principle of golang synergy
SOA(面向服务架构)是什么?
MPEG音频编码三十年
New UI Sifang aggregate payment system source code / new usdt withdrawal / latest update security upgrade to fix XSS vulnerability patch vulnerability
随机推荐
navicat新建数据库
Pytest环境部署+用例执行管理+用例参数化
MySQL 操作数据库数据报错:Fatal error encountered during command execution
第09章_性能分析工具的使用
C language: Little Lele and Euclid
常用hooks总结
工科男生:20岁未满,平凡但不平庸
盘点国内外项目协同管理软件:SaaS和定制化成趋势
11.书写规则-伪目标
并发模式之单例和不变模式
Wechat applet - Advanced chapter Lin UI component library source code analysis button component (II)
6-21 vulnerability exploitation MySQL weak password cracking
并发模式之生产者消费者模式
Cloud development pocket toolbox wechat applet source code
idea替换所有文件中的内容
混淆矩阵学习笔记
Flink内核源码(七)Flink SQL提交流程
外挂---线段树懒标记板子+简单数学推理
DHCP protocol detailed analysis
[untitled]