-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
1264 lines (1201 loc) · 32.5 KB
/
main.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
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<iostream>
#include<vector>
#include<map>
#include <graphics.h>
#include <conio.h>
#include <time.h>
using namespace std;
/* 棋盘 */
/*---x--->
|
y board
|
v */
/* 搜索思路 */
//电脑先手时 玩家执白 电脑执黑 电脑希望(黑-白)MAX
//玩家先手时 玩家执黑 电脑执白 电脑希望(白-黑)MAX
/* 得分计算思路 */
/*
对于一个棋子 计算其周围几个方向的得分和作为该棋子得分
col slope = 1
\ | /
\ | /
\|/
----.---- row
/|\
/ | \
/ | \
slope = -1
对于一个棋盘 计算所有棋子得分和作为棋盘得分
*/
//棋盘中间值
#define MID 7
//计算得分用 活五到死二的得分
#define live5 100000
#define live4 10000
#define live3 1000
#define live2 100
#define live1 10
#define die4 1000
#define die3 100
#define die2 10
//棋子颜色的宏定义
#define B 1
#define W 2
//鼠标位置的宏定义
#define inB 1
#define inW 2
#define in3 3
#define in4 4
#define in5 5
#define inBoard 0
#define inReload 1
#define inEnd 2
//后端
//打印棋盘数组
void print(int board[][15]) {
for (int y = 0; y < 15; ++y) {
for (int x = 0; x < 15; ++x) {
cout << board[y][x];
}
cout << endl;
}
cout << "----------------" << endl;
}
//供score调用 输入7个连续位置的棋子序列 返回该序列的黑子得分
double getTypeB(int* pieces) {
//0.....6
int count = 0;
double score = 0;
for (int i = 1; i <= 5; ++i) {
if (pieces[i] == 1) {
count = 0;
int j = i;
while (j <= 5) {
if (pieces[j] == 1) {
count++;
j++;
}
else break;
}
//i:第一个黑 j:第一个非黑或6
switch (count)
{
case 1:
if (pieces[i - 1] != 2 && pieces[j] != 2) score += live1;
break;
case 2:
if (pieces[i - 1] != 2 && pieces[j] != 2) score += live2;
else score += die2;
break;
case 3:
if (pieces[i - 1] != 2 && pieces[j] != 2) score += live3;
else score += die3;
break;
case 4:
if (pieces[i - 1] != 2 && pieces[j] != 2) score += live4;
else score += die4;
break;
case 5:
score += live5;
break;
default:
break;
}
i = j - 1; //第二次i的位置从j开始
}
}
return score;
}
//供score调用 输入7个连续位置的棋子序列 返回该序列的白子得分
double getTypeW(int* pieces) {
//0.....6
int count = 0;
double score = 0;
for (int i = 1; i <= 5; ++i) {
if (pieces[i] == 2) {
count = 0;
int j = i;
while (j <= 5) {
if (pieces[j] == 2) {
count++;
j++;
}
else break;
}
//i:第一个白 j:第一个非白或6
switch (count)
{
case 1:
if (pieces[i - 1] != 1 && pieces[j] != 1) score += live1;
break;
case 2:
if (pieces[i - 1] != 1 && pieces[j] != 1) score += live2;
else score += die2;
break;
case 3:
if (pieces[i - 1] != 1 && pieces[j] != 1) score += live3;
else score += die3;
break;
case 4:
if (pieces[i - 1] != 1 && pieces[j] != 1) score += live4;
else score += die4;
break;
case 5:
score += live5;
break;
default:
break;
}
i = j - 1; //第二次i的位置从j开始
}
}
return score;
}
//输入棋盘 返回黑子或白子得分 color指定黑子或白子(下同)
double score(int board[][15], int color) {
double score = 0.0;
int pieces[7]; //1-5处理
int xx, yy;
int F = 0;
for (int len = 0; len <= 7; ++len) {
int flag = 0;
for (int y = MID - len; y <= MID + len; ++y) {
for (int x = MID - len; x <= MID + len; ++x) {
if (y != MID - len && y != MID + len) {
if (x != MID - len && x != MID + len) continue;
}
if (board[y][x]) { //有子
flag = 1;
F = 1;
//col
for (int i = 5; i >= 0; --i) {
for (int j = 0; j <= 6; ++j) {
xx = x;
yy = y - i + j;
if (yy < 0 || yy > 14) pieces[j] = 2;
else pieces[j] = board[yy][xx];
}
if (color == 1) score += getTypeB(pieces);
else score += getTypeW(pieces);
}
//row
for (int i = 5; i >= 0; --i) {
for (int j = 0; j <= 6; ++j) {
xx = x - i + j;
yy = y;
if (xx < 0 || xx > 14) pieces[j] = 2;
else pieces[j] = board[yy][xx];
}
if (color == 1) score += getTypeB(pieces);
else score += getTypeW(pieces);
}
//slope = -1
for (int i = 5; i >= 0; --i) {
for (int j = 0; j <= 6; ++j) {
xx = x - i + j;
yy = y - i + j;
if (xx < 0 || xx > 14 || yy < 0 || yy > 14) pieces[j] = 2;
else pieces[j] = board[yy][xx];
}
if (color == 1) score += getTypeB(pieces);
else score += getTypeW(pieces);
}
//slope = 1
for (int i = 5; i >= 0; --i) {
for (int j = 0; j <= 6; ++j) {
xx = x - i + j;
yy = y + i - j;
if (xx < 0 || xx > 14 || yy < 0 || yy > 14) pieces[j] = 2;
else pieces[j] = board[yy][xx];
}
if (color == 1) score += getTypeB(pieces);
else score += getTypeW(pieces);
}
}
}
}
if (F == 1 && flag == 0) return score;
}
return score;
}
//供isWin调用 输入5个连续位置的棋子序列 返回是否形成5连
bool isWin_pieces(int* pieces, int color) {
if (color == B) {
for (int i = 0; i <= 4; ++i) {
if (pieces[i] != B) return false;
}
return true;
}
else if (color == W) {
for (int i = 0; i <= 4; ++i) {
if (pieces[i] != W) return false;
}
return true;
}
return true;
}
//输入棋盘和最新落子位置 返回是否胜利
bool isWin(int board[][15], int x, int y, int color) {
int pieces[5]; //1-5处理
int xx = 0;
int yy = 0;
if (color == B) {
//col
for (int i = 4; i >= 0; --i) {
for (int j = 0; j <= 4; ++j) {
xx = x;
yy = y - i + j;
if (yy < 0 || yy > 14) pieces[j] = 2;
else pieces[j] = board[yy][xx];
}
if (isWin_pieces(pieces, B) == true) return true;
}
//row
for (int i = 4; i >= 0; --i) {
for (int j = 0; j <= 4; ++j) {
xx = x - i + j;
yy = y;
if (xx < 0 || xx > 14) pieces[j] = 2;
else pieces[j] = board[yy][xx];
}
if (isWin_pieces(pieces, B) == true) return true;
}
//slope = -1
for (int i = 4; i >= 0; --i) {
for (int j = 0; j <= 4; ++j) {
xx = x - i + j;
yy = y - i + j;
if (xx < 0 || xx > 14 || yy < 0 || yy > 14) pieces[j] = 2;
else pieces[j] = board[yy][xx];
}
if (isWin_pieces(pieces, B) == true) return true;
}
//slope = 1
for (int i = 4; i >= 0; --i) {
for (int j = 0; j <= 4; ++j) {
xx = x - i + j;
yy = y + i - j;
if (xx < 0 || xx > 14 || yy < 0 || yy > 14) pieces[j] = 2;
else pieces[j] = board[yy][xx];
}
if (isWin_pieces(pieces, B) == true) return true;
}
}
else if (color == W) {
//col
for (int i = 4; i >= 0; --i) {
for (int j = 0; j <= 4; ++j) {
xx = x;
yy = y - i + j;
if (yy < 0 || yy > 14) pieces[j] = 1;
else pieces[j] = board[yy][xx];
}
if (isWin_pieces(pieces, W) == true) return true;
}
//row
for (int i = 4; i >= 0; --i) {
for (int j = 0; j <= 4; ++j) {
xx = x - i + j;
yy = y;
if (xx < 0 || xx > 14) pieces[j] = 1;
else pieces[j] = board[yy][xx];
}
if (isWin_pieces(pieces, W) == true) return true;
}
//slope = -1
for (int i = 4; i >= 0; --i) {
for (int j = 0; j <= 4; ++j) {
xx = x - i + j;
yy = y - i + j;
if (xx < 0 || xx > 14 || yy < 0 || yy > 14) pieces[j] = 1;
else pieces[j] = board[yy][xx];
}
if (isWin_pieces(pieces, W) == true) return true;
}
//slope = 1
for (int i = 4; i >= 0; --i) {
for (int j = 0; j <= 4; ++j) {
xx = x - i + j;
yy = y + i - j;
if (xx < 0 || xx > 14 || yy < 0 || yy > 14) pieces[j] = 1;
else pieces[j] = board[yy][xx];
}
if (isWin_pieces(pieces, W) == true) return true;
}
}
return false;
}
//输入点p 返回是否出界
bool isOut(int* p) {
if (p[0] < 0 || p[0] > 14 || p[1] < 0 || p[1] > 14) return true;
else return false;
}
//供pre和pre2调用 输入棋盘和上次对手落子坐标(x,y) 若对手成四 输出可堵截点p1, p2
bool isFour(int board[][15], int x, int y, int color, int* p1, int*p2) {
int pieces[5];
int xx, yy;
//col
for (int i = 4; i >= 0; --i) {
for (int j = 0; j <= 4; ++j) {
xx = x;
yy = y - i + j;
if (yy < 0 || yy > 14) {
if (color == B) pieces[j] = W;
else if (color == W) pieces[j] = B;
}
else pieces[j] = board[yy][xx];
}
if (color == B) {
if (pieces[0] == B && pieces[1] == B && pieces[2] == B && pieces[3] == B) {
p1[0] = x;
p1[1] = y - i - 1;
p2[0] = x;
p2[1] = y - i - 1 + 5;
return true;
}
if (pieces[1] == B && pieces[2] == B && pieces[3] == B && pieces[4] == B) {
p1[0] = x;
p1[1] = y - i;
p2[0] = x;
p2[1] = y - i + 5;
return true;
}
}
else if (color == W) {
if (pieces[0] == W && pieces[1] == W && pieces[2] == W && pieces[3] == W) {
p1[0] = x;
p1[1] = y - i - 1;
p2[0] = x;
p2[1] = y - i - 1 + 5;
return true;
}
if (pieces[1] == W && pieces[2] == W && pieces[3] == W && pieces[4] == W) {
p1[0] = x;
p1[1] = y - i;
p2[0] = x;
p2[1] = y - i + 5;
return true;
}
}
}
//row
for (int i = 4; i >= 0; --i) {
for (int j = 0; j <= 4; ++j) {
xx = x - i + j;
yy = y;
if (xx < 0 || xx > 14) {
if (color == B) pieces[j] = W;
else if (color == W) pieces[j] = B;
}
else pieces[j] = board[yy][xx];
}
if (color == B) {
if (pieces[0] == B && pieces[1] == B && pieces[2] == B && pieces[3] == B) {
p1[0] = x - i - 1;
p1[1] = y;
p2[0] = x - i - 1 + 5;
p2[1] = y;
return true;
}
if (pieces[1] == B && pieces[2] == B && pieces[3] == B && pieces[4] == B) {
p1[0] = x - i;
p1[1] = y;
p2[0] = x - i + 5;
p2[1] = y;
return true;
}
}
else if (color == W) {
if (pieces[0] == W && pieces[1] == W && pieces[2] == W && pieces[3] == W) {
p1[0] = x - i - 1;
p1[1] = y;
p2[0] = x - i - 1 + 5;
p2[1] = y;
return true;
}
if (pieces[1] == W && pieces[2] == W && pieces[3] == W && pieces[4] == W) {
p1[0] = x - i;
p1[1] = y;
p2[0] = x - i + 5;
p2[1] = y;
return true;
}
}
}
//slope = -1
for (int i = 4; i >= 0; --i) {
for (int j = 0; j <= 4; ++j) {
xx = x - i + j;
yy = y - i + j;
if (xx < 0 || xx > 14 || yy < 0 || yy > 14) {
if (color == B) pieces[j] = W;
else if (color == W) pieces[j] = B;
}
else pieces[j] = board[yy][xx];
}
if (color == B) {
if (pieces[0] == B && pieces[1] == B && pieces[2] == B && pieces[3] == B) {
p1[0] = x - i - 1;
p1[1] = y - i - 1;
p2[0] = x - i - 1 + 5;
p2[1] = y - i - 1 + 5;
return true;
}
if (pieces[1] == B && pieces[2] == B && pieces[3] == B && pieces[4] == B) {
p1[0] = x - i;
p1[1] = y - i;
p2[0] = x - i + 5;
p2[1] = y - i + 5;
return true;
}
}
else if (color == W) {
if (pieces[0] == W && pieces[1] == W && pieces[2] == W && pieces[3] == W) {
p1[0] = x - i - 1;
p1[1] = y - i - 1;
p2[0] = x - i - 1 + 5;
p2[1] = y - i - 1 + 5;
return true;
}
if (pieces[1] == W && pieces[2] == W && pieces[3] == W && pieces[4] == W) {
p1[0] = x - i;
p1[1] = y - i;
p2[0] = x - i + 5;
p2[1] = y - i + 5;
return true;
}
}
}
//slope = 1
for (int i = 4; i >= 0; --i) {
for (int j = 0; j <= 4; ++j) {
xx = x - i + j;
yy = y + i - j;
if (xx < 0 || xx > 14 || yy < 0 || yy > 14) {
if (color == B) pieces[j] = W;
else if (color == W) pieces[j] = B;
}
else pieces[j] = board[yy][xx];
}
if (color == B) {
if (pieces[0] == B && pieces[1] == B && pieces[2] == B && pieces[3] == B) {
p1[0] = x - i - 1;
p1[1] = y + i + 1;
p2[0] = x - i - 1 + 5;
p2[1] = y + i + 1 - 5;
return true;
}
if (pieces[1] == B && pieces[2] == B && pieces[3] == B && pieces[4] == B) {
p1[0] = x - i;
p1[1] = y + i;
p2[0] = x - i + 5;
p2[1] = y + i - 5;
return true;
}
}
else if (color == W) {
if (pieces[0] == W && pieces[1] == W && pieces[2] == W && pieces[3] == W) {
p1[0] = x - i - 1;
p1[1] = y + i + 1;
p2[0] = x - i - 1 + 5;
p2[1] = y + i + 1 - 5;
return true;
}
if (pieces[1] == W && pieces[2] == W && pieces[3] == W && pieces[4] == W) {
p1[0] = x - i;
p1[1] = y + i;
p2[0] = x - i + 5;
p2[1] = y + i - 5;
return true;
}
}
}
return false;
}
/**
* @brief 极大小树计算前的预处理 判断对手是否成四
* @param[in] player 判断逻辑
* \ 1 电脑执黑,判断白子4连
* \ 2 电脑执白, 判断黑子4连
* @param[in] board 棋盘
* @param[in] x_last, y_last 对手(即玩家)上次落子位置
* @param[out] max_place 电脑此次应该落子位置
*/
bool pre(int player, int board[][15], int x_last, int y_last, int * max_place) {
int p1[2] = { 0 };
int p2[2] = { 0 };
if (player == 1) { //电脑执黑 检测白子的4连
if (isFour(board, x_last, y_last, W, p1, p2)) {
if (isOut(p1) && !isOut(p2) && board[p2[1]][p2[0]] == 0) {
max_place[0] = p2[0];
max_place[1] = p2[1];
return true;
}
if (isOut(p2) && !isOut(p1) && board[p1[1]][p1[0]] == 0) {
max_place[0] = p1[0];
max_place[1] = p1[1];
return true;
}
if (!isOut(p1) && board[p1[1]][p1[0]] == 0) {
max_place[0] = p1[0];
max_place[1] = p1[1];
return true;
}
if (!isOut(p2) && board[p2[1]][p2[0]] == 0) {
max_place[0] = p2[0];
max_place[1] = p2[1];
return true;
}
}
return false;
}
if (player == 2) { //电脑执白 检测黑子的4连
if (isFour(board, x_last, y_last, B, p1, p2)) {
if (isOut(p1) && !isOut(p2) && board[p2[1]][p2[0]] == 0) {
max_place[0] = p2[0];
max_place[1] = p2[1];
return true;
}
if (isOut(p2) && !isOut(p1) && board[p1[1]][p1[0]] == 0) {
max_place[0] = p1[0];
max_place[1] = p1[1];
return true;
}
if (!isOut(p1) && board[p1[1]][p1[0]] == 0) {
max_place[0] = p1[0];
max_place[1] = p1[1];
return true;
}
if (!isOut(p2) && board[p2[1]][p2[0]] == 0) {
max_place[0] = p2[0];
max_place[1] = p2[1];
return true;
}
}
return false;
}
return false;
}
/**
* @brief 极大小树计算前的预处理 判断自己是否成四
* @param[in] player 判断逻辑
* \ 1 电脑执白,判断白子4连
* \ 2 电脑执黑, 判断黑子4连
* @param[in] board 棋盘
* @param[in] x_last, y_last 自己(即电脑)上次落子位置
* @param[out] max_place 电脑此次应该落子位置
*/
bool pre2(int player, int board[][15], int x_last, int y_last, int * max_place) {
int p1[2] = { 0 };
int p2[2] = { 0 };
if (player == 1) { //电脑执白 检测白子胜利
if (isFour(board, x_last, y_last, W, p1, p2)) {
if (isOut(p1) && !isOut(p2) && board[p2[1]][p2[0]] == 0) {
max_place[0] = p2[0];
max_place[1] = p2[1];
return true;
}
if (isOut(p2) && !isOut(p1) && board[p1[1]][p1[0]] == 0) {
max_place[0] = p1[0];
max_place[1] = p1[1];
return true;
}
if (!isOut(p1) && board[p1[1]][p1[0]] == 0) {
max_place[0] = p1[0];
max_place[1] = p1[1];
return true;
}
if (!isOut(p2) && board[p2[1]][p2[0]] == 0) {
max_place[0] = p2[0];
max_place[1] = p2[1];
return true;
}
}
return false;
}
if (player == 2) { ////电脑执黑 检测黑子胜利
if (isFour(board, x_last, y_last, B, p1, p2)) {
if (isOut(p1) && !isOut(p2) && board[p2[1]][p2[0]] == 0) {
max_place[0] = p2[0];
max_place[1] = p2[1];
return true;
}
if (isOut(p2) && !isOut(p1) && board[p1[1]][p1[0]] == 0) {
max_place[0] = p1[0];
max_place[1] = p1[1];
return true;
}
if (!isOut(p1) && board[p1[1]][p1[0]] == 0) {
max_place[0] = p1[0];
max_place[1] = p1[1];
return true;
}
if (!isOut(p2) && board[p2[1]][p2[0]] == 0) {
max_place[0] = p2[0];
max_place[1] = p2[1];
return true;
}
}
return false;
}
return false;
}
/**
* @brief alpha-beta剪枝的极大级小搜索树
* @param[in] player 固定填2 电脑执黑,电脑希望(黑-白)max
* @param[in] board 棋盘
* @param[in] alpha, beta 初始值分别为INT_MIN, INT_MAX
* @param[in] step 当前思考深度 初始值为1
* @param[in] steps 最大思考深度
* @param[out] max_place 电脑此次应该落子位置
*/
double maxmin(int player, int board[][15], double alpha, double beta, int step, int max_step, int * max_place) {
if (step > max_step) {
return score(board, B) - score(board, W);
}
double best = 0.0;
int F = 0;
if (player == 1) { // MAX
best = INT_MIN;
for (int len = 0; len <= 7; ++len) { //棋盘遍历方式是由内圈向外圈遍历,直到某一圈找不到棋子
int flag = 0;
for (int y = MID - len; y <= MID + len; ++y) {
for (int x = MID - len; x <= MID + len; ++x) {
if (y != MID - len && y != MID + len) {
if (x != MID - len && x != MID + len) continue;
}
if (board[y][x]) { //有子
F = 1;
flag = 1;
for (int yy = y - 1; yy <= y + 1; ++yy) {
for (int xx = x - 1; xx <= x + 1; ++xx) {
if (xx == x && yy == y) continue; //当前点
if (xx < 0 || xx > 14 || yy < 0 || yy > 14) continue; //出界
if (board[yy][xx]) continue; //已有子
//if (isConsidered.find(yy * 15 + x) != isConsidered.end()) continue; //已考虑过
//generate a new board
board[yy][xx] = 1;
double cur = maxmin(2, board, alpha, beta, step + 1, max_step, max_place);
board[yy][xx] = 0;
if (cur > best) {
best = cur;
alpha = cur;
max_place[0] = xx;
max_place[1] = yy;
}
if (alpha > beta) return alpha;
}
}
}
}
}
if (F == 1 && flag == 0) {
return best;
}
}
return best;
}
else if (player == 2) { // MIN
best = INT_MAX;
for (int len = 0; len <= 7; ++len) {
int flag = 0;
for (int y = MID - len; y <= MID + len; ++y) {
for (int x = MID - len; x <= MID + len; ++x) {
if (y != MID - len && y != MID + len) {
if (x != MID - len && x != MID + len) continue;
}
if (board[y][x]) {
F = 1;
flag = 1;
for (int yy = y - 1; yy <= y + 1; ++yy) {
for (int xx = x - 1; xx <= x + 1; ++xx) {
if (xx == x && yy == y) continue; //当前点
if (xx < 0 || xx > 14 || yy < 0 || yy > 14) continue; //出界
if (board[yy][xx]) continue; //已有子
//if (isConsidered.find(yy * 15 + x) != isConsidered.end()) continue; //已考虑过
//generate a new board
board[yy][xx] = 2;
double cur = maxmin(1, board, alpha, beta, step + 1, max_step, max_place);
board[yy][xx] = 0;
if (cur < best) {
best = cur;
beta = cur;
}
if (alpha > beta) return beta;
}
}
}
}
}
if (F == 1 && flag == 0) {
return best;
}
}
return best;
}
}
/**
* @brief alpha-beta剪枝的极大级小搜索树
* @param[in] player 固定填2 电脑执白,电脑希望(白-黑)max
* @param[in] board 棋盘
* @param[in] alpha, beta 初始值分别为INT_MIN, INT_MAX
* @param[in] step 当前思考深度 初始值为1
* @param[in] steps 最大思考深度
* @param[out] max_place 电脑此次应该落子位置
*/
double maxmin2(int player, int board[][15], double alpha, double beta, int step, int max_step, int * max_place) {
//电脑后手 执白
if (step > max_step) {
return score(board, W) - score(board, B);
}
double best = 0.0;
int F = 0;
if (player == 1) { // MIN
best = INT_MAX;
for (int len = 0; len <= 7; ++len) { ////棋盘遍历方式是由内圈向外圈遍历,直到某一圈找不到棋子
int flag = 0;
for (int y = MID - len; y <= MID + len; ++y) {
for (int x = MID - len; x <= MID + len; ++x) {
if (y != MID - len && y != MID + len) {
if (x != MID - len && x != MID + len) continue;
}
if (board[y][x]) { //有子
F = 1;
flag = 1;
for (int yy = y - 1; yy <= y + 1; ++yy) {
for (int xx = x - 1; xx <= x + 1; ++xx) {
if (xx == x && yy == y) continue; //当前点
if (xx < 0 || xx > 14 || yy < 0 || yy > 14) continue; //出界
if (board[yy][xx]) continue; //已有子
//if (isConsidered.find(yy * 15 + x) != isConsidered.end()) continue; //已考虑过
//generate a new board
board[yy][xx] = 1;
double cur = maxmin(2, board, alpha, beta, step + 1, max_step, max_place);
board[yy][xx] = 0;
if (cur < best) {
best = cur;
beta = cur;
}
if (alpha > beta) return beta;
}
}
}
}
}
if (F == 1 && flag == 0) {
return best;
}
}
return best;
}
else if (player == 2) { // MAX
best = INT_MIN;
for (int len = 0; len <= 7; ++len) {
int flag = 0;
for (int y = MID - len; y <= MID + len; ++y) {
for (int x = MID - len; x <= MID + len; ++x) {
if (y != MID - len && y != MID + len) {
if (x != MID - len && x != MID + len) continue;
}
if (board[y][x]) {
F = 1;
flag = 1;
for (int yy = y - 1; yy <= y + 1; ++yy) {
for (int xx = x - 1; xx <= x + 1; ++xx) {
if (xx == x && yy == y) continue; //当前点
if (xx < 0 || xx > 14 || yy < 0 || yy > 14) continue; //出界
if (board[yy][xx]) continue; //已有子
//if (isConsidered.find(yy * 15 + x) != isConsidered.end()) continue; //已考虑过
//generate a new board
board[yy][xx] = 2;
double cur = maxmin(1, board, alpha, beta, step + 1, max_step, max_place);
board[yy][xx] = 0;
if (cur > best) {
best = cur;
alpha = cur;
max_place[0] = xx;
max_place[1] = yy;
}
if (alpha > beta) return alpha;
}
}
}
}
}
if (F == 1 && flag == 0) {
return best;
}
}
return best;
}
}
//前端
//欢迎页
void ini() {
initgraph(600, 480);
setbkcolor(YELLOW);
cleardevice();
RECT r = { 600 / 2 - 150, 480 / 2 - 100, 600 / 2 + 150, 480 / 2 - 100 + 40 };
LOGFONT f;
gettextstyle(&f);
f.lfQuality = ANTIALIASED_QUALITY;
f.lfHeight = 30;
settextstyle(&f);
settextcolor(BLACK);
drawtext("五子棋", &r, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
RECT r0 = { 600 / 2 - 150, 455 - 230, 600 / 2 + 150, 471 - 230 };
gettextstyle(&f);
f.lfQuality = ANTIALIASED_QUALITY;
f.lfHeight = 20;
settextstyle(&f);
settextcolor(BLACK);
drawtext("08116124 杨雨尧", &r0, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
RECT r1 = { 420, 455 - 100, 580, 471 - 100 };
gettextstyle(&f);
f.lfQuality = ANTIALIASED_QUALITY;
f.lfHeight = 20;
settextstyle(&f);
settextcolor(BLACK);
drawtext("按任意键继续...", &r1, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
}
//执方选择页
void chooseColor() {
setbkcolor(YELLOW);
cleardevice();
RECT r = { 600 / 2 - 150, 480 / 2 - 100, 600 / 2 + 150, 480 / 2 - 100 + 40 };
LOGFONT f;
gettextstyle(&f);
f.lfQuality = ANTIALIASED_QUALITY;
f.lfHeight = 30;
settextstyle(&f);
settextcolor(BLACK);
drawtext("请选择执方", &r, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
setfillcolor(WHITE);
solidrectangle(600 / 2 - 50, 480 / 2 - 40, 600 / 2 + 50, 480 / 2 - 40 + 40);
RECT r1 = { 600 / 2 - 35, 480 / 2 - 40, 600 / 2 + 35, 480 / 2 - 40 + 40 };
gettextstyle(&f);
f.lfQuality = ANTIALIASED_QUALITY;
f.lfHeight = 25;
settextstyle(&f);
settextcolor(BLACK);
setbkcolor(WHITE);
drawtext(_T("先手(黑)"), &r1, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
setbkcolor(YELLOW);
solidrectangle(600 / 2 - 50, 480 / 2 + 20, 600 / 2 + 50, 480 / 2 + 20 + 40);
RECT r2 = { 600 / 2 - 35, 480 / 2 + 20, 600 / 2 + 35, 480 / 2 + 20 + 40 };
gettextstyle(&f);
f.lfQuality = ANTIALIASED_QUALITY;
f.lfHeight = 25;
settextstyle(&f);
settextcolor(BLACK);
setbkcolor(WHITE);
drawtext(_T("后手(白)"), &r2, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
setbkcolor(YELLOW);
}
//AI思考深度选择页
void chooseDeep() {
setbkcolor(YELLOW);
cleardevice();
RECT r = { 600 / 2 - 150, 480 / 2 - 100, 600 / 2 + 150, 480 / 2 - 100 + 40 };
LOGFONT f;
gettextstyle(&f);
f.lfQuality = ANTIALIASED_QUALITY;
f.lfHeight = 30;
settextstyle(&f);
settextcolor(BLACK);
drawtext("AI思考深度", &r, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
setfillcolor(WHITE);
solidrectangle(600 / 2 - 30 - 30 - 60, 480 / 2 - 40, 600 / 2 - 30 - 30, 480 / 2 - 40 + 40);
RECT r1 = { 600 / 2 - 30 - 30 - 60, 480 / 2 - 40, 600 / 2 - 30 - 30, 480 / 2 - 40 + 40 };
gettextstyle(&f);
f.lfQuality = ANTIALIASED_QUALITY;
f.lfHeight = 25;
settextstyle(&f);
settextcolor(BLACK);
setbkcolor(WHITE);
drawtext(_T("3"), &r1, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
setbkcolor(YELLOW);
setfillcolor(WHITE);
solidrectangle(600 / 2 - 30, 480 / 2 - 40, 600 / 2 + 30, 480 / 2 - 40 + 40);
RECT r2 = { 600 / 2 - 30, 480 / 2 - 40, 600 / 2 + 30, 480 / 2 - 40 + 40 };
gettextstyle(&f);
f.lfQuality = ANTIALIASED_QUALITY;
f.lfHeight = 25;
settextstyle(&f);
settextcolor(BLACK);
setbkcolor(WHITE);
drawtext(_T("4"), &r2, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
setbkcolor(YELLOW);
setfillcolor(WHITE);
solidrectangle(600 / 2 + 30 + 30, 480 / 2 - 40, 600 / 2 + 30 + 30 + 60, 480 / 2 - 40 + 40);
RECT r3 = { 600 / 2 + 30 + 30, 480 / 2 - 40, 600 / 2 + 30 + 30 + 60, 480 / 2 - 40 + 40 };
gettextstyle(&f);
f.lfQuality = ANTIALIASED_QUALITY;
f.lfHeight = 25;
settextstyle(&f);
settextcolor(BLACK);
setbkcolor(WHITE);
drawtext(_T("5"), &r3, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
setbkcolor(YELLOW);
}
//棋盘主界面
void mainBoard() {
IMAGE img1(452, 452);
setbkcolor(WHITE);
cleardevice();
loadimage(&img1, _T("board3.png"), 452, 452, false);
putimage(10, 10, &img1);
//reload
setlinecolor(RED);
rectangle(500, 380, 570, 410);
RECT r2 = { 500, 380, 570, 410 };
drawtext(_T("重新开始"), &r2, DT_CENTER | DT_VCENTER | DT_SINGLELINE);