|
미니 님이 쓰신 글 :
: 행맨게임은 꼭 완성된 단어가 전부떠도 이기는것이지만 몇몇 이미 알아낸 알파벳만가지고 추측해서 맟추면 그것도 이기는걸로 치는거거든요?그리구 한 알파벳.. 여기서는 c라고 입력하면 c가 들어있는부분은 전부 알려줘야하구요.. 그렇게해서 15번 시행중에 단어를 맞추게되면 이기는게임이예요.^^ 이 바탕을 근거로 기존에 소스를 조금씩만 변형해서 알려주세요 그럼 이만...^^
: 그리구 꼭 이글에 답하시는 분 아니더라도 다른분이라도 어떻게 하는지 알려주시면 감사하게 볼께요..
:
:
:
:
:
행맨 C 소스입니다.
각 기회마다 한 문자 또는 전체 단어를 입력해야 합니다.
#include<stdio.h>
#include<conio.h>
#include<string.h>
int MatchChar(const char *pWord,char ch,int *pMatched,int *pMatchedCount);
int MatchWord(const char *pWord, char *pGuess);
void ShowMatched(const char *pWord,const int *pMatched,int x,int y);
int main(void)
{
const int MaxTrial=15;
const char *const pWord="concatenation";
const int wordLen=strlen(pWord);
int matchedCount=0;
int matchedIndices[16]={0,};
char strGuess[1024];
int i;
const int y=wherey();
for(i=1 ;i<=wordLen ;i=i+1){
putchar('_');
}
printf("\n : Input a Character or a Word.");
for(i=1 ;i<=MaxTrial ;i=i+1){
gotoxy(1,y+1);
printf("%2i/%2i",i,MaxTrial);
gotoxy(37,y+1);
clreol();
if(!gets(strGuess)) break;
if(strlen(strGuess)==1){
if(MatchChar(pWord,strGuess[0],matchedIndices,&matchedCount)>0){
ShowMatched(pWord,matchedIndices,1,y);
if(matchedCount==wordLen) break;
}
}
else{
if(MatchWord(pWord,strGuess)!=0){
gotoxy(1,y);
puts(pWord);
matchedCount=wordLen;
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 *pMatched,int *pMatchedCount)
{
const char *p=pWord;
int index,matched=0;
if(!pWord || !pMatched || !pMatchedCount) return 0;
if(ch>='A' && ch<='Z') ch = ch + ('a'-'A');
while(p=strchr(p,ch)){
index=p-pWord;
if(pMatched[index]==0){
pMatched[index]=1;
matched = matched+1;
p=p+1;
}else{
break;
}
}
*pMatchedCount = *pMatchedCount + matched;
return matched;
}
//---------------------------------------------------------------------------
int MatchWord(const char *pWord,char *pGuess)
{
if(!pWord || !pGuess) return 0;
strlwr(pGuess);
if(strcmp(pWord,pGuess)==0) return 1;
else return 0;
}
//---------------------------------------------------------------------------
void ShowMatched(const char *pWord,const int *pMatched,int x,int y)
{
int i,len;
if(!pWord || !pMatched) return;
len = strlen(pWord);
gotoxy(x,y);
for(i=0 ;i<len ;i=i+1){
if(pMatched[i]!=0) {
putchar(pWord[i]);
}else{
putchar('_');
}
}
}
//---------------------------------------------------------------------------
|