C++小游戏教程之猜数游戏的实现

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

C++小游戏教程之猜数游戏的实现

JYqwq   2022-11-08 我要评论

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. 累计猜测次数与判断数字

记一个变量tms,每次加一。

判断分为四种情况:

1.当x∉[1,100] 时,抛出错误。

if(x<1||x>100) puts("The number is error.");

2.当x>ans 时,说明数字过大,输出。

else if(x>ans) puts("The number is larger than my number!");

3.当x<ans 时,类似,数字过小,输出。

else if(x<ans) puts("The number is smaller than my number!");

4.当x=ans 时,正确,提示输出。

else puts("Oh, you are right!");

外层的循环条件,只要x≠ans时,就执行。

while(x!=ans)
{
    ...
}

1-4. 输出猜测次数

输出tms 并终止。

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] 范围的数,让系统猜。太大输入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≥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():要我有何用?

如果还是猜50,效果:

计算机:惊不惊喜,意不意外!

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;
}

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们