File: fuseki.c

package info (click to toggle)
gnugo 2.4-2
  • links: PTS
  • area: main
  • in suites: potato
  • size: 1,816 kB
  • ctags: 1,828
  • sloc: ansic: 22,091; tcl: 401; sh: 376; makefile: 202
file content (1721 lines) | stat: -rw-r--r-- 44,111 bytes parent folder | download
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
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\
 * This is GNU GO, a Go program. Contact gnugo@gnu.org, or see   *
 * http://www.gnu.org/software/gnugo/ for more information.      *
 *                                                               *
 * Copyright 1999 by the Free Software Foundation.               *
 *                                                               *
 * This program is free software; you can redistribute it and/or *
 * modify it under the terms of the GNU General Public License   *
 * as published by the Free Software Foundation - version 2.     *
 *                                                               *
 * This program is distributed in the hope that it will be       *
 * useful, but WITHOUT ANY WARRANTY; without even the implied    *
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR       *
 * PURPOSE.  See the GNU General Public License in file COPYING  *
 * for more details.                                             *
 *                                                               *
 * You should have received a copy of the GNU General Public     *
 * License along with this program; if not, write to the Free    *
 * Software Foundation, Inc., 59 Temple Place - Suite 330,       *
 * Boston, MA 02111, USA                                         *
\* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */




#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

#include "liberty.h"

#define UPPER_LEFT  1
#define UPPER_RIGHT 2
#define LOWER_LEFT  4
#define LOWER_RIGHT 8

#define V_FIRST  870		/* base value for first corner stone */
#define V_ENCLOS 840		/* base value for enclosure          */
#define V_KAKARI 825		/* base value for approach           */
#define V_OBA    810		/* base value for side big move      */


/* constants for easier tunning */
static const int no_stone = 0;	/* no stone in that corner */
static const int my_open = 1;	/* some known first corner move, not 4-4 or 3-3 */
static const int you_open = 2;
static const int my_hoshi = 3;	/* 4-4 or 3-3 */
static const int you_hoshi = 4;
static const int my_one = 5;	/* some unknown first corner move */
static const int you_one = 6;
/* 2 stones */
static const int my_shimari = 7;
static const int you_shimari = 8;
static const int my_kakari = 9;
static const int you_kakari = 10;
static const int my_gray = 11;
static const int you_gray = 12;
static const int my_2 = 27;
static const int you_2 = 28;
/* 3 stones */
static const int my_3 = 13;
static const int you_3 = 14;
static const int my_gray_shimari = 15;
static const int you_gray_shimari = 16;
static const int my_pincer = 17;
static const int you_pincer = 18;
static const int my_3_gray = 19;
static const int you_3_gray = 20;
/* 4 stones */
static const int my_4 = 21;
static const int you_4 = 22;
static const int my_4_gray = 23;
static const int you_4_gray = 24;
static const int gray_4 = 25;
/* 5 stones and more */
static const int played_5 = 26;


static int corner_done, fuseki_ended;
static int firstmove = 1;

typedef struct {
  int nb_stone;
  int color;
  int closed;
  int kado;
  int coord[4];
} corner_status;

static corner_status angle[4];	/*initialized here to 0 */
static corner_status short_angle[4];	/* initialized here to 0 */

/*****************************************************************/
static int where(int i);
static int right(int i);
static void relative(int *i, int *j, int corner);
static int composite(int i, int j, int corner);
static void decompose(int i, int corner, int *m, int *n);
static void absolute(int corner, int *m, int *n);
static void analyze_corner(corner_status * cs, int corner, int color);
static int readcorner(int color, int corner);
static int play_empty_corner(int *m, int *n, int *val, int *equal_moves, int color);
static int play_somewhere(int *m, int *n, int *val, int *equal_moves, int color);
static int choose_second(corner_status * ang, int corner, int *m, int *n, int color, int sym);

static int openregion(int i1, int i2, int j1, int j2);
static void absolute_coord_old(int corner, int *m, int *n);
static void analyze_sides(int corner, int *table, int color);
static void choose_corner_move(int corner, int *m, int *n);
static void maybe_oba(int *bestval, int *besti, int *bestj, int ti, int tj, int color);
static int search_oba(int *i, int *j, int color);
static int search_small_oba(int *i, int *j, int color);
static void maybe_extend(int *bestval, int *besti, int *bestj, int ti, int tj, int color);
static int search_extend(int *i, int *j, int color);
static int known_stone(int km);
static int known_stone_2(int km);
static int area_color_c(int compos, int corner);
int fuseki(int *i, int *j, int *val, int *equal_moves, int color);
static int fuseki_19(int *i, int *j, int *val, int *equal_moves, int color);
/*****************************************************************/

/* The corner moves. */

static int corners[][2] =
{
  {3,3},
  {3,4},
  {4,3},
  {4,4},
  {5,3},
  {3,5},
  {5,4},
  {4,5},
};

/* Relative weights for different corner moves at different board
   sizes. */

/* up to 11x11 */
static int small_board[] =
{
  50,       /* 3-3 */
  18,       /* 3-4 */
  17,       /* 4-3 */
  15,       /* 4-4 */
  0,        /* 5-3 */
  0,        /* 3-5 */
  0,        /* 5-4 */
  0,        /* 4-5 */
};

/* 12x12 to 15x15 */
static int medium_board[] =
{
  30,       /* 3-3 */
  20,       /* 3-4 */
  20,       /* 4-3 */
  22,       /* 4-4 */
  2,        /* 5-3 */
  2,        /* 3-5 */
  2,        /* 5-4 */
  2,        /* 4-5 */
};

/* 16x16 and larger */
static int large_board[] =
{
  15,       /* 3-3 */
  15,       /* 3-4 */
  15,       /* 4-3 */
  35,       /* 4-4 */
  5,        /* 5-3 */
  5,        /* 3-5 */
  5,        /* 5-4 */
  5,        /* 4-5 */
};


static int known_stone(int km)
{
  switch (km) {
  case 304:
  case 305:
  case 403:
  case 405:
  case 503:
  case 504:
  case 303:
  case 404:
    return 1;
  }
  return 0;
}

static int known_stone_2(int km)
{
  switch (km) {
  case 304:
  case 305:
  case 403:
  case 405:
  case 503:
  case 504:
  case 303:
  case 404:
  case 603:
  case 604:
  case 306:
  case 406:
    return 1;
  }
  return 0;
}


