Turbo-C
C++Builder  |  Delphi  |  FireMonkey  |  C/C++  |  Free Pascal  |  Firebird
볼랜드포럼 BorlandForum
 경고! 게시물 작성자의 사전 허락없는 메일주소 추출행위 절대 금지
터보-C 포럼
Q & A
FAQ
팁&트릭
강좌/문서
자료실
Lua 게시판
볼랜드포럼 홈
헤드라인 뉴스
IT 뉴스
공지사항
자유게시판
해피 브레이크
공동 프로젝트
구인/구직
회원 장터
건의사항
운영진 게시판
회원 메뉴
북마크
볼랜드포럼 광고 모집

C/C++ Q/A
[2703] 행맨 ( C 소스 )
임문환.실업자 [origin] 3228 읽음    2003-05-26 18:10
미니 님이 쓰신 글 :
:  행맨게임은 꼭 완성된 단어가 전부떠도 이기는것이지만 몇몇 이미 알아낸 알파벳만가지고 추측해서 맟추면 그것도 이기는걸로 치는거거든요?그리구 한 알파벳.. 여기서는 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('_');
  }
}
}
//---------------------------------------------------------------------------

+ -

관련 글 리스트
2702 다시질문할께여그럼... 미니 1249 2003/05/26
4108     Re:다시질문할께여그럼... 임문환.실업자 1364 2003/05/26
2703     행맨 ( C 소스 ) 임문환.실업자 3228 2003/05/26
Google
Copyright © 1999-2015, borlandforum.com. All right reserved.