---
# System prepended metadata

title: 程式設計小專題—電子雞自己猜數字

---

# 程式設計小專題—電子雞自己猜數字

## 改寫過的程式碼
```cpp=
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

void guessGame(void);       //遊戲的函式
void ascii(void);           //圖案的函式
int isCorrect(int, int);    //猜測結果回報
int hrange = 1000;          //範圍最大值
int lrange = 1;             //範圍最小值

//主函式
int main(void){
    guessGame();
}

//執行遊戲的函式
void guessGame(void){
    int answer;         //正確答案    
    int guess;          //猜測的答案
    int response;       //是否要繼續的選擇
    int times = 0;      //猜的次數

    do{
        answer = 1 + rand() % 1000;  //1~1000隨機挑一個數字當正確答案

        //印出問題
        puts("I have a number between 1 and 1000.\n"
           "Can you guess my number?\n" 
           "Please type your first guess.");
        guess = (hrange + lrange) / 2;    //從中間值開始猜
        printf("\nAnswer is %d\n", guess);
        times = times + 1;      //猜一次次數加1

        //讓電腦自己猜數字
        while (!isCorrect(guess, answer))   //答案是錯的持續執行迴圈
        {
            guess = (hrange + lrange) / 2;
            printf("\nAnswer is %d\n", guess);
            times = times + 1;  //猜一次次數加1
                
        }
    

        //完成猜測
        printf("\nExcellent! You guessed the number!\n\n");
        ascii();   //印出圖案
        printf("\nUse %d times to guess\n",times);   //說明猜了幾次
        printf("Would you like to play again?\n");
        printf("%s", "Please type ( 1=yes, 2=no )? \n");
        scanf("%d", &response);
        lrange = 1;
        hrange = 1000;
        times = 0;
        puts("");
    }
    while (response == 1);
}
    

//猜測結果的函式
int isCorrect(int guess, int answer)
{
    //回答正確
    if (guess == answer)
        return 1;

    //回答錯誤
    if (guess < answer){      
        printf( "%s", "\nToo low. Try again.\n" );   //如果猜的數字比正確數字小
        lrange = guess + 1;
    }
    else if (guess > answer){
        printf( "%s", "\nToo high. Try again.\n" );  //如果猜的數字比正確數字大
        hrange = guess - 1;
    }
    return 0;
}

//最後的成功(？圖案的函式
void ascii (void){
    printf("__________________???????????????_________________\n");
    printf("_________________?????????????????________________\n");
    printf("____________???????????????????????????___________\n");
    printf("___________???????_______________???????__________\n");
    printf("________?????_________________________?????_______\n");
    printf("_______?????___________________________?????______\n");
    printf("_____????_________________________________????____\n");
    printf("_____????_________________________________????____\n");
    printf("_____????_____???????_________???????_____????____\n");
    printf("____???_______???????_________???????_______??????\n");
    printf("__????________???????_________???????_________????\n");
    printf("__????________________________________________????\n");
    printf("__????________________________________________????\n");
    printf("__????________________________________________????\n");
    printf("__????________________________________________????\n");
    printf("__????________________________________________????\n");
    printf("__????______?_________________________?_______????\n");
    printf("__????_____????_____________________????______????\n");
    printf("____???_____?????_________________?????_____??????\n");
    printf("_____????_____????_______________????_____????____\n");
    printf("_____????_______???????????????????_______????____\n");
    printf("_____????_________???????????????_________????____\n");
    printf("_______?????___________________________?????______\n");
    printf("________?????_________________________?????_______\n");
    printf("___________???????_______________???????__________\n");
    printf("____________??????_______________??????___________\n");
    printf("__________________???????????????_________________\n");
    printf("__________________???????????????_________________\n");
}
```
### 說明
每次都從中間值開始猜，這樣速度比較快。

