-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAI.cpp
More file actions
1267 lines (1255 loc) · 25.3 KB
/
AI.cpp
File metadata and controls
1267 lines (1255 loc) · 25.3 KB
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"head.h"
using namespace std;
extern short now[][9];
extern short record[][9][9];
extern short record_i[100];
extern short record_j[100];
extern int turns;
extern int deep_limit;//搜索深度
extern int search_to_end_turns;
extern int mode;
extern int win_mode;
const int large_num=30000;
const int larger_num=60000;
const int depth=60;//要大于搜索深度*2(因为所有skip过的地方要占2倍空间)
int times_of_skips=0;
int maxmin[depth]={0};//剪枝时,用于存储max,min节点的alpha,beta值
int deep_now=0;//搜索深度,以下了几个子为准
bool search_all=0;//等于0,表示没有到残局判别标准,不搜索到底;否则,到残局判别标准,搜索到底
int number=0;
bool AI(short whose_turn)
{
int count_board();
int search_min(short whose_turn);
int pre_score_board(short whose_turn,int i,int j);
int score_board(short whose_turn);
int max=-larger_num;//已搜索到的最佳走法的估值
int max_i=-1,max_j=-1;//最佳走子的坐标
short save_board[9][9]={0};//用于存储下电脑没有落子时的局面
int i=0,j=0,m=0,n=0,temp=0,finish=0;
//以下判断是否作完美搜索,或调整搜索深度
temp=count_board();
if(temp>=search_to_end_turns)
search_all=1;
number=temp;
//以下存局
for(i=0;i<9;i++)
{
for(j=0;j<9;j++)
save_board[i][j]=now[i][j];
}
//以下进行预评价局面,以优化搜索顺序,从而提升剪枝效率
short score_list[50]={0};//可以走子位置一步以后的局面评价分数
short i_list[50]={0};//可以走子位置的行数
short j_list[50]={0};//可以走子位置的列数
short now_num=0;//现在走子的编号
short total_num=0;//一共可行的走子数量
for(i=1;i<9;i++)
{
for(j=1;j<9;j++)
{
if(now[i][j]==0)//如果这个地方还没有落子
{
now[i][j]=whose_turn;//假设落子
if(my_reverse(i,j)==0)//非法落子点
now[i][j]=0;//复原这个位置的状态
else
{
now_num++;
total_num++;
score_list[now_num]=pre_score_board(whose_turn,i,j);
i_list[now_num]=i;
j_list[now_num]=j;
for(m=1;m<9;m++)
{
for(n=1;n<9;n++)
now[m][n]=save_board[m][n];//回溯
}
}
}
}
}
//以下冒泡排序
int rank_temp=0;
for(i=1;i<total_num;i++)
{
for(j=1;j<=total_num-i;j++)
{
if(score_list[j]<score_list[j+1])
{
rank_temp=score_list[j];
score_list[j]=score_list[j+1];
score_list[j+1]=rank_temp;
rank_temp=i_list[j];
i_list[j]=i_list[j+1];
i_list[j+1]=rank_temp;
rank_temp=j_list[j];
j_list[j]=j_list[j+1];
j_list[j+1]=rank_temp;
}
}
}
//以下寻找最佳落子点
for(i=1;i<=total_num;i++)
{
now[i_list[i]][j_list[i]]=whose_turn;
my_reverse(i_list[i],j_list[i]);
maxmin[deep_now]=max;//存储节点数据,准备剪枝
deep_now++;
temp=search_min(whose_turn);//调用假想敌函数
deep_now--;//回溯
if(mode==1)
cout<<temp<<" ";
if(temp>max)//更优的走子,大于等于号是不对的,会使剪枝出错
{
max=temp;
max_i=i_list[i];
max_j=j_list[i];
}
for(m=1;m<9;m++)
{
for(n=1;n<9;n++)
now[m][n]=save_board[m][n];//回溯
}
if((search_all==1)&&(win_mode==1))
{
if(max>0)
{
finish=1;
break;//在min里不能剪枝?
}
}
}
//以下落子
now[max_i][max_j]=whose_turn;
my_reverse(max_i,max_j);
record_i[turns]=max_i;
record_j[turns]=max_j;
return 1;
}
int search_min(short whose_turn)
{
int count_board();
int search_max(short whose_turn);
int pre_score_board(short whose_turn,int i,int j);
int score_board(short whose_turn);
//以下判断是否触发结束搜索条件
bool to_end=0;//1为结束搜索,调用局面评价函数;0为继续搜索
if(count_board()==64)//是否已经下满全局?
to_end=1;
else if(times_of_skips==2)//是否连续pass了两回合,双方都无子可下?
{
times_of_skips=0;
to_end=1;
}
else if((deep_now>=deep_limit)&&(search_all==0))//是否在不完美搜索中达到了搜索深度?
to_end=1;
//如果触发结束搜索条件
if(to_end==1)
return score_board(whose_turn);
//如果没有触发结束搜索条件,判断是否因为该方无子可下而跳过本回合
int i=0,j=0,m=0,n=0,temp=large_num,step=0;
if(judge_end(3-whose_turn)==0)
{
times_of_skips++;
maxmin[deep_now]=temp;
deep_now++;
temp=search_max(whose_turn);
deep_now--;
return temp;
}
//如果既不结束搜索也不跳过本回合
times_of_skips=0;
int min=large_num;//已搜索到的最佳走法的估值
short save_board[9][9]={0};//用于存储下电脑没有落子时的局面
//以下存局
for(i=0;i<9;i++)
{
for(j=0;j<9;j++)
save_board[i][j]=now[i][j];
}
if(number+deep_now>59)
{
//以下寻找最佳落子点
for(i=0;i<8;i++)
{
if(i==0)
i=8;
for(j=1;j<9;j++)
{
if(now[i][j]==0)//如果这个地方还没有落子
{
now[i][j]=3-whose_turn;//假设落子
if(my_reverse(i,j)==0)//非法落子点
{
now[i][j]=0;//复原这个位置的状态
continue;//搜索下一个位置
}
else//合法落子点
{
maxmin[deep_now]=min;//存储节点数据,准备剪枝
deep_now++;
temp=search_max(whose_turn);//调用假想敌函数
deep_now--;//回溯
if(temp<min)//更优的走子,小于等于号是不对的,会使剪枝出错
min=temp;
for(m=1;m<9;m++)
{
for(n=1;n<9;n++)
now[m][n]=save_board[m][n];//回溯
}
for(step=deep_now-1;step>=0;step=step-2)//是0的话给出的解是精确的,但对每个点给出的估值不准确
{
if(min<=maxmin[step])//alpha剪枝
return min;
}
}
}
}
if(i==8)
i=0;
}
return min;
}
//以下进行预评价局面,以优化搜索顺序,从而提升剪枝效率
short score_list[50]={0};//可以走子位置一步以后的局面评价分数
short i_list[50]={0};//可以走子位置的行数
short j_list[50]={0};//可以走子位置的列数
short now_num=0;//现在走子的编号
short total_num=0;//一共可行的走子数量
for(i=1;i<9;i++)
{
for(j=1;j<9;j++)
{
if(now[i][j]==0)//如果这个地方还没有落子
{
now[i][j]=3-whose_turn;//假设落子
if(my_reverse(i,j)==0)//非法落子点
now[i][j]=0;//复原这个位置的状态
else
{
now_num++;
total_num++;
score_list[now_num]=pre_score_board(3-whose_turn,i,j);
i_list[now_num]=i;
j_list[now_num]=j;
for(m=1;m<9;m++)
{
for(n=1;n<9;n++)
now[m][n]=save_board[m][n];//回溯
}
}
}
}
}
//以下冒泡排序
int rank_temp=0;
for(i=1;i<total_num;i++)
{
for(j=1;j<=total_num-i;j++)
{
if(score_list[j]<score_list[j+1])
{
rank_temp=score_list[j];
score_list[j]=score_list[j+1];
score_list[j+1]=rank_temp;
rank_temp=i_list[j];
i_list[j]=i_list[j+1];
i_list[j+1]=rank_temp;
rank_temp=j_list[j];
j_list[j]=j_list[j+1];
j_list[j+1]=rank_temp;
}
}
}
//以下寻找最佳落子点
for(i=1;i<=total_num;i++)
{
now[i_list[i]][j_list[i]]=3-whose_turn;
my_reverse(i_list[i],j_list[i]);
maxmin[deep_now]=min;//存储节点数据,准备剪枝
deep_now++;
temp=search_max(whose_turn);//调用假想敌函数
deep_now--;//回溯
if(temp<min)//更优的走子,小于等于号是不对的,会使剪枝出错
min=temp;
for(m=1;m<9;m++)
{
for(n=1;n<9;n++)
now[m][n]=save_board[m][n];//回溯
}
for(step=deep_now-1;step>=0;step=step-2)//是0的话给出的解是精确的,但对每个点给出的估值不准确
{
if(min<=maxmin[step])//alpha剪枝
return min;
}
}
return min;
}
int search_max(short whose_turn)
{
int count_board();
int search_min(short whose_turn);
int pre_score_board(short whose_turn,int i,int j);
int score_board(short whose_turn);
//以下判断是否触发结束搜索条件
bool to_end=0;//1为结束搜索,调用局面评价函数;0为继续搜索
if(count_board()==64)//是否已经下满全局?
to_end=1;
else if(times_of_skips==2)//是否连续pass了两回合,双方都无子可下?
{
times_of_skips=0;
to_end=1;
}
else if((deep_now>=deep_limit)&&(search_all==0))//是否在不完美搜索中达到了搜索深度?
to_end=1;
//如果触发结束搜索条件
if(to_end==1)
return score_board(whose_turn);
//如果没有触发结束搜索条件,判断是否因为该方无子可下而跳过本回合
int i=0,j=0,m=0,n=0,temp=-large_num,step=0;
if(judge_end(whose_turn)==0)
{
times_of_skips++;
maxmin[deep_now]=temp;
deep_now++;
temp=search_min(whose_turn);
deep_now--;
return temp;
}
//如果既不结束搜索也不跳过本回合
times_of_skips=0;
int max=-large_num;//已搜索到的最佳走法的估值
short save_board[9][9]={0};//用于存储下电脑没有落子时的局面
//以下存局
for(i=0;i<9;i++)
{
for(j=0;j<9;j++)
save_board[i][j]=now[i][j];
}
if(number+deep_now>59)
{
//以下寻找最佳落子点
for(i=0;i<8;i++)
{
if(i==0)
i=8;
for(j=1;j<9;j++)
{
if(now[i][j]==0)//如果这个地方还没有落子
{
now[i][j]=whose_turn;//假设落子
if(my_reverse(i,j)==0)//非法落子点
{
now[i][j]=0;//复原这个位置的状态
continue;//搜索下一个位置
}
else//合法落子点
{
maxmin[deep_now]=max;//存储节点数据,准备剪枝
deep_now++;
temp=search_min(whose_turn);//调用假想敌函数
deep_now--;//回溯
if(temp>max)//更优的走子,大于等于号是不对的,会使剪枝出错
max=temp;
for(m=1;m<9;m++)
{
for(n=1;n<9;n++)
now[m][n]=save_board[m][n];//回溯
}
if((search_all==1)&&(win_mode==1))
{
if(max>0)
return max;
}
for(step=deep_now-1;step>=0;step=step-2)//是0的话给出的解是精确的,但对每个点给出的估值不准确
{
if(max>=maxmin[step])//beta剪枝
return max;
}
}
}
}
if(i==8)
i=0;
}
return max;
}
//以下进行预评价局面,以优化搜索顺序,从而提升剪枝效率
short score_list[50]={0};//可以走子位置一步以后的局面评价分数
short i_list[50]={0};//可以走子位置的行数
short j_list[50]={0};//可以走子位置的列数
short now_num=0;//现在走子的编号
short total_num=0;//一共可行的走子数量
for(i=1;i<9;i++)
{
for(j=1;j<9;j++)
{
if(now[i][j]==0)//如果这个地方还没有落子
{
now[i][j]=whose_turn;//假设落子
if(my_reverse(i,j)==0)//非法落子点
now[i][j]=0;//复原这个位置的状态
else
{
now_num++;
total_num++;
score_list[now_num]=pre_score_board(whose_turn,i,j);
i_list[now_num]=i;
j_list[now_num]=j;
for(m=1;m<9;m++)
{
for(n=1;n<9;n++)
now[m][n]=save_board[m][n];//回溯
}
}
}
}
}
//以下冒泡排序
int rank_temp=0;
for(i=1;i<total_num;i++)
{
for(j=1;j<=total_num-i;j++)
{
if(score_list[j]<score_list[j+1])
{
rank_temp=score_list[j];
score_list[j]=score_list[j+1];
score_list[j+1]=rank_temp;
rank_temp=i_list[j];
i_list[j]=i_list[j+1];
i_list[j+1]=rank_temp;
rank_temp=j_list[j];
j_list[j]=j_list[j+1];
j_list[j+1]=rank_temp;
}
}
}
//以下寻找最佳落子点
for(i=1;i<=total_num;i++)
{
now[i_list[i]][j_list[i]]=whose_turn;
my_reverse(i_list[i],j_list[i]);
maxmin[deep_now]=max;//存储节点数据,准备剪枝
deep_now++;
temp=search_min(whose_turn);//调用假想敌函数
deep_now--;//回溯
if(temp>max)//更优的走子,大于等于号是不对的,会使剪枝出错
max=temp;
for(m=1;m<9;m++)
{
for(n=1;n<9;n++)
now[m][n]=save_board[m][n];//回溯
}
if((search_all==1)&&(win_mode==1))
{
if(max>0)
return max;
}
for(step=deep_now-1;step>=0;step=step-2)//是0的话给出的解是精确的,但对每个点给出的估值不准确
{
if(max>=maxmin[step])//beta剪枝
return max;
}
}
return max;
}
int count_board()//数棋盘上共计落了多少个子
{
int i=0,j=0;
int num=0;
for(i=1;i<9;i++)
{
for(j=1;j<9;j++)
{
if(now[i][j]!=0)
num++;
}
}
return num;
}
int score_board(short whose_turn)//综合局面评价函数
{
int special_pattern_stable_discs(short whose_turn);
int edge_disc(short whose_turn);
int special_pattern_balanced_edge(short whose_score,short whose_turn);
int i=0,j=0;
int num=0;
if(search_all==1)//到残局判别标准
{
for(i=1;i<9;i++)
{
for(j=1;j<9;j++)
{
if(now[i][j]==whose_turn)
num++;
else if(now[i][j]==3-whose_turn)
num--;
}
}
return num;
}
//没到残局判别标准
int already_lost=1;//防止自己的子被对方吃光
for(i=1;i<9;i++)
{
for(j=1;j<9;j++)
{
if(now[i][j]==whose_turn)
{
already_lost=0;
break;
}
}
if(already_lost==0)//不是“=”!
break;
}
int x=0;//下子在星位需极其谨慎
if((now[1][1]==0)&&(now[2][2]==whose_turn))
x++;
if((now[1][8]==0)&&(now[2][7]==whose_turn))
x++;
if((now[8][1]==0)&&(now[7][2]==whose_turn))
x++;
if((now[8][8]==0)&&(now[7][7]==whose_turn))
x++;
int c=0;//下子在C点需谨慎
if((now[1][1]==0)&&(now[1][2]==whose_turn)&&(now[1][3]==0))
c++;
if((now[1][1]==0)&&(now[2][1]==whose_turn)&&(now[3][1]==0))
c++;
if((now[1][8]==0)&&(now[1][7]==whose_turn)&&(now[1][6]==0))
c++;
if((now[1][8]==0)&&(now[2][8]==whose_turn)&&(now[3][8]==0))
c++;
if((now[8][1]==0)&&(now[8][2]==whose_turn)&&(now[8][3]==0))
c++;
if((now[8][1]==0)&&(now[7][1]==whose_turn)&&(now[6][1]==0))
c++;
if((now[8][8]==0)&&(now[8][7]==whose_turn)&&(now[8][6]==0))
c++;
if((now[8][8]==0)&&(now[7][8]==whose_turn)&&(now[6][8]==0))
c++;
int cc=0;//下子在C点需谨慎
if((now[1][1]==0)&&(now[1][2]==whose_turn)&&(now[1][3]==3-whose_turn))
cc++;
if((now[1][1]==0)&&(now[2][1]==whose_turn)&&(now[3][1]==3-whose_turn))
cc++;
if((now[1][8]==0)&&(now[1][7]==whose_turn)&&(now[1][6]==3-whose_turn))
cc++;
if((now[1][8]==0)&&(now[2][8]==whose_turn)&&(now[3][8]==3-whose_turn))
cc++;
if((now[8][1]==0)&&(now[8][2]==whose_turn)&&(now[8][3]==3-whose_turn))
cc++;
if((now[8][1]==0)&&(now[7][1]==whose_turn)&&(now[6][1]==3-whose_turn))
cc++;
if((now[8][8]==0)&&(now[8][7]==whose_turn)&&(now[8][6]==3-whose_turn))
cc++;
if((now[8][8]==0)&&(now[7][8]==whose_turn)&&(now[6][8]==3-whose_turn))
cc++;
/*
//开局要居中
if(now[4][4]==whose_turn)
num+=1;
if(now[4][5]==whose_turn)
num+=1;
if(now[5][4]==whose_turn)
num+=1;
if(now[5][5]==whose_turn)
num+=1;
*/
//综合评价局面
int val_table[65] = {0,70,110,125,133,137,139,141,142};
int val = 143;
for(int cnt = 9; cnt < 65; ++cnt ){
val_table[cnt] = val;
++val;
}
num=num-edge_disc(whose_turn)+edge_disc(3-whose_turn);
num=num+val_table[special_pattern_stable_discs(whose_turn)]-val_table[special_pattern_stable_discs(3-whose_turn)];
//num=num+special_pattern_balanced_edge(whose_turn,whose_turn)-special_pattern_balanced_edge(3-whose_turn,whose_turn);
if(already_lost==1)
num=num-4500;
//num=num-70*x-55*c-210*cc;
num=num-7*x-5*c-15*cc;
return num;
}
int pre_score_board(short whose_turn,int i,int j)//预估
{
int edge_disc(short whose_turn);
int num=0;
num=num-edge_disc(whose_turn)+edge_disc(3-whose_turn);
if(i==1)
{
if((j==1)||(j==8))
num+=10;
else if((j==2)||(j==7))
num-=3;
}
else if(i==2)
{
if((j==1)||(j==8))
num-=3;
else if((j==2)||(j==7))
num-=7;
}
else if(i==7)
{
if((j==1)||(j==8))
num-=3;
else if((j==2)||(j==7))
num-=7;
}
else if(i==8)
{
if((j==1)||(j==8))
num+=10;
else if((j==2)||(j==7))
num-=3;
}
return num;
}
int edge_disc(short whose_turn)//散度
{
int i=0,j=0,sum=0;
bool edge[9][9]={0};
for(i=2;i<8;i++)
{
for(j=2;j<8;j++)
{
if(now[i][j]==whose_turn)
{
if(now[i-1][j]==0)
edge[i-1][j]=1;
if(now[i+1][j]==0)
edge[i+1][j]=1;
if(now[i][j+1]==0)
edge[i][j+1]=1;
if(now[i][j-1]==0)
edge[i][j-1]=1;
if(now[i-1][j-1]==0)
edge[i-1][j-1]=1;
if(now[i-1][j+1]==0)
edge[i-1][j+1]=1;
if(now[i+1][j-1]==0)
edge[i+1][j-1]=1;
if(now[i+1][j+1]==0)
edge[i+1][j+1]=1;
}
}
}
for(i=1;i<9;i++)
{
for(j=1;j<9;j++)
{
if(edge[i][j]==1)
sum++;
}
}
return sum;
}
int special_pattern_stable_discs(short whose_turn)//定式1---稳定子判断(只是部分常见形,还差直稳定子)
{
int i=0,j=0,sum=0;
bool special[9][9]={0};
//以下计算边角稳定子
if(now[1][1]==whose_turn)
{
special[1][1]=1;
for(i=2;i<8;i++)
{
if(now[i][1]==whose_turn)
special[i][1]=1;
else
break;
}
for(i=2;i<8;i++)
{
if(now[1][i]==whose_turn)
special[1][i]=1;
else
break;
}
}
if(now[8][1]==whose_turn)
{
special[8][1]=1;
for(i=2;i<8;i++)
{
if(now[8][i]==whose_turn)
special[8][i]=1;
else
break;
}
for(i=7;i>1;i--)
{
if(now[i][1]==whose_turn)
special[i][1]=1;
else
break;
}
}
if(now[1][8]==whose_turn)
{
special[1][8]=1;
for(i=2;i<8;i++)
{
if(now[i][8]==whose_turn)
special[i][8]=1;
else
break;
}
for(i=7;i>1;i--)
{
if(now[1][i]==whose_turn)
special[1][i]=1;
else
break;
}
}
if(now[8][8]==whose_turn)
{
special[8][8]=1;
for(i=7;i>1;i--)
{
if(now[8][i]==whose_turn)
special[8][i]=1;
else
break;
}
for(i=7;i>1;i--)
{
if(now[i][8]==whose_turn)
special[i][8]=1;
else
break;
}
}
//以下计算中腹稳定子
int m=0;
bool flag=0;
for(m=1;m<=8;m++)
{
flag=1;
for(i=1;i<=m;i++)
{
if(now[i][m-i+1]!=whose_turn)
{
flag=0;
break;
}
}
if(flag==1)
{
for(i=1;i<=m;i++)
special[i][m-i+1]=1;
}
else
break;
}
for(m=1;m<=8;m++)
{
flag=1;
for(i=1;i<=m;i++)
{
if(now[i][8-m+i]!=whose_turn)
{
flag=0;
break;
}
}
if(flag==1)
{
for(i=1;i<=m;i++)
special[i][8-m+i]=1;
}
else
break;
}
for(m=1;m<=8;m++)
{
flag=1;
for(i=9-m;i<=8;i++)
{
if(now[i][i+m-8]!=whose_turn)
{
flag=0;
break;
}
}
if(flag==1)
{
for(i=9-m;i<=8;i++)
special[i][i+m-8]=1;
}
else
break;
}
for(m=1;m<=8;m++)
{
flag=1;
for(i=9-m;i<=8;i++)
{
if(now[i][17-m-i]!=whose_turn)
{
flag=0;
break;
}
}
if(flag==1)
{
for(i=9-m;i<=8;i++)
special[i][17-m-i]=1;
}
else
break;
}
//以下计算边角插入稳定子
flag=1;
for(i=1;i<=8;i++)
{
if(now[1][i]==0)
{
flag=0;
break;
}
}
if(flag==1)
{
for(i=1;i<=8;i++)
{
if(now[1][i]==whose_turn)
special[1][i]=1;
}
}
flag=1;
for(i=1;i<=8;i++)
{
if(now[8][i]==0)
{
flag=0;
break;
}
}
if(flag==1)
{
for(i=1;i<=8;i++)
{
if(now[8][i]==whose_turn)
special[8][i]=1;
}
}
flag=1;
for(i=1;i<=8;i++)
{
if(now[i][1]==0)
{
flag=0;
break;
}
}
if(flag==1)
{
for(i=1;i<=8;i++)
{
if(now[i][1]==whose_turn)
special[i][1]=1;
}
}
flag=1;
for(i=1;i<=8;i++)
{
if(now[i][8]==0)
{
flag=0;
break;
}
}
if(flag==1)
{
for(i=1;i<=8;i++)
{
if(now[i][8]==whose_turn)
special[i][8]=1;
}
}
//以下计数
for(i=1;i<9;i++)
{
for(j=1;j<9;j++)
{
if(special[i][j]==1)
sum++;
}
}
return sum;
}
int special_pattern_balanced_edge(short whose_score,short whose_turn)//定式2---中局中的边定式
{
int a=0,b=0,c=0,d=0,sum=0;
if((now[1][1]==0)&&(now[1][8]==0))
{
if(now[1][2]==whose_score)
a+=1;
else if(now[1][2]==3-whose_score)
a+=2;
if(now[1][3]==whose_score)
a+=3;
else if(now[1][3]==3-whose_score)
a+=6;
if(now[1][4]==whose_score)
a+=9;
else if(now[1][4]==3-whose_score)
a+=18;
if(now[1][5]==whose_score)
a+=27;
else if(now[1][5]==3-whose_score)
a+=54;
if(now[1][6]==whose_score)
a+=81;
else if(now[1][6]==3-whose_score)
a+=162;
if(now[1][7]==whose_score)
a+=243;
else if(now[1][7]==3-whose_score)
a+=486;
}
if((now[8][1]==0)&&(now[8][8]==0))
{
if(now[8][2]==whose_score)
b+=1;
else if(now[8][2]==3-whose_score)
b+=2;
if(now[8][3]==whose_score)
b+=3;
else if(now[8][3]==3-whose_score)
b+=6;
if(now[8][4]==whose_score)
b+=9;
else if(now[8][4]==3-whose_score)
b+=18;
if(now[8][5]==whose_score)
b+=27;
else if(now[8][5]==3-whose_score)
b+=54;
if(now[8][6]==whose_score)
b+=81;
else if(now[8][6]==3-whose_score)
b+=162;
if(now[8][7]==whose_score)
b+=243;
else if(now[8][7]==3-whose_score)
b+=486;
}
if((now[1][1]==0)&&(now[8][1]==0))
{
if(now[2][1]==whose_score)
c+=1;
else if(now[2][1]==3-whose_score)
c+=2;
if(now[3][1]==whose_score)
c+=3;
else if(now[3][1]==3-whose_score)
c+=6;
if(now[4][1]==whose_score)
c+=9;
else if(now[4][1]==3-whose_score)
c+=18;
if(now[5][1]==whose_score)
c+=27;
else if(now[5][1]==3-whose_score)
c+=54;
if(now[6][1]==whose_score)
c+=81;
else if(now[6][1]==3-whose_score)
c+=162;
if(now[7][1]==whose_score)
c+=243;
else if(now[7][1]==3-whose_score)
c+=486;
}
if((now[1][8]==0)&&(now[8][8]==0))
{
if(now[2][8]==whose_score)
d+=1;
else if(now[2][8]==3-whose_score)
d+=2;
if(now[3][8]==whose_score)
d+=3;
else if(now[3][8]==3-whose_score)
d+=6;
if(now[4][8]==whose_score)
d+=9;
else if(now[4][8]==3-whose_score)
d+=18;
if(now[5][8]==whose_score)
d+=27;