static int where(int i)
{
  switch (i) {
  case UPPER_LEFT:
    return 0;
  case UPPER_RIGHT:
    return 1;
  case LOWER_LEFT:
    return 2;
  case LOWER_RIGHT:
    return 3;
  }
  ASSERT((i == UPPER_LEFT || i == UPPER_RIGHT || i == LOWER_LEFT || i == LOWER_RIGHT), 1, 1);
  return -1;
}

static int right(int i)
{
  switch (i) {
  case UPPER_LEFT:
    return LOWER_LEFT;
  case UPPER_RIGHT:
    return UPPER_LEFT;
  case LOWER_LEFT:
    return LOWER_RIGHT;
  case LOWER_RIGHT:
    return UPPER_RIGHT;
  }
  ASSERT((i == UPPER_LEFT || i == UPPER_RIGHT || i == LOWER_LEFT || i == LOWER_RIGHT), 1, 1);
  return -1;
}



/* check if region from i1, j1 to i2, j2 is open */

static int openregion(int i1, int i2, int j1, int j2)
{
  int x, y;

  if (i1 > i2)
    return openregion(i2, i1, j1, j2);
  if (j1 > j2)
    return openregion(i1, i2, j2, j1);
  for (x = i1; x <= i2; x++)
    for (y = j1; y <= j2; y++)
      if (p[x][y] != EMPTY)
	return 0;
  return 1;
}

static void analyze_corner(corner_status * cs, int corner, int color)
{
  int other;
  other = OTHER_COLOR(color);

  switch (cs->nb_stone) {
  case 0:
    cs->kado = no_stone;
    break;
  case 1:
    if (known_stone(cs->coord[0])) {
      if (cs->coord[0] == 303 || cs->coord[0] == 404)
	cs->kado = my_hoshi;
      else
	cs->kado = my_open;
    } else {
      cs->kado = my_one;
    }
    if (cs->color == other)
      cs->kado++;
    break;
  case 2:
    if (known_stone_2(cs->coord[0]) && known_stone_2(cs->coord[1])) {
      if (cs->color == color) {
	cs->kado = my_shimari;
      } else if (cs->color == other) {
	cs->kado = you_shimari;
      } else {
	if (cs->coord[0] == 303 || cs->coord[1] == 303) {
	  if (area_color_c(303, corner) == color)
	    cs->kado = my_kakari;
	  else
	    cs->kado = you_kakari;
	} else if (cs->coord[0] == 304 || cs->coord[1] == 304) {
	  if (area_color_c(304, corner) == color)
	    cs->kado = my_kakari;
	  else
	    cs->kado = you_kakari;
	} else if (cs->coord[0] == 403 || cs->coord[1] == 403) {
	  if (area_color_c(403, corner) == color)
	    cs->kado = my_kakari;
	  else
	    cs->kado = you_kakari;
	} else if (cs->coord[0] == 305 || cs->coord[1] == 305) {
	  if (area_color_c(305, corner) == color)
	    cs->kado = my_kakari;
	  else
	    cs->kado = you_kakari;
	} else if (cs->coord[0] == 503 || cs->coord[1] == 503) {
	  if (area_color_c(503, corner) == color)
	    cs->kado = my_kakari;
	  else
	    cs->kado = you_kakari;
	} else if (area_color_c(303, corner) == color)
	  cs->kado = my_gray;
	else
	  cs->kado = you_gray;
      }
    } else {
      if (cs->color == color) {
	cs->kado = my_2;
      } else if (cs->color == other) {
	cs->kado = you_2;
      } else {
	if (area_color_c(303, corner) == color)
	  cs->kado = my_gray;
	else
	  cs->kado = you_gray;
      }
    }
    break;
  case 3:
    if (cs->color == color)
      cs->kado = my_3;
    else if (cs->color == other)
      cs->kado = you_3;
    else {
      int c1, c2, c3;
      c1 = area_color_c(cs->coord[0], corner);
      c2 = area_color_c(cs->coord[1], corner);
      c3 = area_color_c(cs->coord[2], corner);
      if ((c1 == color && (c2 == color || c3 == color)) || (c2 == color && c3 == color))
	cs->kado = my_3_gray;
      else
	cs->kado = you_3_gray;

/*look for pincer, or approched shimari */
      c1 = area_color_c(303, corner);
      c2 = area_color_c(306, corner);
      c3 = area_color_c(603, corner);

      if (c2 == c3 && c1 != c2) {	/* pincer ! */
	if (c1 == color)
	  cs->kado = my_pincer;
	else if (c1 == other)
	  cs->kado = you_pincer;
      } else if (c1 == color && cs->kado == my_gray && (c1 == c2 || c1 == c3)) {
	cs->kado = my_gray_shimari;	/* approched enclosure ! */
      } else if (c1 == other && cs->kado == you_gray && (c1 == c2 || c1 == c3)) {
	cs->kado = you_gray_shimari;	/* approched enclosure ! */
      }
    }
    break;
  case 4:
    if (cs->color == color)
      cs->kado = my_4;
    else if (cs->color == other)
      cs->kado = you_4;
    else {
      int c1, c2;
      c1 = c2 = 0;
      if (area_color_c(cs->coord[0], corner) == color)
	c1++;
      else
	c2++;
      if (area_color_c(cs->coord[1], corner) == color)
	c1++;
      else
	c2++;
      if (area_color_c(cs->coord[2], corner) == color)
	c1++;
      else
	c2++;
      if (area_color_c(cs->coord[3], corner) == color)
	c1++;
      else
	c2++;
      if (c1 == 3)
	cs->kado = my_4_gray;
      else if (c2 == 3)
	cs->kado = you_4_gray;
      else
	cs->kado = gray_4;
    }
    break;
  default:
    cs->kado = played_5;
  }				/*switch */
  return;
}

