# **D1173566小專題 電子雞自己猜數字**
常常可以看到與電腦互動的猜數字遊戲,以往都是電腦設定數字,使用者來猜。但這次比較不一樣的地方是,讓電腦自己來玩這個遊戲。
以下是我設計的程式碼,一起來看看吧。
```
#include <stdio.h>
#include <stdlib.h>
int title() ; // function prototype
int guessType() ; // function prototype
int guessGame1(int answer, int time) ;
int guessGame2(int ansert, int time) ; // function prototype
void asciiArt(int answer, int time) ; // function prototype
void asciiArt2(int answer, int time) ; // function prototype
int isCorrect(int answer, int guess, int time) ;
int end() ; // function prototype
int main(){
int again = 1 ;
while (again != 0){
title() ;
guessType() ;
printf("Do you wanna play again? ( 1=yes , 0=exit )\n") ;
scanf("%d", &again) ;
}
end() ;
return 0 ;
}
// end main
int title(){
// start game
printf("========================================================================\n"
"I have a number between 1 and 1000.\n"
"Can you guess my number?\n"); // title
}
int guessType(){
int answer ;
int type ;
int time = 0 ;
answer = 1 + rand() % 1000 ; // randomly generated number
printf("What kinds of guessType do you want? ( 1=slow one , 2=fast one )\n") ;
scanf("\n%d", &type) ;
if (type == 1) { // choose the guessType
guessGame1(answer, time) ;
}
else if(type == 2){ // choose the guessType
guessGame2(answer, time) ;
}
}
int guessGame1(int answer, int time){ //guessType1
int guess = 0 ;
printf("========================================================================\n"
"Please type your first guess.\n") ;
// play
while (isCorrect(guess, answer, time) != 1){ //keep guessing if the answer is wrong
printf("%d\n", guess) ;
++guess ;
time++ ;
}
asciiArt(answer, time) ;
}
int guessGame2(int answer, int time){ //guessType2
int min = 0, max = 1000, ans = 500 ;
printf("========================================================================\n"
"Please type your first guess.\n") ;
// play
time++ ;
printf("%d\n", ans) ;
int correct = isCorrect(answer, ans, time) ;
while (correct != 1){ //keep guessing if the answer is wrong
if (correct == 0){
max = ans - 1 ;
}
else if (correct == 2){
min = ans + 1 ;
}
ans = min + (max - min) / 2 ;
printf("%d\n", ans) ;
correct = isCorrect(answer, ans, time) ;
time++ ;
}
asciiArt2(answer, time) ;
}
void asciiArt(int answer, int time){
// if the answer is currect show the ascii art
printf("====================================================================="
"===\nI found the currect number !!\nAWESOME!!\n");
printf("====================================================================="
"===\n"
" ,~. * ******** "
"******* ******** ** **********\n"
" ,-'__ `-, * "
"**//////**/**////** /**///// **** /////**///\n"
" {,-' `. } ,') * ** // "
"/** /** /** **//** /**\n"
" ,( a ) `-.__ ,',')~, * /** "
"/******* /******* ** //** /**\n"
" <=.) ( `-.__,==' ' ' '} * /** "
"*****/**///** /**//// ********** /**\n"
" ( ) / * //** "
"////**/** //** /** /**//////** /**\n"
" /` .- `\\ * //******** "
"/** //**/********/** /** /**\n"
" | ( } * //////// // "
" // //////// // // //\n"
" \\_, '. }\n"
" \\_, '. }\n"
" '._. ,_) / "
" ** ******* ****** *\n"
" | / .' "
" /** **/////** /*////** *\n"
" \\ | _ .-' "
" /** ** //**/* /** *\n"
" \\__/;--.||-' "
" /**/** /**/****** *\n"
" _|| _||__ __ "
" /**/** /**/*//// ** *\n"
" _ __.-` '`)(` `' ```._) "
" ** /**//** ** /* /** *\n"
" jgs (_`,- ,-' `''-. '-._) "
" //***** //******* /******* *\n"
" ( ( / '.__.' "
" ///// /////// /////// *\n"
" `''`'--'\n") ;
printf("\n\nThe currect number is: %d\n",answer) ; // print the currect number
printf("\nGuess Total: %d\n\n", time) ; // show the total of guesses
}
void asciiArt2(int answer, int time){
//if the answer is currect show the ascii art
printf("========================================================================\nI found the currect number !!\nAWESOME!!\n") ;
printf("========================================================================\n"
" .-. .--''` )\n"
" _ | |/` .-'`\n"
" ( `\\ /`\n"
" _) _. -'._\n"
" /` .' .-.-;\n"
" `).' / \\ \\\n"
" (`, \\_o/_o/__\n"
" / .-''` ``'-.\n"
" { /` ,___.--''`\n"
" { ; '-. \\ \\\\ * ******** ******* ******** ** **********\n"
" _ _ { |'-....-`'.\\_\\\\ * **//////**/**////** /**///// **** /////**///\n"
" / './ '. \\ \\ `'` * ** // /** /** /** **//** /**\n"
" _ \\ \\ | \\ \\\\ * /** /******* /******* ** //** /**\n"
" ( '-.J \\_..----.._ __) `\\--..__ * /** *****/**///** /**//// ********** /**\n"
" .-` ` `\\ ''--...--. * //** ////**/** //** /** /**//////** /**\n"
" (_,.--''`/` .- `\\ .__ _) * //******** /** //**/********/** /** /**\n"
" | ( } .__ _) * //////// // // //////// // // //\n"
" \\_, '. }_ - _.'\n"
" \\_, '. } `'--'\n"
" '._. ,_) / ** ******* ****** *\n"
" | / .' /** **/////** /*////** *\n"
" \\ | _ .-' /** ** //**/* /** *\n"
" \\__/;--.||-' /**/** /**/****** *\n"
" _|| _||__ __ /**/** /**/*//// ** *\n"
" _ __.-` '`)(` `' ```._) ** /**//** ** /* /** *\n"
" jgs (_`,- ,-' `''-. '-._) //***** //******* /******* *\n"
" ( ( / '.__.' ///// /////// /////// *\n"
" `''`'--'\n") ;
printf("\n\nThe currect number is: %d\n",answer) ;
printf("Guess Total: %d\n",time) ;
}
int isCorrect(int answer, int guess, int time){
if (guess < answer){
printf("Lower\n") ;
return 2 ;
}
else if (guess > answer){
printf("Higher\n") ;
return 0 ;
}
else{
printf("Correct\n") ;
return 1 ;
}
}
int end(){
printf("====================================================================="
"===\nHope to see you next time !!"); // exit
}
```
**我提供兩種猜數字方法**
第一種:最簡單但是最慢的方法(從0開始猜)
第二種:進階一點(夾擠定理)
# 使用最基本的從0開始猜方法
**(每次數字增加1) 逐步的猜出答案**
```
int guessGame1(int answer, int time){ //guessType1
int guess = 0 ;
printf("========================================================================\n"
"Please type your first guess.\n") ;
// play
while (isCorrect(guess, answer, time) != 1){ //keep guessing if the answer is wrong
printf("%d\n", guess) ;
++guess ;
time++ ;
}
asciiArt(answer, time) ;
}
```
# 使用較為進階的方法(夾擠定理)
設定最大值及最小值,再利用公式 Answer=Max-(Max-min)/2 和 Answer=min+(Max-min)/2 等公式計算出答案。
透過夾擠定理能較快得知數字的範圍,猜出答案。
```
int guessGame2(int answer, int time){ //guessType2
int min = 0, max = 1000, ans = 500 ;
printf("========================================================================\n"
"Please type your first guess.\n") ;
// play
time++ ;
printf("%d\n", ans) ;
int correct = isCorrect(answer, ans, time) ;
while (correct != 1){ //keep guessing if the answer is wrong
if (correct == 0){
max = ans - 1 ;
}
else if (correct == 2){
min = ans + 1 ;
}
ans = min + (max - min) / 2 ;
printf("%d\n", ans) ;
correct = isCorrect(answer, ans, time) ;
time++ ;
}
asciiArt2(answer, time) ;
}
```
以上就是我期中專題製作