# 小專題1 電子雞自己猜數字 D1177960
## 原來的模板
#### 以下為一個可以跟電腦互動的猜數字小遊戲 (Randomly generate numbers between 1 and 1000 for user to guess)
#### replit連結: https://replit.com/@6581639/Cai-Shu-Zi#main.c
``` 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
```
## 策略 1
#### 直接砍半從500開始猜數字, 要是答案小於500, 那就500開始減1直到猜到數字為止, 相反要是答案大於500, 那就一直加1
#### replit連結: https://replit.com/@6581639/Dian-Zi-Ji-Zi-Ji-Cai-Shu-Zi#main.c
```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(); // funcyion ASCII
int main(void){
// prompt for guess
puts("歡迎來到電子雞自己猜數字");
puts("一個看電腦自己猜數字的程式 順便浪費時間");
// 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;
puts("\n===============開始浪費時間===============");
//count from 500
guess = 500;
printf("%d\n",guess);
int call = isCorrect(guess, answer); //
int guesstimes = 1; //count the guesses
// loop until correct number
while (call != 1){ //if not case 1, star case 2 or case 3
if (call == 2){ //case 2, when the answer is higher than guess
guess++;
printf("%d\n",guess);
call = isCorrect(guess, answer);
guesstimes++;
}
else if (call == 3){ //case 3, when the answer is lesser than guess
guess--;
printf("%d\n",guess);
call = isCorrect(guess, answer);
guesstimes++;
}
}
ASCII(); //ASCII function
puts("終於猜到數字了\n");
printf("電腦總共猜了%d次\n", guesstimes);
puts("你想再浪費一次時間嗎?");
printf("想(請輸入1) 不想(請輸入2)");
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("太小了\n");
return 2;
}else{
printf("太大了\n");
return 3;
}
return 0;
} // end function isCorrect
//if guess is correct , print correct_banner
void ASCII(){ //star ASCII function
printf(" _=====_ _=====_ \n");
printf(" / _____ \\ / _____ \\ \n");
printf(" +.-'_____'-.---------------------------.-'_____'-.+ \n");
printf(" / | | '. S O N Y .' | _ | \\ \n");
printf(" / ___| /|\\ |___ \\ / ___| /_\\ |___ \\ \n");
printf(" / | | | ; __ _ ; | _ _ | ; \n");
printf(" | | <--- ---> | | |__| |_:> | ||_| (_)| |⠀\n");
printf(" | |___ | ___| ;SELECT START ; |___ ___| ;⠀\n");
printf(" |\\ | \\|/ | / _ ___ _ \\ | (X) | /|⠀\n");
printf(" | \\ |_____| .','" "', |___| ,'" "', '. |_____| .' |⠀\n");
printf(" | '-.______.-' / \\ANALOG/ \\ '-._____.-' | \n");
printf(" | | |------| | |⠀\n");
printf(" | /\\ / \\ /\\ | \n");
printf(" | / '.___.' '.___.' \\ | \n");
printf(" | / \\ | \n");
printf(" \\ / \\ / \n");
printf(" \\________/ \\_________/ \n");
} //end ASCII function
```
## 策略 2
#### 直接讓電腦隨機猜 1~1000, 就一直隨便猜, 所以有可能會猜無限次, 這個程式當純時來浪費時間的
#### replit連結: https://replit.com/@6581639/Dian-Zi-Ji-Zi-Ji-Cai-Shu-Zi-2#main.c
``` 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(); // funcyion ASCII
int main(void){
// prompt for guess
puts("歡迎來到電子雞自己猜數字");
puts("一個看電腦自己猜數字的程式 順便浪費時間");
// 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;
puts("\n===============開始浪費時間===============");
// guess a random number
guess = 1 + rand() % 1000;
printf("%d\n",guess);
int call = isCorrect(guess, answer);
int guesstimes = 1; //count the guesses
// loop until correct number
while (call != 1){ //if not case 1, star case 2 or case 3
guess = 1 + rand() % 1000;// guess == random number
if (call == 2){ //case 2, when the answer is higher than guess
printf("%d\n",guess);
call = isCorrect(guess, answer);
guesstimes++;
}
else if (call == 3){ //case 3, when the answer is lesser than guess
printf("%d\n",guess);
call = isCorrect(guess, answer);
guesstimes++;
}
}
ASCII(); //ASCII function
puts("終於猜到數字了\n");
printf("電腦總共猜了%d次\n", guesstimes);
puts("你想再浪費一次時間嗎?");
printf("想(請輸入1) 不想(請輸入2)");
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("太小了\n");
return 2;
}else{
printf("太大了\n");
return 3;
}
return 0;
} // end function isCorrect
//if guess is correct , print correct_banner
void ASCII(){ //star ASCII function
printf(" _=====_ _=====_ \n");
printf(" / _____ \\ / _____ \\ \n");
printf(" +.-'_____'-.---------------------------.-'_____'-.+ \n");
printf(" / | | '. S O N Y .' | _ | \\ \n");
printf(" / ___| /|\\ |___ \\ / ___| /_\\ |___ \\ \n");
printf(" / | | | ; __ _ ; | _ _ | ; \n");
printf(" | | <--- ---> | | |__| |_:> | ||_| (_)| |⠀\n");
printf(" | |___ | ___| ;SELECT START ; |___ ___| ;⠀\n");
printf(" |\\ | \\|/ | / _ ___ _ \\ | (X) | /|⠀\n");
printf(" | \\ |_____| .','" "', |___| ,'" "', '. |_____| .' |⠀\n");
printf(" | '-.______.-' / \\ANALOG/ \\ '-._____.-' | \n");
printf(" | | |------| | |⠀\n");
printf(" | /\\ / \\ /\\ | \n");
printf(" | / '.___.' '.___.' \\ | \n");
printf(" | / \\ | \n");
printf(" \\ / \\ / \n");
printf(" \\________/ \\_________/ \n");
} //end ASCII function
```
## 策略 3
#### 輸入一個最大值和一個最小值, 之後根據猜的數字大小更換猜測的範圍
#### replit連結: https://replit.com/@6581639/Dian-Zi-Ji-Zi-Ji-Cai-Shu-Zi-3#main.c
```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(); // funcyion ASCII
int main(void){
// prompt for guess
puts("歡迎來到電子雞自己猜數字");
puts("一個看電腦自己猜數字的程式 順便浪費時間");
// 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;
puts("\n===============開始浪費時間===============");
// guess a random number
guess = 1 + rand() % 1000;
printf("%d\n",guess);
int max=1000, min=1;
int call = isCorrect(guess, answer); //
int guesstimes = 1; //count the guesses
// loop until correct number
while (call != 1){ //if not case 1, star case 2 or case 3
if (call == 2){ //case 2, when the answer is higher than guess
printf("%d\n",guess);
min = guess + 1;
guesstimes++;
}
else if (call == 3){ //case 3, when the answer is lesser than guess
printf("%d\n",guess);
max = guess - 1;
guesstimes++;
}
guess = rand()%(max-min+1) + min; // a number between min and max
call = isCorrect(guess, answer);
}
ASCII(); //ASCII function
puts("終於猜到數字了\n");
printf("電腦總共猜了%d次\n", guesstimes);
puts("你想再浪費一次時間嗎?");
printf("想(請輸入1) 不想(請輸入2)");
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("太小了\n");
return 2;
}else{
printf("太大了\n");
return 3;
}
return 0;
} // end function isCorrect
//if guess is correct , print correct_banner
void ASCII(){ //star ASCII function
printf(" _=====_ _=====_ \n");
printf(" / _____ \\ / _____ \\ \n");
printf(" +.-'_____'-.---------------------------.-'_____'-.+ \n");
printf(" / | | '. S O N Y .' | _ | \\ \n");
printf(" / ___| /|\\ |___ \\ / ___| /_\\ |___ \\ \n");
printf(" / | | | ; __ _ ; | _ _ | ; \n");
printf(" | | <--- ---> | | |__| |_:> | ||_| (_)| |⠀\n");
printf(" | |___ | ___| ;SELECT START ; |___ ___| ;⠀\n");
printf(" |\\ | \\|/ | / _ ___ _ \\ | (X) | /|⠀\n");
printf(" | \\ |_____| .','" "', |___| ,'" "', '. |_____| .' |⠀\n");
printf(" | '-.______.-' / \\ANALOG/ \\ '-._____.-' | \n");
printf(" | | |------| | |⠀\n");
printf(" | /\\ / \\ /\\ | \n");
printf(" | / '.___.' '.___.' \\ | \n");
printf(" | / \\ | \n");
printf(" \\ / \\ / \n");
printf(" \\________/ \\_________/ \n");
} //end ASCII function
```