基于C语言实现五子棋游戏完整实例代码

时间:2021-11-16 08:15:46

本文实例讲述了基于C语言实现五子棋游戏的方法,代码备有比较完整的注释,可以帮助读者更好的加以理解。

五子棋游戏代码如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
/*
 * 使用键盘的上下左右键移动棋盘,空格键表示下棋,ESC键退出程序
 */
#include <stdio.h>
#include <stdlib.h>
#include <bios.h>
#include <graphics.h>
#include<malloc.h>
/*
 * 对应键盘键的十六进制数字
 */
#define ESC 0x11b
#define UP 0x4800
#define DOWN 0x5000
#define LEFT 0x4b00
#define RIGHT 0x4d00
#define BLANK 0x3920
 
#define PLAYER1 1
#define PLAYER2 2
#define COMPUTER 2
#define LENGTH 15
#define SEARCH_DEEP 2
/*
 * 用来在函数can_expand()查找可以扩展的节点步长
 */
#define STEP 1
 
/************全局变量定义***************/
int       x1 = 240,
        y1 = 240,
        oldx = 240,
        oldy = 240;
int       key_mode;
int       key_net;
int       step_sum = 0;
int       chessman[LENGTH][LENGTH];
int       depth = 2; /* 搜索的深度 */
int       a = 0,
        b = 0;
int       flag_run;
int       win_flag = 0;
 
 
typedef struct five_chess *point;
struct five_chess {
  int       x;
  int       y;
  int       layer;
  double     value;
  double     score;
  int       chess[LENGTH][LENGTH];
  int       record[LENGTH][LENGTH];
} A;
 
int       stack_deep0 = 0;
point      stack_c[10];
point      close[600];
 
void
push(point s0)
{
  if (stack_deep0 < 10)
 stack_c[stack_deep0++] = s0;
}
 
point
top()
{
  if (stack_deep0 > 0)
 return stack_c[stack_deep0 - 1];
  /*else return 一个什么样的东西?*/
}
 
void
pop()
{
  if (stack_deep0 > 0)
 stack_deep0--;
}
 
int
is_empty()
{
  if (stack_deep0 != 0)
 return 1;
  else
 return 0;
}
 
 
 
/************函数的声明**************/
void      five();
void      show();
int       win_or_not(int x0, int y0, int who,
   int chessman[LENGTH][LENGTH], int a);
void      set_chessman();
void      print_result();
/************评价函数部分************/
double     score_row(int i, int j, int chessman[LENGTH][LENGTH]);
double     score_col(int i, int j, int chessman[LENGTH][LENGTH]);
double     score_diag_45(int i, int j, int chessman[LENGTH][LENGTH]);
double     score_diag_135(int i, int j, int chessman[LENGTH][LENGTH]);
double     total_score(int who_running, int chessman[LENGTH][LENGTH]);
double     score(int chessman[LENGTH][LENGTH]);
int       rowdt(int i, int j, int chessman[LENGTH][LENGTH]);
int       coldt(int i, int j, int chessman[LENGTH][LENGTH]);
int       diadt(int i, int j, int chessman[LENGTH][LENGTH]);
int       vdiadt(int i, int j, int chessman[LENGTH][LENGTH]);
int       can_expand(int i, int j, int chessman[LENGTH][LENGTH]);
void     copy(point s1, point s0);
 
int
POW(int s, int t)
{
  int       sum = s,
          i;
  if (t <= 0)
 return 1;
  for (i = 0; i < t; i++)
 sum *= sum;
  return sum;
 
}
 
 
/*
 * 定义computer先手
 */