/* update angle[], return 1 if two last moves found */
static int readcorner(int color, int corner)
{
  int i1, i2, j1, j2, x, y, cmp;
  corner_status cs, scs;
  int nbmove = 0;

  i1 = i2 = j1 = j2 = 0;

  if (short_angle[where(corner)].closed)
    return 0;

  switch (corner) {
  case UPPER_LEFT:
    i1 = 0;
    i2 = 7;
    j1 = 0;
    j2 = 7;
    break;
  case UPPER_RIGHT:
    i1 = 0;
    i2 = 7;
    j1 = 11;
    j2 = 18;
    break;
  case LOWER_LEFT:
    i1 = 11;
    i2 = 18;
    j1 = 0;
    j2 = 7;
    break;
  case LOWER_RIGHT:
    i1 = 11;
    i2 = 18;
    j1 = 11;
    j2 = 18;
    break;
  }

  cs.nb_stone = 0;
  cs.color = EMPTY;
  cs.closed = 0;
  cs.kado = 0;
  cs.coord[0] = cs.coord[1] = cs.coord[2] = cs.coord[3] = 0;
  scs = cs;

  for (x = i1; x <= i2; x++)
    for (y = j1; y <= j2; y++)
      if (p[x][y] != EMPTY) {
	cs.nb_stone++;
	/* 0+1=1 0+2=2 1+1=1 1+2=gray 2+2=2 */
	cs.color |= p[x][y];
	cmp = composite(x, y, corner);
	if (cs.nb_stone <= 4)
	  cs.coord[cs.nb_stone - 1] = cmp;
	if (cmp < 700 && cmp % 100 < 7) {
	  scs.nb_stone++;
	  scs.color |= p[x][y];
	  if (scs.nb_stone <= 4)
	    scs.coord[scs.nb_stone - 1] = cmp;
	}
      }
  if (cs.nb_stone != angle[where(corner)].nb_stone) {

/* at least one last 2 moves is in that corner */
    if (cs.nb_stone == angle[where(corner)].nb_stone + 2)
      nbmove = 2;
    else
      nbmove = 1;

/* finding what is the situation in the corner */
    analyze_corner(&cs, corner, color);

    angle[where(corner)].nb_stone = cs.nb_stone;
    angle[where(corner)].color = cs.color;
    angle[where(corner)].kado = cs.kado;
    angle[where(corner)].coord[0] = cs.coord[0];
    angle[where(corner)].coord[1] = cs.coord[1];
    angle[where(corner)].coord[2] = cs.coord[2];
    angle[where(corner)].coord[3] = cs.coord[3];

    if (cs.nb_stone >= 5)
      angle[where(corner)].closed = 1;
    }
  if (scs.nb_stone != short_angle[where(corner)].nb_stone) {

/* finding what is the situation in the corner */
    analyze_corner(&scs, corner, color);

    short_angle[where(corner)].nb_stone = scs.nb_stone;
    short_angle[where(corner)].color = scs.color;
    short_angle[where(corner)].kado = scs.kado;
    short_angle[where(corner)].coord[0] = scs.coord[0];
    short_angle[where(corner)].coord[1] = scs.coord[1];
    short_angle[where(corner)].coord[2] = scs.coord[2];
    short_angle[where(corner)].coord[3] = scs.coord[3];

    if (scs.nb_stone >= 5)
      short_angle[where(corner)].closed = 1;
  }
  return nbmove;
}

/*********************************************************************

3 systems of coord :

- absolute : [0..18] [0..18], the same that is used in p[m][n]
- relative to a corner : [1-10] [1-10] + corner, usefull to talk about 
  point 4 4 in upper right corner. Beware symetry ! 
  Just consider we are in upper left corner, and trust conversion functions.
- "composite" : way to write coord relative to a corner : 4-4 is number 404, 
  point 10-4 is number 1004, point 4-10 is 410.

warning about symetry :

relative & composite : 

u_left          u_right
  34         43
43 +    +    + 34

   +    +    +
   
34 +    +    + 43
  43         34
l_left          l_right

***************************************************************************/

/*from relative to absolute, too maintain compatibility with old fuseki.c */
static void absolute_coord_old(int corner, int *m, int *n)
{
    switch (corner) {
    case UPPER_LEFT:
      *m = *m - 1;
      *n = *n - 1;
      break;
    case UPPER_RIGHT:
      *m = *m - 1;
      *n = board_size - *n;
      break;
    case LOWER_LEFT:
      *m = board_size - *m;
      *n = *n - 1;
      break;
    case LOWER_RIGHT:
      *m = board_size - *m;
      *n = board_size - *n;
      break;
    }
}

/*from relative to absolute */
static void absolute(int corner, int *m, int *n)
{
  int s;
  switch (corner) {
  case UPPER_LEFT:
    *m = *m - 1;
    *n = *n - 1;
    break;
  case UPPER_RIGHT:
    s = *m;
    *m = *n - 1;
    *n = board_size - s;
    break;
  case LOWER_LEFT:
    s = *m;
    *m = board_size - *n;
    *n = s - 1;
    break;
  case LOWER_RIGHT:
    *m = board_size - *m;
    *n = board_size - *n;
    break;
  }
}

/*from absolute to relative */
static void relative(int *i, int *j, int corner)
{
  int s;
  switch (corner) {
  case UPPER_LEFT:
    (*i)++;
    (*j)++;
    break;
  case UPPER_RIGHT:
    s = *j;
    *j = *i + 1;
    *i = board_size - s;
    break;
  case LOWER_LEFT:
    s = *i;
    *i = *j + 1;
    *j = board_size - s;
    break;
  case LOWER_RIGHT:
    *i = board_size - *i;
    *j = board_size - *j;
    break;
  }
}

/* from absolute to composite */
static int composite(int i, int j, int corner)
{
  relative(&i, &j, corner);
  return (100 * i + j);
}

/* from composite to absolute */
static void decompose(int i, int corner, int *m, int *n)
{
  *m = i / 100;
  *n = i % 100;
  absolute(corner, m, n);
}

/* to get color around a "composite" stone */
static int area_color_c(int compos, int corner)
{
  int i, j;
  decompose(compos, corner, &i, &j);
  return area_color(i, j);
}

/***********************************************************************
   propose a move in a corner where one stone is
   already played, return the value of the move 
   ***********************************************************************/
