---
# System prepended metadata

title: '**小專題1 電子雞自己猜數字 D1149989**'

---

# **小專題1 電子雞自己猜數字 D1149989**
## 原程式
以下為一個可以跟電腦互動的猜數字小遊戲
```c=
// 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
```
## 修改後
讓電腦能夠自己猜測數字，並將猜測過程以及猜了幾次列印，並在猜對時給予ASCII圖案獎勵。
### 策略一
每次猜測時，由最大數與最小數的中數開始猜測，若答案小於猜測時，最大數改為猜測值-1，若大於則將最小數改為猜測值+1。此方法較策略二快得許多。
```c=
// 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
void ASCII(); //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");
      int M=1000, m=1, guesstime=1, A; //設最大數M、最小數m和A呼叫函式isCorrect()
      guess = (M + m) / 2; //一開始由中間數猜
      printf("%d\n",guess);
      A = isCorrect(guess,answer);
      //不斷重設最小和最大數直到猜中為止
      while(A != 1){ //不斷重設最小和最大數直到猜中為止        
        if(A == 2){
          m = guess + 1;
        }else if(A == 3){
          M = guess - 1;
        }
        guess = (M + m) / 2;
        printf("%d\n",guess); //列印當前猜測
        A = isCorrect(guess,answer);
        guesstime++;
        }
        ASCII();
        printf("\nExcellent! You guessed the number! Use %d time!\n",guesstime);
        puts("Would you like to play again?");// prompt for another game
        printf("Please type ( 1=yes, 2=no )? ");
        scanf("%d",&response);
   } 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 " );
      return 2;
   }
   else{
      printf( "%s", "Too high. Try again?\n " );
      return 3;    
   }
   return 0;
} // end function isCorrect
//猜中數字列印圖案獎勵
void ASCII(){
  printf("   ▄██████▄   ▄██████▄   ▄██████▄  ████████▄       ▄█  ▄██████▄  ▀█████████▄  \n");
  printf("  ███    ███ ███    ███ ███    ███ ███   ▀███     ███ ███    ███   ███    ███ \n");
  printf("  ███    █▀  ███    ███ ███    ███ ███    ███     ███ ███    ███   ███    ███ \n");
  printf(" ▄███        ███    ███ ███    ███ ███    ███     ███ ███    ███  ▄███▄▄▄██▀  \n");
  printf("▀▀███ ████▄  ███    ███ ███    ███ ███    ███     ███ ███    ███ ▀▀███▀▀▀██▄  \n");
  printf("  ███    ███ ███    ███ ███    ███ ███    ███     ███ ███    ███   ███    ██▄ \n");
  printf("  ███    ███ ███    ███ ███    ███ ███   ▄███     ███ ███    ███   ███    ███ \n");
  printf("  ████████▀   ▀██████▀   ▀██████▀  ████████▀  █▄ ▄███  ▀██████▀  ▄█████████▀  \n");
  printf("                                              ▀▀▀▀▀▀                          \n");
}
```
### 策略二
讓電腦由1開始猜，每次猜測後+1，直到猜到為止，因為是將所有數由小到大一個一個猜，所以最多可以猜到1000次，因此效率較策略一來得差。
```c=
// 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
void ASCII(); //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=1; // 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");
      int guesstime=1; //猜測次數
      for(guess=1;guess<=1000;guess++){ //從1猜到1000直到猜中為止
        if(isCorrect(guess,answer) == 2){
          guesstime++;
        }else if(isCorrect(guess,answer) == 1){
          printf("%d\n",guess);
          break;
        }
      }
        ASCII();
        printf("\nExcellent! You guessed the number! Use %d time!\n",guesstime);
        puts("Would you like to play again?");// prompt for another game
        printf("Please type ( 1=yes, 2=no )? ");
        scanf("%d",&response);
   } 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("%d\n",g);
      printf("%s", "Too low. Try again?\n" );
      return 2;
    }
   return 0;
} // end function isCorrect
//猜中數字列印圖案獎勵
void ASCII(){
  printf("   ▄██████▄   ▄██████▄   ▄██████▄  ████████▄       ▄█  ▄██████▄  ▀█████████▄  \n");
  printf("  ███    ███ ███    ███ ███    ███ ███   ▀███     ███ ███    ███   ███    ███\n");
  printf("  ███    █▀  ███    ███ ███    ███ ███    ███     ███ ███    ███   ███    ███ \n");
  printf(" ▄███        ███    ███ ███    ███ ███    ███     ███ ███    ███  ▄███▄▄▄██▀  \n");
  printf("▀▀███ ████▄  ███    ███ ███    ███ ███    ███     ███ ███    ███ ▀▀███▀▀▀██▄  \n");
  printf("  ███    ███ ███    ███ ███    ███ ███    ███     ███ ███    ███   ███    ██▄ \n");
  printf("  ███    ███ ███    ███ ███    ███ ███   ▄███     ███ ███    ███   ███    ███ \n");
  printf("  ████████▀   ▀██████▀   ▀██████▀  ████████▀  █▄ ▄███  ▀██████▀  ▄█████████▀  \n");
  printf("                                              ▀▀▀▀▀▀                          \n");
}
```