#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <windows.h>
typedef struct {
int number;
int divisor;
int attempt;
int cnt_for_hint;
int cnt_for_question;
double duration;
} GameElement;
void game(void);
void game_animation(void);
void pause(void);
void question(GameElement *state);
void hint(GameElement *state);
void ending_message(GameElement *state);
void time_limit(GameElement *state);
void print_message(char *s);
int multiple_judgment(int a, int k);
int remainder_6(int a);
int sum_number(int a);
int divisor_cnt(int a);
int main() {
srand(time(NULL));
printf("Hello welcome to the game 'Guess the unknown number'\n\n\n"
"----------------<Rules>----------------------------\n\n"
"The unknown number is between 1~100\n\n"
"Enter '1', '2', '3' to ask if the number is the multiple of 3, 7, or 11\n\n"
"Enter '4' to figure out the remainder when divided by 6\n\n"
"Enter '5' to figure out the sum of the numbers in each place\n\n"
"Enter '6' to figure out whether the number is a prime number or a composite number\n\n"
"Enter 'H' to require a hint\n\n"
"The chance for a hint is only once\n\n"
"You lose the game when you fail to figure out the answer within 3 attempts\n\n"
"There is a time limit of 100 seconds\n\n"
"The time is measured from the moment the start key is pressed\n"
"-----------------------------------------------------\n\n");
char game_start_key;
printf("Are you ready to play the game? Enter 'y' to start.\nenter:");
while(1) {
scanf(" %c", &game_start_key);
if(game_start_key=='y') {
game();
break;
} else if(game_start_key=='n') {
printf("Why?\n");
char refuse_message[50]="You refused to play the game.";
game_animation();
print_message(refuse_message);
pause();
break;
} else {
printf("You entered the wrong key. Try again.\nenter:");
}
}
}
void game(void) {
GameElement state = {0};
state.number = rand() % 100 + 1;
state.divisor = divisor_cnt(state.number);
int answer = -1;
game_animation();
printf("Game Start!\n\n");
time_t start = time(NULL);//시간 측정 시작
while(state.number != answer) {
printf("Enter 'Q' to ask a question, 'A' to enter the answer or 'H' for hint:");//게임 동작 규칙
char key;
scanf(" %c", &key);
getchar();
if(key == 'Q') {
question(&state);
} else if(key == 'A') {
printf("answer: ");
scanf("%d", &answer);//숫자 정답 입력 받기
if(state.number == answer) { //정답 맞았을 떄
state.attempt++;
time_t end = time(NULL);//시간 측정 끝내기
state.duration = difftime(end, start);
ending_message(&state);//엔딩메시지
break;//while 문 빠져나오기
} else {
//정답이 틀렸을 경우
state.attempt++;
if(state.attempt > 2) {
char fail_message[20]="You failed";
pause();
printf("\nThe number was %d\n", state.number);
pause();
printf("You failed to figure out the answer within 3 attempts.\n");
game_animation();
print_message(fail_message);
pause();
break;
}
printf("You are incorrect. Try again.\n");
}
} else if(key == 'H') {
hint(&state);
} else {
printf("There is no such command\n");}
}
}
void question(GameElement *state) {
int question_key;
int number = state->number;
int divisor = state->divisor;
printf("\nenter the key (1~6) to ask a question\nenter:");//질문키 목록
scanf("%d", &question_key);//질문키 입력 받기
getchar();
switch(question_key) {
case 1:
if(multiple_judgment(number, 3) == 1)//3의 배수 검사
printf("\nThe number is a multiple of 3\n");
else
printf("\nThe number is not a multiple of 3\n");
break;
case 2:
if(multiple_judgment(number, 7) == 1)//7의 배수 검사
printf("\nThe number is a multiple of 7\n");
else
printf("\nThe number is not a multiple of 7\n");
break;
case 3:
if(multiple_judgment(number, 11) == 1)//11의 배수 검사
printf("\nThe number is a multiple of 11\n");
else
printf("\nThe number is not a multiple of 11\n");
break;
case 4:
printf("\nThe remainder when divided by 6 is %d\n", remainder_6(number));
break;//6으로 나눴을 때 나머지 검사
case 5:
printf("\nThe sum of the numbers in each place is %d\n", sum_number(number));
break;//각자리 숫자의 합
case 6:
if(divisor == 1)
printf("The number has only 1 divisor\n");
else if(divisor == 2)
printf("The number is a prime number\n");
else
printf("The number is a composite number\n");
break;
default:
printf("There is no such command\n");
return;
}
state->cnt_for_question++;
}
void hint(GameElement *state) {
int cnt_for_hint = state->cnt_for_hint;
int number = state->number;
int divisor = state->divisor;
if(cnt_for_hint != 0)//초기값은 0이라 실행된다
printf("There is no more chance for hint.\n");
else {
int type_of_hint=rand()%3;
int hint_number;
switch(type_of_hint) {
case 0:
hint_number=rand()%number;
printf("The number is bigger than %d\n", hint_number);
break;
case 1:
hint_number=rand()%(100-number)+number+1;
printf("The number is smaller than %d\n", hint_number);
break;
default:
printf("The number of divisors is %d\n", divisor);
}
state->cnt_for_hint++;
}
}
void ending_message(GameElement *state) {
int number = state->number;
int attempt = state->attempt;
int cnt_for_hint = state->cnt_for_hint;
int cnt_for_question = state->cnt_for_question;
double duration = state->duration;
pause();
printf("\nCongratulations! Your answer was correct. The number was %d\n", number);
if(attempt == 1) {
if(cnt_for_hint != 0) {
pause();
printf("You figured out the number out of %d attempt %d questions and one chance of hint\n\n", attempt, cnt_for_question);
} else {
pause();
printf("You figured out the number out of %d attempt %d questions and you didn't use the chance for hint\n\n", attempt, cnt_for_question);
}
} else {
if(cnt_for_hint != 0) {
pause();
printf("You figured out the number out of %d attempts %d questions and one chance of hint\n\n", attempt, cnt_for_question);
} else {
pause();
printf("You figured out the number out of %d attempts %d questions and you didn't use the chance for hint\n\n", attempt, cnt_for_question);
}
}
time_limit(state);
}
void time_limit(GameElement *state) {
double duration = state->duration;
if(duration > 60) {
int min=(int)duration/60;
int sec=(int)duration-min*60;
if(duration < 100) {
pause();
printf("Total time: %dminute %dseconds\n", min, sec);
pause();
printf("You managed to guess the correct answer within 100 seconds!\n");
char message[20]="You won the game!";
game_animation();
print_message(message);
} else if(duration < 120) {
pause();
printf("Total time: %dminute %dseconds\n", min, sec);
pause();
printf("You failed to guess the correct answer within 100 seconds.\n");
char message[20]="You failed";
game_animation();
print_message(message);
} else {
pause();
printf("Total time: %dminutes %dseconds\n", min, sec);
pause();
printf("You failed to guess the correct answer within 100 seconds.\n");
char message[20]="You failed";
game_animation();
print_message(message);
}
} else {
pause();
printf("Total time: %.0lfseconds\n", duration);
pause();
printf("You managed to guess the correct answer within 100 seconds!\n");
char message[20]="You won the game!";
game_animation();
print_message(message);
}
}
void print_message(char *s) {
int i=0;
while(s[i] != '\0') {
pause();
printf("%c", s[i]);
i++; }
}
void game_animation(void) {
for(int i = 0; i < 8; i++) {
pause();
printf(".\n");//게임 시작 모션
}
}
void pause(void) {
Sleep(250);
}
int multiple_judgment(int a, int k) {
return (a%k)?0:1;
}
int remainder_6(int a) {
int remainder = a % 6;
return remainder;
}
int sum_number(int a) {
int sum=0;
while(a > 0) {
sum = sum + a % 10;
a = a / 10;
}
return sum;
}
int divisor_cnt(int a) {
if(a == 1) {
return 1;
} else {
int divisor = 2;
for(int i = 2; i * i <= a; i++) {
if(a % i == 0){
divisor += 2;
if(i * i == a)
divisor--;
}
}
return divisor;
}
}
'코드백업' 카테고리의 다른 글
| 완전이진트리 전위, 중위, 후위 순회 - 배열구현 (0) | 2026.04.27 |
|---|---|
| 콘솔 게임 코드 최종본 (0) | 2025.05.12 |