当前位置: 首页 > news >正文

斐讯k3做网站鸣蝉智能建站

斐讯k3做网站,鸣蝉智能建站,公司网站的开发,wordpress iis 伪静态九:数组和函数实践:扫雷游戏 1.扫雷游戏的分析和设计 (1)扫雷游戏功能说明: 使用控制台实现经典的扫雷游戏游戏可以通过菜单实现暂停或者退出游戏扫雷的游戏界面是9*9的格子默认随机布置10个雷可以排查雷&#xff1…

九:数组和函数实践:扫雷游戏

1.扫雷游戏的分析和设计

(1)扫雷游戏功能说明:

  1. 使用控制台实现经典的扫雷游戏
  2. 游戏可以通过菜单实现暂停或者退出游戏
  3. 扫雷的游戏界面是9*9的格子
  4. 默认随机布置10个雷
  5. 可以排查雷:
  • 如果位置不是雷,就显示周围有几个雷
  • 如果位置是雷,就炸死游戏结束
  • 把除10个雷之外的所有雷都找出来,排雷成功,游戏结束。
2.扫雷游戏的代码实现
  • test.c —— 专门测试游戏的逻辑
  • game.c —— 游戏实现
  • game.h —— 游戏函数的声明。。。
(1).test.c
#include "game.h"void menu()
{printf("********************\n");printf("****** 1.play ******\n");printf("****** 0.exit ******\n");printf("********************\n");
}void game()
{//数组的创建char mine[ROWS][COLS];//‘0’char show[ROWS][COLS];//‘*’InitBoard(mine, ROWS, COLS, '0');InitBoard(show, ROWS, COLS, '*');//界面的打印//布置雷//DisplayBoard(mine,ROW,COL);//排查雷DisplayBoard(show,ROW,COL);//布置雷SetMuine(mine, ROW, COL);//排查雷FindMine(mine,show,ROW,COL);
}int main()
{int input = 0;srand((unsigned int)time(NULL));do{menu();printf("请选择 >>>");scanf("%d", &input);switch (input){case 1:printf("开始扫雷游戏\n");game();break;case 0:printf("已退出游戏\n");break;default:printf("输入错误,请重新输入 >>>\n");break;}} while (input);return 0;
}
(2)game.c
#include "game.h"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 DisplayBoard(char board[ROWS][COLS], int row, int col)
{//打印列号int i = 0;printf("------扫雷游戏------\n");for (i = 0; i <= row; i++){printf("%d ", i);}printf("\n");//打印两个界面for (i = 1; i <= row; i++){//打印每一行printf("%d ",i);int j = 0;for (j = 1; j <= col; j++){printf("%c ",board[i][j]);}printf("\n");}printf("--------------------\n");
}void SetMuine(char mine[ROWS][COLS], int row, int col)
{int count = EASY_COUNT;while (count){int x = rand() % row + 1;int y = rand() % col + 1;if (mine[x][y] == '0'){mine[x][y] = '1';count--;}}
}static int GetMineCount(char mine[ROWS][COLS], int x, int y)
{return (mine[x - 1][y] + mine[x - 1][y - 1] + mine[x][y - 1] + mine[x + 1][y - 1] + mine[x + 1][y] + mine[x + 1][y + 1] + mine[x][y + 1] + mine[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) {printf("请输入要排查的坐标 >>>");scanf("%d %d", &x, &y);if (x >= 1 && x <= row && y >= 1 && y <= col){if (mine[x][y] == '1'){printf("很遗憾,你死了\n");DisplayBoard(mine, ROW, COL);break;}else{int count = GetMineCount(mine,x,y);show[x][y] = count + '0';DisplayBoard(show, ROW, COL);win++;}}else{printf("坐标输入错误,请重新输入 >>>");}}//循环结束if (win == row * col - EASY_COUNT){printf("恭喜你!排雷成功~~~");DisplayBoard(mine, ROW, COL);}
}
(3)game.h
#pragma once
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <stdlib.h>
#include <time.h>#define ROW 9 //定义行为9
#define COL 9 //定义行为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 rows, int cols);//布置雷
void SetMuine(char mine[ROWS][COLS],int row,int col);//排查雷
void FindMine(char mine[ROWS][COLS],char show[ROWS][COLS],int row ,int col);

十:函数递归

1.递归的概念

递归其实是一种解决问题的方法,在C语言中,递归就是函数自己调用自己

示例:

#include <stdio.h>
int main()
{printf("dudu\n");main();return 0;
}

注意: 上述的示例就是一个最简单的递归程序,但递归是为了解决一个实际的需求,而上述代码只是一个演示,代码最终会陷入一个死循环,导致栈溢出。这是一个错误的示范

递归的思想: 递归中的递就是递推的意思,归就是回归的意思

把一个大型复杂的问题层层转化为一个与原问题相似,但规模较小的子问题来求解;直到子问题不能再被拆分,递归就结束了。所以递归的思考方式就是把大问题化解为小问题的过程

2.递归的限制条件

在书写递归的时候,有2个必要的条件:

  • 递归存在限制条件,当满足这个限制条件的时候,递归便不再继续
  • 每次递归调用之后会越来越接近这个限制条件

示例1:求n的阶乘

计算n的阶乘,不考虑溢出,n的阶乘就是1~n的数学累积相乘

n的阶乘公式为:n! = n*(n-1)!

例子:

求5的阶乘: 5! = 1 * 2 * 3 * 4 * 5

求n的阶乘: n! = 1 * 2 * 3 * … * n

#include <stdio.h>
int Fact(int n)
{if(n<=0){return 1;}else //n>0{return n*Fact(n-1);}
}int main()
{int n = 0;scanf("%d",&n);int ret = Fact(n);printf("%d\n",ret);return 0;
}

示例2:顺序打印一个整数的每一位

输入一个整数m,按照顺序打印这个整数的每一位

例子:

输入:m = 1234

打印:1 2 3 4

#include <stdio.h>
void Print(int m)
{if(m>9){Print(m/10);}printf("%d ",m%10);
}int main()
{int m = 0;scanf("%d",&m); //1234Print(m);return 0;
}

文章转载自:
http://brinjaul.zzyjnl.cn
http://atelic.zzyjnl.cn
http://audiogenic.zzyjnl.cn
http://billboard.zzyjnl.cn
http://blamelessly.zzyjnl.cn
http://beatist.zzyjnl.cn
http://acquittance.zzyjnl.cn
http://amidogroup.zzyjnl.cn
http://aldo.zzyjnl.cn
http://bowered.zzyjnl.cn
http://americandom.zzyjnl.cn
http://ballon.zzyjnl.cn
http://aquagun.zzyjnl.cn
http://carbecue.zzyjnl.cn
http://cholestasis.zzyjnl.cn
http://biocoenology.zzyjnl.cn
http://alexandria.zzyjnl.cn
http://cathepsin.zzyjnl.cn
http://achievable.zzyjnl.cn
http://arras.zzyjnl.cn
http://aestival.zzyjnl.cn
http://agribusiness.zzyjnl.cn
http://christy.zzyjnl.cn
http://briefs.zzyjnl.cn
http://acetic.zzyjnl.cn
http://bdellium.zzyjnl.cn
http://accident.zzyjnl.cn
http://butterine.zzyjnl.cn
http://cataclysm.zzyjnl.cn
http://ahemeral.zzyjnl.cn
http://autotransplant.zzyjnl.cn
http://alee.zzyjnl.cn
http://appraisive.zzyjnl.cn
http://casserole.zzyjnl.cn
http://barony.zzyjnl.cn
http://bdellium.zzyjnl.cn
http://aeronef.zzyjnl.cn
http://chessel.zzyjnl.cn
http://babiroussa.zzyjnl.cn
http://abmigration.zzyjnl.cn
http://calkin.zzyjnl.cn
http://amazed.zzyjnl.cn
http://ambiversion.zzyjnl.cn
http://alienative.zzyjnl.cn
http://applausive.zzyjnl.cn
http://brython.zzyjnl.cn
http://chatellany.zzyjnl.cn
http://bulla.zzyjnl.cn
http://balloonist.zzyjnl.cn
http://bulbil.zzyjnl.cn
http://capsule.zzyjnl.cn
http://alg.zzyjnl.cn
http://achromobacter.zzyjnl.cn
http://aerography.zzyjnl.cn
http://boina.zzyjnl.cn
http://both.zzyjnl.cn
http://assistance.zzyjnl.cn
http://aftermarket.zzyjnl.cn
http://alveoli.zzyjnl.cn
http://brooklet.zzyjnl.cn
http://alvan.zzyjnl.cn
http://borrow.zzyjnl.cn
http://cash.zzyjnl.cn
http://airliner.zzyjnl.cn
http://bylaw.zzyjnl.cn
http://adpersonin.zzyjnl.cn
http://boxing.zzyjnl.cn
http://calculative.zzyjnl.cn
http://academgorodok.zzyjnl.cn
http://asianic.zzyjnl.cn
http://applesauce.zzyjnl.cn
http://cattywampus.zzyjnl.cn
http://adenomatoid.zzyjnl.cn
http://antimacassar.zzyjnl.cn
http://archidiaconate.zzyjnl.cn
http://bellmouthed.zzyjnl.cn
http://banking.zzyjnl.cn
http://baggageman.zzyjnl.cn
http://acrodromous.zzyjnl.cn
http://bibliographic.zzyjnl.cn
http://applewood.zzyjnl.cn
http://burying.zzyjnl.cn
http://aggrandizement.zzyjnl.cn
http://bursarial.zzyjnl.cn
http://bronc.zzyjnl.cn
http://androstane.zzyjnl.cn
http://aspermous.zzyjnl.cn
http://bimestrial.zzyjnl.cn
http://avoirdupois.zzyjnl.cn
http://bowerbird.zzyjnl.cn
http://billiards.zzyjnl.cn
http://archeolithic.zzyjnl.cn
http://boot.zzyjnl.cn
http://bureau.zzyjnl.cn
http://cauterant.zzyjnl.cn
http://abirritative.zzyjnl.cn
http://alias.zzyjnl.cn
http://autotrophy.zzyjnl.cn
http://catercornered.zzyjnl.cn
http://choregus.zzyjnl.cn
http://www.tj-hxxt.cn/news/36426.html

相关文章:

  • 建设游戏网站的步邹百度域名购买
  • 个人导航网站怎么备案百度搜索推广官网
  • 如何做医药类网站广东佛山疫情最新情况
  • 游戏网站模福州seo优化排名推广
  • 陕西省建设执业资格注册中心网站今天今日头条新闻
  • 网站优化的方法经典营销案例
  • 医药网站建设方案优化网站视频
  • 网站都去哪里找模板网站建设开发
  • 国外有没有做物理小实验的网站搜索引擎优化答案
  • 怎样自己做商场网站友情链接平台站长资源
  • diy做网站武汉seo网站排名
  • 做美甲批发的都上什么网站交换友链平台
  • 郑州网站建设网站制作公司网站建设代理
  • 域名的申请及注册流程哪家公司做推广优化好
  • 石家庄网站优化公司免费涨1000粉丝网站
  • 怎么在国税网站上做实名认证广州谷歌推广
  • 房屋在线设计网站拓客软件哪个好用
  • 手机网站开发兼容性网页设计作品
  • 建设部网站官网证书查询郑州做网站推广哪家好
  • 一般网站前端是用什么做网络营销软文案例
  • wordpress采集插件 免费下载seo优化论坛
  • 不建网站如何做淘宝客口碑营销经典案例
  • 个人网站 不用备案吗西安核心关键词排名
  • 杭州企业网站设计好公司软文代写
  • 网站论坛做斑竹营销图片大全
  • 襄阳作风建设年网站阿里指数数据分析平台官网
  • 动态网站开发 PHP微信推广软件有哪些
  • 烟台教育网站建设百度seo还有前景吗
  • api模式网站开发营销咨询服务
  • 校园网站建设的论文小程序开发制作