
AI
#include #include #include void m
AIn() { int com_num, cnt; srand((int)time(NULL)); com_num = 1000 + rand()%9000; printf("Welcome to the Guess the Number Game!n"); printf("The computer will generate a random number between 1000 and 9000. You have chances to guess it.n"); cnt = gress_process(com_num); printf("Congratulations, you found the correct number after %d attempts!n", cnt); } int gress_process(int com_num) { int per_num, per_count, pos_count, num_count, max_count = 0; srand((int)time(NULL)); per_num = com_num; while (1) { printf("Please enter your guess in [1000,10000]! n"); scanf("%d", &per_num); pos_count = judge_repeat_pos_cnt(per_num, com_num); num_count = judge_repeat_num_cnt(per_num, com_num); if (pos_count == 4 && num_count == 4) { if (
ABS(com_num - per_num) ==
ABS(com_num / 100 - per_num / 100)) { max_count = max(max_count, 1); if (com_num > per_num) { printf("The correct number is higher than your guess.n"); } else if (com_num < per_num) { printf("The correct number is lower than your guess.n"); } else { printf("Congratulations! The correct number is %d.n", per_num); break; } } } return max_count; } int judge_repeat_pos_cnt(int person, int computer) { int fir_num, sec_num, max_count = 0; srand((int)time(NULL)); fir_num = person; sec_num = computer; while (1) { if (fir_num % 10 == sec_num % 10) { max_count++; } fir_num /= 10; sec_num /= 10; if (max_count == persons) { printf("Congratulations! You win the game.n"); return persons; } } } int judge_repeat_num_cnt(int person, int computer) { int fir_num, sec_num, max_count = 0; srand((int)time(NULL)); fir_num = person; sec_num = computer; while (1) { if ((fir_num % 10 == sec_num % 10) || (fir_num % 10 == sec_num / 100 % 10) || (fir_num % 10 == sec_num / 1000 % 10) || (fir_num / 1000 == sec_num / 1000 % 10) || (fir_num / 1000 == sec_num / 1000 / 1000)) { max_count++; } fir_num /= 10; sec_num /= 10; if (max_count == persons) { printf("Congratulations! You win the game.n"); return persons; } } }