point
expand(point s0)
{
  int       flag;
  int       i,
          j;
  point      new_chess = (point) malloc(sizeof(struct five_chess));
/*************************这里出错***********************************/
  for (i = 0; i < LENGTH; i++)
 for (j = 0; j < LENGTH; j++)
   new_chess->chess[i][j] = s0->chess[i][j];
 
  for (i = 0; i < LENGTH; i++)
 for (j = 0; j < LENGTH; j++)
   new_chess->record[i][j] = s0->chess[i][j];
 
/*************************这里出错***********************************/
  if (s0->layer % 2 == 0)
 flag = COMPUTER;
  else
 flag = PLAYER1;
 
 
  for (i = 0; i < LENGTH; i++)
 for (j = 0; j < LENGTH; j++) {
 
   if (s0->record[i][j])             /*如果有棋子*/
 continue;
   if (can_expand(i, j, s0->chess) == 0)  /*如果有棋子,而且沿水平,树直,左上—右下,右上—左下,四个方向可以扩展*/
 continue;
   s0->record[i][j] = flag;
   new_chess->chess[i][j] = flag;
   new_chess->layer = s0->layer + 1;
   new_chess->x = i;
   new_chess->y = j;
   new_chess->record[i][j] = flag;
   return new_chess;
 }
  /*
   * for(i=5;i<10;i++) for(j=5;j<10;j++){ if(s0->record[i][j]) continue;
   * if(can_expand(i,j,s0->chess)==0) continue; s0->record[i][j]=flag;
   * new_chess->x=i; new_chess->y=j; new_chess->record[i][j]=flag;
   * new_chess->layer=s0->layer+1; new_chess->chess[i][j]=flag ; return
   * new_chess; } for(i=2;i<12;i++) for(j=2;j<12;j++){
   * if(i>4&&i<10&&j>4&&j<10) continue; if(s0->record[i][j]) continue;
   * if(can_expand(i,j,s0->chess)==0) continue; s0->record[i][j]=flag;
   * new_chess->x=i; new_chess->y=j; new_chess->record[i][j]=flag;
   * new_chess->layer=s0->layer+1; new_chess->chess[i][j]=flag; return
   * new_chess;
   *
   * } for(i=0;i<LENGTH;i++) for(j=0;j<LENGTH;j++){
   * if(i>1&&i<12&&j>1&&j<12) continue; if(s0->record[i][j]) continue;
   * if(can_expand(i,j,s0->chess)==0) continue; s0->record[i][j]=flag;
   * new_chess->chess[i][j]=flag; new_chess->layer=s0->layer+1;
   * new_chess->x=i; new_chess->y=j; new_chess->record[i][j]=flag; return
   * new_chess; }
   */
  new_chess->layer = -1;
  return new_chess;
}
 
 
 
void
computer()
{
  int       i,
          j,
          k,
          num = 0;
  int       break_now = 0;
  int       break_then = 0;
  int       go_on = 0;
  point     s0 = NULL,
          s1 = NULL,
          s2 = NULL,
          max_chess = NULL;
  point     temps = NULL,
          s01;
  /*
   * 堆栈的初始化
   */
  stack_deep0 = 0;
  s0 = malloc(sizeof(struct five_chess));
  for (i = 0; i < 600; i++)              /*为什么是600*/
 close[i] = NULL;                 /*close是一个point 数组*/
  close[num++] = s0;
  for (i = 0; i < LENGTH; i++)
 for (j = 0; j < LENGTH; j++) {
   s0->chess[i][j] = chessman[i][j];
   s0->record[i][j] = chessman[i][j];
 }
  s0->layer = 0;
  s0->value = -3000000;
  s0->score = -3000000;
  push(s0);
  while (is_empty() != 0) {        /*看是栈否为空*/
 s01 = top();                  /*如果不是空*/
 s1 = expand(s01);             /*从栈顶开始展开*/
 close[num++] = s1;
 if (s1->layer == -1) {
   pop();
   continue;
 }
 go_on =
   win_or_not((s1->x + 1) * 30, (s1->y + 1) * 30, 2, s1->chess,
     1);
 if (go_on == 2) {
   a = (s1->x + 1) * 30;
   b = (s1->y + 1) * 30;
   break_then = 1;
   break;
 }
 go_on =
   win_or_not((s1->x + 1) * 30, (s1->y + 1) * 30, 1, s1->chess,
     1);
 if (go_on == 1) {
   a = (s1->x + 1) * 30;
   b = (s1->y + 1) * 30;
   break_then = 1;
   break;
 }
 s1->value = 30000;
 push(s1);
 while (1) {
   s1 = top();
   s2 = expand(s1);
   if (s2->layer == -1) {
 pop();
 if (s1->value > top()->value) {
   top()->value = s1->value;
   max_chess = s1;
 }
 free(s2);
 break;
   }/*end of if*/
   s2->score = score(s2->chess);
   temps = top();
   if (s2->score < temps->value)
 temps->value = s2->score;
   free(s2);
 }/*end of whiile(1) */
  }
  if (break_then == 0) {
 for (i = 0; i < LENGTH; i++) {
   for (j = 0; j < LENGTH; j++)
 if (max_chess->chess[i][j] != chessman[i][j]) {
   a = i * 30 + 30;
   b = j * 30 + 30;
   break_now = 1;
   break;
 }
   if (break_now == 1)
 break;
 }
  }
  for (i = 0; i < 600; i++) {
 if (close[i] == NULL)
   continue;
 free(close[i]);
  }
 
}
 
