Hoon222y

기본 오목 Code 본문

개발 /[QT] - 오목 AI 개발

기본 오목 Code

hoon222y 2017. 7. 10. 19:06
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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
#define CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <Windows.h>
#include <time.h>
 
void redraw_screen(int **board);
void initialize();
void gotoxy(int x,int y);
void do_turn_com(int **board);
void do_put_com(int row, int col);
void do_turn_student(int **board);
void do_put_student(int row, int col);
int check_result(int **board);
void LOG_ERROR(char *msg);
void setcolor(int textcolor);
void COM_AI_step_1(int **board);
void game_run();
 
int lastest_put_position_com = -1;
int lastest_put_position_student = -1;
int round_count = 0;
int **ipp_cur_board = NULL;
int SIZE_OF_BOARD = 10;
char MARKER_NOTHING = '+';
char MARKER_COM = '@';
char MARKER_STUDENT = '0';
 
 
void main()
{
    srand((unsigned int) time(NULL));
    for(;;)
    {
        game_run();
        getchar();
        fflush(stdin);
        round_count = 0;
    }
 
}
 
void game_run()
{
    
    int game_result = 0;
    initialize();
    redraw_screen(ipp_cur_board);
    //setcolor(6) -> Highlight(GOLD)
    //setcolor(8) -> origin color
    //setcolor(9) -> Highlight(Blue)
    for(;;)
    {
        printf("-> STUDENT Turn\n");
        do_turn_student(ipp_cur_board);
        if((game_result = check_result(ipp_cur_board)) != 0)
        {
            break;
        }
        redraw_screen(ipp_cur_board);
        printf("-> STUDENT Turn\n-> [Done]");
 
        printf("-> COM Turn\n");
        do_turn_com(ipp_cur_board);
        if((game_result = check_result(ipp_cur_board)) != 0)
        {
            break;
        }
        redraw_screen(ipp_cur_board);
        printf("-> COM Turn\n-> [Done]");
 
    }
    if(game_result == 1)
    {
        gotoxy(0,11);
        printf("                                            ");
        gotoxy(0,11);
        setcolor(6);
        printf("[Round %03d]COM Win!\n", round_count);
        setcolor(8);
    }
    if(game_result == 2)
    {
        gotoxy(0,11);
        printf("                                            ");
        gotoxy(0,11);
        setcolor(6);
        printf("[Round %03d]STUDENT Win!\n", round_count);
        setcolor(8);
    }
    if(game_result == -2)
    {
        gotoxy(0,11);
        printf("                                            ");
        gotoxy(0,11);
        setcolor(6);
        printf("[Round %03d]DRAW!\n", round_count);
        setcolor(8);
    }
}
 
