---
# System prepended metadata

title: '**D1149682 電子雞自己猜數字**'

---

# **D1149682 電子雞自己猜數字**
**分兩種猜法**
第一種從500猜，蹦每次猜中間值
第二種則是從最大最小值中間猜亂數
```
int auto_guess(int min, int max, int guess_way) {
  int guess;
  //-----[猜測方法1: 猜中間值 ]--------------------------------------
  if (guess_way == 1) {
    guess = (min + max) / 2;

    //-----[猜測方法2: 猜個亂數]--------------------------------------
  } else if (guess_way == 2) {
    guess = rand() % (max + 1 - min) + min;
  }
  return guess;
}
```
判斷的地方
分成三種結果
x=1的時候是正確答案
x=2時太低,並把min設為guess+1
x=3太高,並把max設為guess
```
do {
    i = 0;
    int guess_way = 1;
    srand(time(NULL));
    // generate random number between 1 and 1000
    // 1 is shift, 1000 is scaling factor
    answer = 1 + rand() % 1000;
    // printf("answer=%d\n",answer);f
    // prompt for guess
    printf("==============================================\n");
    printf("I have a number between 1 and 1000.(==%d)\n", answer);

    printf("請問要用哪種猜法?\n");
    printf("1:猜中間值 ");
    printf("2:猜亂數 ==>");
    scanf("%d", &guess_way);
    printf("Can you guess my number?\n");
    printf("Please type your first guess.\n");
    // scanf("%d", &guess);
    min = 1;
    max = 1000;
    guess = auto_guess(min, max, guess_way);
    printf("%d\n", guess);
    x = isCorrect(guess, answer);
    //-------------------------------------------
    while (x != 1) {

      if (x == 2) {
        min = guess + 1;
        max = max;
      } else if (x == 3) {
        min = min;
        max = guess;
      }
      guess = auto_guess(min, max, guess_way);
      i++;
      printf("%d\n", guess);
      x = isCorrect(guess, answer);
    }
```

show出ASCii碼
```
void show_ascii() {
  puts("\n    \\\n"
       "    (o>\n"
       " \\_//)\n"
       " \\_/_)\n"
       "  _|_ ");
}
```
