|
전에 어떤님이 게시판에 올려놓으신건데 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;
}
}
}
|