当前位置:网站首页>扫雷简单版
扫雷简单版
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);//将雷的分布打印
}
}
边栏推荐
猜你喜欢

Source code of Jiugongge heart puzzle Applet / source code of main wechat applet with traffic
![[error reporting] node:internal/modules/cjs/loader:936 [solution]](/img/73/849c58c814c1a47dff0cde0e365c75.png)
[error reporting] node:internal/modules/cjs/loader:936 [solution]

冰冰学习笔记:运算符重载---日期类的实现

New conch movie theme template m3.1 fully decrypted version multifunctional apple cmsv10 background adaptive theme open source fully decrypted version

会议OA项目之我的审批功能

Confusion matrix learning notes

Some new ideas about time complexity

C language: Little Lele and hexadecimal conversion

金山云回港上市:中国TO B云厂商的港股奔袭

OSPF experiment
随机推荐
Analysis of concepts and terms in data warehouse
R语言ERROR: compilation failed for package ‘****‘
Etcd implementation of large-scale service governance application practice
.NET 序列化枚举为字符串
MySQL - the difference between count (field), count (primary key), count (1), count (*)
Pytest environment deployment + use case execution management + use case parameterization
Thirty years of MPEG audio coding
Summary of common hooks
groupby 方法
混淆矩阵学习笔记
Confusion matrix learning notes
并发模式之生产者消费者模式
Multiple inheritance and derived class member identification
第十天笔记
【luogu P8352】小 N 的独立集(DP套DP)(性质)
解读AI机器人养宠引领时尚潮流
C语言:空心正方形图案
FTP协议详解
盘点国内外项目协同管理软件:SaaS和定制化成趋势
Polygon zkevm - Introduction to HERMEZ 2.0