static int 
choose_second(corner_status * ang, int corner, int *m, int *n, int color, int sym)
{
  int enclo_val, appro_val;
  int cor;
  int upper, left, first;
  int kado, kado_right, kado_left, kado_opposite;
  int stone_left, stone_right, color_right, color_left;
  int other, nb_mycol, nb_othercol, nb_gray;

#define SYM(x)	(sym?((x)/100+((x)%100)*100):(x))


  /* the position of the already played stone */
  first = ang[where(corner)].coord[0];
  if (!sym) {
    switch (first) {		/* use symetry */
    case 403:
    case 503:
    case 504:
      return choose_second(ang, corner, m, n, color, 1);
    }
  } else {
    first = SYM(first);
  }
  
  other = OTHER_COLOR(color);

  /* the stones position in this ang */
  kado = ang[where(corner)].kado;
  cor = right(corner);
  if (!sym) {
    kado_right = ang[where(cor)].kado;
    stone_right = ang[where(cor)].nb_stone;
    color_right = ang[where(cor)].color;
    cor = right(cor);
    kado_opposite = ang[where(cor)].kado;
    cor = right(cor);
    kado_left = ang[where(cor)].kado;
    stone_left = ang[where(cor)].nb_stone;
    color_left = ang[where(cor)].color;
  } else {
    kado_left = ang[where(cor)].kado;
    stone_left = ang[where(cor)].nb_stone;
    color_left = ang[where(cor)].color;
    cor = right(cor);
    kado_opposite = ang[where(cor)].kado;
    cor = right(cor);
    kado_right = ang[where(cor)].kado;
    stone_right = ang[where(cor)].nb_stone;
    color_right = ang[where(cor)].color;
  }


  enclo_val = V_ENCLOS;		/*base value for enclosure */
  appro_val = V_KAKARI;		/*base value for approach */
  

  upper = area_color_c(SYM(409), corner);	/* upper side color */
  left = area_color_c(SYM(904), corner);	/* left side color */

/* count how many corner we got */
  nb_mycol = nb_othercol = nb_gray = 0;
  cor = UPPER_RIGHT;
  do {
    if (ang[where(cor)].color == color)
      nb_mycol++;
    else if (ang[where(cor)].color == other)
      nb_othercol++;
    else
      nb_gray++;
    cor = right(cor);
  } while (cor != UPPER_RIGHT);
  
  /* if we got weak groups, decrease return value of kakari */
  appro_val -= number_weak(color) * 10;
  
  /* if we need to approach other corner */
  if ((nb_mycol + nb_gray) < nb_othercol)
    appro_val += 10;
  if (nb_mycol == 0)
    appro_val += 10;
  if (nb_mycol > nb_othercol) {
    enclo_val += 10;
    appro_val -= 10;
  }
  if (ang[where(corner)].color == color) {

/**********************************************************************/
/* we played first in this corner, look for corner enclosure value    */
/**********************************************************************/
    switch (first) {
    case 303:
      /* not an urgent fuseki move here, if any, 
	 it will be find by joseki pattern */
      {
	int i, j, ii, jj;
	int v = enclo_val;
	v -= 90;
	if (upper == color || left == color) {
	  v += 15;
	  if (upper == color && left == color)
	    decompose(404, corner, m, n);
	  else if (upper == color)
	    decompose(405, corner, m, n);
	  else if (left == color)
	    decompose(504, corner, m, n);
	  
	  if (color_left == other && stone_left >= 2)
	    v += 15;
	  if (color_right == other && stone_right >= 2)
	    v += 15;
	} else if (upper == other && left == other) {
	  v += 55;
	  decompose(306, corner, &i, &j);
	  decompose(603, corner, &ii, &jj);
	  if (diff_moyo(i, j, color) > diff_moyo(ii, jj, color))
	    decompose(306, corner, m, n);
	  else
	    decompose(603, corner, m, n);
	} else if (upper == other || left == other) {
	  decompose(306, corner, &i, &j);
	  decompose(603, corner, &ii, &jj);
	  if (diff_moyo(i, j, color) > diff_moyo(ii, jj, color))
	    decompose(306, corner, m, n);
	  else
	    decompose(603, corner, m, n);
	  v += 15;
	} else
	  v = 0;
	return v;
      }				/*303 */
      
    case 304:			/* corner enclosure */
      
      {
	int v = enclo_val;
	if (upper == color) {	/* we got upper side, the 3 extension side */
	  if (left == color) {
	    /* play high enclosure, maybe a double wing ! */
	    decompose(SYM(504), corner, m, n);
	    return v + 10;
	  } else if (left == other) {
	    /* play low enclosure */
	    decompose(SYM(503), corner, m, n);
	    return v;
	  } else {		/*empty on left */
	    /* play enclosure, merely high one */
	    if ((3.0 * rand() / (RAND_MAX + 1.0)) == 1)
	      decompose(SYM(503), corner, m, n);
	    else
	      decompose(SYM(504), corner, m, n);
	    if (area_color_c(SYM(1404), corner) == color)
	      return v + 5;
	    else
	      return v;
	  }
	} else if (upper == other) {
	  /* opponent on upper side, the big extension side */
	  if (left == color) {
	    /* play low enclosure */
	    decompose(SYM(503), corner, m, n);
	    return v;
	  } else if (left == other) {
	    /* play urgent low enclosure, since we are surrounded */
	    decompose(SYM(503), corner, m, n);
	    v += 30;
	    return v;
	  } else {		/* empty on left */
	    /* play enclosure, merely high one */
	    if ((3.0 * rand() / (RAND_MAX + 1.0)) == 1)
	      decompose(SYM(503), corner, m, n);
	    else
	      decompose(SYM(504), corner, m, n);
	    v += 8;
	    return v;
	  }
	} else {		/* empty on upper side */
	  v += 5;
	  if (left == color) {
	    if (area_color_c(SYM(414), corner) == color) {
	      /* play high enclosure, more high */
	      if ((4.0 * rand() / (RAND_MAX + 1.0)) == 1)
		decompose(SYM(503), corner, m, n);
	      else
		decompose(SYM(504), corner, m, n);
	    } else
	      decompose(SYM(503), corner, m, n);
	    v += 5;
	    return v;
	  } else if (left == other) {
	    v += 6;
	    /* play low enclosure, maybe large one */
	    decompose(SYM(503), corner, m, n);
	    return v + 1;
	  } else {		/* empty on both sides */
	    if ((3.0 * rand() / (RAND_MAX + 1.0)) == 1)
	      decompose(SYM(503), corner, m, n);
	    else
	      decompose(SYM(504), corner, m, n);
	    return v;
	  }
	}
      }				/*304 & 403 by sym */
      
    case 404:{
      /* friendly hoshi, not an urgent fuseki move here */
      /* if any, it will be find by joseki pattern */
      int i, j, ii, jj;
      int v = enclo_val;
      if (upper == other && left == other) {
	v -= 40;
	decompose(306, corner, &i, &j);
	decompose(603, corner, &ii, &jj);
	if (diff_moyo(i, j, color) > diff_moyo(ii, jj, color))
	  decompose(306, corner, m, n);
	else
	  decompose(603, corner, m, n);
      } else {
	v = 0;
      }
      return v;
    }				/*404 */
    
    
    case 305:{
      /* if we got one extension side, play enclosure */
      if (left == color || upper == color) {
	decompose(SYM(403), corner, m, n);
	return enclo_val;
      } else {
	/* no urgent move by here ? */
	decompose(SYM(403), corner, m, n);
	return enclo_val - 10;
      }
    }				/* 305 & 503 by sym */
    
    case 405:{
      /* if we got one extension side, play enclosure */
      if (left == color || upper == color) {
	decompose(SYM(403), corner, m, n);
	return enclo_val + 4;
      } else {
	/* no urgent move by here ? */
	decompose(SYM(403), corner, m, n);
	return enclo_val - 10;
      }
    }				/* 405 & 504 by sym */
    
    default:
      /* I played first in the corner, 
	 it's a strange move : wait and see */
      return 0;
    }				/* switch */
  }
  /*if (ang[where(corner)].color == color) */
/****************************************************************************/
/* opponent play first in this corner, look for kakari                      */
/****************************************************************************/
  else {
    switch (first) {
    case 303:{			/* 4-4 approach upon 3-3 */
      int tcorner;
      int v = appro_val;
      
      /* don't cap 3-3 if other corner move is possible */
      tcorner = right(corner);
      if (ang[where(tcorner)].nb_stone < 2)
	return 0;
      tcorner = right(tcorner);
      if (ang[where(tcorner)].nb_stone < 2)
	return 0;
      tcorner = right(tcorner);
      if (ang[where(tcorner)].nb_stone < 2)
	return 0;
      if (style & STY_FEARLESS)
	v += 15;
      decompose(404, corner, m, n);
      if (upper == color && left == color)
	return v - 5;
      else if (upper == color || left == color)
	return v - 15;
      else
	return v - 25;
    }				/*303 */

    case 304:
      {				/* kakari against 3-4 stone */
	int v = appro_val;
	
	if (upper == color && left == color) {
	  /* we have both sides, maybe play high */
	  if ((int) (3.0 * rand() / (RAND_MAX + 1.0)) == 1)
	    decompose(SYM(504), corner, m, n);
	  else
	    decompose(SYM(503), corner, m, n);
	  return v + 11;
	} else if (upper == color && left == other) {
	  /* we have only upper side, play high */
	  if ((int) (3.0 * rand() / (RAND_MAX + 1.0)) == 1)
	    decompose(SYM(503), corner, m, n);
	  else
	    decompose(SYM(504), corner, m, n);
	  return v + 3;
	} else {
	  /* otherwise, play low kakari ... */
	  decompose(SYM(503), corner, m, n);
	  return v;
	}
      }				/*304 & 403 by sym */
      
    case 404:{			/* try to approach on the open side, or 
				   on the side of my color */
      if (upper == EMPTY) {
	if (left == color) {
	  /* approach on my side */
	  decompose(603, corner, m, n);
	  return appro_val + 5;
	} else if (left == other) {
	  /* approach on empty side */
	  decompose(306, corner, m, n);
	  return appro_val;
	} else {
	  /* both sides empty, approach move less urgent, 
	     if one corner is of my color, chose this side */
	  if (area_color_c(414, corner) == color && area_color_c(1404, corner) == other) {
	    decompose(306, corner, m, n);
	    return appro_val - 8;
	  } else if (area_color_c(414, corner) == other && area_color_c(1404, corner) == color) {
	    decompose(603, corner, m, n);
	    return appro_val - 8;
	  } else {		/* chose by random, move less urgent */
	    /* play sometimes high approach move */
	    if ((int) (2.0 * rand() / (RAND_MAX + 1.0)) == 1) {
	      if ((int) (5.0 * rand() / (RAND_MAX + 1.0)) == 1)
		decompose(604, corner, m, n);
	      else
		decompose(603, corner, m, n);
	      return appro_val - 20;
	    } else {
	      if ((int) (5.0 * rand() / (RAND_MAX + 1.0)) == 1)
		decompose(406, corner, m, n);
	      else
		decompose(306, corner, m, n);
	      return appro_val - 20;
	    }
	  }
	}
      } else if (upper == color) {
	if (left == color) {	/* got both sides ! */
	  int il, jl, iu, ju;
	  decompose(904, corner, &il, &jl);
	  decompose(409, corner, &iu, &ju);
	  /* approach move less urgent, maybe we could play san-san invasion ? */
	  if (area_stone(il, jl) >= 3 && area_stone(iu, ju) >= 3
	      && area_space(il, jl) >= 16 && area_space(iu, ju) >= 16) {
	    /* launch early 3-3 invasion */
	    decompose(303, corner, m, n);
	    return appro_val - 25;
	  } else
	    /* play the side we are less strong */
	    if (area_space(iu, ju) < area_space(il, jl)) {
	      /* play upper */
	      decompose(306, corner, m, n);
	      return appro_val - 30;
	    } else if (area_space(iu, ju) > area_space(il, jl)) {
	      /* play left */
	      decompose(603, corner, m, n);
	      return appro_val - 30;
	    } else if ((int) (2.0 * rand() / (RAND_MAX + 1.0)) == 1) {
	      decompose(306, corner, m, n);
	      return appro_val - 30;
	    } else {
	      decompose(603, corner, m, n);
	      return appro_val - 30;
	    }
	} else {		/* approach from upper side */
	  decompose(306, corner, m, n);
	  return appro_val;
	}
      } else {		/*if upper == other */
	if (left == other) {	/* opponent got both sides ! */
	  int il, jl, iu, ju;
	  decompose(904, corner, &il, &jl);
	  decompose(409, corner, &iu, &ju);

	  /* maybe 3-3 invasion if opponent very strong on both sides */
	  if (area_space(il, jl) < area_stone(iu, ju)) {
	    /* play the side where opponent is less strong */
	    decompose(306, corner, m, n);
	    return appro_val - 12;
	  } else if (area_stone(il, jl) > area_stone(iu, ju)) {
	    decompose(603, corner, m, n);
	    return appro_val - 12;
	  } else if ((int) (2.0 * rand() / (RAND_MAX + 1.0)) == 1) {
	    decompose(306, corner, m, n);
	    return appro_val - 13;
	  } else {
	    decompose(603, corner, m, n);
	    return appro_val - 13;
	  }
	} else {		/* approach empty side or my side */
	  decompose(603, corner, m, n);
	  return appro_val;
	}
      }
      break;
    }				/*404 */
    

    case 305:{
      /* if we got side &  corner, try special 4-5 kakari */
      if (left == color && area_color_c(SYM(1404), corner) == color)
	decompose(SYM(504), corner, m, n);
      else			/* standard kakari */
	decompose(SYM(403), corner, m, n);
      return appro_val;
    }				/*305 & 503 by sym */
    
    case 405:{
      /* standard kakari is 4-3, but 3-3 is possible */
      if ((int) (5.0 * rand() / (RAND_MAX + 1.0)) == 1)
	decompose(303, corner, m, n);
      else
	decompose(SYM(403), corner, m, n);
      return appro_val;
    }				/*405  & 504 by sym */
    
    default:			/* we got a strange first move in the corner */
      {
	/* if 4-4 or 3-3 seems to be not owned by the opponent, play
	   there, this should be better than nothing, but low priority */
	if (area_color_c(404, corner) == EMPTY) {
	  decompose(404, corner, m, n);
	  return appro_val - 40;
	} else {		/* look around 3-3 */
	  if (area_color_c(303, corner) == EMPTY) {
	    decompose(303, corner, m, n);
	    return appro_val - 45;
	  } else {
	    if (area_color_c(403, corner) == EMPTY
		|| area_color_c(304, corner) == EMPTY) {
	      decompose(303, corner, m, n);
	      return appro_val - 48;
	    }
	  }
	}
	
	return 0;		/* no proposed move */
	
      }				/*default */
    }				/*switch */
  }
  
  return 0;			/* should not be possible */
}