/**********************************************************/
void
main()
{
  int       key;
  int       play_with_who = 1;
 
  printf("1.Play with human\n2.Play with computer\nPlease choice: ");
  scanf("%d", &play_with_who);
 
  five();              /*显示棋盘*/
  show();
 
  if (play_with_who == 1) {   /*人与人玩*/
 while (1) {  /*设置人与人玩的界面*/
   settextstyle(0, 0, 2);
   if ((step_sum + 1) % 2) {
 setcolor(1);
 outtextxy(500, 180, "Player2");
 setcolor(4);
 outtextxy(500, 180, "Player1");
   } else {
 setcolor(1);
 outtextxy(500, 180, "Player1");
 setcolor(10);
 outtextxy(500, 180, "Player2");
   }
 
 
   if (bioskey(1))
 /*
  * 按了一次键盘那么就true,执行下面代码,这是bios。h
  */
   {
 key = bioskey(0);
 /*
  * 返回一个键盘值,如没有按键,则一直等待
  */
 switch (key) {
 case ESC:
   exit(0);
 case LEFT:
   if (x1 > 30) {
  x1 -= 30;
  show();   /*显示方框*/
   }
   break;
 case UP:
   if (y1 > 30) {
  y1 -= 30;
  show();
   }
   break;
 case RIGHT:
   if (x1 < 450) {
  x1 += 30;
  show();
   }
   break;
 case DOWN:
   if (y1 < 450) {
  y1 += 30;
  show();
   }
   break;
 case BLANK:         /*按下空格键后放置棋子*/
   {
  if (chessman[x1 / 30][y1 / 30])
    break;       /*如果当前位置有棋子,不能放置,退出*/
  step_sum++;    /*如果没有棋子,下一步谁走加1*/
  /*
  * P1 设置棋子
  */
  if (step_sum % 2) {
    setcolor(15);  /*画棋子*/
    setfillstyle(SOLID_FILL, 15); /* 封闭图形,进行实体填充*/
    circle(x1, y1, 10);      /*画圆*/
    floodfill(x1, y1, 15);    /*填充圆*/
    chessman[x1 / 30][y1 / 30] = PLAYER1;        /*设置棋盘状态*/
    win_flag = win_or_not(x1, y1, 1, chessman, 0);   /*分析游戏是否结束,谁胜谁败*/
    if (win_flag == 1)
  outtextxy(480, 240, "P1 Win");
    else if (win_flag == 3)
  outtextxy(480, 240, "DOGFALL");
    if (win_flag != 0) {           /*如果没人胜,游戏继续*/
  while (bioskey(1) == 0);
  closegraph();            /*what this mean?*/
    }
  } else { /* P2 设置棋子 */
 
    setcolor(12);           
    setfillstyle(SOLID_FILL, 12);
    circle(x1, y1, 10);
    floodfill(x1, y1, 12);
    chessman[x1 / 30][y1 / 30] = PLAYER2;
    win_flag = win_or_not(x1, y1, 2, chessman, 0);
    if (win_flag == 2)
  outtextxy(480, 240, "P2 Win");
    else if (win_flag == 3)
  outtextxy(480, 240, "DOGFALL");
    if (win_flag != 0) {
  while (bioskey(1) == 0);
  closegraph();
    }
  }
 
 
 
   }
   break;
 }
   }
 }
  } else {
 chessman[7][7] = COMPUTER;       /*人和电脑玩,电脑先走一步*/     
 setcolor(12);
 setfillstyle(SOLID_FILL, 12);
 circle(240, 240, 10);
 floodfill(240, 240, 12);
 flag_run = 0;          /*有什么用?*/
 step_sum++;          /*下一步谁走?*/
 while (1) {
   while (1) {
 if (flag_run == 1)
   break;
 if (bioskey(1)) {
   key = bioskey(0);
   /*
    * 返回一个键盘值,如没有按键,则一直等待
    */
   switch (key) {
 
   case ESC:
  exit(0);
   case LEFT:
  if (x1 > 30) {
    x1 -= 30;
    show();
  }
  break;
   case UP:
  if (y1 > 30) {
    y1 -= 30;
    show();
  }
  break;
   case RIGHT:
  if (x1 < 450) {
    x1 += 30;
    show();
  }
  break;
   case DOWN:
  if (y1 < 450) {
    y1 += 30;
    show();
  }
  break;
   case BLANK:
  {
    if (chessman[x1 / 30 - 1][y1 / 30 - 1])
  break;                             /*有棋子了不走*/
 
    setcolor(15);
    setfillstyle(SOLID_FILL, 15); /* 封闭图形,进行实体填充
     */
    circle(x1, y1, 10);
    floodfill(x1, y1, 15);               /*画棋子*/
    chessman[x1 / 30 - 1][y1 / 30 - 1] = PLAYER1;
 
    flag_run = 1;                 /*有什么用?*/
    step_sum++;                 /*下一步谁走*/
    win_flag = win_or_not(x1, y1, 1, chessman, 0);  /*谁胜谁负*/
    if (win_flag == 1)
  outtextxy(480, 240, "P1 Win");
    else if (win_flag == 3)
  outtextxy(480, 240, "DOGFALL");
    if (win_flag != 0) {
  while (bioskey(1) == 0);                 /*没有人胜则继续等待下棋*/
  closegraph();
    }
 
  }
   } /* switch */
 
 }
 
   }
   computer();                      /*调用智能体*/
   /*
   * a,b存放的是现在电脑准备下的位置
   * 返回一个a,b的结构体不是更好,用全局变量不爽啊
   * struct {
   *     int a;
   *     int b;
   * }
   */
 
   setcolor(12);
   setfillstyle(SOLID_FILL, 12);
   circle(a, b, 10);
   floodfill(a, b, 12);
   chessman[a / 30 - 1][b / 30 - 1] = COMPUTER;
   flag_run = 0;
   step_sum++;
   win_flag = win_or_not(a, b, 2, chessman, 0);
   if (win_flag == 2)
 outtextxy(480, 240, "ComputerWin");
   else if (win_flag == 3)
 outtextxy(480, 240, "DOGFALL");
   if (win_flag != 0) {
 while (bioskey(1) == 0);
 closegraph();
   }
 
 
 
 
 
 }
  }
 
}
void
five()
{
  int       i,
          j;
  /*
   * 画棋盘的过程
   */
  int       gdriver = DETECT,
          gmode;
  registerbgidriver(EGAVGA_driver);
  initgraph(&gdriver, &gmode, " ");
  /*
   * 对显示适配器进行配置
   */
  setbkcolor(1);
 
  for (i = 0; i < 30; i++) {
 setcolor((i >= 2) ? 9 : i);
 rectangle(i, i, 479 - i, 479 - i); /* 画矩形边框 */
  }
  /*
   * 画棋盘
   */
  for (i = 1; i < 14; i++)
 for (j = 1; j < 14; j++) {
   setcolor(14);
   line(30 + 30 * i, 30, 30 + 30 * i, 449);
   line(30, 30 + 30 * i, 449, 30 + 30 * i);
 }
  /*
   * 画整个图的边框
   */
  for (i = 0; i < 15; i++) {
 setcolor(i);
 rectangle(i, i, 640 - i, 480 - i);
 line(480 - i, 15, 480 - i, 465);
  }
  /*
   * 输出屏幕右侧的信息
   */
  setcolor(4);
  settextstyle(0, 0, 2);
  outtextxy(500, 45, "GOBANG");
  setcolor(10);
  settextstyle(0, 0, 1);
  outtextxy(500, 90, "Designed By");
  outtextxy(514, 118, "Ye Binbin");
  outtextxy(480, 140, "from class A of CS");
 
 
}
 
