日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無先后,達(dá)者為師

網(wǎng)站首頁 編程語言 正文

C++小游戲教程之猜數(shù)游戲的實(shí)現(xiàn)_C 語言

作者:JYqwq ? 更新時(shí)間: 2022-12-07 編程語言

0. 引言

本章主要講解如何做一個(gè)簡易的猜數(shù)游戲,分為用戶猜數(shù)和系統(tǒng)猜數(shù)。

前置芝士:

「C++小游戲教程」基本技巧(1)——隨機(jī)化

1. 用戶猜數(shù)

系統(tǒng)想好一個(gè)在 [ 1 , 100 ] [1,100][1,100] 之間的整數(shù),由用戶來猜數(shù),而系統(tǒng)只能回答“過大”“過小”“正確”。

1-1. 設(shè)置答案數(shù)與猜測數(shù)

使用隨機(jī)數(shù)來隨機(jī)一個(gè) [ 1 , 100 ] [1,100][1,100] 的整數(shù),猜測數(shù)初始設(shè)置為 ? 1 -1?1。

srand(time(0));
int x=-1,ans=rand()%100+1;

1-2. 系統(tǒng)說明要求與讀入數(shù)字

讓系統(tǒng)講清楚每次猜的數(shù)字的范圍。
然后就直接讓用戶輸入數(shù)字。

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

1-3. 累計(jì)猜測次數(shù)與判斷數(shù)字

記一個(gè)變量tms,每次加一。

判斷分為四種情況:

1.當(dāng)x?[1,100] 時(shí),拋出錯(cuò)誤。

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

2.當(dāng)x>ans 時(shí),說明數(shù)字過大,輸出。

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

3.當(dāng)x<ans 時(shí),類似,數(shù)字過小,輸出。

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

4.當(dāng)x=ans 時(shí),正確,提示輸出。

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

外層的循環(huán)條件,只要x≠ans時(shí),就執(zhí)行。

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

1-4. 輸出猜測次數(shù)

輸出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. 系統(tǒng)猜數(shù),但是是進(jìn)化史

用戶想好一個(gè)[1,100] 范圍的數(shù),讓系統(tǒng)猜。太大輸入L,太小輸入S,正確輸入R。

有了上面的操作,我們讓系統(tǒng)猜,寫起來整體還是很簡單的,但是要讓系統(tǒng)聰明些。

先擺出程序框架:

#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——我會(huì)瞎猜!

系統(tǒng)只會(huì)瞎猜:

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

效果顯著:

為系統(tǒng)堅(jiān)持不懈的精神點(diǎn)贊!

2-2. 代碼 v2.0——我會(huì)縮小范圍!

顯然,我們可以每一次縮小猜測范圍。

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

效率提升了:

系統(tǒng):我是最快的!

2-3. 代碼 v3.0——我會(huì)清白!

Never gonna tell a lie and hurt you~

前面的程序判定不了我們在說謊,因此我們可以就 v2.0 添加一些東西(當(dāng)l≥r 時(shí)必定不合法)。

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——我會(huì)二分!

沒錯(cuò),就是眾望所歸的二分。

改動(dòng)這個(gè)即可:

int t=l+r>>1;

rand():要我有何用?

如果還是猜50,效果:

計(jì)算機(jī):驚不驚喜,意不意外!

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

原文鏈接:https://blog.csdn.net/Leo_Chenjy/article/details/127703281

欄目分類
最近更新