当前位置:网站首页>扫雷小游戏
扫雷小游戏
2022-08-02 00:14:00 【GD_small_bit】
在C语言的学习过程中,自主实现小游戏能提高我们对C语言的兴趣,我今天将为大家带来扫雷小游戏的实现。
扫雷游戏需要的文档
game.c ----- 游戏代码的实现
game.h----- 游戏代码的声明
test.c ----- 扫雷小游戏的检验
菜单的实现
1.选择是否开始游戏
2.选择是否退出游戏
3.判断非法输入
代码如下:
void menu ()
{
printf("######################################################\n");
printf("############### 1.play ####### 0.exit ################\n");
printf("######################################################\n");
}
int main ()
{
int input = 0;
do
{
menu();
scanf("%d",&input);
switch(input)
{
case 1:
printf("游戏开始\n");
game();
break;
case 0:
printf("退出游戏。\n");
break;
default:
printf("输入错误,请重新输入。\n");
break;
}
}while(input);
}
初始化布置雷的表格和排雷信息的表格
void InitBoard(char Board[ROWS][COLS],int rows,int cols,char set)
{
int i = 0;
for(i=0;i<rows;i++)
{
int j = 0;
for(j=0;j<cols;j++)
{
Board[i][j]=set;
}
}
}
随机布置雷
void setmine (char Board[ROWS][COLS],int row,int col)
{
int count = 0;
while(count<easy_count)
{
int x = rand()%row+1;
int y = rand()%col+1;
if(x>=1 && x<=row && y>=1 && y<=col)
{
if(Board[x][y]=='0')
{
Board[x][y]='1';
count++;
}
}
}
}
打印排雷信息
void DisplayBoard(char Board[ROWS][COLS],int row,int col)
{
int i = 0;
int j = 0;
for(i=0;i<=col;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");
}
}
排查雷
int mine_count (char Board[ROWS][COLS],int x,int y)
{
return Board[x-1][y-1]+
Board[x-1][y]+
Board[x-1][y+1]+
Board[x][y-1]+
Board[x][y+1]+
Board[x+1][y-1]+
Board[x+1][y]+
Board[x+1][y+1]-8*'0';
}
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)
{
scanf("%d %d",&x,&y);
//判断坐标合法
if(x>=1 && x <=row && y>=1 && y<=col)
{
//判断该坐标是否被排过
if(Show[x][y]=='*')
{
if(Mine[x][y]=='0')
{
int ret = mine_count(Mine,x,y);
Show[x][y] = ret + '0';
DisplayBoard(Show,ROW,COL);
win++;
}
else
{
printf("你已经被炸死了。\n");
break;
}
}
else
{
printf("你已经排查过该坐标。\n");
}
}
else
{
printf("输入的坐标不合分,请重新输入。\n");
}
}
}
扫雷游戏的整体声明
#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 Board[ROWS][COLS],int rows,int cols,char set);
void DisplayBoard(char Board[ROWS][COLS],int row,int col);
void setmine (char Board[ROWS][COLS],int row,int col);
void Findmine(char Mine[ROWS][COLS],char Show[ROWS][COLS],int row ,int col);
函数的整体调用
void game ()
{
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);
}
关注点一点,下期更精彩。
边栏推荐
- els strip deformation
- Active Disturbance Rejection Control of Substation Inspection Robot Based on Data Drive
- Multidimensional Correlation Time Series Modeling Method Based on Screening Partial Least Squares Regression of Correlation Variables
- Statement执行update语句
- 当奈飞的NFT忘记了Web2的业务安全
- 22. The support vector machine (SVM), gaussian kernel function
- Mean Consistency Tracking of Time-Varying Reference Inputs for Multi-Agent Systems with Communication Delays
- 回顾历史5次经济衰退时期:这一次可能会有何不同?
- 具有通信时延的多自主体系统时变参考输入的平均一致性跟踪
- Disk and file system management
猜你喜欢
【CodeTON Round 2 (Div. 1 + Div. 2, Rated, Prizes!)(A~D)】
Pytorch seq2seq 模型架构实现英译法任务
攻防世界-web-Training-WWW-Robots
How to find new potential projects?Tools recommended
c语言字符和字符串函数总结(二)
These 4 computer notepad software, you have to try
Identify memory functions memset, memcmp, memmove, and memcpy
面试:简单介绍你参与的一个项目
渗透测试与攻防对抗——渗透测试基础
Interview high-frequency test questions solution - stack push and pop sequence, effective parentheses, reverse Polish expression evaluation
随机推荐
2022/08/01 Study Notes (day21) Generics and Enums
Constructor, this keyword, method overloading, local variables and member variables
这 4 款电脑记事本软件,得试试
使用jOOQ将Oracle风格的隐式连接自动转换为ANSI JOIN
Quick solution for infix to suffix and prefix expressions
Disk and file system management
基于超参数自动寻优的工控网络入侵检测
Mean Consistency Tracking of Time-Varying Reference Inputs for Multi-Agent Systems with Communication Delays
众筹DAO“枯萎”的缩影:曾拍下《沙丘》未出版手稿的Spice DAO解散
An Enhanced Model for Attack Detection of Industrial Cyber-Physical Systems
JSP out. The write () method has what function?
What is the function of the JSP Taglib directive?
Knowing the inorder traversal of the array and the preorder traversal of the array, return the postorder history array
什么是低代码(Low-Code)?低代码适用于哪些场景?
JSP 如何获取request对象中的路径信息呢?
[Solution] Emqx startup under win10 reports Unable to load emulator DLL, node.db_role = EMQX_NODE__DB_ROLE = core
业务测试如何避免漏测 ?
C language character and string function summary (2)
uni-app project summary
CRS management and maintenance