/*
 * 移动光标
 */
void
show()
{
  setcolor(1);
 
  if (oldx < 450) {
 if (oldy > 30)
   line(oldx + 7, oldy - 15, oldx + 15, oldy - 15);
 if (oldy > 30)
   line(oldx + 15, oldy - 15, oldx + 15, oldy - 7);
 if (oldy < 450)
   line(oldx + 15, oldy + 7, oldx + 15, oldy + 15);
 if (oldy < 450)
   line(oldx + 15, oldy + 15, oldx + 7, oldy + 15);
  }
  if (oldx > 30) {
 if (oldy < 450)
   line(oldx - 7, oldy + 15, oldx - 15, oldy + 15);
 if (oldy < 450)
   line(oldx - 15, oldy + 15, oldx - 15, oldy + 7);
 if (oldy > 30)
   line(oldx - 15, oldy - 7, oldx - 15, oldy - 15);
 if (oldy > 30)
   line(oldx - 15, oldy - 15, oldx - 7, oldy - 15);
  }
  setcolor(12);
  if (x1 < 450) {
 if (y1 > 30)
   line(x1 + 7, y1 - 15, x1 + 15, y1 - 15);
 if (y1 > 30)
   line(x1 + 15, y1 - 15, x1 + 15, y1 - 7);
 if (y1 < 450)
   line(x1 + 15, y1 + 7, x1 + 15, y1 + 15);
 if (y1 < 450)
   line(x1 + 15, y1 + 15, x1 + 7, y1 + 15);
  }
 
  if (x1 > 30) {
 if (y1 < 450)
   line(x1 - 7, y1 + 15, x1 - 15, y1 + 15);
 if (y1 < 450)
   line(x1 - 15, y1 + 15, x1 - 15, y1 + 7);
 if (y1 > 30)
   line(x1 - 15, y1 - 7, x1 - 15, y1 - 15);
 if (y1 > 30)
   line(x1 - 15, y1 - 15, x1 - 7, y1 - 15);
  }
  oldx = x1;
  oldy = y1;
 
}
 
 
 
