• 「C++小游戏教程」猜数游戏


    0. 引言

    本章主要讲解如何做一个简易的猜数游戏,分为用户猜数和系统猜数。
    前置芝士:


    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

    1-2. 系统说明要求与读入数字

    让系统讲清楚每次猜的数字的范围。
    然后就直接让用户输入数字。

    printf("I have a number from 1 to 100. Please have a guess: ");
    scanf("%d",&x);
    
    • 1
    • 2

    1-3. 累计猜测次数与判断数字

    记一个变量 t m s tms tms,每次加一。
    判断分为四种情况:

    • x ∉ [ 1 , 100 ] x\notin [1,100] x/[1,100] 时,抛出错误。
      if(x<1||x>100) puts("The number is error.");
      
      • 1
    • x > a n s x>ans x>ans 时,说明数字过大,输出。
      else if(x>ans) puts("The number is larger than my number!");
      
      • 1
    • x < a n s xx<ans 时,类似,数字过小,输出。
      else if(x<ans) puts("The number is smaller than my number!");
      
      • 1
    • x = a n s x=ans x=ans 时,正确,提示输出。
      else puts("Oh, you are right!");
      
      • 1

    外层的循环条件,只要 x ≠ a n s x\neq ans x=ans 时,就执行。

    while(x!=ans)
    {
    	...
    }
    
    • 1
    • 2
    • 3
    • 4

    1-4. 输出猜测次数

    输出 t m s tms tms 并终止。

    printf("You guessed it %d times.",tms);
    
    • 1

    完整代码:

    #include
    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;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    效果:
    用户猜数


    2. 系统猜数,但是是进化史

    用户想好一个 [ 1 , 100 ] [1,100] [1,100] 范围的数,让系统猜。太大输入 L,太小输入 S,正确输入 R

    有了上面的操作,我们让系统猜,写起来整体还是很简单的,但是要让系统聪明些。
    先摆出程序框架:

    #include
    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;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    2-1. 代码 v1.0——我会瞎猜!

    系统只会瞎猜:

    printf("I guess the number is %d.Is it right(R, L or S)? ",rand()%100+1);
    
    • 1

    效果显著
    2-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;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    效率提升了:
    2-2
    系统:我是最快的!

    2-3. 代码 v3.0——我会清白!

    Never gonna tell a lie and hurt you~
    前面的程序判定不了我们在说谎,因此我们可以就 v2.0 添加一些东西(当 l ≥ r l\ge r lr 时必定不合法)。

    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;
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    聪明多了:
    2-3

    2-4. 代码 v4.0——我会二分!

    没错,就是众望所归的二分。
    改动这个即可:

    int t=l+r>>1;
    
    • 1

    rand():要我有何用?
    如果还是猜 50 50 50,效果:
    2-4

    计算机:惊不惊喜,意不意外!
    But——《1 times》!
    稍微改改即可,这里作者就不改了懒得改
    最终代码:

    #include
    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;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30

    3. 后记

    本章详解了两种猜数游戏的实现,笔者在写的时候是一边写文章一边写代码一边截图的,也耗费了一定的精力。当然,这些不算什么,能让读者有所收获,才是最重要的!

  • 相关阅读:
    让生活更加精致的APP?
    双11预售在即,小红书品牌如何高效分析竞品?
    Redis学习笔记18:基于spring data redis及lua脚本的分布式锁
    c++入门99题21-30
    微信小程序如何使用原生Websocket api与Asp.Net Core SignalR 通信
    解决nginx: [emerg] unknown directive “stream“ in /etc/nginx/nginx.conf问题
    javascript实现常用数组方法重写
    Web安全专业学习路线
    【从零开始学zabbix】一丶Zabbix的介绍与部署Zabbix
    Serverless Devs 重大更新,基于 Serverless 架构的 CI/CD 框架:Serverless-cd
  • 原文地址:https://blog.csdn.net/Leo_Chenjy/article/details/127703281