flrto

C언어 보물찾기 본문

C/Academy

C언어 보물찾기

갈릭새우칩 2019. 6. 20. 18:23
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#include <stdio.h>
#include <Windows.h>
#include <limits.h>
#include <stdlib.h>
#include <time.h>
#include <conio.h>
 
const int SIZE_X  = 20;
const int SIZE_Y  = 10;
const int MapWall = 1;
const int Player  = 2;
const int Star    = 3;
 
int StarX = 0;
int StarY = 0;
 
int PlayerX = 0;
int PlayerY = 0;
 
int AutoX = 0;
int AutoY = 0;
 
int MO = 0;
int AutoFlag = 0;
 
int list[SIZE_Y][SIZE_X] =
{
    { 11111111111111111111 },
    { 10000000000000000001 },
    { 10000000000000000001 },
    { 10000000000000000001 },
    { 10000000000000000001 },
    { 10000000000000000001 },
    { 10000000000000000001 },
    { 10000000000000000001 },
    { 10000000000000000001 },
    { 11111111111111111111 }
};
 
void KeyChk();
void DrawWindow();
void Init();
void Mission();
void Auto();
 
int main(void)
{
    Init();
    while (MO != 1)
    {
        DrawWindow();
        KeyChk();
        Mission();
        Auto();
    }
    return 0;
}
 
void Init()
{
    PlayerX = 5;
    PlayerY = 5;
 
    list[PlayerY][PlayerX] = 2;
    srand(time(NULL));
    while (list[StarY][StarX] != 3)
    {
        StarX = rand() % SIZE_X;
        StarY = rand() % SIZE_Y;
        if (list[StarY][StarX] != MapWall && list[StarY][StarX] != Player)
        {
            list[StarY][StarX] = Star;
        }
    }
}
 
void Auto()
{
    if (AutoFlag == 1)
    {
        Sleep(100);
        if (AutoX != 0)
        {
            if (AutoX > 0)
            {
                PlayerX++;
                AutoX--;
                
            }
            if (AutoX < 0)
            {
                PlayerX--;
                AutoX++;
            }
        }
        if (AutoX == 0 && AutoY != 0)
        {
            if (AutoY > 0)
            {
                PlayerY++;
                AutoY--;
            }
            if (AutoY < 0)
            {
                PlayerY--;
                AutoY++;
            }
        }
    }
}
 
void Mission()
{
    if (StarX == PlayerX && StarY == PlayerY)
    {
        MO = 1;
    }
    if (MO == 1)
    {
        system("cls");
        printf("보물을 찾았습니다.\n");
    }
}
 
void DrawWindow()
{
    system("cls");
    for (int y = 0; y < SIZE_Y; y++)
    {
        for (int x = 0; x < SIZE_X; x++)
        {
            if (list[y][x] == 1)
                printf("#");
            else if (PlayerX == x && PlayerY == y)
                printf("$");
            else if (list[y][x] == 3)
                printf("*");
            else
                printf(" ");
        }
        printf("\n");
    }
    printf("\nStarX : %d\tStarY : %d\nPlayerX : %d\tPlayerY : %d", StarX, StarY, PlayerX, PlayerY, AutoX, AutoY);
}
 
void KeyChk()
{
    if (AutoFlag != 1)
    {
        char ch;
        ch = _getch();
        switch (ch)
        {
        case 'w'/*119*/:
            printf("\n위로\n");
            if (list[--PlayerY][PlayerX] == 1)
                PlayerY++;
            break;
        case 's'/*97*/:
            printf("\n밑으로\n");
            if (list[++PlayerY][PlayerX] == 1)
                PlayerY--;
            break;
        case 'a'/*100*/:
            printf("\n왼쪽으로\n");
            if (list[PlayerY][--PlayerX] == 1)
                PlayerX++;
            break;
        case 'd'/*115*/:
            printf("\n오른쪽으로\n");
            if (list[PlayerY][++PlayerX] == 1)
                PlayerX--;
            break;
        case 'p'/*115*/:
            AutoX = StarX - PlayerX;
            AutoY = StarY - PlayerY;
            AutoFlag = 1;
            break;
        }
    }
}
cs


'C > Academy' 카테고리의 다른 글

C언어 간단한 로그인 프로그램  (1) 2019.06.25
문제  (0) 2019.06.20
C 언어 로또(배열 사용 X)  (0) 2019.06.14
C 언어 두 수 크기 비교 함수  (0) 2019.06.14
C 언어 난수 값 맞추기 게임  (0) 2019.06.13
Comments