C언어로 행맨 게임 만드는데 조건 때문에 질문 드립니다.
코드는
#include <stdio.h>
#include <stdlib.h>
int check(char s[], char a[], char ch[]);
void answer_01(char s[], int n);
int main(void) {
char solution[100] = "meet at midnight"; //정답을 저장하는 배열변수
char answer[100] = "____ __ ________"; //현재까지 사용자가 맞춘것을 보여주는 배열함수
char ch;
int try = 0;
while (1) {
printf("문자열을 입력하시오 : %s\n", answer);
fflush(stdout);
printf("글자를 추측하시오 : ");
fflush(stdout);
ch = getchar();
if (check(solution, answer, ch) == 1) { //함수 check의 반환값이 1일때
printf("%s\n", answer);
break; //while문을 빠져나간다.
}
fflush(stdin);
if (try == 10) { //if(s) //만약 "try"변수가 10과 같으면
answer_01(solution, try); //answer 라는 함수를 solution, try인자를 전달후
return 0; //프로그램을 끝낸다.
} //if(e)
try++; //while문을 반복후 "try"라는 함수를 증가시킨다.
}
printf("정답입니다.\n시도횟수는 : %d", try);
return 0;
}
int check(char s[], char a[], char ch[]) {
int i;
for (i = 0; s[i] != NULL; i++) { //반복문 for문을 돌린다. s[i] 즉,solution이 NULL이 아닐때까지
if (s[i] == ch) //"solution"배열 변수와 사용자가 입력한 "ch"가 같으면
a[i] = ch; //"answer"배열 변수에 해당하는 배열에 "ch"를 저장한다.
}
if (strcmp(s, a) == 0) { //"solution"배열과 "answer"가 일치하면
return 1; //1을 리턴한다. 즉 while문을 빠져나간다.
}
else
return 0; //그렇지 않으면 0을 리턴한다.
}
void answer_01(char s[], int n) {
printf("시도횟수를 초과하셔서 실패하셧습니다.\n시도횟수는 : %d\n", n);
fflush(stdin);
}
추가 조건이 "실패했을때 정답을 출력하라" 인데요.
도와주시면 감사하겠습니다!