-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLandMine.cpp
394 lines (343 loc) · 7.33 KB
/
LandMine.cpp
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
#define MAX 20
#define XYSIZE 13
#include <stdio.h>
#include <windows.h>
#include "Console.h"
#include <time.h>
#include <conio.h>
#include <process.h>
// 격자판에 일정한 갯수의 지뢰가 깔림
// 블럭을 탐색하면 주위에 지뢰가 몇 개 있는지 알려주기
int Map[XYSIZE][XYSIZE] = { NULL };
int MineCnt[XYSIZE][XYSIZE] = { NULL };
struct OBJ {
int x = 0;
int y = 0;
};
OBJ Mine[MAX] = { NULL };
OBJ Player;
OBJ Loading[6];
struct PROCESS {
bool flag = false;
bool active = false;
};
PROCESS Front_map[XYSIZE][XYSIZE] = { false , };
CONSOLE_CURSOR_INFO cursorInfo;
bool MCheck = false;
void Init();
void CreateMap();
void CreateMine();
void CreateMinecnt();
void DrawMap();
void InputProcess();
void DrawPlayer();
void GameOver(int N);
void MineCntFind(int Playerx, int Playery);
void StartMenu();
void GameStart();
void Check_FlagToMine();
void GUI();
int randx();
int main() {
// 게임 세팅
Init();
// 게임 시작
StartMenu();
// 맵 초기화 및 생성
CreateMap();
// 맵에 지뢰 생성
CreateMine();
// MineCnt 생성
CreateMinecnt();
// 게임 스타트
GameStart();
}
void Init() {
// 흰색 커서 지우기
cursorInfo.bVisible = 0;
cursorInfo.dwSize = 1;
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursorInfo);
srand(time(NULL));
// 화면 크기 조정
system("mode con cols=70 lines=13");
// 플레이어좌표 초기화
Player.x = 0;
Player.y = 0;
}
void CreateMap() {
for (int i = 0; i < XYSIZE; i++) {
for (int j = 0; j < XYSIZE; j++) {
Map[i][j] = 0;
Front_map[i][j].flag = false;
Front_map[i][j].active = false;
MineCnt[i][j] = 0;
}
}
}
void CreateMine() {
for (int i = 0,Cnt = 0; i < MAX; i++) {
while(1) {
MCheck = false;
Mine[i].x = randx();
Mine[i].y = randx();
// 중복체크 체크
for (int j = 0; j < i; j++) {
if (Mine[i].x == Mine[j].x && Mine[i].y == Mine[j].y) {
MCheck = true;
break;
}
}
// 중복이 아닐 경우
if (MCheck == false) {
break;
}
}
// 맵에 적용
Map[Mine[i].y][Mine[i].x] = -5;
}
}
void CreateMinecnt() {
// 지뢰개수 만들기
for (int y = 0; y < XYSIZE; y++) {
for (int x = 0; x < XYSIZE; x++) {
if (Map[y][x] != -5) {
for (int i = y - 1; i < y + 2; i++) {
if (i >= 0 && i < XYSIZE) {
for (int j = x - 1; j < x + 2; j++) {
if (j >= 0 && j < XYSIZE) {
if (Map[i][j] == -5) {
MineCnt[y][x]++;
}
}
}
}
}
Map[y][x] = MineCnt[y][x];
}
}
}
}
void DrawMap() {
for (int i = 0; i < XYSIZE; i++) {
for (int j = 0; j < XYSIZE; j++) {
if (Front_map[i][j].active) {
if (Map[i][j] >= 1) {
GotoXY(j * 2, i);
SetColor(0, 14);
switch (Map[i][j]) {
case 1: printf("①"); break;
case 2: printf("②"); break;
case 3: printf("③"); break;
case 4: printf("④"); break;
case 5: printf("⑤"); break;
case 6: printf("⑥"); break;
case 7: printf("⑦"); break;
case 8: printf("⑧"); break;
}
}
else if (Map[i][j] == 0) {
GotoXY(j * 2, i);
SetColor(0, 15);
printf("□");
}
else if (Map[i][j] == -5) {
GotoXY(j * 2, i);
SetColor(0, 12);
printf("★");
}
}
else {
if (Front_map[i][j].flag) {
GotoXY(j * 2, i);
SetColor(0, 9);
printf("¶");
}
else {
GotoXY(j * 2, i);
SetColor(0, 8);
printf("■");
}
}
}
}
}
void DrawPlayer() {
// 흰색 커서 활성화
cursorInfo.bVisible = 1;
cursorInfo.dwSize = 1;
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursorInfo);
srand(time(NULL));
// 커서 위치
SetColor(0, 15);
GotoXY(Player.x, Player.y);
}
void InputProcess() {
// 왼쪽으로 이동
if (GetAsyncKeyState(VK_LEFT) && 0x8000) {
if (Player.x - 2 >= 0) {
Player.x -= 2;
}
}
// 오른쪽으로 이동
if (GetAsyncKeyState(VK_RIGHT) && 0x8000) {
if (Player.x + 2 < XYSIZE * 2) {
Player.x += 2;
}
}
// 위로 이동
if (GetAsyncKeyState(VK_UP) && 0x8000) {
if (Player.y - 1 >= 0) {
Player.y -= 1;
}
}
// 아래로 이동
if (GetAsyncKeyState(VK_DOWN) && 0x8000) {
if (Player.y + 1 < XYSIZE) {
Player.y += 1;
}
}
// 지뢰 체크키
if (GetAsyncKeyState(VK_LCONTROL) && 0x8000) {
if (Map[Player.y][Player.x / 2] == -5) {
GameOver(0);
return;
}
MineCntFind(Player.x / 2, Player.y);
}
// 깃발 꽂기
if (GetAsyncKeyState(VK_SPACE) && 0x8000) {
if (Front_map[Player.y][Player.x / 2].flag) {
Front_map[Player.y][Player.x / 2].flag = false;
}
else {
Front_map[Player.y][Player.x / 2].flag = true;
}
}
}
void MineCntFind(int Playerx, int Playery) {
int i, j;
if (Playery < 0 || Playery >= XYSIZE) return;
if (Playerx < 0 || Playerx >= XYSIZE) return;
if (Front_map[Playery][Playerx].active) return;
if (Map[Playery][Playerx] == -5) return;
if (Map[Playery][Playerx] >= 1) {
Front_map[Playery][Playerx].active = true;
return;
}
Front_map[Playery][Playerx].active = true;
for (i = Playerx - 1; i < Playerx + 2; i++) {
for (j = Playery - 1; j < Playery + 2; j++) {
MineCntFind(i,j);
}
}
}
void GameOver(int N) {
// 전체 맵 보여주기
for (int i = 0; i < XYSIZE; i++) {
for (int j = 0; j < XYSIZE; j++) {
if (Front_map[i][j].active) continue;
Front_map[i][j].active = true;
}
}
DrawMap();
Sleep(2000);
// 종료메뉴
while (1) {
Clear();
SetColor(0, 15);
GotoXY(30, 3);
if (N == 0) printf("실패!");
else if (N == 1) printf("승리!");
GotoXY(20, 7);
printf("시작메뉴로 가기 - 왼쪽ALT키");
GotoXY(26, 8);
printf("종료 - ESC키");
if (GetAsyncKeyState(VK_ESCAPE) && 0x8000) {
exit(0);
}
else if (GetAsyncKeyState(VK_LMENU) && 0x8000) {
break;
}
}
main();
return;
}
void StartMenu() {
while (1) {
Clear();
SetColor(0, 15);
GotoXY(30, 4);
printf("지뢰 찾기");
GotoXY(20, 9);
printf("시작하려면 엔터키를 누르십시오");
if (GetAsyncKeyState(VK_RETURN) && 0x8000) {
break;
}
Sleep(100);
}
}
void GameStart() {
while (1) {
Clear();
// GUI
GUI();
// 맵 그리기
DrawMap();
// 입력키
InputProcess();
// 승리체크
Check_FlagToMine();
// 포인터 그리기
DrawPlayer();
Sleep(50);
}
}
void Check_FlagToMine() {
int cnt = 0;
for (int TMPy = 0; TMPy < XYSIZE; TMPy++) {
for (int TMPx = 0; TMPx < XYSIZE; TMPx++) {
if (Front_map[TMPy][TMPx].flag) {
if (Map[TMPy][TMPx] == -5) {
cnt++;
}
}
}
}
if (cnt == XYSIZE) {
GameOver(1);
}
}
void GUI() {
GotoXY(XYSIZE * 2 + 4, 3);
printf("조작키");
GotoXY(XYSIZE * 2 + 4, 6);
printf("오른쪽 : → / 왼쪽 : ←");
GotoXY(XYSIZE * 2 + 4, 7);
printf("위 : ↑ / 아래 : ↓");
GotoXY(XYSIZE * 2 + 4, 9);
printf("깃발 : SPACE");
GotoXY(XYSIZE * 2 + 4, 10);
printf("지뢰확인 : LCTRL");
GotoXY(XYSIZE * 2 + 17, 1);
printf("지뢰 개수 : %d", MAX);
// 흰색 커서 지우기
cursorInfo.bVisible = 0;
cursorInfo.dwSize = 1;
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursorInfo);
srand(time(NULL));
}
// 랜덤
int randx()
{
static int flag = 0;
int retVal;
if (flag == 0)
{
srand(time(NULL));
rand(); rand(); rand(); rand();
srand(rand());
flag = 1;
}
retVal = rand() % XYSIZE;
return retVal;
}