---
# System prepended metadata

title: '程式小專題:電子雞'

---

# 程式小專題:電子雞

先跑一次程式，就可以知道要把手動輸入scanf的地方改掉，改成電腦可以自己判斷的公式。

```cpp=
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);//這邊

      while (!isCorrect(guess, answer)) 
         scanf("%d", &guess);//要改掉
```
因為跟第四次上機考的程式有點類似，所以我先把第四次上機考跟上面程式碼類似的地方寫出來

```cpp=
int guess(int num)
{
    if(num == res)
        return 3;
    else if(num > res)
        return 2;
    else if(num < res)
        return 1;
    else return 0;
}

int solve()
{
	int left=1,right=range;
	int mid;
	
	while(left<=right){
		
		mid=left+(right-left)/2;
		if(guess(mid)==3) return mid;
		if(guess(mid)==1) left=mid+1;
		else right=mid; 
	}
	return 1;
}
```

類似的功能是這兩個function，因此我將原本要輸入的部分用solve替換，之後再加上可以計算次數的功能
```cpp=
int solve(answer)
{
	int left=1,right=1000;
	int mid=0;
	int i=1;
	
	while(left<=right){
		mid=left+(right-left)/2;
		printf("\ncount:%d   guessNumber:%d\n",i,mid);
		i++;
		if(guess(mid,answer)==3) break;
		if(guess(mid,answer)==1) {
			left=mid+1;
			printf( "%s", "Too low. Try again.\n? " );
		}
		else {
			right=mid;
			printf( "%s", "Too high. Try again.\n? " ); 
		}
	}
	return 1;
}
```
最後再根據題目要求加入ASCII的圖案
```cpp=
void ascii(){
	printf("________00000000000___________000000000000_________\n");
    printf("______00000000_____00000___000000_____0000000______\n");
    printf("____0000000_____________000______________00000_____\n");
    printf("___0000000_______________0_________________0000____\n");
    printf("__000000____________________________________0000___\n");
    printf("__00000_____________________________________ 0000__\n");
    printf("_00000______________________________________00000__\n");
    printf("_00000_____________________________________000000__\n");
    printf("__000000_________________________________0000000___\n");
    printf("___0000000______________________________0000000____\n");
    printf("_____000000____________________________000000______\n");
    printf("_______000000________________________000000________\n");
    printf("__________00000_____________________0000___________\n");
    printf("_____________0000_________________0000_____________\n");
    printf("_______________0000_____________000________________\n");
    printf("_________________000_________000___________________\n");
    printf("_________________ __000_____00_____________________\n");
    printf("______________________00__00_______________________\n");
    printf("________________________00_________________________\n");
}
```

以下是完整的程式碼
```cpp=
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

void guessGame(void);
int guess(int mid,int answer);

int main(void){
	guessGame();
} 

void guessGame(void){
	int answer;
    int response;
	int solve(int answer); 
	void ascii();
	
    do {
		answer = 1 + rand() % 1000;
		puts("I have a number between 1 and 1000.\n" 
           "Can you guess my number?\n" 
           "Please type your first guess.");
        printf("%s", "? ");
        solve(answer);

		ascii();
		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);
}

void ascii(){
	printf("________00000000000___________000000000000_________\n");
    printf("______00000000_____00000___000000_____0000000______\n");
    printf("____0000000_____________000______________00000_____\n");
    printf("___0000000_______________0_________________0000____\n");
    printf("__000000____________________________________0000___\n");
    printf("__00000_____________________________________ 0000__\n");
    printf("_00000______________________________________00000__\n");
    printf("_00000_____________________________________000000__\n");
    printf("__000000_________________________________0000000___\n");
    printf("___0000000______________________________0000000____\n");
    printf("_____000000____________________________000000______\n");
    printf("_______000000________________________000000________\n");
    printf("__________00000_____________________0000___________\n");
    printf("_____________0000_________________0000_____________\n");
    printf("_______________0000_____________000________________\n");
    printf("_________________000_________000___________________\n");
    printf("_________________ __000_____00_____________________\n");
    printf("______________________00__00_______________________\n");
    printf("________________________00_________________________\n");
}

int solve(answer)
{
	int left=1,right=1000;
	int mid=0;
	int i=1;
	
	while(left<=right){
		mid=left+(right-left)/2;
		printf("\ncount:%d   guessNumber:%d\n",i,mid);
		i++;
		if(guess(mid,answer)==3) break;
		if(guess(mid,answer)==1) {
			left=mid+1;
			printf( "%s", "Too low. Try again.\n? " );
		}
		else {
			right=mid;
			printf( "%s", "Too high. Try again.\n? " ); 
		}
	}
	return 1;
}
int guess(int mid,int answer)
{
    if(mid == answer)
        return 3;
    else if(mid > answer)
        return 2;
    else if(mid < answer)
        return 1;
    else return 0;
}
```




