|
그 코드는 제가 작성한 것으로서 C로만 해달고 해서 C로 코딩했습니다.
행맨이라는 개념자체를 잘 몰라서 그 당시 질문하신 분이 요구한대로 했습니다.
단, 중복된 문자는 한 번만 맞추면 되는지 여부는 질문에 명시되지 않아서 중복된 갯수만큼 맞추어야 되는 걸로 했습니다.
그리고, 'concatenation'의 문자수(길이)가 13이고 기회가 15번이므로 3번 이상 틀리면 나머지 기회를 다 사용해도 당연히 전체를 맞출 수 없게 됩니다. 따라서, 그때는 중도에 끝나는 겁니다.
마지막으로, gotoxy 함수는 콘솔 상에서 출력 시작 좌표를 지정하는 것입니다.
미니 님이 쓰신 글 :
: 전에 어떤님이 게시판에 올려놓으신건데 15번 안에 맞추는 행맨 게임이랍니다.. 그런데 해보니깐 3~4번만에 못맞추면 끝나고 중복되는 알파뱃은 하나씩 나오네요...어디가 잘못된거죠? 어떻게 고쳐야하나요..
: 그리고 gotoxy라는 부분이 있는데 이부분은 금시초문이라.. 이거말고 다른 방법 없나요?(참고로 전 배열과 포인터부분까지밖엔 모르거든요...)
:
: #include<stdio.h>
: #include<conio.h>
: #include<string.h>
:
: int MatchChar(const char *pWord,char ch,int *matched);
:
: int main(void)
: {
: const int MaxTrial=15;
: const char *const pWord="concatenation";
: const int wordLen=strlen(pWord);
: int matchedCount=0;
: int matchedIndice[16]={0,};
: char ch;
: int i ,idx;
: const int y=wherey();
:
: for(i=1 ;i<=wordLen ;i=i+1){
: putchar('_');
: }
:
: printf("\n : Input a Character. ");
:
: for(i=1 ;i<=MaxTrial ;i=i+1){
: gotoxy(1,y+1);
: printf("%2i/%2i",i,MaxTrial);
: gotoxy(i+26,y+1);
: ch=getche();
: if((idx=MatchChar(pWord,ch,matchedIndice))>=0){
: matchedCount=matchedCount+1;
: gotoxy(idx+1,y);
: putchar(ch);
: if(matchedCount==wordLen) break;
: }else if(wordLen-matchedCount>MaxTrial-i){
: break;
: }
: }
:
: gotoxy(1,y+2);
: if(matchedCount==wordLen) {
: printf("You Win!");
: }else{
: printf("Failed!");
: }
:
: getch();
: return 0;
: }
:
:
: int MatchChar(const char *pWord,char ch,int *matched)
: {
: const char *p=pWord;
: int index;
: if(!pWord || !matched) return -1;
: while(1){
: p=strchr(p,ch);
: if(!p) return -1;
: index=p-pWord;
: if(matched[index]==0){
: matched[index]=1;
: return index;
: }else{
: p=p+1;
: }
: }
: }
|