「C++小游戏教程」猜数游戏
0. 引言
本章主要讲解如何做一个简易的猜数游戏,分为用户猜数和系统猜数。
前置芝士:
- 「C++小游戏教程」基本技巧(1)——随机化
1. 用户猜数
系统想好一个在 [1,100][1,100][1,100] 之间的整数,由用户来猜数,而系统只能回答“过大”“过小”“正确”。
1-1. 设置答案数与猜测数
使用随机数来随机一个 [1,100][1,100][1,100] 的整数,猜测数初始设置为 −1-1−1。
srand(time(0));
int x=-1,ans=rand()%100+1;
1-2. 系统说明要求与读入数字
让系统讲清楚每次猜的数字的范围。
然后就直接让用户输入数字。
printf("I have a number from 1 to 100. Please have a guess: ");
scanf("%d",&x);
1-3. 累计猜测次数与判断数字
记一个变量 tmstmstms,每次加一。
判断分为四种情况:
- 当 x∉[1,100]x\notin [1,100]x∈/[1,100] 时,抛出错误。
if(x<1||x>100) puts("The number is error.");
- 当 x>ansx>ansx>ans 时,说明数字过大,输出。
else if(x>ans) puts("The number is larger than my number!");
- 当 x<ansx<ansx<ans 时,类似,数字过小,输出。
else if(x<ans) puts("The number is smaller than my number!");
- 当 x=ansx=ansx=ans 时,正确,提示输出。
else puts("Oh, you are right!");
外层的循环条件,只要 x≠ansx\neq ansx=ans 时,就执行。
while(x!=ans)
{...
}
1-4. 输出猜测次数
输出 tmstmstms 并终止。
printf("You guessed it %d times.",tms);
完整代码:
#include<bits/stdc++.h>
using namespace std;int main()
{srand(time(0));int x=-1,ans=rand()%100+1,tms=0;while(x!=ans){printf("I have a number from 1 to 100. Please have a guess: ");scanf("%d",&x);tms++;if(x<1||x>100) puts("The number is error.");else if(x>ans) puts("The number is larger than my number!");else if(x<ans) puts("The number is smaller than my number!");else puts("Oh, you are right!");}printf("You guessed it %d times.",tms);return 0;
}
效果:
2. 系统猜数,但是是进化史
用户想好一个 [1,100][1,100][1,100] 范围的数,让系统猜。太大输入
L
,太小输入S
,正确输入R
。
有了上面的操作,我们让系统猜,写起来整体还是很简单的,但是要让系统聪明些。
先摆出程序框架:
#include<bits/stdc++.h>
using namespace std;int main()
{srand(time(0));puts("Please think a number from 1 to 100. And then I'll guess it.");puts("If I guess right, you should say \"R\"(Right).");puts("If my guess is too large, you should say \"L\"(Large).");puts("If my guess is too small, you should say \"S\"(Small).");puts("DON'T TELL A LIE!\n");char c='\0';int tms=0;while(c!='R'){//...printf("I guess the number is %d.Is it right(R, L or S)? ",/*...*/);scanf("%c%*c",&c);tms++;if(c=='R') break;//...}printf("I guess it %d times!",tms);return 0;
}
2-1. 代码 v1.0——我会瞎猜!
系统只会瞎猜:
printf("I guess the number is %d.Is it right(R, L or S)? ",rand()%100+1);
效果显著:
为系统坚持不懈的精神点赞!
2-2. 代码 v2.0——我会缩小范围!
显然,我们可以每一次缩小猜测范围。
char c='\0';
int tms=0,l=1,r=100;
while(c!='R')
{int t=rand()%(r-l+1)+l;printf("I guess the number is %d. Is it right(R, L or S)? ",t);scanf("%c%*c",&c);tms++;if(c=='R') break;if(c=='L') r=t;if(c=='S') l=t;
}
效率提升了:
系统:我是最快的!
2-3. 代码 v3.0——我会清白!
Never gonna tell a lie and hurt you~
前面的程序判定不了我们在说谎,因此我们可以就 v2.0 添加一些东西(当 l≥rl\ge rl≥r 时必定不合法)。
char c='\0';
int tms=0,l=1,r=100;
while(c!='R')
{int t=rand()%(r-l+1)+l;printf("I guess the number is %d. Is it right(R, L or S)? ",t);scanf("%c%*c",&c);tms++;if(c=='R') break;if(c=='L') r=t;if(c=='S') l=t;if(l>=r){puts("You told a lie!");return 0;}
}
聪明多了:
2-4. 代码 v4.0——我会二分!
没错,就是众望所归的二分。
改动这个即可:
int t=l+r>>1;
rand():要我有何用?
如果还是猜 505050,效果:
计算机:惊不惊喜,意不意外!
But——《1 times》!
稍微改改即可,这里作者就不改了懒得改。
最终代码:
#include<bits/stdc++.h>
using namespace std;int main()
{puts("Please think a number from 1 to 100. And then I'll guess it.");puts("If I guess right, you should say \"R\"(Right).");puts("If my guess is too large, you should say \"L\"(Large).");puts("If my guess is too small, you should say \"S\"(Small).");puts("DON'T TELL A LIE!\n");char c='\0';int tms=0,l=1,r=100;while(c!='R'){int t=l+r>>1;printf("I guess the number is %d. Is it right(R, L or S)? ",t);scanf("%c%*c",&c);tms++;if(c=='R') break;if(c=='L') r=t;if(c=='S') l=t;if(l>=r){puts("You told a lie!");return 0;}}printf("I guess it %d times!",tms);return 0;
}
3. 后记
本章详解了两种猜数游戏的实现,笔者在写的时候是一边写文章一边写代码一边截图的,也耗费了一定的精力。当然,这些不算什么,能让读者有所收获,才是最重要的!
「C++小游戏教程」猜数游戏相关推荐
- python连续猜数游戏_python实现猜数游戏
本文实例为大家分享了python实现猜数游戏的具体代码,供大家参考,具体内容如下 一.问题描述: 使用python开发一个猜数小游戏,程序随机产生0~1024之间的数字,用户输入猜测数字,程序告诉用户 ...
- python猜数游戏续_python猜数游戏续改编
answer=456 n=input('Enteranumber:') correct=len([iforiinnifiinstr(answer)]) exact=len([afora,binzip( ...
- 采用python语言实现猜数游戏_python实现猜数游戏
本文实例为大家分享了python实现猜数游戏的具体代码,供大家参考,具体内容如下 一.问题描述: 使用python开发一个猜数小游戏,程序随机产生0~1024之间的数字,用户输入猜测数字,程序告诉用户 ...
- php猜数游戏63,PHP如何实现猜数游戏 PHP实现猜数游戏代码示例
PHP如何实现猜数游戏?本篇文章小编给大家分享一下PHP实现猜数游戏代码示例,文章代码介绍的很详细,小编觉得挺不错的,现在分享给大家供大家参考,有需要的小伙伴们可以来看看. 猜数游戏有两种玩法: 第一 ...
- python连续猜数游戏_Python多猜数游戏
我会重新安排一下你的代码,这样它就不会偏离你所做的太远.在from random import randint guessesTaken = 0 randomNumbers = [] Guess = ...
- 猜数游戏教学设计C语言,猜数游戏优秀教学设计
猜数游戏优秀教学设计 篇一:一年级<猜数游戏>教学设计 教学内容:北师大版一年级上册第三单元<猜数游戏> 教学目标: 1.在猜数游戏的活动中,进一步理解加减法的意义,探索并掌握 ...
- Python案例:猜数游戏
Python案例:猜数游戏 一.猜数游戏概述 Every programmer has a story about how they learned to write their first prog ...
- c语言制作猜数游戏教程,C语言实现猜数游戏
本文实例为大家分享了C语言实现猜数游戏的具体代码,供大家参考,具体内容如下 代码如下 #include #include #include void game() { srand((unsigned ...
- 用Java实现猜数游戏:在程序中预设一个0-9之间的整数,让用户通过键盘输入所猜的数,如果大于预设的数,显示“遗憾,太大了” ;小于预设的数,显示“遗憾,太小了” ,如此循环,直至猜中该数,
猜数游戏 在程序中预设一个0-9之间的整数,让用户通过键盘输入所猜的数,如果大于预设的数,显示"遗憾,太大了" ;小于预设的数,显示"遗憾,太小了" ,如此循环 ...
最新文章
- CentOS 7 yum安装Zabbix
- jetty的Form too large异常解决方案
- Exchange2010各角色对软件环境的前提条件
- VM 7 下ubuntu安装vmtools
- LoadRunner测试问题及解决方法总结
- java使用Crawler4j开发爬虫
- eclipse的servlet默认不执行index_MySQL之索引及执行计划分析
- Atom飞行手册翻译: 4.2 深入键表(keymap)
- solr配置中文分词器
- linux安装软件-rpm命令解析
- spring-第十篇之XML Schema的简化配置,p、c、util命名空间
- 01-nodeJs下载及安装
- 网页文字提取插件-网页文字提取器
- JavaScript实现动态显示时间
- LeetCode 34. 在排序数组中查找元素的第一个和最后一个位置 Find First and Last Position of Element in Sorted Array
- 动态规划之《高楼扔鸡蛋》问题详解 LeetCode 887.鸡蛋掉落
- android x86启动卡死,[更新]Android-x86启动成功,但是还有点小问题。
- linux 如何延迟100ms,Linux延时函数
- android话费查询接口,Android 小应用--一键查话费移动版(附源码)
- 【android】:android错误之Unparsed appt errors
热门文章
- 微软SDL流程终极整理总结
- Windows忘记BIOS密码/操作系统密码处理办法汇总
- spring session、spring security和redis整合的简单使用
- Unity Shader - 后处理:油画效果
- 创业第16天,设计竞价助手的关键字添加界面
- 图标题中的汉字序号改为阿拉伯序号,如“图二-1改为图2-1”
- Android 红米Note5 刷机魔趣系统
- 产能过剩蔓延到光伏产业 结构性改革下如何去产能?
- Slysoft All-in-One 1.9 (21/03/2007)
- 用Django半天时间开发一个员工管理系统实例教程分享