void
set_chessman()
{
  /*
   * 棋子有三种状态,0是未初始状态,1是控制方棋子,2是对方棋子
   */
  int       i,
          j;
  for (i = 0; i < 15; i++)
 for (j = 0; j < 15; j++)
   chessman[i][j] = 0;
}
 
 
/*
 * 0表示没有赢,1表示p1胜利,2表示p2胜利,3表示平局
 */
int
win_or_not(int x0, int y0, int who, int chessman[LENGTH][LENGTH], int a)
{
  int       i = x0 / 30 - 1,
          j = y0 / 30 - 1;
  int       who_run = who;
  int       line_sum = -1;
  int       tmp_i = i,
          tmp_j = j;
  int       c;
  if (a == 1) {
 /*
 * 测试第一层扩展是否满足赢的条件
 */
 c = chessman[i][j];
 chessman[i][j] = who_run;
  }
 
 
  while (1) {  /* 查找共行的棋子是否连接了五个 */
 while (tmp_i >= 0 && line_sum != 4) {
   if (chessman[tmp_i--][j] == who_run)
 line_sum++;
   else
 break;
 }
 if (line_sum == 4)
   line_sum++;
 tmp_i = i;
 while (tmp_i <= 15 && line_sum != 5) {
   if (chessman[tmp_i++][j] == who_run)
 line_sum++;
   else
 break;
 }
 if (line_sum == 5) {
   if (a == 1)
 chessman[i][j] = c;
   return who_run;
 }
 line_sum = -1;
 tmp_i = i;
 break;
 
  }
  while (1) {  /* 查找共列的棋子是否连接了五个 */
 while (tmp_j >= 0 && line_sum != 4) {
   if (chessman[i][tmp_j--] == who_run)
 line_sum++;
   else
 break;
 }
 if (line_sum == 4)
   line_sum++;
 tmp_j = j;
 while (tmp_j <= 15 && line_sum != 5) {
   if (chessman[i][tmp_j++] == who_run)
 line_sum++;
   else
 break;
 }
 if (line_sum == 5) {
   if (a == 1)
 chessman[i][j] = c;
   return who_run;
 }
 line_sum = -1;
 tmp_j = j;
 break;
 
  }
  while (1) {  /* 查找上对角线上是否连接了五个 */
 while (line_sum != 4 && tmp_i <= 15 && tmp_j >= 0) {
   if (chessman[tmp_i++][tmp_j--] == who_run)
 line_sum++;
   else
 break;
 }
 tmp_i = i;
 tmp_j = j;
 if (line_sum == 4)
   line_sum++;
 while (line_sum != 5 && tmp_i >= 0 && tmp_j <= 15) {
   if (chessman[tmp_i--][tmp_j++] == who_run)
 line_sum++;
   else
 break;
 }
 if (line_sum == 5) {
   if (a == 1)
 chessman[i][j] = c;
   return who_run;
 }
 tmp_i = i;
 tmp_j = j;
 line_sum = -1;
 break;
  }
  while (1) {  /* 查找下对角线上是否连接了五个 */
 while (line_sum != 4 && tmp_i >= 0 && tmp_j >= 0) {
   if (chessman[tmp_i--][tmp_j--] == who_run)
 line_sum++;
   else
 break;
 }
 tmp_i = i;
 tmp_j = j;
 if (line_sum == 4)
   line_sum++;
 while (line_sum != 5 && tmp_i <= 15 && tmp_j <= 15) {
   if (chessman[tmp_i++][tmp_j++] == who_run)
 line_sum++;
   else
 break;
 }
 if (line_sum == 5) {
   if (a == 1)
 chessman[i][j] = c;
   return who_run;
 }
 break;
  }
  if (step_sum == 225) {
 if (a == 1)
   chessman[i][j] = c;
 return 3;
  }
  if (a == 1)
 chessman[i][j] = c;
  return 0;
 
}
 
 
 