/* change the probability table for first corner move if a
   play already occured on a side */

static void 
analyze_sides(int corner, int *table, int color)
{
  int scol;
  int other;
  
  if (style & STY_NO_FUSEKI)
    return;
  other = OTHER_COLOR(color);
  scol = area_color_c(904, corner);
  
  if (scol == other) {
    table[1] = 0;		/* 3-4 */
  } else if (scol == color) {
    table[1] += 10;		/* 3-4 */
    table[3] += 5;		/* 4-4 */
  } else {
    if (area_color_c(1404, corner) == other)
      table[1] -= 10;		/* 3-4 */
  }
  
  scol = area_color_c(409, corner);
  
  if (scol == other) {
    table[2] = 0;		/* 4-3 */
  } else if (scol == color) {
    table[2] += 10;		/* 4-3 */
    table[3] += 5;		/* 4-4 */
  } else {
    if (area_color_c(414, corner) == other)
      table[2] -= 10;		/* 4-3 */
  }
}


static void 
choose_corner_move(int corner, int *m, int *n)
{
  int *table=0;
  int sum_of_weights=0;
  int i;
  int q;
  
  if (board_size<=11)
    table = small_board;
  else if (board_size<=15)
    table = medium_board;
  else 
    table = large_board;
  
  for (i=0; i<8 ;i++)
    sum_of_weights += table[i];
  
  q = rand() % sum_of_weights;
  for (i=0; i<8; i++) {
    q -= table[i];
    if (q<0)
      break;
  }
  *m = corners[i][0];
  *n = corners[i][1];
  absolute_coord_old(corner, m, n);
}


