SteffenLee

[C언어]게임 만들기 - 행맨[Hang Man](텍스트 기반) 본문

Progamming/C,C++

[C언어]게임 만들기 - 행맨[Hang Man](텍스트 기반)

SteffenLee 2017. 9. 23. 23:13

가장 최근에 만들었던 게임입니다.

 

 

게임을 만들면서 사용했던 함수와 헤더입니다.

stdlib.h 헤더는 rand() 함수와 system 관련 함수를 사용 목적으로 선언하였고

string.h 헤더는 strlen, strcmp함수를 사용하기 위해 선언,

time.h  헤더는 srand()를 하기 위해 선언,

windows.h는 Sleep 함수를 사용하기 위해 선언

stdbool.h 헤더는 bool 자료형을 사용하기 위해 선언하였습니다. 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <windows.h>
#include <stdbool.h>    
#define WORD_COUNT 15
//#define DEBUG
 
void map2();
void title();
void overlap(char input);//중복 확인
void wrong(char input);//틀린 알파벳 체크
bool correct(char input);//맞은 알파벳 체크
void print_input_word();
cs

 

이 프로그램에서 사용된 전역변수입니다.

1
2
3
4
5
6
7
8
9
10
11
char word[WORD_COUNT][15= { "world""heroes""voca""source""korea""storm""walk""sound""perfect""school""independent""friendship",
"master""background""generate" };
char save[30= { 0, };
int i, j, p, q;
char tmp[10];// 단어 저장
char input;//알파벳 입력
char temp;//공백 처리
int local = 0;//단어 위치
int life = 10;//생명    
char under_bar = '_';//맵에 사용
int k = 0;//중복 출력 방지
cs

 

 

메인 함수 부분입니다.

srand를 이용하여 매번 시드 값을 다르게 해서 매번 다르게 난수를 생성합니다.

알파벳 한 개를 입력받고 그 뒤에 scanf 한 번 더 써서 남은 엔터를 지워줍니다.

알파벳이 입력되면 중복체크와 정답과 오답 체크를 합니다.

#ifdef는 디버깅용 입니다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
int main() {
    title();
    srand(time(NULL));
    local = rand() % WORD_COUNT;
    //printf("%s\n", word[len]); //단어 체크
 
    while (1) {
        printf("Life : %d\n", life);
        map2();
        printf("알파벳을 입력하세요 : ");
        scanf_s("%c"&input);
        scanf_s("%c"&temp); // 남은 엔터값 지워주기
 
        overlap(input);
        
        save[p] = input;//입력 받는거 저장
        p += 1;
 
 
            if (!correct(input)) {
                wrong(input);
            }
 
            print_input_word();
 
            for (i = 0; i < strlen(word[local]); ++i) {
                if (!strcmp(tmp, word[local])) {
                    map2();
                    printf("You Win!\n");
                    return 0;
                }
            }
 
            if (life == 0) {
                printf("Game Over\n");
                printf("정답은 : ");
                printf("%s\n", word[local]);
                return 0;
            }
 
#ifdef DEBUG
            printf("[DEBUG]: ");
            for (int i = 0; i < 30; i++) {
                printf("%c ", save[i]);
            }
            printf("\n");
#endif
    }
}
cs

 

맵을 만드는 부분의 함수입니다.

strlen을 사용해서 단어의 길이 만큼 맵을 만듭니다. 

그리고 알파벳을 맞추면 그 위치에 단어를 덮어씁니다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
void map2() {
    printf("┏");
    for (i = 0; i < strlen(word[local]); ++i) {
        printf("━");
    }
 
    printf("┓\n");
    printf("┃");
 
    for (i = 0; i<strlen(word[local]); ++i) {
        printf("%c ", under_bar/*word[local][i]*/);
        for (j = 0; j < strlen(word[local]); ++j) {
            if (word[local][i] == tmp[j]) {
                printf("\b\b%c ",tmp[i]);
            }
        }
    }
 
    printf("┃\n");
    printf("┗");
    for (i = 0; i < strlen(word[local]); ++i) {
        printf("━");
    }
    printf("┛\n");
}
 
cs

 

 

입력된 알파벳의 중복을 체크해주는 부분입니다.

1
2
3
4
5
6
7
8
void overlap(char input) {
    for (q = 0; q <= p; ++q) {
        if (save[q] == input) {
            printf("OverLap\n");
            break;
        }
    }
}
cs

 

 

틀린 알파벳을 체크하는 함수입니다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void  wrong(char input) {
    for (j = 0; j < strlen(word[local]); ++j) { //단어 틀린거 체크
        if (input != word[local][i]) {
            printf("Wrong!");
            Sleep(500);
            k += 1;
            life -= 1;
            if (k >= 1) {
                system("cls");
                break;
            }
        }
    }
}
 
cs

 

 

맞은 알파벳을 체크하는 함수입니다.

이곳에선 bool 자료형을 사용하여 값을 true, false형태로 넘겨 정답을 체크후 wrong 함수로 넘어가 wrong의 출력을 방지했습니다.

그리고 independent와 같이 알파벳이 중복되는게 있을 때 정답을 체크하면 1개 이상이 체크가 되어 correct가 여러 번

출력되는 것을 막았습니다.

 

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
bool correct(char input) {
    bool memo[200= { false, };
    bool result = false;
    for (i = 0; i < strlen(word[local]); ++i) { //단어 맞는거 체크
        if (input == word[local][i]) {
            if (memo[input])
            {
                result = true;
                tmp[i] = word[local][i];
                printf("Correct!");
                system("cls");
                continue;
            }
            result = true;
            tmp[i] = word[local][i];
            printf("Correct!");
            Sleep(500);
            system("cls");
            memo[input] = true;
        }
    }
    return result;
}
cs

 

 

 

 

 

입력된 단어를 출력하는 부분입니다.

1
2
3
4
5
6
7
void print_input_word() {
    printf("Input Word : ");
    for (int i = 0; i < 30; i++) {
        printf("%c ", save[i]);
    }
    printf("\n");
}
cs

 

 

콘솔창의 제목을 바꾸어 주는 부분입니다.

1
2
3
void title() {
    system("title Hang Man");
}
cs

 

 

마지막으로 구동 영상을 올리겠습니다.

 

bgm : Different Heaven & EH!DE - My Heart[NCS Release]

link : https://www.youtube.com/watch?v=jK2aIUmmdP4

 

'Progamming > C,C++' 카테고리의 다른 글

[C++]STL vector  (0) 2018.04.07
[C/C++]동적할당 연결리스트  (0) 2018.04.05
[C++]decltype 타입 지정자  (0) 2018.03.11
[C언어]게임 만들기 - 숫자야구.ver2(2인용)  (1) 2017.10.11
[C언어]게임 만들기 - 숫자야구  (1) 2017.09.22
Comments