double
score_row(int i, int j, int chessman[LENGTH][LENGTH])
{
  int       sum_chessmen = 0;
  double     score = 0;
  int       mid_j;
  int       who_running = chessman[i][j];
  if (j == LENGTH) {
 while (chessman[i][j] == who_running) {
   j--;
   sum_chessmen++;
 }
 if (sum_chessmen >= 5)
   score = 200000;
 else {
   if (chessman[i][j] == 0) /* 没有下子,活的情况 */
 score = 2000 / POW(10, 4 - sum_chessmen);
   else
 score = 0; /* 死的情况 */
 }
  } else {
 while (chessman[i][j] == who_running && j != LENGTH) {
   j++;
   sum_chessmen++;
 }
 mid_j = j;
 j = j - sum_chessmen - 1;
 while (chessman[i][j] == who_running && j != -1) {
   j--;
   sum_chessmen++;
 }
 if (j >= 0 && mid_j < LENGTH) {
   if (chessman[i][j] == 0 && chessman[i][mid_j] == 0)
 score = 18000 / POW(50, 4 - sum_chessmen);
   else if ((chessman[i][j] != 0 && chessman[i][mid_j] == 0)
    || (chessman[i][j] == 0 && chessman[i][mid_j] != 0))
 score = 2000 / POW(10, 4 - sum_chessmen);
   else
 score = 0;
 }
 if (j < 0 && mid_j < LENGTH) {
   if (chessman[i][mid_j] == 0)
 score = 2000 / POW(10, 4 - sum_chessmen);
   else
 score = 0;
 }
 if (j >= 0 && mid_j >= LENGTH) {
   if (chessman[i][j] == 0)
 score = 2000 / POW(10, 4 - sum_chessmen);
   else
 score = 0;
 }
 if (j < 0 && mid_j >= LENGTH)
   score = 0;
  }
  return score;
}
 
