C++小游戲教程之猜數(shù)游戲的實(shí)現(xiàn)
0. 引言
本章主要講解如何做一個(gè)簡(jiǎn)易的猜數(shù)游戲,分為用戶猜數(shù)和系統(tǒng)猜數(shù)。
前置芝士:
1. 用戶猜數(shù)
系統(tǒng)想好一個(gè)在 [ 1 , 100 ] [1,100][1,100] 之間的整數(shù),由用戶來猜數(shù),而系統(tǒng)只能回答“過大”“過小”“正確”。
1-1. 設(shè)置答案數(shù)與猜測(cè)數(shù)
使用隨機(jī)數(shù)來隨機(jī)一個(gè) [ 1 , 100 ] [1,100][1,100] 的整數(shù),猜測(cè)數(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ì)猜測(cè)次數(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. 輸出猜測(cè)次數(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)猜,寫起來整體還是很簡(jiǎn)單的,但是要讓系統(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ì)縮小范圍!
顯然,我們可以每一次縮小猜測(cè)范圍。
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~
前面的程序判定不了我們?cè)谡f謊,因此我們可以就 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;
}
到此這篇關(guān)于C++小游戲教程之猜數(shù)游戲的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)C++猜數(shù)游戲內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C語言指針如何實(shí)現(xiàn)字符串逆序反轉(zhuǎn)
這篇文章主要介紹了C語言指針如何實(shí)現(xiàn)字符串逆序反轉(zhuǎn),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07