int check_result(int **board)
{
    //착수 결과
    //0 = 특이사항 없음
    //1 = COM 승리
    //2 = STUDENT 승리
    //-1 = ERROR
    //-2 = 무승부
 
    int count_0 = 0;
 
    for(int ROW = 0; ROW < SIZE_OF_BOARD; ++ROW)
    {
        for(int COL = 0; COL < SIZE_OF_BOARD; ++COL)
        {
            if(board[ROW][COL] == 0)
            {
                ++count_0;
                continue;
            }
            if(ROW < 6 && board[ROW][COL] == board[ROW+1][COL] && board[ROW+1][COL] == board[ROW+2][COL] && board[ROW+2][COL] == board[ROW+3][COL] && board[ROW+3][COL] == board[ROW+4][COL])
            {
                setcolor(4);
                gotoxy(COL,ROW);
                printf("%c",board[ROW+0][COL]==1?MARKER_COM:MARKER_STUDENT);
                gotoxy(COL,ROW+1);
                printf("%c",board[ROW+1][COL]==1?MARKER_COM:MARKER_STUDENT);
                gotoxy(COL,ROW+2);
                printf("%c",board[ROW+2][COL]==1?MARKER_COM:MARKER_STUDENT);
                gotoxy(COL,ROW+3);
                printf("%c",board[ROW+3][COL]==1?MARKER_COM:MARKER_STUDENT);
                gotoxy(COL,ROW+4);
                printf("%c",board[ROW+4][COL]==1?MARKER_COM:MARKER_STUDENT);
                setcolor(8);
                return board[ROW][COL];
            }
            if(COL < 6 && board[ROW][COL] == board[ROW][COL+1&& board[ROW][COL+1== board[ROW][COL+2&& board[ROW][COL+2== board[ROW][COL+3&& board[ROW][COL+3== board[ROW][COL+4])
            {
                setcolor(4);
                gotoxy(COL,ROW);
                printf("%c",board[ROW][COL+0]==1?MARKER_COM:MARKER_STUDENT);
                gotoxy(COL+1,ROW);
                printf("%c",board[ROW][COL+1]==1?MARKER_COM:MARKER_STUDENT);
                gotoxy(COL+2,ROW);
                printf("%c",board[ROW][COL+2]==1?MARKER_COM:MARKER_STUDENT);
                gotoxy(COL+3,ROW);
                printf("%c",board[ROW][COL+3]==1?MARKER_COM:MARKER_STUDENT);
                gotoxy(COL+4,ROW);
                printf("%c",board[ROW][COL+4]==1?MARKER_COM:MARKER_STUDENT);
                setcolor(8);
                return board[ROW][COL];
            }
            if(COL < 6 && ROW < 6 && board[ROW][COL] == board[ROW+1][COL+1&& board[ROW+1][COL+1== board[ROW+2][COL+2&& board[ROW+2][COL+2== board[ROW+3][COL+3&& board[ROW+3][COL+3== board[ROW+4][COL+4])
            {
                setcolor(4);
                gotoxy(COL,ROW);
                printf("%c",board[ROW+0][COL+0]==1?MARKER_COM:MARKER_STUDENT);
                gotoxy(COL+1,ROW+1);
                printf("%c",board[ROW+1][COL+1]==1?MARKER_COM:MARKER_STUDENT);
                gotoxy(COL+2,ROW+2);
                printf("%c",board[ROW+2][COL+2]==1?MARKER_COM:MARKER_STUDENT);
                gotoxy(COL+3,ROW+3);
                printf("%c",board[ROW+3][COL+3]==1?MARKER_COM:MARKER_STUDENT);
                gotoxy(COL+4,ROW+4);
                printf("%c",board[ROW+4][COL+4]==1?MARKER_COM:MARKER_STUDENT);
                setcolor(8);
                return board[ROW][COL];
            }
            if(COL > 3 && ROW < 6 && board[ROW][COL] == board[ROW+1][COL-1&& board[ROW+1][COL-1== board[ROW+2][COL-2&& board[ROW+2][COL-2== board[ROW+3][COL-3&& board[ROW+3][COL-3== board[ROW+4][COL-4])
            {
                setcolor(4);
                gotoxy(COL,ROW);
                printf("%c",board[ROW+0][COL-0]==1?MARKER_COM:MARKER_STUDENT);
                gotoxy(COL-1,ROW+1);
                printf("%c",board[ROW+1][COL-1]==1?MARKER_COM:MARKER_STUDENT);
                gotoxy(COL-2,ROW+2);
                printf("%c",board[ROW+2][COL-2]==1?MARKER_COM:MARKER_STUDENT);
                gotoxy(COL-3,ROW+3);
                printf("%c",board[ROW+3][COL-3]==1?MARKER_COM:MARKER_STUDENT);
                gotoxy(COL-4,ROW+4);
                printf("%c",board[ROW+4][COL-4]==1?MARKER_COM:MARKER_STUDENT);
                setcolor(8);
                return board[ROW][COL];
            }
        }
    }
    if(count_0 == 0)
    {
        return -2;
    }
    return 0;
}
 
void initialize()
{
    ////화면 모두 지움
    system("cls");
    ////게임판 초기화
    //ipp_cur_board가 NULL이 아니라면 free 수행한다. [memory leak 방지]
    if(ipp_cur_board != NULL)
    {
        for(int i = 0 ; i < SIZE_OF_BOARD; ++i)
        {
            free(ipp_cur_board[i]);
        }
        free(ipp_cur_board);
    }
    //ipp_cur_board 포인터 메모리 할당
    ipp_cur_board = (int **)calloc(SIZE_OF_BOARD, sizeof(int *));
    for(int i = 0 ; i < SIZE_OF_BOARD; ++i)
    {
        ipp_cur_board[i] = (int *)calloc(SIZE_OF_BOARD, sizeof(int));
    }
}
 
void gotoxy(int x,int y)
{
     COORD Cur;
     Cur.X=x;
     Cur.Y=y;
     SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),Cur);
}
 
void setcolor(int textcolor)
{
    WORD    attr;
    DWORD   dummy;
    CONSOLE_SCREEN_BUFFER_INFO BufInfo; 
    HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
 
    GetConsoleScreenBufferInfo(hConsole, &BufInfo);
    ReadConsoleOutputAttribute(hConsole, &attr, 1, BufInfo.dwCursorPosition, &dummy );
 
    attr = textcolor | (attr & 0xf0);
    SetConsoleTextAttribute( hConsole, (WORD)(attr) );
}
 
void redraw_screen(int **board)
{
    if(board == NULL)
    {
        LOG_ERROR("[ERROR] board is NULL! -> Exit Program");
    }
    system("cls");
    for(int ROW = 0 ; ROW < SIZE_OF_BOARD; ROW++)
    {
        for(int COL = 0 ; COL < SIZE_OF_BOARD; COL++)
        {
            if(lastest_put_position_com == ROW*10+COL || lastest_put_position_student == ROW*10+COL)
            {
                setcolor(9);
            }
            gotoxy(COL,ROW);
            setcolor(COL);
            switch (board[ROW][COL])
            {
            case 0:
                setcolor(7);
                printf("%c",MARKER_NOTHING);
                setcolor(8);
                break;
            case 1:
                setcolor(2);
                printf("%c",MARKER_COM);
                setcolor(8);
                break;
            case 2:
                setcolor(9);
                printf("%c",MARKER_STUDENT);
                setcolor(8);
                break;
            default:
                printf("?");
                LOG_ERROR("BOARD DATA is corrupt");
                break;
            }
            if(lastest_put_position_com == ROW*10+COL || lastest_put_position_student == ROW*10+COL)
            {
                setcolor(8);
            }
        }
    }
    printf("\n--------------------- Message ---------------------\n");
}
 
void LOG_ERROR(char *msg)
{
    gotoxy(0,11);
    printf("%s\n",msg);
    exit(1);
}
 
void do_turn_com(int **board)
{
    if (round_count < 3)
    {
        for(;;)
        {
            int tmp_row = rand()%2 + 4;
            int tmp_col = rand()%2 + 4;
            if(board[tmp_row][tmp_col] == 0)
            {
                do_put_com(tmp_row, tmp_col);
                return;
            }
        }
    }
    else
    {
        COM_AI_step_1(board);
    }
    
}
void do_put_com(int row, int col)
{
    if(ipp_cur_board[row][col] != 0)
    {
        LOG_ERROR("RULE Violation : do_put_com");
    }
    ipp_cur_board[row][col] = 1;
    lastest_put_position_com = row * 10 + col;
    round_count++;
}
void do_turn_student(int **board)
{
    for(;;)
    {
        int tmp_row, tmp_col;
        tmp_row = rand()%10;
        tmp_col = rand()%10;
        if(board[tmp_row][tmp_col] == 0)
        {
            do_put_student(tmp_row, tmp_col);
            return;
        }
    }
}
void do_put_student(int row, int col)
{
    if(ipp_cur_board[row][col] != 0)
    {
        LOG_ERROR("RULE Violation : do_put_student");
    }
    ipp_cur_board[row][col] = 2;
    lastest_put_position_student = row * 10 + col;
    round_count++;
}
 
void COM_AI_step_1(int **board)
{
    //tier 1 -> find 4 my stones in a line -> priority put for WIN
    for(int ROW = 0; ROW < SIZE_OF_BOARD; ++ROW)
    {
        for(int COL = 0; COL < SIZE_OF_BOARD; ++COL)
        {
            if(ROW < 6)
            {
                int stone_count = 0;
                int put_pos = -1;
                for (int i = 0; i < 5++i)
                {
                    if(board[ROW+i][COL] == 1
                    {
                        ++stone_count;
                    }
                    else if(board[ROW+i][COL] == 0)
                    {
                        put_pos = (ROW+i)*10 + COL;
                        continue;
                    }
                    else if(board[ROW+i][COL] == 2)
                    {
                        break;
                    }
                }
                if(stone_count == 4 && put_pos != -1)
                {
                    do_put_com(put_pos / 10, put_pos % 10);
                    return;
                }
            }
            if(COL < 6)
            {
                int stone_count = 0;
                int put_pos = -1;
                for (int i = 0; i < 5++i)
                {
                    if(board[ROW][COL+i] == 1
                    {
                        ++stone_count;
                    }
                    else if(board[ROW][COL+i] == 0)
                    {
                        put_pos = (ROW)*10 + COL + i;
                        continue;
                    }
                    else if(board[ROW][COL+i] == 2)
                    {
                        break;
                    }
                }
                if(stone_count == 4 && put_pos != -1)
                {
                    do_put_com(put_pos / 10, put_pos % 10);
                    return;
                }
            }
            if(COL < 6 && ROW < 6)
            {
                int stone_count = 0;
                int put_pos = -1;
                for (int i = 0; i < 5++i)
                {
                    if(board[ROW+i][COL+i] == 1
                    {
                        ++stone_count;
                    }
                    else if(board[ROW+i][COL+i] == 0)
                    {
                        put_pos = (ROW+i)*10 + COL+i;
                        continue;
                    }
                    else if(board[ROW+i][COL+i] == 2)
                    {
                        break;
                    }
                }
                if(stone_count == 4 && put_pos != -1)
                {
                    do_put_com(put_pos / 10, put_pos % 10);
                    return;
                }
            }
            if(COL > 3 && ROW < 6)
            {
                int stone_count = 0;
                int put_pos = -1;
                for (int i = 0; i < 5++i)
                {
                    if(board[ROW+i][COL-i] == 1
                    {
                        ++stone_count;
                    }
                    else if(board[ROW+i][COL-i] == 0)
                    {
                        put_pos = (ROW+i)*10 + COL-i;
                        continue;
                    }
                    else if(board[ROW+i][COL-i] == 2)
                    {
                        break;
                    }
                }
                if(stone_count == 4 && put_pos != -1)
                {
                    do_put_com(put_pos / 10, put_pos % 10);
                    return;
                }
            }
        }
    }
 
    //tier 2 -> find 4 student stones in a line -> put for defence
    for(int ROW = 0; ROW < SIZE_OF_BOARD; ++ROW)
    {
        for(int COL = 0; COL < SIZE_OF_BOARD; ++COL)
        {
            if(ROW < 6)
            {
                int stone_count = 0;
                int put_pos = -1;
                for (int i = 0; i < 5++i)
                {
                    if(board[ROW+i][COL] == 2
                    {
                        ++stone_count;
                    }
                    else if(board[ROW+i][COL] == 0)
                    {
                        put_pos = (ROW+i)*10 + COL;
                        continue;
                    }
                    else if(board[ROW+i][COL] == 1)
                    {
                        break;
                    }
                }
                if(stone_count == 4 && put_pos != -1)
                {
                    do_put_com(put_pos / 10, put_pos % 10);
                    return;
                }
            }
            if(COL < 6)
            {
                int stone_count = 0;
                int put_pos = -1;
                for (int i = 0; i < 5++i)
                {
                    if(board[ROW][COL+i] == 2
                    {
                        ++stone_count;
                    }
                    else if(board[ROW][COL+i] == 0)
                    {
                        put_pos = (ROW)*10 + COL + i;
                        continue;
                    }
                    else if(board[ROW][COL+i] == 1)
                    {
                        break;
                    }
                }
                if(stone_count == 4 && put_pos != -1)
                {
                    do_put_com(put_pos / 10, put_pos % 10);
                    return;
                }
            }
            if(COL < 6 && ROW < 6)
            {
                int stone_count = 0;
                int put_pos = -1;
                for (int i = 0; i < 5++i)
                {
                    if(board[ROW+i][COL+i] == 2
                    {
                        ++stone_count;
                    }
                    else if(board[ROW+i][COL+i] == 0)
                    {
                        put_pos = (ROW+i)*10 + COL+i;
                        continue;
                    }
                    else if(board[ROW+i][COL+i] == 1)
                    {
                        break;
                    }
                }
                if(stone_count == 4 && put_pos != -1)
                {
                    do_put_com(put_pos / 10, put_pos % 10);
                    return;
                }
            }
            if(COL > 3 && ROW < 6)
            {
                int stone_count = 0;
                int put_pos = -1;
                for (int i = 0; i < 5++i)
                {
                    if(board[ROW+i][COL-i] == 2
                    {
                        ++stone_count;
                    }
                    else if(board[ROW+i][COL-i] == 0)
                    {
                        put_pos = (ROW+i)*10 + COL-i;
                        continue;
                    }
                    else if(board[ROW+i][COL-i] == 1)
                    {
                        break;
                    }
                }
                if(stone_count == 4 && put_pos != -1)
                {
                    do_put_com(put_pos / 10, put_pos % 10);
                    return;
                }
            }
        }
    }
 
    //tier 3-1 -> find "01110" in a line -> put for Attack(11110)
    for(int ROW = 0; ROW < SIZE_OF_BOARD; ++ROW)
    {
        for(int COL = 0; COL < SIZE_OF_BOARD; ++COL)
        {
            if(ROW < 6)
            {
                int stone_count = 0;
                for (int i = 0; i < 5++i)
                {
                    if(board[ROW+i][COL] == 1
                    {
                        stone_count += 1<<i;
                    }
                    else if(board[ROW+i][COL] == 2)
                    {
                        break;
                    }
                }
                if(stone_count == 14)
                {
                    do_put_com(ROW, COL);
                    return;
                }
            }
            if(COL < 6)
            {
                int stone_count = 0;
                for (int i = 0; i < 5++i)
                {
                    if(board[ROW][COL+i] == 1
                    {
                        stone_count += 1<<i;
                    }
                    else if(board[ROW][COL+i] == 2)
                    {
                        break;
                    }
                }
                if(stone_count == 14)
                {
                    do_put_com(ROW, COL);
                    return;
                }
            }
            if(COL < 6 && ROW < 6)
            {
                int stone_count = 0;
                for (int i = 0; i < 5++i)
                {
                    if(board[ROW+i][COL+i] == 1
                    {
                        stone_count += 1<<i;
                    }
                    else if(board[ROW+i][COL+i] == 2)
                    {
                        break;
                    }
                }
                if(stone_count == 14)
                {
                    do_put_com(ROW, COL);
                    return;
                }
            }
            if(COL > 3 && ROW < 6)
            {
                int stone_count = 0;
                int put_pos = -1;
                for (int i = 0; i < 5++i)
                {
                    if(board[ROW+i][COL-i] == 1
                    {
                        stone_count += 1<<i;
                    }
                    else if(board[ROW+i][COL-i] == 2)
                    {
                        break;
                    }
                }
                if(stone_count == 14)
                {
                    do_put_com(ROW, COL);
                    return;
                }
            }
        }
    }
 
    //tier 3-2 -> find "02220" in a line -> put for Defence(12220)
    for(int ROW = 0; ROW < SIZE_OF_BOARD; ++ROW)
    {
        for(int COL = 0; COL < SIZE_OF_BOARD; ++COL)
        {
            if(ROW < 6)
            {
                int stone_count = 0;
                for (int i = 0; i < 5++i)
                {
                    if(board[ROW+i][COL] == 2
                    {
                        stone_count += 1<<i;
                    }
                    else if(board[ROW+i][COL] == 1)
                    {
                        break;
                    }
                }
                if(stone_count == 14)
                {
                    do_put_com(ROW, COL);
                    return;
                }
            }
            if(COL < 6)
            {
                int stone_count = 0;
                for (int i = 0; i < 5++i)
                {
                    if(board[ROW][COL+i] == 2
                    {
                        stone_count += 1<<i;
                    }
                    else if(board[ROW][COL+i] == 1)
                    {
                        break;
                    }
                }
                if(stone_count == 14)
                {
                    do_put_com(ROW, COL);
                    return;
                }
            }
            if(COL < 6 && ROW < 6)
            {
                int stone_count = 0;
                for (int i = 0; i < 5++i)
                {
                    if(board[ROW+i][COL+i] == 2
                    {
                        stone_count += 1<<i;
                    }
                    else if(board[ROW+i][COL+i] == 1)
                    {
                        break;
                    }
                }
                if(stone_count == 14)
                {
                    do_put_com(ROW, COL);
                    return;
                }
            }
            if(COL > 3 && ROW < 6)
            {
                int stone_count = 0;
                int put_pos = -1;
                for (int i = 0; i < 5++i)
                {
                    if(board[ROW+i][COL-i] == 2
                    {
                        stone_count += 1<<i;
                    }
                    else if(board[ROW+i][COL-i] == 1)
                    {
                        break;
                    }
                }
                if(stone_count == 14)
                {
                    do_put_com(ROW, COL);
                    return;
                }
            }
        }
    }
    //012    765
    //3*4  ->4*3
    //567    210
    int mirrorside[8= {8,7,6,5,4,3,2,1};
    //lastest_put_position_com
    int origin_row_pos[8= {-1,-1,-1,0,0,1,1,1};
    int origin_col_pos[8= {-1,0,1,-1,1,-1,0,1};
    int mirror_row_pos[8= {1,1,1,0,0,-1,-1,-1};
    int mirror_col_pos[8= {1,0,-1,1,-1,1,0,-1};
    int target_pos_row = lastest_put_position_com / 10;
    int target_pos_col = lastest_put_position_com % 10;
 
    // 1*0 => 111
    for(int i = 0; i < 8++i)
    {
        if(target_pos_row + mirror_row_pos[mirrorside[i]] < 0 || target_pos_row + mirror_row_pos[mirrorside[i]] > 9)
        {
            continue;
        }
        if(target_pos_col + mirror_col_pos[mirrorside[i]] < 0 || target_pos_col + mirror_col_pos[mirrorside[i]] > 9)
        {
            continue;
        }
        if(target_pos_row + origin_row_pos[i] < 0 || target_pos_row + origin_row_pos[i] > 9)
        {
            continue;
        }
        if(target_pos_col + origin_col_pos[i] < 0 || target_pos_col + origin_col_pos[i] > 9)
        {
            continue;
        }
        if (board[target_pos_row + origin_row_pos[i]][target_pos_col + origin_col_pos[i]] == 1 && board[target_pos_row + mirror_row_pos[mirrorside[i]]][target_pos_col + mirror_col_pos[mirrorside[i]]] == 0)
        {
            do_put_com(target_pos_row + mirror_row_pos[mirrorside[i]], target_pos_col + mirror_col_pos[mirrorside[i]]);
            return;
        }
    }
 
    // 10* => 11*
    for(int i = 0; i < 8++i)
    {
        if(target_pos_row + origin_row_pos[i]*2 < 0 || target_pos_row + origin_row_pos[i]*2 > 9)
        {
            continue;
        }
        if(target_pos_col + origin_col_pos[i]*2 < 0 || target_pos_col + origin_col_pos[i]*2 > 9)
        {
            continue;
        }
        if(target_pos_row + origin_row_pos[i] < 0 || target_pos_row + origin_row_pos[i] > 9)
        {
            continue;
        }
        if(target_pos_col + origin_col_pos[i] < 0 || target_pos_col + origin_col_pos[i] > 9)
        {
            continue;
        }
        if (board[target_pos_row + origin_row_pos[i]][target_pos_col + origin_col_pos[i]] == 0 && board[target_pos_row + origin_row_pos[i]*2][target_pos_col + origin_col_pos[i]*2== 1)
        {
            do_put_com(target_pos_row + origin_row_pos[i], target_pos_col + origin_col_pos[i]);
            return;
        }
    }
 
    for(;;)
    {
        int tmp_row = rand()%8 + 1;
        int tmp_col = rand()%8 + 1;
        if(board[tmp_row][tmp_col] == 0)
        {
            do_put_com(tmp_row, tmp_col);
            return;
        }
    }
 
}
cs


'개발 > [QT] - 오목 AI 개발' 카테고리의 다른 글

BetaGo PPT 픽토그램  (0) 2017.08.06
QFtp 관련 STL 참고자료  (2) 2017.07.21
(기록용) 삼성전자 인턴 업무일지  (0) 2017.07.19
오목AI 개발 참고 사이트  (1) 2017.07.18
Comments