double
score_col(int i, int j, int chessman[LENGTH][LENGTH])
{
  int       sum_chessmen = 0,
          mid_i;
  double     score = 0;
  int       who_running = chessman[i][j];
  if (i == LENGTH) {
 while (chessman[i][j] == who_running) {
   i--;
   sum_chessmen++;
 }
 if (sum_chessmen >= 5)
   score = 200000;
 if (chessman[i][j] == 0)
   score = 2000 / POW(10, 4 - sum_chessmen);
 else
   score = 0;
  } else {
 while (chessman[i][j] == who_running) {
   i++;
   sum_chessmen++;
 }
 mid_i = i;
 if (i == LENGTH || chessman[i][j] != who_running) {
   i = i - sum_chessmen;
   while (chessman[i - 1][j] == who_running) {
 i--;
 sum_chessmen++;
   }
   if (i >= 0) {
 if (chessman[i][j] == 0 && chessman[mid_i][j] == 0)
   score = 18000 / POW(50, 4 - sum_chessmen);
 else if ((chessman[i][j] != 0 && chessman[mid_i][j]) == 0
  || (chessman[i][j] == 0
    && chessman[mid_i][j] != 0))
   score = 2000 / POW(10, 4 - sum_chessmen);
 else
   score = 0;
   }
   if (i < 0 && mid_i < LENGTH) {
 if (chessman[mid_i][j] == 0)
   score = 2000 / POW(10, 4 - sum_chessmen);
 else
   score = 0;
   }
   if (i < 0 && mid_i < LENGTH) {
 if (chessman[mid_i][j] == 0)
   score = 2000 / POW(10, 4 - sum_chessmen);
 else
   score = 0;
   }
   if (i >= 0 && mid_i >= LENGTH) {
 if (chessman[i][j] == 0)
   score = 2000 / POW(10, 4 - sum_chessmen);
 else
   score = 0;
   }
 }
  }
  return score;
}
 
double
score_diag_45(int i, int j, int chessman[LENGTH][LENGTH])
{
  int       sum_chessmen = 0;
  double     score = 0;
  int       mid_i,
          mid_j;
  int       who_running = chessman[i][j];
  if (i == LENGTH || j == LENGTH) {
 while (chessman[i][j] == who_running && i > 1 && j > 1) {
   i--;
   j--;
   sum_chessmen++;
 }
 if (sum_chessmen >= 5)
   score = 200000;
 else {
   if (chessman[i][j] == 0)
 score = 2000 / POW(10, 4 - sum_chessmen);
   else
 score = 0;
 }
  } else {
 while (chessman[i][j] == who_running && i <= LENGTH && j <= LENGTH) {
   i++;
   j++;
   sum_chessmen++;
 }
 mid_i = i;
 mid_j = j;
 i = i - sum_chessmen;
 j = j - sum_chessmen;
 while (chessman[i - 1][j - 1] == who_running) {
   i--;
   j--;
   sum_chessmen++;
 }
 if (sum_chessmen >= 5)
   score = 200000;
 if (i >= 0 && j >= 0 && mid_i < LENGTH && mid_j < LENGTH) {
   if (chessman[mid_i][mid_j] == 0 && chessman[i][j] == 0)
 score = 18000 / POW(50, 4 - sum_chessmen);
   else if ((chessman[mid_i][mid_j] == 0 && chessman[i][j] != 0)
    || (chessman[mid_i][mid_j] != 0
  && chessman[i][j] == 0))
 score = 2000 / POW(10, 4 - sum_chessmen);
   else
 score = 0;
 } else {
   if (i >= 0 && j >= 0) {
 if (chessman[i][j] == 0)
   score = 2000 / POW(10, 4 - sum_chessmen);
 else
   score = 0;
   } else if (mid_i < LENGTH && mid_j < LENGTH) {
 if (chessman[mid_i][mid_j] == 0)
   score = 2000 / POW(10, 4 - sum_chessmen);
 else
   score = 0;
   } else
 score = 0;
 }
  }
  return score;
}
 
double
score_diag_135(int i, int j, int chessman[LENGTH][LENGTH])
{
  int       sum_chessmen = 0;
  double     score = 0;
  int       mid_i,
          mid_j;
  int       who_running = chessman[i][j];
  while (chessman[i][j] == who_running && j != -1 && i < LENGTH) {
 i++;
 j--;
 sum_chessmen++;
  }
  mid_i = i;
  mid_j = j;
  j += sum_chessmen;
  i -= sum_chessmen;
  j++;
  i--;
  while (chessman[i][j] == who_running && j != LENGTH) {
 i--;
 j++;
 sum_chessmen++;
  }
  if (sum_chessmen >= 5)
 score = 200000;
  else {
 if (i >= 0 && j < LENGTH && mid_j >= 0 && mid_i < LENGTH) {
   if (chessman[i][j] == 0 && chessman[mid_i][mid_j] == 0)
 score = 18000 / POW(50, 4 - sum_chessmen);
   else {
 if ((chessman[i][j] == 0 && chessman[mid_i][mid_j] != 0)
   || (chessman[i][j] != 0
  && chessman[mid_i][mid_j] == 0))
   score = 2000 / POW(10, 4 - sum_chessmen);
 else
   score = 0;
   }
 } else {
   if (i >= 0 && j < LENGTH) {
 if (chessman[i][j] == 0)
   score = 2000 / POW(10, 4 - sum_chessmen);
 else
   score = 0;
   }
   if (mid_j >= 0 && mid_i < LENGTH) {
 if (chessman[mid_i][mid_j] == 0)
   score = 2000 / POW(10, 4 - sum_chessmen);
 else
   score = 0;
   }
 }
  }
  return score;
}
 
