-
Notifications
You must be signed in to change notification settings - Fork 0
/
final_edition
1138 lines (1115 loc) · 28.4 KB
/
final_edition
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
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <easyx.h>
#include <graphics.h>
#include <iostream>
#include <stdlib.h>
#include <Windows.h>
#include<string>
#include<cstdio>
#include<vector>
#include<algorithm>
#include<fstream>
#include<math.h>
using namespace std;
int board[10][10] = { 0 };//0表无子,1表黑子,-1表白子
int check_board[10][10] = { 0 };//是否要归零还不确定
int edge = 9;
int MOVE[4][2] = { {1,0},{-1,0},{0,1},{0,-1} };
int cnt = 0;
int flag_side = 0, flag_game_over = 0, flag_side_pvp = 1;//flag_side用来选边,flag_game_over用来标记一盘游戏的结束,flag_side初始值为1是考虑到pvp的需求
int MININT = -2147483648;
//int value = 0;//best_row, best_col,row = 0, col = 0,
//int best_value = MININT;
vector<int> available_list;
//for pve,返回值用来选对弈方和退出以及保存
int print_board()
{
initgraph(800, 600); //初始化图形系统
setbkcolor(RGB(255, 239, 213)); //设置图形窗口的背景颜色
cleardevice(); //清屏幕
//下面行读取图像文件
//loadimage(&black, _T("hei.jpg"), 54, 54, FALSE);
//loadimage(&white, _T("bai.jpg"), 54, 54, FALSE);
//loadimage(&cundang, _T("cundang.jpg"), 141, 50, FALSE);
//下面行显示图像文件
//putimage(600, 50, &black);
//putimage(600, 380, &white);
//putimage(600, 490, &cundang);
//下面代码网格
setcolor(RGB(0, 0, 0));
rectangle(60, 60, 540, 540);
setfillcolor(RGB(255, 239, 213));
fillrectangle(60, 60, 540, 540);
setcolor(RGB(0, 0, 0));
line(60, 120, 540, 120);
line(60, 180, 540, 180);
line(60, 240, 540, 240);
line(60, 300, 540, 300);
line(60, 360, 540, 360);
line(60, 420, 540, 420);
line(60, 480, 540, 480);
line(120, 60, 120, 540);
line(180, 60, 180, 540);
line(240, 60, 240, 540);
line(300, 60, 300, 540);
line(360, 60, 360, 540);
line(420, 60, 420, 540);
line(480, 60, 480, 540);
setcolor(RGB(0, 0, 0));//画黑棋选项
rectangle(600, 120, 660, 180);
setfillcolor(RGB(0, 0, 0));
fillrectangle(600, 120, 660, 180);
setcolor(BLACK);//画白棋选项
rectangle(600, 240, 660, 300);
setfillcolor(WHITE);
fillrectangle(600, 240, 660, 300);
outtextxy(632, 423, L"RETURN");
setcolor(RGB(0, 0, 0));
rectangle(600, 420, 720, 440);
outtextxy(643, 483, L"SAVE");
setcolor(RGB(0, 0, 0));
rectangle(600, 480, 720, 500);
/******************************/
//outtextxy(639, 543, L"PAUSE");
//setcolor(RGB(0, 0, 0));
//rectangle(600, 540, 720, 560);
/******************************/
rectangle(590, 60, 748, 320);
outtextxy(600, 65, L"CHOOSE YOUR SIDE");
//void choose_side();
while (1) {//选择对弈方,选择退出或保存
MOUSEMSG msg = GetMouseMsg();
int x = msg.x;
int y = msg.y;
if (msg.mkLButton)
{
//flag_side = choose_side(x, y);
if (x >= 600 && x <= 660 && y >= 120 && y <= 180)//600, 120, 660, 180
{
setfillcolor(RGB(255, 239, 213));
fillrectangle(600, 240, 660, 300);
flag_side = -1;//AI执白
return 1;
}
else if (x >= 600 && x <= 660 && y >= 240 && y <= 300)//600, 240, 660, 300
{
setfillcolor(RGB(255, 239, 213));
fillrectangle(600, 120, 660, 180);
flag_side = 1;//AI执黑
return 1;
}
if (x >= 600 && x <= 720 && y >= 420 && y <= 440) {
return 0;//
}
else if (x >= 600 && x <= 720 && y >= 480 && y <= 500) {
return 2;
}
//else if (x >= 600 && x <= 720 && y >= 540 && y <= 560) {//600, 540, 720, 560
//return 3;
//}
}
FlushMouseMsgBuffer();
}//选择对弈方
}
//for pvp
void print_board_pvp()
{
initgraph(800, 600); //初始化图形系统
setbkcolor(RGB(255, 239, 213)); //设置图形窗口的背景颜色
cleardevice(); //清屏幕
//下面行读取图像文件
//loadimage(&black, _T("hei.jpg"), 54, 54, FALSE);
//loadimage(&white, _T("bai.jpg"), 54, 54, FALSE);
//loadimage(&cundang, _T("cundang.jpg"), 141, 50, FALSE);
//下面行显示图像文件
//putimage(600, 50, &black);
//putimage(600, 380, &white);
//putimage(600, 490, &cundang);
//下面代码网格
setcolor(RGB(0, 0, 0));
rectangle(60, 60, 540, 540);
setfillcolor(RGB(255, 239, 213));
fillrectangle(60, 60, 540, 540);
setcolor(RGB(0, 0, 0));
line(60, 120, 540, 120);
line(60, 180, 540, 180);
line(60, 240, 540, 240);
line(60, 300, 540, 300);
line(60, 360, 540, 360);
line(60, 420, 540, 420);
line(60, 480, 540, 480);
line(120, 60, 120, 540);
line(180, 60, 180, 540);
line(240, 60, 240, 540);
line(300, 60, 300, 540);
line(360, 60, 360, 540);
line(420, 60, 420, 540);
line(480, 60, 480, 540);
outtextxy(632, 223, L"RETURN");
setcolor(RGB(0, 0, 0));
rectangle(600, 220, 720, 240);
outtextxy(643, 283, L"SAVE");
setcolor(RGB(0, 0, 0));
rectangle(600, 280, 720, 300);
//settextstyle(100, 0, _T("Consolas"));
//outtextxy(600, 100, _T("PVP"));
//outtextxy(632, 63, L"BEGIN");
//setcolor(RGB(0, 0, 0));
//rectangle(600, 60, 720, 80);
}
//返回值用来退出以及保存
void print_board_continue_pve(int flag_side, int cnt)
{
cnt = 0;
initgraph(800, 600); //初始化图形系统
setbkcolor(RGB(255, 239, 213)); //设置图形窗口的背景颜色
cleardevice(); //清屏幕
//下面行读取图像文件
//loadimage(&black, _T("hei.jpg"), 54, 54, FALSE);
//loadimage(&white, _T("bai.jpg"), 54, 54, FALSE);
//loadimage(&cundang, _T("cundang.jpg"), 141, 50, FALSE);
//下面行显示图像文件
//putimage(600, 50, &black);
//putimage(600, 380, &white);
//putimage(600, 490, &cundang);
//下面代码网格
setcolor(RGB(0, 0, 0));
rectangle(60, 60, 540, 540);
setfillcolor(RGB(255, 239, 213));
fillrectangle(60, 60, 540, 540);
setcolor(RGB(0, 0, 0));
line(60, 120, 540, 120);
line(60, 180, 540, 180);
line(60, 240, 540, 240);
line(60, 300, 540, 300);
line(60, 360, 540, 360);
line(60, 420, 540, 420);
line(60, 480, 540, 480);
line(120, 60, 120, 540);
line(180, 60, 180, 540);
line(240, 60, 240, 540);
line(300, 60, 300, 540);
line(360, 60, 360, 540);
line(420, 60, 420, 540);
line(480, 60, 480, 540);
if (flag_side == -1) {//AI执白,人点了黑
setcolor(RGB(0, 0, 0));//画黑棋选项,但不加黑底
rectangle(600, 120, 660, 180);
setcolor(BLACK);//画白棋选项
rectangle(600, 240, 660, 300);
setfillcolor(WHITE);
fillrectangle(600, 240, 660, 300);
}
else if (flag_side == 1) {//AI执黑,人点了白
setcolor(BLACK);//画白棋选项,但不加白底
rectangle(600, 240, 660, 300);
setcolor(RGB(0, 0, 0));//画黑棋选项
rectangle(600, 120, 660, 180);
setfillcolor(RGB(0, 0, 0));
fillrectangle(600, 120, 660, 180);
}
outtextxy(632, 423, L"RETURN");
setcolor(RGB(0, 0, 0));
rectangle(600, 420, 720, 440);
outtextxy(643, 483, L"SAVE");
setcolor(RGB(0, 0, 0));
rectangle(600, 480, 720, 500);
//接下来恢复棋盘
int x0 = 60, y0 = 60;
for (int i = 0; i < edge; i++) {
for (int j = 0; j < edge; j++) {
if (board[i][j] != 0) {
cnt++;
if (board[i][j] == 1) {
setfillcolor(BLACK);
fillcircle(x0 + j * 60, y0 + i * 60, 25);
}
else if (board[i][j] == -1) {
setfillcolor(WHITE);
fillcircle(x0 + j * 60, y0 + i * 60, 25);
}
}
}
}
}
void print_board_continue_pvp()
{
cnt = 0;
initgraph(800, 600); //初始化图形系统
setbkcolor(RGB(255, 239, 213)); //设置图形窗口的背景颜色
cleardevice(); //清屏幕
//下面行读取图像文件
//loadimage(&black, _T("hei.jpg"), 54, 54, FALSE);
//loadimage(&white, _T("bai.jpg"), 54, 54, FALSE);
//loadimage(&cundang, _T("cundang.jpg"), 141, 50, FALSE);
//下面行显示图像文件
//putimage(600, 50, &black);
//putimage(600, 380, &white);
//putimage(600, 490, &cundang);
//下面代码网格
setcolor(RGB(0, 0, 0));
rectangle(60, 60, 540, 540);
setfillcolor(RGB(255, 239, 213));
fillrectangle(60, 60, 540, 540);
setcolor(RGB(0, 0, 0));
line(60, 120, 540, 120);
line(60, 180, 540, 180);
line(60, 240, 540, 240);
line(60, 300, 540, 300);
line(60, 360, 540, 360);
line(60, 420, 540, 420);
line(60, 480, 540, 480);
line(120, 60, 120, 540);
line(180, 60, 180, 540);
line(240, 60, 240, 540);
line(300, 60, 300, 540);
line(360, 60, 360, 540);
line(420, 60, 420, 540);
line(480, 60, 480, 540);
outtextxy(632, 223, L"RETURN");
setcolor(RGB(0, 0, 0));
rectangle(600, 220, 720, 240);
outtextxy(643, 283, L"SAVE");
setcolor(RGB(0, 0, 0));
rectangle(600, 280, 720, 300);
//接下来恢复棋盘
int x0 = 60, y0 = 60;
for (int i = 0; i < edge; i++) {
for (int j = 0; j < edge; j++) {
if (board[i][j] != 0) {
cnt++;
if (board[i][j] == 1) {
setfillcolor(BLACK);
fillcircle(x0 + j * 60, y0 + i * 60, 25);
}
else if (board[i][j] == -1) {
setfillcolor(WHITE);
fillcircle(x0 + j * 60, y0 + i * 60, 25);
}
}
}
}
}
int print_menu()
{
initgraph(800, 600); //初始化图形系统
setbkcolor(RGB(255, 239, 213)); //设置图形窗口的背景颜色
cleardevice(); //清屏幕
setcolor(RGB(0, 0, 0));
rectangle(60, 60, 300, 120);
rectangle(60, 180, 300, 240);
rectangle(60, 300, 300, 360);
rectangle(60, 420, 300, 480);
//rectangle(60, 540, 300, 598);//可以考虑留给introduction
settextstyle(30, 0, _T("Consolas"));
outtextxy(124, 75, _T("CONTINUE"));
outtextxy(120, 195, _T("BEGIN PVE"));
outtextxy(120, 315, _T("BEGIN PVP"));
outtextxy(154, 435, _T("EXIT"));
settextstyle(200, 0, _T("Consolas"));
outtextxy(370, 100, _T("NoGo"));
settextstyle(100, 0, _T("Consolas"));
outtextxy(600, 300, _T("V"));
settextstyle(55, 0, _T("Consolas"));
outtextxy(660, 336, _T("1.0"));
while (1)
{
MOUSEMSG msg = GetMouseMsg();
int x = msg.x;
int y = msg.y;
if (msg.mkLButton) {
if (x >= 60 && x <= 300 && y >= 60 && y <= 120) {
return 0;//继承上一盘的结果
}
if (x >= 60 && x <= 300 && y >= 180 && y <= 240) {
return 1;//pve module
}
if (x >= 60 && x <= 300 && y >= 300 && y <= 360) {
return 2;//pvp module
}
if (x >= 60 && x <= 300 && y >= 420 && y <= 480) {
closegraph();
exit(0);
}
}
}
//return;
}
//输出AI输的情况的提示
void print_game_over_AI(int flag_side)
{
if (flag_side == -1) {//AI执白,AI输
outtextxy(600, 360, L"BLACK SIDE WINS");
}
else if (flag_side == 1) {//AI执黑,AI输
outtextxy(600, 360, L"WHITE SIDE WINS");
}
return;
}
//输出人类输的情况的提示
void print_game_over_human(int human_flag_side)
{
if (human_flag_side == 1) {//AI执白,AI赢
outtextxy(600, 360, L"WHITE SIDE WINS");
}
else if (human_flag_side == -1) {//AI执黑,AI赢
outtextxy(600, 360, L"BLACK SIDE WINS");
}
return;
}
//输出PVP情况下一方输的提示
void print_game_over_pvp(int flag_side)
{
if (flag_side == 1) {
outtextxy(600, 400, L"BLACK SIDE WINS");
}
else if (flag_side == -1) {
outtextxy(600, 400, L"WHITE SIDE WINS");
}
return;
}
int check(int r, int c)//返回1表示在棋盘内
{
if (r >= 0 && r < 9 && c >= 0 && c < 9)
return 1;
else
return 0;
}
int judge_eye(int r, int c, int flag_side)//返回0表示不是眼或已有落子,返回1表示是眼
{
if (check(r, c)) {
int flag_eye = 0;
for (int i = 0; i < 4; i++) {
int r1 = r + MOVE[i][0];
int c1 = c + MOVE[i][1];
if (check(r1, c1)) {
if (board[r1][c1] != flag_side && board[r1][c1] != 0) {
continue;
}
else
flag_eye = 1;
}
else
continue;
}
if (flag_eye)
return 0;
if (!flag_eye)
return 1;
}
else
return 0;
}
bool judge(int r, int c)//返回1表示能下(与上面保持一致)
{
check_board[r][c] = 1;//表示已经有落子
int flag = 0;
for (int i = 0; i < 4; i++) {
int r1 = r + MOVE[i][0];
int c1 = c + MOVE[i][1];
if (check(r1, c1)) {
if (board[r1][c1] == 0)
flag = 1;
if (board[r1][c1] == board[r][c] && !check_board[r1][c1]) {
if (judge(r1, c1))
flag = 1;
}
}
}
return flag;
}
bool judgement(int r, int c, int flag_side)//返回1表示可下
{
if (board[r][c] == 1 || board[r][c] == -1)
return 0;
board[r][c] = flag_side;
memset(check_board, 0, sizeof(check_board));
//check_board[r][c] = 1;
if (judge(r, c) == 0) {
board[r][c] = 0;
return 0;
}
for (int i = 0; i < 4; i++) {
int r1 = r + MOVE[i][0];
int c1 = c + MOVE[i][1];
if (check(r1, c1)) {
if (board[r1][c1] && !check_board[r1][c1]) {
if (!judge(r1, c1)) {
board[r][c] = 0;
return 0;
}
}
}
}
board[r][c] = 0;
return 1;
}
int judge_eye_eval(int row, int col, int flag_si)
{
int Qi = 4;
if (board[row][col] == 0) {
for (int i = 0; i < 4; i++) {
int row1 = row + MOVE[i][0];
int col1 = col + MOVE[i][1];
if (!check(row1, col1))
Qi--;
else if (board[row1][col1] == -flag_si)
Qi--;
}
if (Qi == 0)
return 1;
else
return 0;
}
else
return 0;
}
int right(int flag_s)
{
int sum_right = 0;
for (int i = 0; i < edge; i++) {
for (int j = 0; j < edge; j++) {
if (board[i][j] == 0) {
if (judge_eye_eval(i, j, flag_s))
sum_right++;
}
}
}
return sum_right;
}
int nega_max(int r, int c, int depth, int flag_s, int alpha, int beta)//flag_side==1表示AI为黑方,flagSide==2表示AI为白方
{
int flag_nega_win = 0;
//int best_value = MININT;
if (depth == 0)
return right(flag_s) + right(-flag_s);
for (int row = 0; row < edge; row++) {
for (int col = 0; col < edge; col++) {
if (!board[row][col]) {
int value = 0;
if (!judgement(row, col, flag_s)) {
continue;
}
else {
flag_nega_win = 1;
board[row][col] = flag_s;
value = -nega_max(row, col, depth - 1, -flag_s, -beta, -alpha);
//value = op_right(flag_s) + fr_right(flag_s);
board[row][col] = 0;
}
if (value >= alpha) {
alpha = value;
}
if (alpha >= beta)
break;
}
else
continue;
}
}
if (!flag_nega_win)//用于返回胜负已定时的情形
return right(flag_s) + right(-flag_s);
return alpha;
}
int visit(int depth, int flag_side, int num_available)
{
int value = MININT;
int chosen_pos = 0;//知道了,这个初值导致了在网站上出现了(0,0)的invalid position
vector<int> possible_pos_list;
for (int row = 0; row < edge; row++) {
for (int col = 0; col < edge; col++) {
if (judgement(row, col, flag_side)) {
board[row][col] = flag_side;
int temp_value = -nega_max(row, col, depth, -flag_side, MININT, MININT) + MININT;
board[row][col] = 0;
if (temp_value >= value) {
if (temp_value == value) {
possible_pos_list.push_back(row * edge + col);
}
if (temp_value > value) {
possible_pos_list.clear();
value = temp_value;
chosen_pos = row * edge + col;
possible_pos_list.push_back(chosen_pos);
}
}
//这个对应botzone
/*if (temp_value > value) {
value = temp_value;
chosen_pos = row * edge + col;
//flag_inequal_right = 1;
if (count > 0) {
flag_inequal_right = 1;
}
}*/
//count++;
}
else
continue;
}
}
if (chosen_pos == 0 && board[0][0] != 0) {
return available_list[rand() % available_list.size()];
}
else {
if (possible_pos_list.size() == 1) {
//outtextxy(600, 320, L"DONE_NEGA");//调试用,正式版需删除
return chosen_pos;
}
else if (possible_pos_list.size() > 1) {
//outtextxy(600, 320, L"DONE_NEGA_RAND");//调试用,正式版需删除
return possible_pos_list[rand() % possible_pos_list.size()];
}
if (possible_pos_list.size() == 0) {
return available_list[rand() % available_list.size()];
}
}
}
int judge_eye_breaker(int r, int c, int flag_side)//返回1表示优先下此处
{
int Qi = 4;
int suround_fr = 0;
if (check(r, c)) {
for (int i = 0; i < 4; i++) {
int r1 = r + MOVE[i][0];
int c1 = c + MOVE[i][1];
if (check(r1, c1)) {
if (board[r1][c1] != 0) {
Qi--;
if (board[r1][c1] == flag_side)
suround_fr++;
}
}
else {
Qi--;
}
}
}
if (Qi == 1 && suround_fr == 0)
return 1;
else
return 0;
}
void choose_side()
{
while (1) {//选择对弈方
MOUSEMSG msg = GetMouseMsg();
if (msg.mkLButton)
{
int x = msg.x;
int y = msg.y;
if (x >= 600 && x <= 660 && y >= 120 && y <= 180)//600, 120, 660, 180
{
setfillcolor(RGB(255, 239, 213));
fillrectangle(600, 240, 660, 300);
flag_side = -1;//AI执白
return;
}
else if (x >= 600 && x <= 660 && y >= 240 && y <= 300)//600, 240, 660, 300
{
setfillcolor(RGB(255, 239, 213));
fillrectangle(600, 120, 660, 180);
flag_side = 1;//AI执黑
return;
}
else if (x >= 600 && x <= 720 && y >= 420 && y <= 440) {
print_menu();
}
}
FlushMouseMsgBuffer();
}//选择对弈方
}
void stop_pve()
{
clearrectangle(600, 480, 720, 500);
while (1)
{
MOUSEMSG msg = GetMouseMsg();
if (msg.mkLButton) {
int x = msg.x;
int y = msg.y;
if (x >= 600 && x <= 720 && y >= 420 && y <= 440) {
break;
}
}
}
FlushMouseMsgBuffer();
return;
}
//用于在menu那停止
void stop_pvp()
{
clearrectangle(600, 480, 720, 500);
while (1)
{
MOUSEMSG msg = GetMouseMsg();
if (msg.mkLButton) {
int x = msg.x;
int y = msg.y;
if (x >= 600 && x <= 720 && y >= 220 && y <= 240) {
break;
}
}
}
FlushMouseMsgBuffer();
return;
}
void pause_pvp()
{
clearrectangle(600, 340, 720, 360);
outtextxy(629, 343, L"CONTINUE");
setcolor(RGB(0, 0, 0));
rectangle(600, 340, 720, 360);
while (1)
{
MOUSEMSG msg = GetMouseMsg();
if (msg.mkLButton) {
int x = msg.x;
int y = msg.y;//600, 540, 720, 560
if (x >= 600 && x <= 720 && y >= 340 && y <= 360) {
break;
}
}
}
FlushMouseMsgBuffer();
clearrectangle(600, 340, 720, 360);
outtextxy(639, 343, L"PAUSE");
setcolor(RGB(0, 0, 0));
rectangle(600, 340, 720, 360);
return;
}
void pause_pve()
{
clearrectangle(600, 540, 720, 560);
outtextxy(629, 543, L"CONTINUE");
setcolor(RGB(0, 0, 0));
rectangle(600, 540, 720, 560);
while (1)
{
MOUSEMSG msg = GetMouseMsg();
if (msg.mkLButton) {
int x = msg.x;
int y = msg.y;//600, 540, 720, 560
if (x >= 600 && x <= 720 && y >= 540 && y <= 560) {
break;
}
}
}
FlushMouseMsgBuffer();
clearrectangle(600, 540, 720, 560);
outtextxy(639, 543, L"PAUSE");
setcolor(RGB(0, 0, 0));
rectangle(600, 540, 720, 560);
return;
}
void stop_menu()
{
clearrectangle(60, 60, 300, 120);
setcolor(RGB(0, 0, 0));
rectangle(60, 60, 300, 120);
settextstyle(30, 0, _T("Consolas"));
outtextxy(138, 75, _T("RETURN"));
while (1)
{
MOUSEMSG msg = GetMouseMsg();
if (msg.mkLButton) {
int x = msg.x;
int y = msg.y;
if (x >= 60 && x <= 300 && y >= 60 && y <= 120) {
break;
}
}
}
FlushMouseMsgBuffer();
}
void first_move_AI(int flag_side)
{
if (flag_side == 1)//人选白棋时AI的第一手棋
{
int x0 = 60; int y0 = 60;
int initial_x = 7, initial_y = 1;
board[initial_y][initial_x] = flag_side;
setfillcolor(BLACK);
fillcircle(x0 + initial_x * 60, y0 + initial_y * 60, 25);
cnt++;
}//人选白棋时AI的第一手棋
return;
}
int save_file(int flag_side)//return 0对应打开失败,mod用来分辨是pvp还是pve,mod为1表示为pve,mod为2表示为pvp
{
ofstream outfile("board.txt");
if (!outfile) {
outtextxy(600, 380, L"SAVE FILE FAILED");
outfile.close();
return 0;
}
else {
outfile << flag_side << ' ' << cnt << endl;
for (int i = 0; i < edge; i++) {
for (int j = 0; j < edge; j++) {
if (j != edge - 1) {
outfile << board[i][j] << ' ';
}
else if (j == edge - 1) {
outfile << board[i][j];
}
}
outfile << endl;
}
outfile.close();
return 1;
}
}
int game_AI_human()
{
outtextxy(639, 543, L"PAUSE");
setcolor(RGB(0, 0, 0));
rectangle(600, 540, 720, 560);
while (1)
{
int depth = 3;
MOUSEMSG msg = GetMouseMsg();
int flag_human_loose = 1;//0表示人还没输
for (int i = 0; i < edge; i++) {
for (int j = 0; j < edge; j++) {
if (board[i][j] == 0) {
if (judgement(i, j, -flag_side)) {
flag_human_loose = 0;
break;
}
}
else
continue;
}
}
if (flag_human_loose) {
print_game_over_human(-flag_side);
clearrectangle(600, 480, 720, 500);//清除SAVE键
}
if (msg.mkLButton)//人执黑,AI执白
{
int finish_loop = 0;//用来让游戏结束
int x = msg.x;
int y = msg.y;
if (x >= 600 && x <= 720 && y >= 420 && y <= 440) {
return 1;//游戏中点return
}
if (x >= 600 && x <= 720 && y >= 480 && y <= 500) {
save_file(flag_side);
}
if (x >= 600 && x <= 720 && y >= 540 && y <= 560) {//600, 540, 720, 560
pause_pve();
}
//以下可改写为一个函数
for (int j = 0; j < edge; j++) {
for (int i = 0; i < edge; i++) {
int x0 = 60, y0 = 60;
if (((x - (x0 + i * 60)) * (x - (x0 + i * 60)) + (y - (y0 + j * 60)) * (y - (y0 + j * 60)) <= 30 * 30) && (board[j][i] == 0)) {
if (judgement(j, i, -flag_side)) {
board[j][i] = -flag_side;
if (flag_side == -1) {
setfillcolor(BLACK);
}
else if (flag_side == 1) {
setfillcolor(WHITE);
}
fillcircle(x0 + i * 60, y0 + j * 60, 25);
setfillcolor(RGB(255, 239, 213));
outtextxy(600, 330, L"YOUR TURN");
clearrectangle(600, 330, 680, 350);
//fillrectangle(600, 320, 720, 400);//测试用
cnt++;
//人走后,马上进AI判断
//vector<int> available_list; //合法位置表
available_list.clear();
int suspension = 0;
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
if (judgement(i, j, flag_side)) {
if (!judge_eye_breaker(i, j, flag_side)) {
available_list.push_back(i * 9 + j);
}
else {
suspension = 1;
board[i][j] = flag_side;
if (flag_side == -1) {
setfillcolor(WHITE);
}
else if (flag_side == 1) {
setfillcolor(BLACK);
}
fillcircle(x0 + j * 60, y0 + i * 60, 25);
outtextxy(600, 330, L"YOUR TURN");
cnt++;
break;
}
}
}
if (suspension)
break;
}
if (!suspension) {
if (available_list.size() == 0) {//若AI没位置下,判AI输
print_game_over_AI(flag_side);
flag_game_over = 1;
}
else {
//int result = available_list[rand() % available_list.size()];
int result = visit(depth, flag_side, available_list.size());
int best_row = result / 9; int best_col = result % 9;
board[best_row][best_col] = flag_side;
if (flag_side == -1) {
setfillcolor(WHITE);
}
else if (flag_side == 1) {
setfillcolor(BLACK);
}
fillcircle(x0 + best_col * 60, y0 + best_row * 60, 25);
outtextxy(600, 330, L"YOUR TURN");
cnt++;
}
}
suspension = 0;
}
else {
print_game_over_human(-flag_side);
flag_game_over = 1;
}
finish_loop = 1;
break;
}
}
if (finish_loop)
break;
}
if (flag_game_over)
break;
}
FlushMouseMsgBuffer();//用于减少采样量
}
return 2;//整盘游戏结束后点return
}
int game_human_human()
{
outtextxy(639, 343, L"PAUSE");
setcolor(RGB(0, 0, 0));
rectangle(600, 340, 720, 360);
int flag_pvp = 2;//在存盘时标记是否为pvp
while (1) {
MOUSEMSG msg = GetMouseMsg();
int flag_human_loose = 1;//0表示人还没输
for (int i = 0; i < edge; i++) {
for (int j = 0; j < edge; j++) {
if (board[i][j] == 0) {
if (judgement(i, j, -flag_side_pvp)) {
flag_human_loose = 0;
break;
}
}
else
continue;
}
}
if (flag_human_loose) {
print_game_over_pvp(-flag_side_pvp);
clearrectangle(600, 280, 720, 300);//清除SAVE键
}
if (msg.mkLButton)
{
int finish_loop = 0;//用来让游戏结束
int change_flag = 0;//用来判断是否转换对弈方
int x = msg.x;
int y = msg.y;
if (x >= 600 && x <= 720 && y >= 220 && y <= 240) {
return 1;//游戏中点return
}
if (x >= 600 && x <= 720 && y >= 280 && y <= 300) {
save_file(flag_pvp);
}
if (x >= 600 && x <= 720 && y >= 340 && y <= 360) {//600, 540, 720, 560
pause_pvp();
}
for (int j = 0; j < edge; j++) {//注意这里在输入棋盘进入board里时,行是j,列是i,找到鼠标点击的位置并落子
for (int i = 0; i < edge; i++) {
int x0 = 60, y0 = 60;
if (((x - (x0 + i * 60)) * (x - (x0 + i * 60)) + (y - (y0 + j * 60)) * (y - (y0 + j * 60)) <= 30 * 30) && (board[j][i] == 0)) {
if (judgement(j, i, flag_side_pvp)) {
board[j][i] = flag_side_pvp;
if (flag_side_pvp == -1) {
setfillcolor(WHITE);
}
else if (flag_side_pvp == 1) {
setfillcolor(BLACK);
}
fillcircle(x0 + i * 60, y0 + j * 60, 25);
setfillcolor(RGB(255, 239, 213));
//fillrectangle(600, 320, 720, 400);
cnt++;
change_flag = 1;
break;
}
else if (board[j][i] != 0) {
break;
}
else if (!judgement(j, i, flag_side_pvp)) {
print_game_over_pvp(-flag_side_pvp);
flag_game_over = 1;
break;
}
}
}
}
if (change_flag) {
flag_side_pvp = -flag_side_pvp;//以上落子有效,换边
}
if (flag_game_over)
break;
}
FlushMouseMsgBuffer();//用于减少采样量
}
return 2;//整盘棋下完后点return
}
void clear_data()
{
for (int i = 0; i < edge; i++) {
for (int j = 0; j < edge; j++) {
board[i][j] = 0;
}
}
cnt = 0; flag_side = 0; flag_game_over = 0;//value = 0;//row = 0, col = 0, best_value = MININT,