当前位置:网站首页>扫雷小游戏
扫雷小游戏
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);
}
关注点一点,下期更精彩。
边栏推荐
猜你喜欢

Pytorch seq2seq 模型架构实现英译法任务

Unknown CMake command “add_action_files“

Knowing the inorder traversal of the array and the preorder traversal of the array, return the postorder history array

Redis的集群模式

不要用jOOQ串联字符串

MLX90640 红外热成像仪测温传感器模块开发笔记(十) 成果展示-红眼睛相机

When Netflix's NFTs Forget Web2 Business Security

632. Minimum interval

短视频seo搜索优化主要内容

22. The support vector machine (SVM), gaussian kernel function
随机推荐
辨析内存函数memset、memcmp、memmove以及memcpy
NFT工具合集
基于数据驱动的变电站巡检机器人自抗扰控制
AXI4协议介绍
Trie详解
IP Core: FIFO
Short video SEO search operation customer acquisition system function introduction
一文概览最实用的 DeFi 工具
JSP Taglib指令具有什么功能呢?
els block deformation judgment.
What does the errorPage attribute of the JSP page directive do?
Pytorch seq2seq 模型架构实现英译法任务
使用jOOQ将Oracle风格的隐式连接自动转换为ANSI JOIN
Routing strategy
Short video SEO optimization tutorial Self-media SEO optimization skills and methods
JSP out.println()方法具有什么功能呢?
An Enhanced Model for Attack Detection of Industrial Cyber-Physical Systems
Disk and file system management
An overview of the most useful DeFi tools
PHP从txt文件中读取数据的方法