```cpp=
int hrange = 1000;          //範圍最大值
int lrange = 1;             //範圍最小值
```
最一開始先宣告全域變數，設定範圍最大值=1000以及最小值=1。
#### 執行遊戲的函式
```cpp=
void guessGame(void){
    int answer;         //正確答案    
    int guess;          //猜測的答案
    int response;       //是否要繼續的選擇
    int times = 0;      //猜的次數
```
先宣告正確答案跟電腦猜測的答案，總猜測次數先宣告為0。
```cpp=
do{
        answer = 1 + rand() % 1000;  //1~1000隨機挑一個數字當正確答案

        //印出問題
        puts("I have a number between 1 and 1000.\n"
           "Can you guess my number?\n" 
           "Please type your first guess.");
        guess = (hrange + lrange) / 2;    //從中間值開始猜
        printf("\nAnswer is %d\n", guess);
        times = times + 1;      //猜一次次數加1

        //讓電腦自己猜數字
        while (!isCorrect(guess, answer))   //答案是錯的持續執行迴圈
        {
            guess = (hrange + lrange) / 2;
            printf("\nAnswer is %d\n", guess);
            times = times + 1;  //猜一次次數加1
                
        }
```
一開始先有一個在1~1000之間隨機選一個數字當作正確答案。
第一次猜的數字就從中間值開始猜起，每猜測一次次數的計算就會+1。
然後把猜測的數字用判斷猜測結果的函式判斷是否正確。
若不正確即在猜一次，直到猜對為止。
#### 判斷猜測結果的函式
```cpp=
//判斷猜測結果的函式
int isCorrect(int guess, int answer)
{
    //回答正確
    if (guess == answer)
        return 1;

    //回答錯誤
    if (guess < answer){      
        printf( "%s", "\nToo low. Try again.\n" );   //如果猜的數字比正確數字小
        lrange = guess + 1;
    }
    else if (guess > answer){
        printf( "%s", "\nToo high. Try again.\n" );  //如果猜的數字比正確數字大
        hrange = guess - 1;
    }
    return 0;
}
```
此為判斷猜測結果的函式。
猜對即回傳1(true)。
若猜錯有兩種可能，一個是猜的數字比正確答案還小，若是這種情況，則猜測範圍最小值應從猜的答案加1開始;另一個是猜的數字比正確答案大，那麼此猜測範圍最大值應從猜的答案減1開始。

#### 成功時印出的圖案的程式碼
```cpp=
void ascii (void){
    printf("__________________¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶_________________\n");
    printf("_________________¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶________________\n");
    printf("____________¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶___________\n");
    printf("___________¶¶¶¶¶¶¶_______________¶¶¶¶¶¶¶__________\n");
    printf("________¶¶¶¶¶_________________________¶¶¶¶¶_______\n");
    printf("_______¶¶¶¶¶___________________________¶¶¶¶¶______\n");
    printf("_____¶¶¶¶_________________________________¶¶¶¶____\n");
    printf("_____¶¶¶¶_________________________________¶¶¶¶____\n");
    printf("_____¶¶¶¶_____¶¶¶¶¶¶¶_________¶¶¶¶¶¶¶_____¶¶¶¶____\n");
    printf("____¶¶¶_______¶¶¶¶¶¶¶_________¶¶¶¶¶¶¶_______¶¶¶¶¶¶\n");
    printf("__¶¶¶¶________¶¶¶¶¶¶¶_________¶¶¶¶¶¶¶_________¶¶¶¶\n");
    printf("__¶¶¶¶________________________________________¶¶¶¶\n");
    printf("__¶¶¶¶________________________________________¶¶¶¶\n");
    printf("__¶¶¶¶________________________________________¶¶¶¶\n");
    printf("__¶¶¶¶________________________________________¶¶¶¶\n");
    printf("__¶¶¶¶________________________________________¶¶¶¶\n");
    printf("__¶¶¶¶______¶_________________________¶_______¶¶¶¶\n");
    printf("__¶¶¶¶_____¶¶¶¶_____________________¶¶¶¶______¶¶¶¶\n");
    printf("____¶¶¶_____¶¶¶¶¶_________________¶¶¶¶¶_____¶¶¶¶¶¶\n");
    printf("_____¶¶¶¶_____¶¶¶¶_______________¶¶¶¶_____¶¶¶¶____\n");
    printf("_____¶¶¶¶_______¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶_______¶¶¶¶____\n");
    printf("_____¶¶¶¶_________¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶_________¶¶¶¶____\n");
    printf("_______¶¶¶¶¶___________________________¶¶¶¶¶______\n");
    printf("________¶¶¶¶¶_________________________¶¶¶¶¶_______\n");
    printf("___________¶¶¶¶¶¶¶_______________¶¶¶¶¶¶¶__________\n");
    printf("____________¶¶¶¶¶¶_______________¶¶¶¶¶¶___________\n");
    printf("__________________¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶_________________\n");
    printf("__________________¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶_________________\n");
}
```
> replit link :https://replit.com/@angle930325/Xiao-Zhuan-Ti-Dian-Zi-Ji-Zi-Ji-Cai-Shu-Zi#main.c