double
total_score(int who_running, int chessman[LENGTH][LENGTH])
{
  /*
   * 统计出在该点上的得分,who_running=1表示人的棋子,2为电脑的棋子
   */
  int       i,
          j;
  double     score = 0;
  for (i = 0; i < LENGTH; i++)
 for (j = 0; j < LENGTH; j++) {
   if (chessman[i][j] == who_running) {
 score += score_row(i, j, chessman);
 score += score_col(i, j, chessman);
 score += score_diag_45(i, j, chessman);
 score += score_diag_135(i, j, chessman);
   }
 }
  return score;
}
 
double
score(int chessman[LENGTH][LENGTH])
{
  /*
   * 计算最终的得分数,分别考虑了在这个位置放对方棋子跟自己棋子的综合
   */
  double     sum1,
            sum2;
  sum1 = total_score(COMPUTER, chessman);
  sum2 = total_score(PLAYER1, chessman);
  return sum1 - sum2;
}
 
/*
 * 扩展-----剪枝过程
 */
 
int
rowdt(int i, int j, int chessman[LENGTH][LENGTH])  /*在树直方向*/
{
  int       k;
  int       midjl = j - STEP,         /*当前棋子的上方*/
          midjr = j + STEP + 1;     /*当前棋子的下方棋子的下方??????*/
  if (midjl < 0)                  
 midjl = 0;         
  if (midjr > LENGTH)
 midjr = LENGTH;
  for (k = midjl; k < midjr; k++)       /**/
 if (chessman[i][k] != 0)         /*如果有棋子*/
   return 1;
  return 0;
}
 
int
coldt(int i, int j, int chessman[LENGTH][LENGTH])     /*水平方向*/
{
  int       k;
  int       midil = i + STEP + 1,         /*当前的右边棋子的右一个*/
          midiu = i - STEP;            /*当前棋子的左一个*/
  if (midiu < 0)
 midiu = 0;
  if (midil > LENGTH)
 midil = LENGTH;
  for (k = midiu; k < midil; k++)
 if (chessman[k][j] != 0)
   return 1;
  return 0;
}
 
int
diadt(int i, int j, int chessman[LENGTH][LENGTH])   /*右上到左下方向*/
{
  int       k,
          midi,
          midj;
  midi = i;
  midj = j;
  for (k = 0; k < STEP; k++) {
 midi++;
 midj--;
 if (midj < 0 || midi >= LENGTH)
   break;
 if (chessman[midi][midj] != 0)
   return 1;
  }
  for (k = 0; k < STEP; k++) {
 i--;
 j++;
 if (i < 0 || j >= LENGTH)
   break;
 if (chessman[i][j] != 0)
   return 1;
  }
  return 0;
}
 
int
vdiadt(int i, int j, int chessman[LENGTH][LENGTH])  /*左上到右下方向*/
{
  int       k,
          midi,
          midj;
  midi = i;
  midj = j;
  for (k = 0; k < STEP; k++) {
 midi--;
 midj--;
 if (midi < 0 || midj < 0)
   break;
 if (chessman[midi][midj] != 0)
   return 1;
  }
  for (k = 0; k < STEP; k++) {
 i++;
 j++;
 if (j >= LENGTH || i >= LENGTH)
   break;
 if (chessman[i][j] != 0)
   return 1;
  }
  return 0;
}
 
 
int
can_expand(int i, int j, int chessman[LENGTH][LENGTH])
{
  if (rowdt(i, j, chessman))
 return 1;
  if (coldt(i, j, chessman))
 return 1;
  if (diadt(i, j, chessman))
 return 1;
  if (vdiadt(i, j, chessman))
 return 1;
  /*
   * 如果不能扩展,返回0
   */
  return 0;
}
 
/************************************************************/