-
Notifications
You must be signed in to change notification settings - Fork 1
/
g25.c
4769 lines (4407 loc) · 155 KB
/
g25.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
/* Copyright 1984-2007 David Fotland. All rights reserved.
* This source code is being distributed under a nondisclosure
* agreement. Do not distribute it without written permission
* from David Fotland. Do not show it to anyone not party to the
* nondisclosure. Maintain good security on any computers or
* disks containing this source code.
*/
#include "g2hd.h"
#include "g2dist.h"
#include "g2fcache.h"
#ifdef G2DEBUGOUTPUT
#include <stdio.h>
#endif
int iskopoint(sqr_t s, int *color);
static int loses_all_semeai(army_t army, army_t winner, int rval);
static int winsemnbr(army_t army);
int getpoteyes(army_t army, int *min);
static int geteyespace3(army_t a1, army_t a2, army_t a3);
static int cnshreyespace(army_t olda, army_t army, int c, int cn);
static int cnshreyepot(army_t olda, army_t army, int c, int cn);
static int geteyespace(army_t a1, army_t a2);
static int ucutdist(sqr_t s, int sdir, int dir, int c);
static void deadgroups(army_t army);
static int th_terr(army_t army, army_t a);
static int th_conns(army_t army, army_t a);
static int getnumeyes(army_t army, short *max);
static void markspot(sqr_t);
static void markpcls(int);
static void findcaptured(int);
static void newdeadgroup(group_t,int,int);
static void fixwasthgroup(group_t);
static void fixwasdeadgroup(group_t);
static void fixarmies(void);
static void fixgralive(void);
static void initarmyalive(army_t);
static int startalive(army_t, int *);
static void getarmylibs(army_t);
static void getarmyex_pot(army_t);
static void check_ex(sqr_t,int,int,army_t,int,int);
static void getarmyuc_pot(army_t);
static void getarmycn_pot(army_t);
static void cnthreat(army_t,group_t,group_t,int);
extern void getarmytv_pot(army_t);
static int threat_live(army_t);
static int connect_live(army_t);
void getarmycnrn_pot(army_t);
static void getarmyrn_pot(army_t);
static void getarmywk_pot(army_t);
static void semeaialive(army_t);
static int miaialive(army_t army);
static void weakalive(army_t);
static void promoteweak(army_t army);
static void newalive(army_t,int,int, int);
static void donewbest(army_t army, list_t *);
# define MAXTERR 18
list_t splitlist; /* list of groups whose armies need splitting */
listval_t nextpot; /* next free potential record */
/* scoring philosophy:
*
* internal scores are kept Chinese style, in the range of +50 per square
* to -50 per square, where +50 is one point for white and -50 is
* one point for black.
*
* A live white stone gets score = +50, a dead white stone gets -50, and
* and unsettled white stone gets 0.
*
* The score of a position is the sum of the scores for each of the squares
* plus a correction for sente and threatened groups. The side to move can
* add capturing the biggest threatened enemy group or saving the biggest
* threatened friendly group or the value of sente, whichever is biggest.
* Value of capturing or saving a threatened group is 50 per stone or liberty.
* (We assume that capturing or saving threatened groups will be gote)
*
* There is another correction for dead groups inside dead groups, which
* are actually alive.
*
* The value of a move is the difference between the score after that move
* is made and the score after a pass is made.
*
* The value of sente (obaval) is 7 points at the beginning of the game
* (since black wins by 7 with the first move
* and declines to 1 point at the end. Added are points for sente attacks on
* enemy groups
*/
extern int pfac[NUMALIVE];
extern int sumeyes[41], sumeyesmax[41], sumpots[41],terr1[5],terr2[5],terr3[5],terr4[5],terr5[5][6];
extern int cfac[3];
extern int pfac8[16], pfac11[16], pfac16[16];
/* since live is total run adds up to 16,
* need two of 0, 3 of 1,2, or 4 of 3, or a 0 and 2 of 1,2,3
* and big groups will have some other low valued ones to add as well.
* use value 1 for a running place where you really can't run at all
* no matter how many of value 1 there are, they will only total to 1.
*/
/* value for 7 must be same or less than value for 3 */
/* 7 and 8 double count with exensions, so keep value low (was 5,2) 5/99 */
int runval[NUMRUN] = { 9, 8, 7, 5, 4, 3, 2, 4, 1, 1, 5, 2, 3, 1 };
int run2val[NUMRUN] = { 5, 4, 4, 3, 2, 2, 1, 3, 1, 1, 3, 1, 1, 1 }; /* from 2 stone wall - must be >= 1/2 of runval */
/* what is runval left if opponent moves first here */
int minrunval[NUMRUN] = { 4, 3, 2, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0};
/* see if g (which is a DEAD group) is inside very weak groups
* which means that g is actually alive!
*/
int deadinsidedead(group_t g) {
list_t ptr;
if (armynbp[grarmy[g]] == EOL)getarmynbp(grarmy[g]);
for (ptr = armynbp[grarmy[g]]; ptr != EOL; ptr = link[ptr])
if (A_ALIVE(list[ptr]) <= UNSETTLED)
return(FALSE);
if (armynbp[grarmy[g]] == EOL)return(FALSE);
return(TRUE);
}
static void markldr(sqr_t s)
{
list_t ptr, ptr2;
int bptr;
for (ptr = ldrflag[s]; ptr != EOL; ptr = link[ptr]) {
if (list[ptr] >= NUMGROUPS+NUMCONNS) {
if (eyeptr[list[ptr]-NUMGROUPS-NUMCONNS] != EOL) {
adflist(list[eyeptr[list[ptr]-NUMGROUPS-NUMCONNS]], &eyelist);
}
#ifdef CHECK
else {
outerror("eyerec error grpieces\n");
}
#endif
}
else if (list[ptr] >= NUMGROUPS)
addlist((listval_t)(list[ptr]-NUMGROUPS),&cnchgd);
else { /* mark the group and any eye that depends on this group */
gralive[list[ptr]] |= 32;
for (bptr = grpieces[list[ptr]]; bptr != -1; bptr = mvnext[bptr]) {
for (ptr2 = ldrflag[mvs[bptr]]; ptr2 != EOL; ptr2 = link[ptr2]) {
if (list[ptr2] >= NUMGROUPS+NUMCONNS) {
if (eyeptr[list[ptr2]-NUMGROUPS-NUMCONNS] != EOL) {
adflist(list[eyeptr[list[ptr2]-NUMGROUPS-NUMCONNS]], &eyelist);
}
}
}
}
}
}
}
static void markspot_all(sqr_t s) {
list_t ptr;
if (eyerec[s] != 0)
adflist(list[eyeptr[eyerec[s]]],&eyelist);
if (cnbrd[s] != EOL)
mrglist(cnbrd[s],&cnchgd);
if (lkbrd[s] != EOL)
mrglist(lkbrd[s],&cnchgd);
if (llbrd[s] != EOL)
mrglist(llbrd[s],&cnchgd);
if (ollbrd[s] != EOL)
mrglist(ollbrd[s],&cnchgd);
mrglist(ldrflag[s],&lookldr);
if (board[s] != NOGROUP) {
gralive[board[s]] |= 32;
mrglist(grcnp[board[s]],&cnchgd); /* all connections to this group */
if (grlibs[board[s]] < 4) {
/* reeval neighbors if low on liberties */
for (ptr = grnbp[board[s]]; ptr != EOL; ptr = link[ptr])
gralive[list[ptr]] |= 32;
}
}
markldr(s);
}
static void markspot(sqr_t s)
{
int bptr; /* mvs ptr, not list ptr */
list_t ptr;
if (eyerec[s] != 0)
adflist(list[eyeptr[eyerec[s]]],&eyelist);
if (cnbrd[s] != EOL)
mrglist(cnbrd[s],&cnchgd);
if (lkbrd[s] != EOL)
mrglist(lkbrd[s],&cnchgd);
if (llbrd[s] != EOL)
mrglist(llbrd[s],&cnchgd);
if (ollbrd[s] != EOL)
mrglist(ollbrd[s],&cnchgd);
mrglist(ldrflag[s],&lookldr);
if (board[s] != NOGROUP) {
gralive[board[s]] |= 32;
mrglist(grcnp[board[s]],&cnchgd); /* all connections to this group */
if (grlibs[board[s]] < 4) {
/* reeval neighbors if low on liberties */
for (ptr = grnbp[board[s]]; ptr != EOL; ptr = link[ptr])
gralive[list[ptr]] |= 32;
}
/* in case move added stone to group */
for (bptr = grpieces[board[s]]; bptr != -1; bptr = mvnext[bptr]) {
markldr(mvs[bptr]);
}
}
else { /* empty point */
markldr(s);
}
}
/* look at places where pieces were added or removed from board since last
* evaluation. mark neighboring groups, connections
* for reevaluation. Eyes are marked in g2eye.c in findeyelist
* if everything is TRUE, all groups will be reevaluated.
* if everything is 2, mark all connections for evaluation.
*/
static void markpcls(int everything) {
list_t ptr;
sqr_t s, sn, sn2, sn3, lptr;
int i, ldtmp, j, ldtm2, k, ldtm3, pptr;
list_t seen = EOL, markseen = EOL;
conn_t cn;
for (ptr = lookldr; ptr != EOL; ptr = link[ptr]) {
if (list[ptr] >= NUMGROUPS+NUMCONNS) {
if (eyeptr[list[ptr]-NUMGROUPS-NUMCONNS] != EOL)
adflist(list[eyeptr[list[ptr]-NUMGROUPS-NUMCONNS]],&eyelist);
}
else if (list[ptr] >= NUMGROUPS) {
cn = list[ptr]-NUMGROUPS;
if (cncnum[cn] != 0 || cnlknum[cn] != 0 || cnllnum[cn] != 0 || cnddnum[cn] != 0 || cnollnum[cn] != 0)
addlist(cn,&cnchgd);
}
else/* if (gralive[list[ptr]] != DEAD) */
gralive[list[ptr]] |= 32;
}
killist(&lookldr);
if (everything == 2 || pclsnext > 20) { /* just do them all */
for (lptr = 0; lptr < boardsquare; ++lptr) {
markspot_all(lptr);
if (ld[lptr] >= 4 && ld[lptr] <= 8 ||
ltrgd[lptr] == 0 && ltr1[lptr] != 0 && ld[lptr] != NOLD)
adflist(lptr,&eyelist);
}
return;
}
for (pptr = 0; pptr < pclsnext; ++pptr) {
s = pcls[pptr];
if (!addlist(s, &seen)) {
continue;
}
if (addlist(s, &markseen)) {
markspot(s);
}
i = fdir[s];
for (ldtmp = ldir[i]; i < ldtmp; ++i) {
sn = s + nbr[i];
if (board[sn] != NOGROUP && board[sn] != board[s]) {
if (addlist(sn, &markseen)) {
markspot(sn); /* another group */
}
}
else if (board[sn] == NOGROUP) { /* empty spot */
markspot(sn);
j = fdir[sn];
for (ldtm2 = ldir[j]; j < ldtm2; ++j) {
sn2 = sn + nbr[j];
if (sn2 == s)continue;
if (board[sn2] != NOGROUP && board[sn2] != board[s] ) {
if (addlist(sn2, &markseen)) {
markspot(sn2);
}
}
else if (board[sn2] == NOGROUP) { /* empty spot */
markspot(sn2);
k = fdir[sn2];
for (ldtm3 = ldir[k]; k < ldtm3; ++k) {
sn3 = sn2 + nbr[k];
if (sn3 == sn)continue;
if (addlist(sn3, &markseen)) {
markspot(sn3);
}
}
}
}
}
}
}
killist(&seen);
killist(&markseen);
}
/* get rid of the ladder flags for ladder g (group number, connection, or
* eye.
*/
void kill_ldrflags(listval_t g) { /* must be int since range bigger than NUMGROUPS */
list_t ptr;
for (ptr = grldr[g]; ptr != EOL; ptr = link[ptr])
dellist(g,&ldrflag[list[ptr]]);
killist(&grldr[g]);
}
/* return TRUE if group g can't be captured if it moves first.
* can't be captured if it gets mlibs+1 in one move, or if it
* can capture a neighboring group big enough. Assume g wins kos.
*/
int cantbecaptured(group_t g, int mlibs) {
list_t tmplist = EOL, ptr, ptr2;
group_t g2;
int libs,maxlibs,cn;
int libstoescape,snap;
libstoescape = mlibs+1;
maxlibs = grlibs[g];
/* look for neighboring groups which can be captured */
if (maxlibs < libstoescape) {
tmplist = EOL;
for (ptr = grnbp[g]; ptr != EOL; ptr = link[ptr]) {
g2 = (group_t)list[ptr];
if (grlibs[g2] == 1) {
snap = FALSE;
if (grsize[g2] == 1) {
for (ptr2 = grnbp[g2]; ptr2 != EOL; ptr2 = link[ptr2])
if (grlibs[list[ptr2]] == 1 &&
list[grlbp[list[ptr2]]] ==
list[grlbp[g2]] &&
lnbn[list[grlbp[g2]]] == 0) {
snap = TRUE;
break; /* snapback */
}
}
if (snap)continue;
grsavemove[g] = list[grlbp[g2]];
libs = grlibs[g];
cpylist(grlbp[g],&tmplist);
libs += grsize[g2];
if (mrglist(grlbp[g2],&tmplist) == 0)--libs;
/* if (grlibs[g] > 1) /* 3/04 play it out if only one liberty for under stones */
for (ptr2 = grnbp[g2]; ptr2 != EOL; ptr2 = link[ptr2])
if (list[ptr2] != g)
libs += mrglist(grlbp[list[ptr2]],&tmplist);
killist(&tmplist);
if (libs > maxlibs) {
maxlibs = libs;
addldrflag(list[grlbp[g2]],g);
}
if (maxlibs >= libstoescape)break;
}
}
}
/* look for extensions and connections for new libs */
if (maxlibs < libstoescape ||
grsavemove[g] == kosquare) /* look for a better move than taking a ko */
for (ptr = grlbp[g]; ptr != EOL; ptr = link[ptr]) {
addldrflag(list[ptr],g);
libs = grlibs[g];
cpylist(grlbp[g],&tmplist);
libs += mrglist(nblbp[list[ptr]],&tmplist)-1;
for (ptr2 = cnbrd[list[ptr]]; ptr2 != EOL; ptr2 = link[ptr2]) {
cn = list[ptr2];
if (cngr1[cn] != g && cngr2[cn] != g)continue;
if (cngr1[cn] == g)
g2 = cngr2[cn];
else
g2 = cngr1[cn];
if (cncnum[cn] != 0 || grlibs[g] > 2)
libs += mrglist(grlbp[g2],&tmplist);
}
if (libs > maxlibs) {
maxlibs = libs;
}
killist(&tmplist);
grsavemove[g] = list[ptr];
if (maxlibs >= libstoescape)break;
}
if (maxlibs >= libstoescape) {
return(TRUE);
}
return(FALSE);
}
/* canbethreatened returns true if a group can possibly be captured if
* the opponent moves first
*/
int canbethreatened(group_t g)
{
int maxlibs,mlibs;
if (grlibs[g] > taclibs[playlevel]) {
return FALSE;
}
maxlibs = getefflibs(g,taclibs[playlevel],g,&mlibs);
if (maxlibs > taclibs[playlevel]) {
return FALSE;
}
return TRUE;
}
/* return TRUE if g is a cutting stone (conservatively). armies aren't figured
* out yet so just look at nearby groups
*/
int cutstone(group_t g)
{
if (grnbp[g] == EOL || link[grnbp[g]] == EOL)
return FALSE;
return TRUE;
}
/* findcaptured finds all the groups which are tactically simple to
* capture even if group moves first and wins all kos. These contribute to
* eyes and unbreakable connections.
*/
static void findcaptured(int everything)
{
group_t g,g2;
list_t comblist = EOL; /* new dead groups, combine armirs */
list_t ptr,ptr2;
int lvl;
/* if (noladder)return; */
for (g = 0; g < maxgr; ++g) {
if (!grlv[g])continue;
if (!everything && gralive[g] < 32)continue;
if (grldr[g] != EOL)kill_ldrflags(g);
lvl = cancapsize[playlevel]*2;
if (grlibs[g] > taclibs[playlevel] || cantbecaptured(g,taclibs[playlevel])) {
if (gralive[g] == DEAD)gralive[g] |= 32;
}
else if (iscaptured(g,80,lvl,taclibs[playlevel],mvmost[playlevel],grcolor[g],g,&grsavemove[g],grcolor[g])) {
newdeadgroup(g, DEAD, pfac[DEAD]);
addlist(g,&comblist);
}
else if (gralive[g] == DEAD) {
gralive[g] |= 32;
}
if (gralive[g] == (32 | DEAD))
fixwasdeadgroup(g);
}
for (ptr = comblist; ptr != EOL; ptr = link[ptr]) {
g = (group_t)list[ptr];
for (ptr2 = grnbp[g]; ptr2 != EOL; ptr2 = link[ptr2])
if (gralive[list[ptr2]] != DEAD &&
!G_THREATENED(list[ptr2]))break;
if (ptr2 != EOL) {
g2 = (group_t)list[ptr2];
addlist(g,&armydeadgroups[grarmy[g2]]);
grdeadarmy[g] = grarmy[g2];
for (ptr2 = link[ptr2]; ptr2 != EOL; ptr2 = link[ptr2]) {
if (gralive[list[ptr2]] != DEAD &&
!G_THREATENED(list[ptr2])) {
combinearmy(grarmy[list[ptr2]],grarmy[g2]);
}
}
}
}
killist(&comblist);
}
/* bdead finds groups which are easily captured so they can count
* as connections and eyes, and groups that are threatened with capture
*/
extern int numgrtacnodes;
static void bdead(int everything)
{
group_t g;
int flag, color;
sqr_t workingmove;
findcaptured(everything); /* find DEAD groups */
flag = everything;
for (g = 0; g < maxgr; ++g) { /* find threatened groups */
if ( !grlv[g]) {
continue;
}
if (gralive[g] == DEAD) {
if (G_THREATENED(g)) {
grthreatened[g] = FALSE;
fixwasthgroup(g);
}
continue;
}
if (flag || gralive[g] >= 32) { /* found ladder cantidate */
if (grlibs[g] == 1 && gralive[g] != DEAD && !snapback(g, 1-grcolor[g])) {
if (iskopoint(list[grlbp[g]], &color)) {
grthreatened[g] = TRUE;
} else {
grthreatened[g] = 2;
}
grcapmove[g] = list[grlbp[g]];
newdeadgroup(g, WEAK_GROUP, pfac[WEAK_GROUP]);
gralive[g] |= 32;
}
else if ((grlibs[g] > 1) &&
gralive[g] != DEAD &&
canbethreatened(g)) {
if (iscaptured(g,80,cancapsize[playlevel],taclibs[playlevel],mvmost[playlevel],1-grcolor[g],g,&grcapmove[g],1-grcolor[g])) {
if (iscaptured(g,80,cancapsize[playlevel],taclibs[playlevel],mvmost[playlevel],1-grcolor[g],g,&workingmove,grcolor[g])) {
grcapmove[g] = workingmove; /* change capturing move to reflect certain capture */
if (grthreatened[g] != 2) {
grthreatened[g] = 2; /* unconditionally threatened */
newdeadgroup(g, WEAK_GROUP, pfac[WEAK_GROUP]);
}
}
else if (grthreatened[g] != 1) { /* this if experimental - 10/1/94 */
grthreatened[g] = 1;
newdeadgroup(g, WEAK_GROUP, pfac[WEAK_GROUP]);
}
gralive[g] |= 32;
}
else if (G_THREATENED(g)) {
grthreatened[g] = FALSE;
fixwasthgroup(g);
}
}
else if (G_THREATENED(g)) {
grthreatened[g] = FALSE;
fixwasthgroup(g);
}
if (gralive[g] == DEAD + 32) { /* 9/05 former dead groups get lower alive value for later checks of attributes */
newdeadgroup(g, WEAK_GROUP, pfac[WEAK_GROUP]);
gralive[g] |= 32;
}
}
} /* end of groups loop */
}
static void newdeadgroup(group_t g,int newalive,int newalprob) {
list_t ptr,ptr2;
sqr_t s;
armyeyes[grarmy[g]] = 0;
killist(&armyeyerecs[grarmy[g]]);
killist(&armyvitalpoints[grarmy[g]]);
if (newalive == DEAD)
grcapmove[g] = grsavemove[g] = NOSQUARE;
if (grdeadarmy[g] != NOARMY) {
addlist(list[armygroups[grdeadarmy[g]]],&splitlist);
}
if (armysize[grarmy[g]] > grsize[g]) {
addlist(g,&splitlist);
}
else if (newalive == DEAD) { /* wont be split, so delete dead armies here - dead group can't have deadarmies */
for (ptr = armydeadgroups[grarmy[g]]; ptr != EOL; ptr = link[ptr]) {
grdeadarmy[list[ptr]] = NOARMY;
}
killist(&armydeadgroups[grarmy[g]]);
}
#ifdef CHECK
if (newalive > DEAD)
outerror("Bad aliveness in newdeadgroup\n");
#endif
pscr = pscr + (newalprob - gralprob[g])*grsize[g]*
cfac[grcolor[g]];
gralive[g] = newalive;
gralprob[g] = gralval[g][0] = gralval[g][1] = newalprob;
markgroup(g);
for (ptr = grlbp[g]; ptr != EOL; ptr = link[ptr]) {
s = list[ptr];
if (gralive[lgr[s]] != DEAD)
lgr[s] = g;
}
mrglist(grcnp[g],&cnchgd); /* not redundant! */
if (G_THREATENED(g))
for (ptr = grnbp[g]; ptr != EOL; ptr = link[ptr])
for (ptr2 = grcnp[list[ptr]]; ptr2 != EOL; ptr2 = link[ptr2])
if (cncnum[list[ptr2]] == 1)
addlist(list[ptr2],&cnchgd);
}
/* group g changed from dead/notdead or threatened/not threatened, so
* mark anything relevant for reevaluation
*/
void markgroup(group_t g) {
int mptr; /* mvs ptr, not list ptr */
list_t ptr,ptr2,ptr3,elist = EOL;
sqr_t sn;
int x,y,i,ldtmp;
#ifdef CHECK
if (g >= NUMGROUPS || !grlv[g])
outerror("Markgroup bad group\n");
#endif
for (mptr = grpieces[g]; mptr != -1; mptr = mvnext[mptr])
for (ptr2 = ldrflag[mvs[mptr]]; ptr2 != EOL; ptr2 = link[ptr2])
if (list[ptr2] >= NUMGROUPS+NUMCONNS) {
if (eyeptr[list[ptr2]-NUMGROUPS-NUMCONNS] != EOL)
addlist((listval_t)(list[ptr2]-NUMGROUPS-NUMCONNS),&elist);
}
else if (list[ptr2] >= NUMGROUPS)
addlist((listval_t)(list[ptr2]-NUMGROUPS),&cnchgd);
for (ptr = grlbp[g]; ptr != EOL; ptr = link[ptr]) {
#ifdef CHECK
if (list[ptr] > 360)
outerror("terhd error markgroup\n");
#endif
if (cnbrd[list[ptr]] != EOL)
mrglist(cnbrd[list[ptr]],&cnchgd);
if (lkbrd[list[ptr]] != EOL)
mrglist(lkbrd[list[ptr]],&cnchgd);
if (llbrd[list[ptr]] != EOL)
mrglist(llbrd[list[ptr]],&cnchgd);
if (ollbrd[list[ptr]] != EOL)
mrglist(ollbrd[list[ptr]],&cnchgd);
if (eyerec[list[ptr]] != 0)addlist(eyerec[list[ptr]],&elist);
if (!S_NEUTRAL(list[ptr]) && ld[list[ptr]] > 5)
adflist(list[ptr],&eyelist);
i = fdir[list[ptr]];
for (ldtmp = ldiag[i]; i < ldtmp; ++i) {
if (board[list[ptr]+diags[i]] != g && eyerec[list[ptr]+diags[i]] != 0)
addlist(eyerec[list[ptr]+diags[i]],&elist);
}
for (ptr2 = nblbp[list[ptr]]; ptr2 != EOL; ptr2 = link[ptr2]) {
if (cnbrd[list[ptr2]] != EOL)
mrglist(cnbrd[list[ptr2]],&cnchgd);
if (lkbrd[list[ptr2]] != EOL)
mrglist(lkbrd[list[ptr2]],&cnchgd);
if (llbrd[list[ptr2]] != EOL)
mrglist(llbrd[list[ptr2]],&cnchgd);
if (ollbrd[list[ptr2]] != EOL)
mrglist(ollbrd[list[ptr2]],&cnchgd);
if (eyerec[list[ptr2]] != 0)
addlist(eyerec[list[ptr2]],&elist);
i = fdir[list[ptr2]];
for (ldtmp = ldiag[i]; i < ldtmp; ++i) {
if (board[list[ptr2]+diags[i]] != g && eyerec[list[ptr2]+diags[i]] != 0)
addlist(eyerec[list[ptr2]+diags[i]],&elist);
}
for (ptr3 = nblbp[list[ptr2]]; ptr3 != EOL; ptr3 = link[ptr3]) {
if (list[ptr3] == list[ptr])continue;
if (lkbrd[list[ptr3]] != EOL)
mrglist(lkbrd[list[ptr3]],&cnchgd);
if (llbrd[list[ptr3]] != EOL)
mrglist(llbrd[list[ptr3]],&cnchgd);
if (ollbrd[list[ptr3]] != EOL)
mrglist(ollbrd[list[ptr3]],&cnchgd);
}
}
}
for (mptr = grpieces[g]; mptr != -1; mptr = mvnext[mptr]) {
sn = mvs[mptr];
adflist(sn,&eyelist);
i = fdir[sn];
for (ldtmp = ldiag[i]; i < ldtmp; ++i) {
if (cnbrd[sn+diags[i]] != EOL)
mrglist(cnbrd[sn+diags[i]],&cnchgd);
if (board[sn+diags[i]] != g && eyerec[sn+diags[i]] != 0)
addlist(eyerec[sn+diags[i]],&elist);
}
x = xval[sn];
y = yval[sn];
if (x-4 < xmin)xmin = x-4;
if (x+4 > xmax)xmax = x+4;
if (y-4 < ymin)ymin = y-4;
if (y+4 > ymax)ymax = y+4;
}
for (ptr = grlbp[g]; ptr != EOL; ptr = link[ptr])
if (eyerec[list[ptr]] != 0)
addlist(eyerec[list[ptr]], &elist);
for (ptr = elist; ptr != EOL; ptr = link[ptr])
adflist(list[eyeptr[list[ptr]]],&eyelist);
killist(&elist);
}
/* group was threatened. Must update its connections and territory
* if nbr has THREAT connections, they must be reevaled.
*/
static void fixwasthgroup(group_t g) {
list_t ptr,ptr2;
markgroup(g);
grcapmove[g] = grsavemove[g] = NOSQUARE; /* only threatened groups have these moves */
for (ptr = grnbp[g]; ptr != EOL; ptr = link[ptr])
for (ptr2 = grcnp[list[ptr]]; ptr2 != EOL; ptr2 = link[ptr2])
if (cntype[list[ptr2]] == CN_THREAT)
addlist(list[ptr2],&cnchgd);
if (gralive[g] != DEAD)
for (ptr = grnbp[g]; ptr != EOL; ptr = link[ptr]) {
if (gralive[list[ptr]] == DEAD) {
if (grdeadarmy[list[ptr]] != NOARMY) {
combinearmy(grarmy[g],grdeadarmy[list[ptr]]);
}
else {
grdeadarmy[list[ptr]] = grarmy[g];
addlist(list[ptr],&armydeadgroups[grarmy[g]]);
}
}
}
}
/* group was dead and is no more. must update its armies, connections,
* eyes, and territory. Mark nearby connections for reeval.
* change its alive to 33 so patterns don't match dead
*/
static void fixwasdeadgroup(group_t g) {
list_t ptr;
int i,ldtmp;
if (grdeadarmy[g] != NOARMY) {
addlist(list[armygroups[grdeadarmy[g]]],&splitlist);
dellist(g,&armydeadgroups[grdeadarmy[g]]);
grdeadarmy[g] = NOARMY;
}
for (ptr = grlbp[g]; ptr != EOL; ptr = link[ptr]) {
if (!S_NEUTRAL(list[ptr]))
adflist(list[ptr], &eyelist);
i = fdir[list[ptr]];
for (ldtmp = ldir[i]; i < ldtmp; ++i) {
if (S_ALIVE(list[ptr]+nbr[i]) == DEAD)
lgr[list[ptr]] = board[list[ptr]+nbr[i]];
}
}
markgroup(g);
for (ptr = grnbp[g]; ptr != EOL; ptr = link[ptr]) {
if (gralive[list[ptr]] == DEAD) {
if (grdeadarmy[list[ptr]] != NOARMY) {
combinearmy(grarmy[g],grdeadarmy[list[ptr]]);
}
else {
grdeadarmy[list[ptr]] = grarmy[g];
addlist(list[ptr],&armydeadgroups[grarmy[g]]);
}
}
}
}
static void fixarmies(void) {
list_t split1,ptr;
split1 = EOL; /* fix up armies */
for (ptr = splitlist; ptr != EOL; ptr = link[ptr])
addlist(grarmy[list[ptr]],&split1);
killist(&splitlist);
for (ptr = charmy; ptr != EOL; ptr = link[ptr]) {
if (board[list[ptr]] != NOGROUP)
addlist(grarmy[board[list[ptr]]],&split1);
}
killist(&charmy);
for (ptr = split1; ptr != EOL; ptr = link[ptr])
splitarmy((army_t)list[ptr]);
killist(&split1);
}
/* starts 8 points, reduces to 1/2 point at 1/2 boardsize moves
* starting value should be the komi, plus a point to account for chinese counting, so 8 points
* value of making an oba - a big territory only gote move.
* should be about half the score change due to a move, so that oba is always added to the side to move to make the
* score independent of who is to move.
* starts larger on smaller boards
* oba must be smaller than actual temperature of the game to avoid strange moves
* so start it at 7 rather than 8
*/
/* min oba must be greater than 25 since that is value of filling dame */
/* must be less than 100 to avoid stupid ataris at the end of the game */
/* since add oba after each move, it should be half the value of a big move
* at this point in the game, so it should end at 1/2 point */
# define MINOBAVAL 25
#define MAXOBAVAL (7*50)
void getobaval()
{
double progress;
int lastmove; /* last move for reducing obaval */
if (problemflag == SOLVEPROBLEM || problemflag == SOLVELDPROBLEM) {
obaval = 150;
return;
}
lastmove = boardsquare/2; /* 180 for a full size board */
if ((signed)msptr > lastmove) {
obaval = MINOBAVAL;
}
progress = (((double)lastmove - msptr))/lastmove; /* 1 to 0 from move 1 to move 180 */
if (progress < 0)
progress = 0;
obaval = MINOBAVAL + (int)((MAXOBAVAL - MINOBAVAL) * progress); /* 8 points maximum */
if (playlevel < SENTELEVEL)
obaval /= 2;
}
/* figure out the aliveness of all groups on board.
* as side effect, must figure out connections and
* radiate influence.
*
* if everything is FALSE, do an incremental evaluation with cached tactics, connections
* if everything is TRUE, reevaluate life of everything, all conns, tactics.
* if everything is 2, reevaluate all eyes as well.
*/
extern int numlifecalls, debugnumlifecalls, numlifetacnodes, numfixgrtacnodes;
extern int numcntacnodes;
extern int numeyetacnodes;
void life(int everything)
{
int nodes;
int lnodes = numnodes;
if (!everything && pclsnext == 0)
return; /* nothing changed - no need to reeval */
numlifecalls++; /* cant/must count all calls - during lookahead should be no extra ones */
/* otherwise debug and release play differently - since no changes for pass */
debugnumlifecalls++;
if (pclsnext >= NUMPCLS)
everything = 2; /* overflowed change array */
splitlist = EOL;
markpcls(everything); /* mark eyes, connections, and groups for reeval */
nodes = numnodes;
bdead(everything); /* find groups which are captured, threatened - before fixli and fixcnprot */
numgrtacnodes += (numnodes - nodes);
fixdistance(); /* distance to nearest stones of each color */
nodes = numnodes;
fixcnprot(everything); /* find protected connections - before fixarmies */
numcntacnodes += (numnodes - nodes);
fixarmies(); /* collect groups into armies */
upltr(); /* find edge territory - before fixli since eye eval uses ltr values */
/* after fixcnprot since ltr values depend on good connection */
nodes = numnodes;
fixli(); /* find eyes (must be after fixarmies!) */
numeyetacnodes += (numnodes - nodes);
nodes = numnodes;
fixgralive(); /* make all aliveness values correct */
numfixgrtacnodes += (numnodes - nodes);
fixlgr();
radiateterr(EOL); /* radiate influence for territory and update liberty values */
pclsnext = 0;
gralvalid = TRUE; /* gralive values are valid */
numlifetacnodes += (numnodes - lnodes);
}
/* if a dead army has a shared connection to a not dead army,
* promote it to unsettled
* if dead army has connection to weak_potential army, propmote to weak_potential
*/
static int connalive(army_t army, int *bgl, int *bprob) {
list_t ptr, cnptr, ptr2;
group_t g;
int gl, alprob, bestgl = DEAD, bestalprob = -50;
if (A_ALIVE(army) < WEAK || A_ALIVE(army) == DEAD)
return FALSE;
for (ptr = armygroups[army]; ptr != EOL; ptr = link[ptr]) {
for (cnptr = grcnp[list[ptr]]; cnptr != EOL; cnptr = link[cnptr]) {
g = cngr1[list[cnptr]];
if (grarmy[g] == army)
g = cngr2[list[cnptr]];
if (grarmy[g] == army)
continue;
if (gralive[g] >= WEAK)
continue;
if (cnprot[list[cnptr]] >= SHARED_CONNECT) {
gl = gralive[g];
alprob = gralprob[g]-10;
if (alprob < -50)
alprob = -50;
if (gl <= ALIVE) {
gl = UNSETTLED_DEAD;
alprob = 0;
}
if (gl < bestgl || gl == bestgl && alprob > bestalprob) {
bestgl = gl;
bestalprob = alprob;
}
/* newalive(army, gl, alprob, -50); 3/04 - get best for reproducibility */
}
else if (cnprot[list[cnptr]] >= CAN_CONNECT) {
gl = WEAK_POTENTIAL;
alprob = -48;
if (gl < bestgl || gl == bestgl && alprob > bestalprob) {
bestgl = gl;
bestalprob = alprob;
}
/* newalive(army, gl, alprob, -50); */
}
}
}
if (armynbp[army] == EOL)
getarmynbp(army);
for (ptr = armynbp[army]; ptr != EOL; ptr = link[ptr]) {
if (A_ALIVE(list[ptr]) != DEAD)continue;
if (armynbp[list[ptr]] == EOL)
getarmynbp((army_t)list[ptr]);
for (ptr2 = armynbp[list[ptr]]; ptr2 != EOL; ptr2 = link[ptr2]) {
if (list[ptr2] == army)continue;
gl = A_ALIVE(list[ptr2]);
alprob = gralprob[list[armygroups[list[ptr2]]]]-10;
if (alprob < -50)
alprob = -50;
if (gl <= ALIVE) {
gl = UNSETTLED_DEAD;
alprob = 0;
}
if (gl < bestgl || gl == bestgl && alprob > bestalprob) {
bestgl = gl;
bestalprob = alprob;
}
}
}
if (bestgl < DEAD) {
/* newalive(army, bestgl, bestalprob, -50); */
*bgl = bestgl;
*bprob = bestalprob;
return TRUE;
}
return FALSE;
}
/* fixgralive finds the new alive value for all marked groups
* and their associated armies
* groups with aliveness DEAD have already been found
* first pass finds point eyes, dead group eyes, surrounded
* territory and the liberties of the army. If this is enough for 2 eyes,
* we are done.
* second pass looks at edgeeyes, and tries to find miai for 2 eyes among
* connecting to another army, expanding along edge, capturing threatened
* group, playing a vital eyemaking point;
* third pass radiates thickness, and judges weak groups based on
* strength of surrounding groups, and ability to run away.
*/
#define MAXGL 200
extern int nummiaitacnodes;
static void fixgralive(void)
{
list_t armylist, armylist2, miailist, changed = EOL, ptr;
int numeyes, numsafeeyes, alprob;
int gl[MAXGL], prob[MAXGL], nextval, i, j;
army_t army, carmy[MAXGL];
group_t g;
int numn;
armylist = armylist2 = miailist = EOL;
nextpot = 1;
for (g = 0; g < maxgr; ++g) {
if (!grlv[g]) {
gralive[g] &= 31;
continue;
}
if (gralive[g] == DEAD) {
initarmyalive(grarmy[g]);
armylibs[grarmy[g]] = grlibs[g];
cpylist(grlbp[g], &armylbp[grarmy[g]]);
continue;
}
else if (addlist(grarmy[g], &armylist))
initarmyalive(grarmy[g]);
}
for (ptr = armylist; ptr != EOL; ptr = link[ptr])
getarmylibs((army_t)list[ptr]); /* every army gets a liberty list */
for (ptr = armylist; ptr != EOL; ptr = link[ptr]) {
army = (army_t)list[ptr];
if (armygroups[army] == EOL ||
armysize[army] == 0 ||
list[armygroups[army]] >= NUMGROUPS ||
!grlv[list[armygroups[army]]]) {