static void 
maybe_extend(int *bestval, int *besti, int *bestj, int ti, int tj, int color)
{
  int oval;
  int swapi;
  int ajust;
  
  ajust = 2;
  
  if ((area_color(ti - 1, tj) == color
       || area_color(ti + 1, tj) == color)
      && openregion(ti - 2, ti + 1, tj - 2, tj + 2)) {
    oval = diff_moyo(ti, tj, color);
    if (oval > *bestval) {
      *bestval = oval;
      *besti = ti;
      *bestj = tj;
    }
    oval = diff_moyo(ti - 1, tj, color) + ajust;
    if (oval > *bestval) {
      *bestval = oval;
      *besti = ti - 1;
      *bestj = tj;
    }
  }
  ti = board_size - ti - 1;
  if ((area_color(ti - 1, tj) == color
       || area_color(ti + 1, tj) == color)
      && openregion(ti - 2, ti + 1, tj - 2, tj + 2)) {
    oval = diff_moyo(ti, tj, color);
    if (oval > *bestval) {
      *bestval = oval;
      *besti = ti;
      *bestj = tj;
    }
    oval = diff_moyo(ti + 1, tj, color) + ajust;
    if (oval > *bestval) {
      *bestval = oval;
      *besti = ti + 1;
      *bestj = tj;
    }
  }
  swapi = board_size - ti - 1;
  ti = tj;
  tj = swapi;
  if ((area_color(ti, tj - 1) == color
       && area_color(ti, tj + 1) == color)
      && openregion(ti - 2, ti + 2, tj - 2, tj + 1)) {
    oval = diff_moyo(ti, tj, color);
    if (oval > *bestval) {
      *bestval = oval;
      *besti = ti;
      *bestj = tj;
    }
    oval = diff_moyo(ti, tj - 1, color) + ajust;
    if (oval > *bestval) {
      *bestval = oval;
      *besti = ti;
      *bestj = tj - 1;
    }
  }
  tj = board_size - tj - 1;
  if ((area_color(ti, tj - 1) == color
       && area_color(ti, tj + 1) == color)
      && openregion(ti - 2, ti + 2, tj - 2, tj + 1)) {
    oval = diff_moyo(ti, tj, color);
    if (oval > *bestval) {
      *bestval = oval;
      *besti = ti;
      *bestj = tj;
    }
    oval = diff_moyo(ti, tj + 1, color) + ajust;
    if (oval > *bestval) {
      *bestval = oval;
      *besti = ti;
      *bestj = tj + 1;
    }
  }
}

