C++小游戲教程之猜數游戲的實現
0. 引言
本章主要講解如何做一個簡易的猜數游戲,分為用戶猜數和系統(tǒng)猜數。
前置芝士:
1. 用戶猜數
系統(tǒng)想好一個在 [ 1 , 100 ] [1,100][1,100] 之間的整數,由用戶來猜數,而系統(tǒng)只能回答“過大”“過小”“正確”。
1-1. 設置答案數與猜測數
使用隨機數來隨機一個 [ 1 , 100 ] [1,100][1,100] 的整數,猜測數初始設置為 − 1 -1−1。
srand(time(0)); int x=-1,ans=rand()%100+1;
1-2. 系統(tǒng)說明要求與讀入數字
讓系統(tǒng)講清楚每次猜的數字的范圍。
然后就直接讓用戶輸入數字。
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!");
外層的循環(huán)條件,只要x≠ans時,就執(zhí)行。
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. 系統(tǒng)猜數,但是是進化史
用戶想好一個[1,100] 范圍的數,讓系統(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——我會瞎猜!
系統(tǒng)只會瞎猜:
printf("I guess the number is %d.Is it right(R, L or S)? ",rand()%100+1);
效果顯著:

為系統(tǒng)堅持不懈的精神點贊!
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;
}
效率提升了:

系統(tǒng):我是最快的!
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;
}
到此這篇關于C++小游戲教程之猜數游戲的實現的文章就介紹到這了,更多相關C++猜數游戲內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
MinGW-w64 C/C++編譯器下載和安裝的方法步驟(入門教程)
如果電腦沒有安裝MinGW-w64 C/C++編譯器,就無法運行gcc命令,本文主要介紹了MinGW-w64 C/C++編譯器下載和安裝的方法步驟,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-02-02

