当前位置:网站首页>3.猜数字游戏
3.猜数字游戏
2022-07-06 09:20:00 【是王久久阿】
猜数字游戏:随机生成1~100的数,让用户去猜。当猜的数比随机数大时,提示“猜大了”;当才的数比随机数小时,提示“猜小了”;猜的数与随机数相同时,提示“猜对了”。
1.整体框架
首先,在开始时要有一个菜单,菜单栏中打印出选项:1.paly、0.exit。
然后,玩家输入选项,当输入1时,开始游戏;输入0时,结束游戏;出去其他数时,重新输入。并且将游戏设置成可以循环玩的,玩完一次后,可以接着玩。
#include<stdio.h>
void menu()
{
printf("******************\n");
printf("***** 1.paly *****\n");
printf("***** 0.exit *****\n");
printf("******************\n");
}
int main()
{
int input = 0;
srand((unsigned int)time(NULL));//用srand修饰rand
do
{
menu();
printf("请选择(1/0):>");
scanf("%d", &input);
switch (input)
{
case 0:
printf("退出游戏\n");
break;
case 1:
printf("开始游戏,请猜数字(1~100)\n");
game();//游戏
break;
default:
printf("输入错误,请重新输入\n");
}
} while (input);
return 0;
}
2.游戏部分
当整体框架写完成后,运行测试一下。测试成功,开始写游戏部分game()代码。
游戏部分的设计主要为:系统生成一个数字——玩家输入——判断——猜错了——提示——重新输入——判断——猜对了(或是次数用尽了)
因为整个过程需要不断的输入、判断,所以需要用到while循环。(注意:生成随机数部分不能放进while循环中,否则每循环一次,都将生成新的随机数)
void game()
{
int num = rand() % 100 + 1;//生成1~100的随机数
int guess = 0;
int count = 6;
while (count--)//6次机会;后置++,先使用再++
{
printf("请输入:>\n");
scanf("%d", &guess);
if (guess > num)
{
printf("猜大了\n");
}
else if (guess < num)
{
printf("猜小了\n");
}
else
{
printf("猜对了\n");
Sleep(500);//停止运行500毫秒
system("cls");//清空屏幕.......
break;
}
}
printf("对不起,机会已用完\n");
Sleep(500);//停止运行500毫秒
system("cls");//清空屏幕
}
(1)当使用rand()函数时创建随机值时,会发现每次的随机值都一样,利用MSDN,查询可知:
这时需要在主函数用srand修饰一下rand的函数,利用时间time,来生成一个随机数。
srand((unsigned int)time(NULL)),这里(unsigned int)将time的返回值强制类型转换,转成换无符号整型,然后给time传一个空指针(NULL)即可。使用rand需要包含头文件<stdlib.h>,使用time需要包含<time.h>。
(2)int num = rand() % 100 + 1,当随机数%100时,其范围是0~99,所以需要再+1。
(3)while (count--),这里指定有count次机会,后置--表示为先使用,再--,当count=0时结束循环。
(4)sleep(a)为停止运行a毫秒,system("cls")为清空屏幕,需包含头文件<windows.h>。
整体代码如下:
//猜数字游戏
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include <windows.h>
void menu()
{
printf("******************\n");
printf("***** 1.paly *****\n");
printf("***** 0.exit *****\n");
printf("******************\n");
}
void game()
{
int num = rand() % 100 + 1;//生成1~100的随机数
int guess = 0;
int count = 6;
while (count--)//6次机会;后置++,先使用再++
{
printf("请输入:>\n");
scanf("%d", &guess);
if (guess > num)
{
printf("猜大了\n");
}
else if (guess < num)
{
printf("猜小了\n");
}
else
{
printf("猜对了\n");
Sleep(500);//停止运行500毫秒
system("cls");//清空屏幕.......
break;
}
}
printf("对不起,机会已用完\n");
Sleep(500);//停止运行500毫秒
system("cls");//清空屏幕
}
int main()
{
int input = 0;
srand((unsigned int)time(NULL));//用srand修饰rand
do
{
menu();
printf("请选择(1/0):>");
scanf("%d", &input);
switch (input)
{
case 0:
printf("退出游戏\n");
break;
case 1:
printf("开始游戏,请猜数字(1~100)\n");
game();//游戏
break;
default:
printf("输入错误,请重新输入\n");
}
} while (input);
return 0;
}
如果想将我们制作的游戏发给同学试玩,将VS中debug改成release,测试并保存,到项目保存的地方找到release文件。
X86为32位,推荐保存X64,将X64发给同学可正常运行。
点击release——将其中的exe文件发给同学即可。
边栏推荐
- Database operation of tyut Taiyuan University of technology 2022 database
- arduino+水位传感器+led显示+蜂鸣器报警
- 西安电子科技大学22学年上学期《射频电路基础》试题及答案
- View UI plus released version 1.3.1 to enhance the experience of typescript
- TYUT太原理工大学2022数据库大题之概念模型设计
- View UI Plus 发布 1.1.0 版本,支持 SSR、支持 Nuxt、增加 TS 声明文件
- Application architecture of large live broadcast platform
- Record: solution of 404 error of servlet accessing database in dynamic web project
- 抽象类和接口
- 西安电子科技大学22学年上学期《信号与系统》试题及答案
猜你喜欢
阿里云微服务(二) 分布式服务配置中心以及Nacos的使用场景及实现介绍
View UI Plus 发布 1.3.0 版本,新增 Space、$ImagePreview 组件
2年经验总结,告诉你如何做好项目管理
Relational algebra of tyut Taiyuan University of technology 2022 database
Alibaba cloud microservices (I) service registry Nacos, rest template and feign client
Differences and application scenarios between MySQL index clock B-tree, b+tree and hash indexes
Alibaba cloud microservices (III) sentinel open source flow control fuse degradation component
阿里云一面:并发场景下的底层细节 - 伪共享问题
Counter attack of flour dregs: redis series 52 questions, 30000 words + 80 pictures in detail.
Arduino+ds18b20 temperature sensor (buzzer alarm) +lcd1602 display (IIC drive)
随机推荐
西安电子科技大学22学年上学期《基础实验》试题及答案
如何保障 MySQL 和 Redis 的数据一致性?
165. Compare version number - string
一文搞定 UDP 和 TCP 高频面试题!
面试必备:聊聊分布式锁的多种实现!
阿里云微服务(一)服务注册中心Nacos以及REST Template和Feign Client
TYUT太原理工大学2022数据库大题之概念模型设计
First acquaintance with C language (Part 2)
MySQL backup -- common errors in xtrabackup backup
最新坦克大战2022-全程开发笔记-2
Cloud native trend in 2022
4.30动态内存分配笔记
Summary of multiple choice questions in the 2022 database of tyut Taiyuan University of Technology
Error: sorting and subscript out of bounds
Network layer 7 protocol
最新坦克大战2022-全程开发笔记-1
162. Find peak - binary search
初识C语言(上)
Music playback (toggle & playerprefs)
What are the advantages of using SQL in Excel VBA