static void 
maybe_oba(int *bestval, int *besti, int *bestj, int ti, int tj, int color)
{
  int oval;
  int swapi;
  int ajust;

  if (style & STY_FEARLESS)	/* play more on the 4th line */
    ajust = 3;
  else
    ajust = 7;

  if (area_color(ti - 1, tj) == EMPTY
      && area_color(ti + 1, tj) == EMPTY
      && openregion(ti - 1, ti + 1, tj - 2, tj + 2)) {
    oval = diff_moyo(ti, tj, color);
    if (oval > *bestval) {
      *bestval = oval;
      *besti = ti;
      *bestj = tj;
    }
    oval = diff_moyo(ti - 1, tj, color) + ajust;
    if (oval > *bestval) {
      *bestval = oval;
      *besti = ti - 1;
      *bestj = tj;
    }
  }
  ti = board_size - ti - 1;
  if (area_color(ti - 1, tj) == EMPTY
      && area_color(ti + 1, tj) == EMPTY
      && openregion(ti - 1, ti + 1, tj - 2, tj + 2)) {
    oval = diff_moyo(ti, tj, color);
    if (oval > *bestval) {
      *bestval = oval;
      *besti = ti;
      *bestj = tj;
    }
    oval = diff_moyo(ti + 1, tj, color) + ajust;
    if (oval > *bestval) {
      *bestval = oval;
      *besti = ti + 1;
      *bestj = tj;
    }
  }
  swapi = board_size - ti - 1;
  ti = tj;
  tj = swapi;
  if (area_color(ti, tj - 1) == EMPTY
      && area_color(ti, tj + 1) == EMPTY
      && openregion(ti - 2, ti + 2, tj - 1, tj + 1)) {
    oval = diff_moyo(ti, tj, color);
    if (oval > *bestval) {
      *bestval = oval;
      *besti = ti;
      *bestj = tj;
    }
    oval = diff_moyo(ti, tj - 1, color) + ajust;
    if (oval > *bestval) {
      *bestval = oval;
      *besti = ti;
      *bestj = tj - 1;
    }
  }
  tj = board_size - tj - 1;
  if (area_color(ti, tj - 1) == EMPTY
      && area_color(ti, tj + 1) == EMPTY
      && openregion(ti - 2, ti + 2, tj - 1, tj + 1)) {
    oval = diff_moyo(ti, tj, color);
    if (oval > *bestval) {
      *bestval = oval;
      *besti = ti;
      *bestj = tj;
    }
    oval = diff_moyo(ti, tj + 1, color) + ajust;
    if (oval > *bestval) {
      *bestval = oval;
      *besti = ti;
      *bestj = tj + 1;
    }
  }
}
    
/* look for a big point on a side, than return its value */

static int 
search_oba(int *i, int *j, int color)
{
  int besti, bestj, bestval;

  besti = bestj = bestval = 0;

  maybe_oba(&bestval, &besti, &bestj, 3, 7, color);
  maybe_oba(&bestval, &besti, &bestj, 3, 8, color);
  maybe_oba(&bestval, &besti, &bestj, 3, 9, color);
  maybe_oba(&bestval, &besti, &bestj, 3, 10, color);
  maybe_oba(&bestval, &besti, &bestj, 3, 11, color);

  if (bestval) {
    *i = besti;
    *j = bestj;
    return V_OBA;
  } else
    return 0;
}

/* look for a medium point on a side, than return its value */

static int 
search_small_oba(int *i, int *j, int color)
{
  int besti, bestj, bestval;

  besti = bestj = bestval = 0;

  maybe_oba(&bestval, &besti, &bestj, 3, 6, color);
  maybe_oba(&bestval, &besti, &bestj, 3, 5, color);
  maybe_oba(&bestval, &besti, &bestj, 3, 12, color);
  maybe_oba(&bestval, &besti, &bestj, 3, 13, color);

  if (bestval) {
    *i = besti;
    *j = bestj;

    return V_OBA - 5;
  } else
    return 0;
}

/* look for an extension on a side, than return its value */
static int 
search_extend(int *i, int *j, int color)
{
  int besti, bestj, bestval;

  besti = bestj = bestval = 0;

  maybe_extend(&bestval, &besti, &bestj, 3, 7, color);
  maybe_extend(&bestval, &besti, &bestj, 3, 8, color);
  maybe_extend(&bestval, &besti, &bestj, 3, 9, color);
  maybe_extend(&bestval, &besti, &bestj, 3, 10, color);
  maybe_extend(&bestval, &besti, &bestj, 3, 11, color);

  if (bestval) {
    *i = besti;
    *j = bestj;
    return V_OBA - 10;
  } else {
    besti = bestj = bestval = 0;

    maybe_extend(&bestval, &besti, &bestj, 3, 6, color);
    maybe_extend(&bestval, &besti, &bestj, 3, 5, color);
    maybe_extend(&bestval, &besti, &bestj, 3, 12, color);
    maybe_extend(&bestval, &besti, &bestj, 3, 13, color);

    if (bestval) {
      *i = besti;
      *j = bestj;
      return V_OBA - 10;
    }
  }

  return 0;
}



/*****************************************************************

 MAIN FUSEKI FUNCTION generate move in empty corners

 *****************************************************************/

int fuseki(int *i, int *j, int *val, int *equal_moves, int color)
{
  int found_one=0;
  int width;  /* Side of the open region required in the corner. */
  int x,y;
  
  if (fuseki_ended)		/* quick exit if fuseki is over */
    return 0;
  
  if (board_size == 19) {
    /* complete fuseki function for 19 x 19 board */
    found_one = fuseki_19(i, j, val, equal_moves, color);
  } else {
    if (board_size<=9 && movenum>1)
      return 0;
    if (board_size < 9) {
      *val=95;
      for(x=board_size/2;x<board_size;x++)
	for (y = board_size / 2; y < board_size; y++) {
	  *i=x;
	  *j=y;
	  if(p[x][y]==EMPTY)
	    return 1;
        }
      *i=*j=-1;
      *val=-1;
      return 0;
    }
    if (board_size<=11) {
      /* For boards of size 11x11 or smaller we first go for the center point. */
      int middle=board_size/2;
      if (openregion(middle-1, middle+1, middle-1, middle+1)) {
	if (BETTER_MOVE(90, *val, *equal_moves)) {
	  *i=middle;
	  *j=middle;
	  found_one=1;
	}
      }
    }
    if (board_size>=18)
      width = 8;
    else
      width = board_size/2;
    
    /* fuseki function if size != 19 */
    
    if (!(corner_done & UPPER_RIGHT)) {
      if (openregion(0, width-1, board_size-width, board_size-1)) {
	if (BETTER_MOVE(85, *val, *equal_moves)) {
	  choose_corner_move(UPPER_RIGHT, i, j);
	  found_one=1;
	}
      } else
	corner_done |= UPPER_RIGHT;
    }
    if (!(corner_done & LOWER_LEFT)) {
      if (openregion(board_size-width,board_size-1,0,width-1)) {
	if (BETTER_MOVE(85, *val, *equal_moves)) {
	  choose_corner_move(LOWER_LEFT, i, j);
	  found_one=1;
	}
      } else
	corner_done |= LOWER_LEFT;
    }
    if (!(corner_done & LOWER_RIGHT)) {
      if (openregion(board_size-width,board_size-1,board_size-width,board_size-1)) {
	if (BETTER_MOVE(85, *val, *equal_moves)) {
	  choose_corner_move(LOWER_RIGHT, i, j);
	  found_one=1;
	}
      } else
	corner_done |= LOWER_RIGHT;
    }
    if (!(corner_done & UPPER_LEFT)) {
      if (openregion(0,width-1,0,width-1)) {
	if (BETTER_MOVE(85, *val, *equal_moves)) {
	  choose_corner_move(UPPER_LEFT, i, j);
	  found_one=1;
	}
      } else
	corner_done |= UPPER_LEFT;
    }
    if (corner_done == UPPER_RIGHT + LOWER_LEFT + LOWER_RIGHT + UPPER_LEFT)
      fuseki_ended = 1;
  }
  
  if (found_one)
    TRACE("fuseki found : %m with value %d\n", *i, *j, *val);
  if (fuseki_ended)
    TRACE("fuseki ended\n");
  
  return found_one;
}


