当前位置:网站首页>c语言基础篇:猜数字小游戏
c语言基础篇:猜数字小游戏
2022-07-15 08:30:00 【虎太郎的继承】
一、设计思路
实现一个猜数字的小游戏
1.菜单
2.电脑随机生成一个随机数
3.玩家输入数字进行比较
4.判断输赢
二、程序设计
1.头文件的包含
由于需要电脑随机生成一个随机数,所以需要使用**rand()函数和srand()**函数
1.rand()
rand()函数用来产生随机数,但是,rand()的内部实现是用线性同余法实现的,是伪随机数,由于周期较长,因此在一定范围内可以看成是随机的。
rand()会返回一个范围在0到RAND_MAX(32767)之间的伪随机数(整数)。
在调用rand()函数之前,可以使用srand()函数设置随机数种子,如果没有设置随机数种子,rand()函数在调用时,自动设计随机数种子为1.随机种子相同,每次产生的随机数也会相同。
rand()函数需要的头文件时:<stdlib.h>
rand()函数的原型:int rand(void);
使用rand()函数产生1-100以内的随机整数:int num = rand() % 100;
2.srand()
srand()函数需要的头文件仍然时:<stdlib.h>
srand()函数的原型:void srand(unsigned int seed);
srand()用来设置rand()产生随机数时随机数种子。参数seed是整数,通常可以利用time(0)或geypid(0)的返回值作为seed。
3.使用rand()和srand()产生指定范围内的随机整数的方法
“模板+加法”的方法
对于任意数,0<=rand()%(n-m+1)<=n-m
0+m<=rand()%(n-m+1)+m<=n-m+m
因此,如果产生[m,n]范围内的随机数num,可用:
int num = rand() % (n-m+1) + m;
由于代码需要打印函数所以要使用头文件stdio.h
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
2.菜单
玩家输入1进入游戏,输入0退出游戏
void menu()
{
printf("******************************\n");
printf("******** 1.play ********\n");
printf("******** 0.exit ********\n");
printf("******************************\n");
}
3.game()函数
void game()
{
int ret = rand() % 100 + 1;//随机获取1-100的数
int guess = 0;
while (1)
{
printf("\n请输入数字:>");
scanf("%d", &guess);
if (guess < ret)
{
printf("猜小了\n");
}
else if (guess > ret)
{
printf("猜大了\n");
}
else
{
printf("恭喜你猜对了!\n");
break;
}
}
}
4.主函数
int main()
{
int input = 0;
srand((unsigned)time(NULL));
do
{
menu();
printf("请输入你选择的选项:>");
scanf("%d", &input);
switch (input)
{
case 1:
game();
break;
case 0:
printf("退出游戏\n");
break;
default:
printf("输入错误,请重新输入\n");
}
} while (input);
return 0;
}
三、效果展示



边栏推荐
- ArkUI常见问题汇总【系列3】
- Niuke SQL brush questions third lie - SQL big factory interview real questions
- 【无标题】csdn
- C language power buckle question 23 of the combined K ascending sequence list. Four methods
- How does Oracle view the database alarm log?
- 微信小程序——在小程序自定义组件中获取元素定位及宽度
- Google API error model
- Database configuration white / black list
- List collection
- MySQL——启动 MySQL报错net start mysql 发生系统错误 5。 拒绝访问
猜你喜欢
随机推荐
【mysql】mysql分别按年/月/日/周分组统计数据
The U.S. government will invest $56million in solar product manufacturing projects
开源篇--精准定位 模型重心坐标
Complex network modeling (6)
Understanding and operation of array
Schéma de traitement à haut taux d'utilisation des Redo
关于NLP自监督学习,面试时被问崩溃了!
Graphic array calculation module numpy (specify numerical type, numerical type, two-dimensional array index, two-dimensional array slice index, array remodeling, array addition, deletion, modification
rman copy 方式迁移数据文件
Oracle的錶空間與數據庫有什麼關系?
Open source -- accurately locate the center of gravity coordinates of the model
Hisilicon universal platform construction: online debugging 3vscode plug-in
Talk about digital transformation Office
What are the differences between database and schema?
Win11删除英文输入法的方法教程
NLP learning | first acquaintance with NLP
TFTLCD 薄膜晶体管液晶显示器——探索者为例
The activities I participated in
如何关闭PolarDB for PostgreSQL?
Std:: vector to array to eigen:: matrix



![[untitled]](/img/6c/df2ebb3e39d1e47b8dd74cfdddbb06.gif)





![Arkui FAQ summary [series 3]](/img/d6/3bd0b3ce22d764d9431f3588ed29a3.png)