-
Notifications
You must be signed in to change notification settings - Fork 0
/
3_Game_Omok(180406).c
459 lines (402 loc) · 13.2 KB
/
3_Game_Omok(180406).c
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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <Windows.h>
#define WIDTH 19
#define HEIGHT 19
#define RIGHT 77
#define LEFT 75
#define UP 72
#define DOWN 80
#define ENTER 13
void gotoxy(int x, int y)
{
COORD CursorPosition = { x,y };
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), CursorPosition);
}
typedef enum RESULT { BLACKFOUL = -2, WHITEWIN = -1, DRAW = 0, BLACKWIN, WHITEFOUL } RESULT;
typedef unsigned short unshort;
typedef struct LOCATION {
int col;
int row;
} LOCATION;
typedef struct LINEINFO {
char maxColor; // white = -1, black = 1
unshort maxLength;
} LINEINFO;
typedef struct STATUS {
char stone; // white = -1, black = 1
LOCATION location;
LINEINFO colL; // →
LINEINFO rowL; // ↓
LINEINFO upL; // ↗
LINEINFO downL; // ↘
struct STATUS *uLink;
struct STATUS *dLink;
struct STATUS *lLink;
struct STATUS *rLink;
} STATUS;
void print_turn(int turn);
void draw_board(STATUS *board, int turn, LINEINFO StraightCol[], LINEINFO StraightRow[], LINEINFO diagonalUp[], LINEINFO diagonalDown[]);
void reset_lineinfo(LINEINFO target[], int limit);
void null_setting(STATUS *target);
STATUS *setting_board(void);
// void print_board(STATUS *target); // 테스트용 소스
char *change_stone(char stone);
STATUS *get_position(STATUS *board, int turn);
STATUS *board_status(STATUS **target);
void line_update(STATUS *board, STATUS *target, LINEINFO StraightCol[], LINEINFO StraightRow[], LINEINFO diagonalUp[], LINEINFO diagonalDown[]);
void lets_judgment(RESULT result);
RESULT result = DRAW;
int main() {
STATUS *board = setting_board();
int turn = 1;
LINEINFO StraightCol[HEIGHT];
reset_lineinfo(StraightCol, HEIGHT);
LINEINFO StraightRow[WIDTH];
reset_lineinfo(StraightRow, WIDTH);
LINEINFO diagonalUp[WIDTH + HEIGHT - 1];
reset_lineinfo(diagonalUp, WIDTH + HEIGHT - 1);
LINEINFO diagonalDown[WIDTH + HEIGHT - 1];
reset_lineinfo(diagonalDown, WIDTH + HEIGHT - 1);
// print_board(board); // 테스트용 소스
do {
draw_board(board, turn, StraightCol, StraightRow, diagonalUp, diagonalDown);
line_update(board, get_position(board, turn), StraightCol, StraightRow, diagonalUp, diagonalDown);
turn++;
} while (!result);
lets_judgment(result);
return 0;
}
void print_turn(int turn) {
if (turn % 2) {
printf("%3d번째 턴, ● : %3d, ○ : %3d\n", turn, (turn - 1) / 2, (turn - 1) / 2);
}
else {
printf("%3d번째 턴, ● : %3d, ○ : %3d\n", turn, turn / 2, turn / 2 - 1);
}
}
void draw_board(STATUS *board, int turn, LINEINFO StraightCol[], LINEINFO StraightRow[], LINEINFO diagonalUp[], LINEINFO diagonalDown[]) {
STATUS *upperline = board;
STATUS *data = board;
system("cls");
int j;
print_turn(turn);
// 바둑판 상단
printf(" ");
for (int i = 0; i < WIDTH; i++) { printf("%2d", diagonalUp[i].maxLength); }
printf("\n");
printf(" ");
for (int i = 0; i < WIDTH; i++) { printf("%s", change_stone(diagonalUp[i].maxColor)); }
printf("\n");
printf(" ");
for (int i = 0; i < WIDTH; i++) {
printf("↗");
}
printf("\n");
// 메인 바둑판
data = board;
printf(" ┌─");
for (int i = 0; i < WIDTH; i++) {
printf("┬─");
}
printf("┐\n");
j = 0;
while (upperline != NULL) {
data = upperline;
printf("%2d%s→├─", StraightCol[j].maxLength, change_stone(StraightCol[j].maxColor));
while (data != NULL) {
if (change_stone(data->stone) == "◐") {
printf("┼─");
}
else {
printf("%s", change_stone(data->stone));
}
data = data->rLink;
}
printf("┤↗%s%2d↘%s%2d\n", change_stone(diagonalUp[WIDTH - 1 + j].maxColor), diagonalUp[WIDTH - 1 + j].maxLength, change_stone(diagonalDown[WIDTH - 1 + HEIGHT - 1 - j].maxColor), diagonalDown[WIDTH - 1 + HEIGHT - 1 - j].maxLength);
upperline = upperline->dLink;
j++;
}
printf(" └─");
for (int i = 0; i < WIDTH; i++) {
printf("┴─");
}
printf("┘\n");
// 바둑판 하단
printf(" ");
for (int i = 0; i < WIDTH; i++) {
printf("↘");
}
printf("\n");
printf(" ");
for (int i = 0; i < WIDTH; i++) { printf("%s", change_stone(diagonalDown[i].maxColor)); }
printf("\n");
printf(" ");
for (int i = 0; i < WIDTH; i++) { printf("%2d", diagonalDown[i].maxLength); }
printf("\n");
printf(" ");
for (int i = 0; i < WIDTH; i++) {
printf("↓");
}
printf("\n");
printf(" ");
for (int i = 0; i < WIDTH; i++) { printf("%s", change_stone(StraightRow[i].maxColor)); }
printf("\n");
printf(" ");
for (int i = 0; i < WIDTH; i++) { printf("%2d", StraightRow[i].maxLength); }
printf("\n");
}
void reset_lineinfo(LINEINFO target[], int limit) {
for (int i = 0; i < limit; i++) {
target[i].maxColor = 0;
target[i].maxLength = 0;
}
}
void null_setting(STATUS *target) {
target->stone = 0;
(target->location).col = 0;
(target->location).row = 0;
(target->colL).maxColor = 0;
(target->colL).maxLength = 0;
(target->rowL).maxColor = 0;
(target->rowL).maxLength = 0;
(target->upL).maxColor = 0;
(target->upL).maxLength = 0;
(target->downL).maxColor = 0;
(target->downL).maxLength = 0;
target->uLink = NULL;
target->dLink = NULL;
target->lLink = NULL;
target->rLink = NULL;
}
STATUS *setting_board(void) {
STATUS *init; // (0, 0)셀
init = (STATUS *)malloc(sizeof(STATUS));
null_setting(init);
(init->location).col = HEIGHT - 1;
(init->location).row = 0;
STATUS *data;
STATUS *prev = init;
STATUS *upperline;
for (int i = HEIGHT - 2; i >= 0; i--) { // 1열 생성 ↑
data = (STATUS *)malloc(sizeof(STATUS));
null_setting(data);
prev->uLink = data;
data->dLink = prev;
(data->location).col = i;
(data->location).row = 0;
prev = data;
}
init = data; // (0, 0)셀 지정
prev = init;
for (int j = 1; j < WIDTH; j++) { // 1행 생성 →
data = (STATUS *)malloc(sizeof(STATUS));
null_setting(data);
prev->rLink = data;
data->lLink = prev;
(data->location).col = 0;
(data->location).row = j;
prev = data;
}
upperline = init;
for (int i = 1; i < HEIGHT; i++) {
prev = upperline->dLink;
for (int j = 1; j < WIDTH; j++) {
data = (STATUS *)malloc(sizeof(STATUS));
null_setting(data);
prev->rLink = data;
data->lLink = prev;
(data->location).col = i;
(data->location).row = j;
prev = data;
}
upperline = upperline->dLink;
}
upperline = init;
for (int j = 1; j < HEIGHT; j++) {
prev = upperline->rLink;
data = (upperline->dLink)->rLink;
for (int i = 1; i < WIDTH; i++) {
prev->dLink = data;
data->uLink = prev;
prev = prev->rLink;
data = data->rLink;
}
upperline = upperline->dLink;
}
return init;
}
/*
void print_board(STATUS *target) { // 테스트용 소스
STATUS *upperline = target;
STATUS *data = target;
while (upperline != NULL) {
data = upperline;
while (data != NULL) {
printf("%02d, %02d, %p, %p, %p, %p\n", (data->location).col, (data->location).row, data->uLink, data->dLink, data->lLink, data->rLink);
data = data->rLink;
}
upperline = upperline->dLink;
}
}
*/
char *change_stone(char stone) {
if (stone == -1) return "●";
else if (stone == 1) return "○";
else return "◐";
}
STATUS *get_position(STATUS *board, int turn) {
STATUS *data = board;
STATUS *target = NULL;
int k = 1;
int x = 9, y = 9;
for (int i = 0; i < x; i++) { data = data->rLink; }
for (int j = 0; j < y; j++) { data = data->dLink; }
int cursor_x = 27, cursor_y = 14;
do {
int z = _getch(); // char c 변수에 문자를 입력 받는다
switch (z) {
case RIGHT:
++x, cursor_x += 2;
if (x >= WIDTH) {
--x, cursor_x -= 2;// 만약 오목판을 넘어선다면 입력이 무시가 됩니다.
}
else data = data->rLink;
gotoxy(cursor_x, cursor_y); // 아니라면 커서를 입력한 좌표로 이동합니다.
break; // 그리고 다시 입력을 받기 위해 swtich문을 빠져 나가며 다시 루트가 됩니다.
case LEFT:
--x, cursor_x -= 2;
if (x < 0) {
++x, cursor_x += 2;
}
else data = data->lLink;
gotoxy(cursor_x, cursor_y);
break;
case UP:
--y, --cursor_y;
if (y < 0) {
++y, ++cursor_y;
}
else data = data->uLink;
gotoxy(cursor_x, cursor_y);
break;
case DOWN:
++y, ++cursor_y;
if (y >= HEIGHT) {
--y, --cursor_y;
}
else data = data->dLink;
gotoxy(cursor_x, cursor_y);
break;
case ENTER: //ENTER가 입력되면 현재 위치를 오목 배열에 저장
if (data->stone == 0) {
if (turn % 2) {
data->stone = -1; // status[HEIGHT][WIDTH]에 대입
target = board_status(&data);
}
else {
data->stone = 1;
target = board_status(&data);
}
k = 0; // 무한루프를 벗어납니다.
}
}
} while (k);
return target;
}
STATUS *board_status(STATUS **target) {
int threeRule = 0;
(*target)->colL.maxColor = (*target)->stone; // →
(*target)->colL.maxLength += 1; // →
if ((*target)->lLink != NULL && ((*target)->lLink)->stone == (*target)->stone) {
(*target)->colL.maxLength += ((*target)->lLink)->colL.maxLength;
((*target)->lLink)->colL.maxLength += 1;
}
if ((*target)->rLink != NULL && ((*target)->rLink)->stone == (*target)->stone) {
(*target)->colL.maxLength += ((*target)->rLink)->colL.maxLength;
((*target)->rLink)->colL.maxLength += 1;
}
(*target)->rowL.maxColor = (*target)->stone; // ↓
(*target)->rowL.maxLength += 1; // ↓
if ((*target)->uLink != NULL && ((*target)->uLink)->stone == (*target)->stone) {
(*target)->rowL.maxLength += ((*target)->uLink)->rowL.maxLength;
((*target)->uLink)->rowL.maxLength += 1;
}
if ((*target)->dLink != NULL && ((*target)->dLink)->stone == (*target)->stone) {
(*target)->rowL.maxLength += ((*target)->dLink)->rowL.maxLength;;
((*target)->dLink)->rowL.maxLength += 1;
}
(*target)->upL.maxColor = (*target)->stone; // ↗
(*target)->upL.maxLength += 1; // ↗
if ((*target)->uLink != NULL && (*target)->rLink != NULL && (((*target)->uLink)->rLink)->stone == (*target)->stone) {
(*target)->upL.maxLength += (((*target)->uLink)->rLink)->upL.maxLength;
(((*target)->uLink)->rLink)->upL.maxLength += 1;
}
if ((*target)->dLink != NULL && (*target)->lLink != NULL && (((*target)->dLink)->lLink)->stone == (*target)->stone) {
(*target)->upL.maxLength += (((*target)->dLink)->lLink)->upL.maxLength;
(((*target)->dLink)->lLink)->upL.maxLength += 1;
}
(*target)->downL.maxColor = (*target)->stone; // ↘
(*target)->downL.maxLength += 1; // ↘
if ((*target)->dLink != NULL && (*target)->rLink != NULL && (((*target)->dLink)->rLink)->stone == (*target)->stone) {
(*target)->downL.maxLength += (((*target)->dLink)->rLink)->downL.maxLength;
(((*target)->dLink)->rLink)->downL.maxLength += 1;
}
if ((*target)->uLink != NULL && (*target)->lLink != NULL && (((*target)->uLink)->lLink)->stone == (*target)->stone) {
(*target)->downL.maxLength += (((*target)->uLink)->lLink)->downL.maxLength;
(((*target)->uLink)->lLink)->downL.maxLength += 1;
}
if ((*target)->colL.maxLength == 3) { threeRule += 1; }
if ((*target)->rowL.maxLength == 3) { threeRule += 1; }
if ((*target)->upL.maxLength == 3) { threeRule += 1; }
if ((*target)->downL.maxLength == 3) { threeRule += 1; }
if (threeRule > 1) {
if ((*target)->stone == -1) { result = WHITEFOUL; }
else { result = BLACKFOUL; }
}
if ((*target)->colL.maxLength >= 5 || (*target)->rowL.maxLength >= 5 || (*target)->upL.maxLength >= 5 || (*target)->downL.maxLength >= 5) {
if ((*target)->stone == -1) { result = WHITEWIN; }
else { result = BLACKWIN; }
}
return *target;
}
void line_update(STATUS *board, STATUS *target, LINEINFO StraightCol[], LINEINFO StraightRow[], LINEINFO diagonalUp[], LINEINFO diagonalDown[]) {
STATUS *upperline = board;
STATUS *data = board;
int col = (target->location).col;
int row = (target->location).row;
int up = col + row; // ↗
int down = HEIGHT - 1 - col + row; // ↘
while (upperline != NULL) {
data = upperline;
while (data != NULL) {
if ((data->location).col == col) {
if (StraightCol[col].maxLength < (data->colL).maxLength) { StraightCol[col].maxColor = data->stone; StraightCol[col].maxLength = (data->colL).maxLength; }
else if (StraightCol->maxLength == (data->colL).maxLength) { StraightCol[col].maxColor = 0; }
}
if ((data->location).row == row) {
if (StraightRow[row].maxLength < (data->rowL).maxLength) { StraightRow[row].maxColor = data->stone; StraightRow[row].maxLength = (data->rowL).maxLength; }
else if (StraightRow->maxLength == (data->rowL).maxLength) { StraightRow[row].maxColor = 0; }
}
if ((data->location).col + (data->location).row == up) {
if (diagonalUp[up].maxLength < (data->upL).maxLength) { diagonalUp[up].maxColor = data->stone; diagonalUp[up].maxLength = (data->upL).maxLength; }
else if (diagonalUp->maxLength == (data->upL).maxLength) { diagonalUp[up].maxColor = 0; }
}
if (HEIGHT - 1 - (data->location).col + (data->location).row == down) {
if (diagonalDown[down].maxLength < (data->downL).maxLength) { diagonalDown[down].maxColor = data->stone; diagonalDown[down].maxLength = (data->downL).maxLength; }
else if (diagonalDown->maxLength == (data->downL).maxLength) { diagonalDown[down].maxColor = 0; }
}
data = data->rLink;
}
upperline = upperline->dLink;
}
}
void lets_judgment(RESULT result) {
system("cls");
if (result == WHITEFOUL) { printf("백돌의 반칙패\n"); }
else if (result == WHITEWIN) { printf("백돌의 승리\n"); }
else if (result == BLACKWIN) { printf("흑돌의 승리\n"); }
else if (result == BLACKFOUL) { printf("흑돌의 반칙패\n"); }
else { printf("무승부\n"); }
}