# **電子雞自己猜數字
# **
將老師給的範本(讓玩家自己猜數字)的程式,改成讓電腦可以自己猜數字。
**以下為原程式碼:**
```
// Randomly generate numbers between 1 and 1000 for user to guess.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void guessGame(void); // function prototype
int isCorrect(int, int); // function prototype
int main(void)
{
// srand( time( 0 ) ); // seed random number generator
guessGame();
} // end main
// guessGame generates numbers between 1 and 1000
// and checks user's guess
void guessGame(void)
{
int answer; // randomly generated number
int guess; // user's guess
int response; // 1 or 2 response to continue game
// loop until user types 2 to quit game
do {
// generate random number between 1 and 1000
// 1 is shift, 1000 is scaling factor
answer = 1 + rand() % 1000;
// prompt for guess
puts("I have a number between 1 and 1000.\n"
"Can you guess my number?\n"
"Please type your first guess.");
printf("%s", "? ");
scanf("%d", &guess);
// loop until correct number
while (!isCorrect(guess, answer))
scanf("%d", &guess);
// prompt for another game
puts("\nExcellent! You guessed the number!\n"
"Would you like to play again?");
printf("%s", "Please type ( 1=yes, 2=no )? ");
scanf("%d", &response);
puts("");
} while (response == 1);
} // end function guessGame
// isCorrect returns true if g equals a
// if g does not equal a, displays hint
int isCorrect(int g, int a)
{
// guess is correct
if (g == a)
return 1;
// guess is incorrect; display hint
if (g < a)
printf( "%s", "Too low. Try again.\n? " );
else
printf( "%s", "Too high. Try again.\n? " );
return 0;
} // end function isCorrect
```
**想法:**
原程式碼為需要使用者自己輸入數字,並且看提示看要猜大猜小,首先需要把scanf的地方取代掉,改成讓電腦自己猜數字,並且要參考上一次的答案做一個新的範圍。
rand()為產生亂數的方法,1+rand()%1000的意思為在1-1000內隨機產生一個亂數,可以做為電腦亂猜的答案,亦可作為"第一次"猜的數字。
>>記得在每次猜完數字後,要把次數累積進去計算猜測次數的變數內,這邊把該變數命名為times,並利用times++累積次數。
在第一次猜測玩數字後,輸入了guess參數後,寫if條件式判別猜測的數字大於答案or小於答案。
接下來會用到一個"限制rand()範圍"的公式,以下為公式:
a = rand() % (b - c + 1) + c
a參數為下一個猜測的數字
b參數為上一輪猜測數字的最大值
c參數為上一輪猜測數字的最小值
下面接著需要while loop,並用到兩個if條件式,一個為猜測的數字比答案小的情況下,另一個為猜測數字比答案大的情況下。
**猜>答案:**
```
if (guess < answer)
```
**答案>猜:**
```
else
```
>>因為只有兩種情況,if條件式寫出第ㄧ種後,剩下的會自動歸類為第二種。
接下來是寫statment,在上述兩種情況下的最大與最小值不一樣,所以需要想想。
當猜測數字較小時,代表下一個猜測範圍的最大值為amswer,最小值為guess,並把amswer與guess套入到公式內對應位置,反之則位置互換。
**if條件式的statment:**
```
guess = rand() % (answer - guess + 1) + guess
```
**else條件式的statment:**
```
guess = rand() % (guess - answer + 1) + answer
```
**最後需要用printf印出該次猜測數字的答案,並且利用參數times累積次數:**
```
printf("%d\n",guess); //印出本次猜測結果,不論對錯
times++;
```
最後不要忘記,用printf印出獎勵圖案。
**以下為完整的程式碼:**
```
// Randomly generate numbers between 1 and 1000 for user to guess.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void guessGame(void); // function prototype
int isCorrect(int, int); // function prototype
int answer; // randomly generated number
int guess; // user's guess
int response; // 1 or 2 response to continue game
int main(void)
{
// srand( time( 0 ) ); // seed random number generator
guessGame();
} // end main
// guessGame generates numbers between 1 and 1000
// and checks user's guess
void guessGame(void)
{
// loop until user types 2 to quit game
do {
// generate random number between 1 and 1000
// 1 is shift, 1000 is scaling factor
int times = 0; //guess times
answer = 1 + rand() % 1000;
// prompt for guess
puts("I have a number between 1 and 1000.\n"
"Can you guess my number?\n"
"Please type your first guess.");
printf("%s", "? ");
guess = 1 + rand() % 1000; //讓電腦自己在1000內猜數字
times++;
printf("%d\n",guess); //印出電腦第一次猜的數字
// loop until correct number
while (!isCorrect(guess, answer))
{
if (guess < answer) //當猜的數字比答案小,把範圍縮至猜的數與答案之間
{
guess = rand() % (answer - guess + 1) + guess; //讓電腦自己在範圍內內猜數字
}
else //當猜的數字比答案大,把範圍縮至答案與猜的數之間
{
guess = rand() % (guess - answer + 1) + answer; //讓電腦自己在範圍內內猜數字
}
printf("%d\n",guess); //印出本次猜測結果,不論對錯
times++;
}
printf("toatl guess times:%d",times);
// prompt for another game
puts("\nExcellent! You guessed the number!\n"
"Would you like to play again?\n");
printf(" _,'| _.-''``-...___..--';)\n");
printf(" /_ \'. __..-' , ,--...--'''\n");
printf(" <\ .`--''' ` /'\n");
printf(" `-';' ; ; ;\n");
printf(" __...--'' ___...--_..' .;.'\n");
printf("(,__....----''' (,..--'' \n"); //印出獎勵圖案
printf("%s", "Please type ( 1=yes, 2=no )? ");
scanf("%d", &response);
puts("");
} while (response == 1);
} // end function guessGame
// isCorrect returns true if g equals a
// if g does not equal a, displays hint
int isCorrect(int g, int a)
{
// guess is correct
if (g == a)
return 1;
// guess is incorrect; display hint
if (g < a)
printf( "%s", "Too low. Try again.\n? " );
else
printf( "%s", "Too high. Try again.\n? " );
return 0;
} // end function isCorrect
```
replit連結:[https://replit.com/@Rui5261/Dian-Zi-Ji-Zi-Ji-Cai-Shu-Zi#main.c](https://)