当前位置:网站首页>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文件发给同学即可。

边栏推荐
- Voir ui plus version 1.3.1 pour améliorer l'expérience Typescript
- (ultra detailed onenet TCP protocol access) arduino+esp8266-01s access to the Internet of things platform, upload real-time data collection /tcp transparent transmission (and how to obtain and write L
- Differences and application scenarios between MySQL index clock B-tree, b+tree and hash indexes
- CorelDRAW plug-in -- GMS plug-in development -- Introduction to VBA -- GMS plug-in installation -- Security -- macro Manager -- CDR plug-in (I)
- 2-year experience summary, tell you how to do a good job in project management
- 西安电子科技大学22学年上学期《射频电路基础》试题及答案
- Introduction and use of redis
- TYUT太原理工大学2022数据库之关系代数小题
- Tyut outline of 2022 database examination of Taiyuan University of Technology
- All in one 1405: sum and product of prime numbers
猜你喜欢

2-year experience summary, tell you how to do a good job in project management

Alibaba cloud microservices (IV) service mesh overview and instance istio

Fairygui bar subfamily (scroll bar, slider, progress bar)

Chromatic judgement bipartite graph

阿里云微服务(四) Service Mesh综述以及实例Istio

10 minutes pour maîtriser complètement la rupture du cache, la pénétration du cache, l'avalanche du cache

arduino+DS18B20温度传感器(蜂鸣器报警)+LCD1602显示(IIC驱动)

System design learning (I) design pastebin com (or Bit.ly)

121 distributed interview questions and answers

继承和多态(下)
随机推荐
[Topic terminator]
Dark chain lock (lca+ difference on tree)
KF UD decomposition pseudo code implementation advanced [2]
Record: newinstance() obsolete replacement method
Record: the solution of MySQL denial of access when CMD starts for the first time
继承和多态(上)
View UI Plus 发布 1.3.0 版本,新增 Space、$ImagePreview 组件
2022 National Games RE1 baby_ tree
10 minutes pour maîtriser complètement la rupture du cache, la pénétration du cache, l'avalanche du cache
121 distributed interview questions and answers
2年经验总结,告诉你如何做好项目管理
Employment of cashier [differential constraint]
View UI plus released version 1.2.0 and added image, skeleton and typography components
阿里云一面:并发场景下的底层细节 - 伪共享问题
系统设计学习(一)Design Pastebin.com (or Bit.ly)
凡人修仙学指针-1
TYUT太原理工大学2022软工导论简答题
121道分布式面试题和答案
面渣逆袭:Redis连环五十二问,三万字+八十图详解。
MPLS experiment