본문 바로가기
삽질/Com

행맨게임

by 푸딩s 2009. 10. 24.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <ctype.h>
#include <conio.h>
#include <windows.h>

 

void start(void);
int init_quiz(char*);
void add_word(void);
void toupper_word(char*);
void init_show(char*, int);
void print(char*);
int compare(char, char*, char*);

 

void main(void)
{
    int no, flag=1;
   
    srand((unsigned)time(NULL));
    while(1)
    {
        system("cls");
        printf("\n *** MENU ***\n");
        printf(" 1. 게임 시작\n 2. 단어 추가\n 3. 게임 종료\n -> [ ]\b\b");
        scanf("%d", &no);
        fflush(stdin);
       
        switch(no)
        {
        case 1 : start(); break;
        case 2 : add_word(); break;
        case 3 : exit(1);
        } 
        getch();
    }
}

void start(void)
{
    char quiz[40], ch, show[40], word[40];
    int cnt = 7;
   
    if(init_quiz( quiz ) == 1)
        return;
   
    init_show(show, strlen(quiz));
   
    do
    {
        system("cls");
        print(show);
       
        printf("\n\t\tA - Z : ");
        ch = toupper(ch = getchar()); // 입력받은 문자를 대문자로
        fflush(stdin);
       
        if(compare(ch, quiz, show) == 0) // 입력받은 문자가 존재하지 않음
        {
            printf("\n\n\t\t입력하신 문자 [%c]는 존재하지 않습니다.\n", ch);
            printf("\t\t기회는 총 [%d]번 남았습니다.\n", --cnt);
        }
        else // 입력받은 문자가 존재함 
        {
            print(show);
            if(strcmp(show, quiz) == 0) break;
           
            printf("\n\t\t생각나는 단어가 있으면 단어를 입력하시고\n");
            printf("\t\t생각나는 단어가 없으면 enter를 입력하세요. : ");
            gets(word);
            strupr(word);
            if(word[0] != '\0' && strcmp(word, quiz) != 0)
                printf("\n\t\t입력하신 단어는 정답이 아닙니다.\n");
            else if(strcmp(quiz, word) == 0)
                break;
        }
        Sleep(1000); // 1초간 멈춤
    }while(cnt > 0);
   
    if(cnt == 0) // 실패
    {
        printf("\n\n\t\t아쉽습니다. 기회를 다 소진하셨습니다.\n");
        printf("\t\t답은...");
        print(quiz);
    }
    else // 성공
        printf("\n\n\t\t축하합니다. 정답입니다.\n");
}

int compare(char ch, char* quiz, char* show)
{
    int flag = 0;
    while(*quiz != '\0')
    {
        if(*quiz == ch)
            *show = ch, flag = 1;
        quiz++, show++;
    }
    return flag;
}

void print(char* word)
{
    printf("\n\n\t\t\t");
    while(*word != '\0')
        printf("%c   ", *word++);
    printf("\n\n");
}

void init_show(char* show, int len)
{
    while(len-- > 0)
        *show++ = '_';
    *show = '\0';
}

int init_quiz(char* quiz)
{
    FILE *fp;
    int cnt = 0;
   
    fp = fopen("dic.txt", "r");
    if(fp == NULL)
    {
        printf("\n\tDIC.TXT에 저장되어있는 단어가 없습니다.\n");
        printf("\t단어를 추가하신 후 사용해주세요.\n");
        return 1;
    }
   
    while(fscanf(fp, "%s", quiz) != EOF)
        cnt++;
   
    cnt = rand() % cnt;
    rewind(fp);
   
    for(; cnt >= 0; cnt--)
        fscanf(fp, "%s", quiz);
   
    fclose(fp);
   
    return 0;
}

void add_word(void)
{
    char word[40], buf[40];
    FILE *fp;
   
    printf("\n\t추가할 단어를 입력 : ");
    gets(word);
    strupr(word);
   
    fp = fopen("dic.txt", "r");
    if(fp != NULL) // 중복체크
    {
        while(fscanf(fp, "%s", buf) != EOF)
        {
            if(strcmp(buf, word) == 0)
                break;
        }
       
        fclose(fp);
        if(strcmp(buf, word) == 0)
        {
            printf("\n\t입력하신 단어는 존재합니다.\n");
            printf("\t단어추가를 취소합니다.\n");
            return;
        }
    }
   
    fp = fopen("dic.txt", "a");
    fprintf(fp, "%s\n", word);
    fclose(fp);
   
    printf("\n\t[%s]단어를 추가하였습니다.\n", word);
}

void toupper_word(char* word)
{
    while(*word != '\0')
    {
        if(islower(*word)) 
            *word = toupper(*word);
        word++;
    }
}


'삽질 > Com' 카테고리의 다른 글

트리  (0) 2009.12.06
이중연결리스트  (0) 2009.12.06
빙고게임  (1) 2009.12.06
주석 삭제 프로그램  (0) 2009.12.06
프로그래밍 전문가가 되기 위한 Dicajohn의 7가지 방법  (0) 2009.10.24

댓글