/***********************************************************************

fuseki function, for board size 19, return 1 if found a move, else 0

************************************************************************/

static int 
fuseki_19(int *i, int *j, int *val, int *equal_moves, int color)
{
  int found_one = 0;
  int corner;
  int lm = 0;
  
  /* update corner information */
  
  corner = UPPER_RIGHT;
  do {
    lm += readcorner(color, corner);
    corner = right(corner);
  } while ((firstmove || lm < 2) && corner != UPPER_RIGHT);
  
  /* look for empty corner to play */
  found_one = play_empty_corner(i, j, val, equal_moves, color);
  
  /* if we don't want complex fuseki, stop there */
  if (style & STY_NO_FUSEKI) {
    if (found_one) {
      *val -= (V_FIRST - 850);	/* so val is always 85 */
    } else {
      fuseki_ended = 1;
    }
  } else {
    /* real fuseki begin here : look for an approch move, shimari or side move */
    if (!found_one)
      found_one = play_somewhere(i, j, val, equal_moves, color);
  }
  
  /* convert internal value of move to external */
  if (found_one)
    *val = *val / 10;
  
  return found_one;
}



/* return 1 if a move in an empty corner is found */

static int 
play_empty_corner(int *m, int *n, int *val, int *equal_moves, int color)
{
  int corner;
  int found_empty = 0;
  int table[8];			/* probability of each possible move */
  int sum_of_weights;
  int q, i;

/* find some empty corner */
  corner = UPPER_RIGHT;

  do {
    if (angle[where(corner)].nb_stone == 0) {

      if (BETTER_MOVE((firstmove && color == BLACK && corner == UPPER_RIGHT) ? V_FIRST + 9 : V_FIRST, *val, *equal_moves)) {
	for (i = 0; i < 8; i++)
	  table[i] = large_board[i];
	sum_of_weights = 0;
	/* looking on the sides for changing probability of possible corner moves */
	analyze_sides(corner, table, color);
	
	for (i = 0; i < 8; i++)
	  sum_of_weights += table[i];
	q = (double) sum_of_weights *rand() / (RAND_MAX + 1.0);
	
	for (i = 0; i < 8; i++) {
	  q -= table[i];
	  if (q < 0)
	    break;
	}
	*m = corners[i][0];
	*n = corners[i][1];
	
	absolute(corner, m, n);
	found_empty = 1;
      }
    }
    corner = right(corner);
  } while (corner != UPPER_RIGHT);
  
  if (firstmove)
    firstmove = 0;

  return found_empty;
}


/* every corner is played, look for a second move in a corner */
static int play_somewhere(int *m, int *n, int *val, int *equal_moves, int color)
{
  int corner;
  int found_move = 0;
  int found_proposal = 0;
  int fval = 0;
  int i, j, minus;

  i = j = 0;
  minus = 0;

  /* decrease value to not disturb fight when more corners are disputed */
  corner = UPPER_RIGHT;
  do {
    if (short_angle[where(corner)].nb_stone <= 3
	&& short_angle[where(corner)].color == GRAY_BORDER)
      minus += 8;
    corner = right(corner);
  } while (corner != UPPER_RIGHT);


  /* find some corner with only one stone played */
  corner = UPPER_RIGHT;
  do {
    if (angle[where(corner)].nb_stone == 1) {
      fval = choose_second(angle, corner, &i, &j, color, 0) - minus;
      if (fval > 0) {
	found_proposal = 1;
	if (BETTER_MOVE(fval, *val, *equal_moves)) {
	  *m = i;
	  *n = j;
	  found_move = 1;
	}
      }
    }
    corner = right(corner);
  } while (corner != UPPER_RIGHT);


/*  search oba... */
  /* look for big points on middle side */

    fval = search_oba(&i, &j, color) - minus;
    if (fval > 0) {
    found_proposal = 1;
    if (BETTER_MOVE(fval, *val, *equal_moves)) {
      *m = i;
      *n = j;
      found_move = 1;
    }
  }
  /* find some corner with two stones played, using short_angle */
  corner = UPPER_RIGHT;
  do {
    if (angle[where(corner)].nb_stone == 2 && short_angle[where(corner)].nb_stone == 1) {
      fval = choose_second(short_angle, corner, &i, &j, color, 0) - minus - 15;
      if (fval > 0) {
	found_proposal = 1;
      if (BETTER_MOVE(fval, *val, *equal_moves)) {
	*m = i;
	*n = j;
	found_move = 1;
}
    }
  }
    corner = right(corner);
  } while (corner != UPPER_RIGHT);

  /* look for little big points near middle side */

  if (!found_proposal) {
    fval = search_small_oba(&i, &j, color) - minus;
    if (fval > 0) {
      found_proposal = 1;
      if (BETTER_MOVE(fval, *val, *equal_moves)) {
	*m = i;
	*n = j;
	found_move = 1;
      }
    }
  }
  /* look for early extension */

  if (!found_proposal) {
    fval = search_extend(&i, &j, color) - minus;
    if (fval > 0) {
      found_proposal = 1;
      if (BETTER_MOVE(fval, *val, *equal_moves)) {
	*m = i;
	*n = j;
	found_move = 1;
      }
    }
  }
  if (!found_proposal)
    fuseki_ended = 1;
  if ((style & STY_TENUKI) && found_move)
    *val = 850;
  return found_move;
}





/*
 * Local Variables:
 * tab-width: 8
 * c-basic-offset: 2
 * End:
 */