-
Notifications
You must be signed in to change notification settings - Fork 0
/
지뢰찾기.cpp
424 lines (387 loc) · 9.93 KB
/
지뢰찾기.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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
#include <stdio.h>
#include <conio.h>
#include <time.h>
#include <windows.h>
#define MAX_X 20
#define MAX_Y 20
#define LEFT 75
#define RIGHT 77
#define UP 72
#define DOWN 80
#define SPACEBAR 32
#define ESC 27
struct Node
{
BOOL mine;
BOOL strip;
BOOL flag;
BOOL wall;
int mineCnt;
};
struct APoint
{
int x;
int y;
};
void mainMenu();
void levelSelectMenu();
void initMap(struct Node (*gameMap)[MAX_Y], int gameLevel, int* retMineCnt);
void randMine(struct Node (*gameMap)[MAX_Y], int gameLevel, int* retMineCnt);
int gameStart(struct Node (*gameMap)[MAX_Y], int gameLevel, int* retMineCnt);
void displayMap(struct Node (*gameMap)[MAX_Y], int gameLevel, int* retMineCnt);
void mineRecursive(struct Node (*gameMap)[MAX_Y], int tmpX, int tmpY);
int resultFunc(struct Node (*gameMap)[MAX_Y], int gameLevel, int* retMineCnt, int tmpRetMineCnt);
int levelSelect(struct Node (*gameMap)[MAX_Y]);
int randx(int x, int y);
void gotoxy(int x, int y);
void textcolor(int color_number);
int main()
{
struct Node gameMap[MAX_X][MAX_Y];
int gameLevel=10;
int select;
int retMineCnt;
int elapsedTime=0;
int level1=999, level2=999, level3=999;
while(1)
{
system("cls");
mainMenu();
fflush(stdin); scanf("%d",&select);
system("cls");
switch(select)
{
case 1:
initMap(gameMap, gameLevel, &retMineCnt);
elapsedTime = gameStart(gameMap, gameLevel, &retMineCnt);
if(gameLevel == 10) if(level1 > elapsedTime) level1 = elapsedTime;
else if(gameLevel == 5) if(level2 > elapsedTime) level2 = elapsedTime;
else if(gameLevel == 0) if(level3 > elapsedTime) level3 = elapsedTime;
break;
case 2:
gameLevel = levelSelect(gameMap);
break;
case 3:
printf("▤▤ 최고 기록 ▤▤\n");
printf(" 1.초 급 : %d \n", level1);
printf(" 2.중 급 : %d \n", level2);
printf(" 3.고 급 : %d \n", level3);
system("pause");
break;
case 0:
puts("게임을 종료합니다");
return 0;
default:
break;
}
}
}
void mainMenu()
{
textcolor(14);
puts("┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓");
puts("┃ ▤▤ 지뢰 찾기 게임 ▤▤ ┃");
puts("┃ ┃");
puts("┃ 1. 게임 시작 ┃");
puts("┃ ┃");
puts("┃ 2. 난이도 선택 ┃");
puts("┃ ┃");
puts("┃ 3. 최고 점수 ┃");
puts("┃ ┃");
puts("┃ 0. 종 료 ┃");
puts("┃ ┃");
puts("┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛");
printf(" Select > ");
}
void levelSelectMenu()
{
textcolor(11);
puts("┏━━━━━━━━━━━━━━┓");
puts("┃ ▤▤ 난이도 선택 ▤▤ ┃");
puts("┃ ┃");
puts("┃ 1. 초 급 ┃");
puts("┃ ┃");
puts("┃ 2. 중 급 ┃");
puts("┃ ┃");
puts("┃ 3. 고 급 ┃");
puts("┃ ┃");
puts("┗━━━━━━━━━━━━━━┛");
printf(" Select > ");
}
void initMap(struct Node (*gameMap)[MAX_Y], int gameLevel, int* retMineCnt)
{
int i, j;
int cx, cy;
for(i=0; i<MAX_X-gameLevel; i++)
{
for(j=0; j<MAX_Y-gameLevel; j++)
{
gameMap[i][j].wall = 0;
gameMap[i][j].flag = 0;
gameMap[i][j].mine = 0;
gameMap[i][j].strip = 0;
gameMap[i][j].mineCnt = 0;
}
}
for(i=0; i<MAX_X-gameLevel; i++)
{
for(j=0; j<MAX_Y-gameLevel; j++)
{
if(i==0 || i==MAX_X-gameLevel-1 || j==0 || j==MAX_Y-gameLevel-1)
gameMap[i][j].wall = 1;
else
gameMap[i][j].strip = 0;
}
}
randMine(gameMap, gameLevel, retMineCnt);
for(i=0; i<MAX_X-gameLevel; i++)
{
for(j=0; j<MAX_Y-gameLevel; j++)
{
for(cx=i-1; cx<i+2; cx++)
{
for(cy=j-1; cy<j+2; cy++)
{
if(gameMap[cx][cy].mine == 1)
{
gameMap[i][j].mineCnt++;
}
}
}
}
}
}
void randMine(struct Node (*gameMap)[MAX_Y], int gameLevel, int* retMineCnt)
{
int i,j;
int mineCntSet;
if(gameLevel == 10) mineCntSet = 10;
else if(gameLevel == 5) mineCntSet = 30;
else if(gameLevel == 0) mineCntSet = 80;
else {}
*retMineCnt = mineCntSet;
do
{
i = randx(1,MAX_X-gameLevel-2);
j = randx(1,MAX_Y-gameLevel-2);
if(gameMap[i][j].mine == 0)
{
gameMap[i][j].mine = 1;
mineCntSet--;
}
}while(mineCntSet);
}
int gameStart(struct Node (*gameMap)[MAX_Y], int gameLevel, int* retMineCnt)
{
struct APoint player;
char ch;
int i, j;
int tmpRetMinCnt;
time_t startTime;
time_t endTime;
double elapsedTime=0;
time(&startTime);
tmpRetMinCnt = *retMineCnt;
player.x = 2;
player.y = 1;
displayMap(gameMap, gameLevel, retMineCnt);
gotoxy(player.x, player.y);
while(1)
{
if(kbhit())
{
ch = getch();
switch(ch)
{
case LEFT:
if(player.x-2 > 0) gotoxy(player.x-=2, player.y);
break;
case RIGHT:
if(player.x < (MAX_Y-gameLevel-2)*2) gotoxy(player.x+=2, player.y);
break;
case UP:
if(player.y-1 > 0) gotoxy(player.x, --player.y);
break;
case DOWN:
if(player.y+1 < MAX_X-gameLevel-1) gotoxy(player.x, ++player.y);
break;
default:
break;
}
if(ch == 'f' || ch == 'F') // flag
{
if(gameMap[player.y][player.x/2].flag == 0) {gameMap[player.y][player.x/2].flag = 1; --*retMineCnt;}
else {gameMap[player.y][player.x/2].flag = 0; ++*retMineCnt;}
if(resultFunc(gameMap, gameLevel, retMineCnt, tmpRetMinCnt) == 1)
{
time(&endTime);
elapsedTime = difftime(endTime, startTime);
gotoxy(((MAX_X-gameLevel)*2)+3, 5);
printf("▤ 경과 시간 : %3d 초 ", (int)elapsedTime);
gotoxy(((MAX_X-gameLevel)*2)+3, 6);
system("pause");
return (int)elapsedTime;
}
}
else if(ch == SPACEBAR)
{
if(gameMap[player.y][player.x/2].mine != 1)
{
mineRecursive(gameMap, player.y, player.x/2);
if(resultFunc(gameMap, gameLevel, retMineCnt, tmpRetMinCnt) == 1)
{
time(&endTime);
elapsedTime = difftime(endTime, startTime);
gotoxy(((MAX_X-gameLevel)*2)+3, 5);
printf("▤ 경과 시간 : %3d 초 ", (int)elapsedTime);
gotoxy(((MAX_X-gameLevel)*2)+3, 6);
system("pause");
return (int)elapsedTime;
}
}
else // 지뢰일 경우
{
for(i=0; i<MAX_X-gameLevel; i++)
{
for(j=0; j<MAX_Y-gameLevel; j++)
{
gameMap[i][j].strip = 1;
}
}
displayMap(gameMap, gameLevel, retMineCnt);
printf("You die...!! ");
gotoxy(((MAX_X-gameLevel)*2)+3, 5);
system("pause");
return 999;
}
}
else if(ch == ESC)
{
return 999;
}
displayMap(gameMap, gameLevel, retMineCnt);
gotoxy(player.x, player.y);
}
Sleep(10);
}
}
void displayMap(struct Node (*gameMap)[MAX_Y], int gameLevel, int* retMineCnt)
{
int i,j;
gotoxy(0,0);
for(i=0; i<MAX_X-gameLevel; i++)
{
for(j=0; j<MAX_Y-gameLevel; j++)
{
if(gameMap[i][j].wall == 1) {textcolor(4); printf("■");}
else
{
if(gameMap[i][j].strip == 0)
{
if(gameMap[i][j].flag == 0) {textcolor(3); printf("■");}
else {textcolor(9); printf("¶");}
}
else if(gameMap[i][j].strip == 1)
{
if(gameMap[i][j].mine == 1) {textcolor(12); printf("※");}
else
{
if(gameMap[i][j].mineCnt == 0) {textcolor(6); printf("○");}
else if(gameMap[i][j].mineCnt == 1) {textcolor(6); printf("①");}
else if(gameMap[i][j].mineCnt == 2) {textcolor(6); printf("②");}
else if(gameMap[i][j].mineCnt == 3) {textcolor(6); printf("③");}
else if(gameMap[i][j].mineCnt == 4) {textcolor(6); printf("④");}
else if(gameMap[i][j].mineCnt == 5) {textcolor(6); printf("⑤");}
else if(gameMap[i][j].mineCnt == 6) {textcolor(6); printf("⑥");}
else if(gameMap[i][j].mineCnt == 7) {textcolor(6); printf("⑦");}
else if(gameMap[i][j].mineCnt == 8) {textcolor(6); printf("⑧");}
}
}
}
}
printf("\n");
}
gotoxy(((MAX_X-gameLevel)*2)+3, 3); textcolor(14); printf("▤ 깃발 개수 : %2d", *retMineCnt);
gotoxy(((MAX_X-gameLevel)*2)+3, 4);
}
void mineRecursive(struct Node (*gameMap)[MAX_Y], int tmpX, int tmpY)
{
int i,j;
if(gameMap[tmpX][tmpY].wall == 1) return;
if(gameMap[tmpX][tmpY].strip == 1) return;
if(gameMap[tmpX][tmpY].mineCnt)
{
gameMap[tmpX][tmpY].strip = 1;
return;
}
gameMap[tmpX][tmpY].strip = 1;
for(i=tmpX-1; i<tmpX+2; i++)
{
for(j=tmpY-1; j<tmpY+2; j++)
{
mineRecursive(gameMap, i, j);
}
}
}
int resultFunc(struct Node (*gameMap)[MAX_Y], int gameLevel, int* retMineCnt, int tmpRetMineCnt)
{
int i,j;
int stripCnt=0;
int tmpCnt;
tmpCnt = tmpRetMineCnt;
for(i=0; i<MAX_X-gameLevel; i++)
{
for(j=0; j<MAX_Y-gameLevel; j++)
{
if(gameMap[i][j].wall != 1 && gameMap[i][j].strip == 0) stripCnt++;
}
}
if(*retMineCnt == 0 && stripCnt == tmpCnt)
{
displayMap(gameMap, gameLevel, retMineCnt);
gotoxy(((MAX_X-gameLevel)*2)+3, 4);
printf("Good...!! ");
gotoxy(((MAX_X-gameLevel)*2)+3, 5);
return 1;
}
return 0;
}
int levelSelect(struct Node (*gameMap)[MAX_Y])
{
int select;
while(1)
{
system("cls");
levelSelectMenu();
fflush(stdin); scanf("%d", &select);
if(select == 1) return 10;
else if(select == 2) return 5;
else if(select == 3) return 0;
else continue;
}
}
int randx(int x, int y)
{
static int flag = 0;
int retVal;
if(flag == 0)
{
srand(time(NULL));
rand();rand();rand();rand();
srand(rand());
flag = 1;
}
retVal = rand() % (y-x+1) + x;
return retVal;
}
void gotoxy(int x, int y)
{
COORD cd;
cd.X=x;
cd.Y=y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), cd);
}
void textcolor(int color_number)
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), color_number);
}