-
Notifications
You must be signed in to change notification settings - Fork 1
/
g29.c
4700 lines (4391 loc) · 155 KB
/
g29.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 "g2basic.h"
#include <memory.h>
#ifdef G2DEBUGOUTPUT
# include <stdio.h>
#endif
# define NUMGEN 120
# undef S_NEUTRAL
# undef NEUTRALLD
static int livesordies(sqr_t starts,int maxlibs,int tm,int color,int ply,listval_t ldrno,int *efflibs, int *maxefflibs, group_t gs, int maxply,int *result, int winsko, int nodesleft);
static void defonelib(int ply,sqr_t starts,group_t g,int tm,group_t gs,listval_t ldrno,int winsko);
static void gendefmoves(int ply, sqr_t starts,group_t g,int tm,listval_t ldrno,int maxlibs,int winsko,int maxbr, int efflibs, int maxefflibs);
/*static void deftwolibs(int ply, sqr_t starts,group_t g,int tm,group_t gs); */
static void genatkmoves(int ply, sqr_t starts,group_t g,int tm,listval_t ldrno,int maxlibs,int winsko,int maxbr);
static void genrestatk(int ply, sqr_t starts,group_t g,int tm,group_t gs,listval_t ldrno,int maxlibs,int winsko);
static void sortmoves(int mvp, int winsko, int ply, int maxbr, int tm, char *name, group_t g, group_t gs, int maxlibs);
static sqr_t jump(sqr_t lib, group_t g);
static void generatemoves(int ply, sqr_t starts, group_t g, int tm, listval_t ldrno, int maxlibs, int winsko, int maxbr, int efflibs, int maxefflibs);
static int makeldrmove(int ply, sqr_t starts, group_t g, int tm, listval_t ldrno, int maxlibs, int winsko, int maxbr);
int solidconnect(sqr_t s, int c, int winsko);
extern int cfac[3];
extern int numtacnodes; /* number of tactical nodes used in a single search */
extern int maxbranchdepth[NUMLEVELS];
extern unsigned char conncapsize[NUMLEVELS];
extern char connmost[NUMLEVELS];
static int numnn; /* number of nodes that have been visited so far */
static sqr_t mmov[NUMGEN]; /* move generated during lookahead */
static int mval[NUMGEN]; /* value of move generated */
static int mvp; /* pointer into these arrays */
static int maxnply[NUMPLY]; /* maximum nodes to try at this ply or below */
static int sply[NUMPLY]; /* first move in mvs[] at each ply */
static int eply[NUMPLY]; /* last move in mvs[] at each ply */
/*#define MOVESORT */
#ifdef MOVESORT
#define NUMBEST 4
static int mprob[NUMGEN]; /* prob that this move will work */
static sqr_t bestresp[PASS][NUMBEST];
static int bestcount[PASS][NUMBEST+1];
static int worstcount[PASS][NUMBEST+1];
#endif
void initbestresp() {
#ifdef MOVESORT
int i, j;
for (i = 0; i < PASS; ++i) {
for (j = 0; j < NUMBEST; ++j) {
bestresp[i][j] = NOPOINT;
bestcount[i][j] = 0;
worstcount[i][j] = 0;
}
bestcount[i][NUMBEST] = 0;
worstcount[i][NUMBEST] = 0;
}
#endif
}
#ifdef MOVESORT
/* 0 to 100 likelihood that this move will work. -1 is unknown */
int bestprob(sqr_t move, sqr_t resp) {
int i;
// return -1;
for (i = 0; i < NUMBEST; ++i) {
if (bestresp[move][i] == resp && bestcount[move][i] > 50)
return 100*bestcount[move][i]/(bestcount[move][i]+worstcount[move][i]);
}
return -1;
}
/* resp refutes move */
void addbestresp(sqr_t move, sqr_t resp) {
int i;
#ifdef CHECK
if (move < 0 || move > PASS || resp < 0 || resp > PASS)
outerr("bad move");
#endif
for (i = 0; i < NUMBEST; ++i) {
if (bestresp[move][i] == resp) {
bestcount[move][i]++;
return;
}
}
for (i = 0; i < NUMBEST; ++i) {
if (bestcount[move][i] == 0) {
bestresp[move][i] = resp;
bestcount[move][i] = 1;
return;
}
}
bestcount[move][NUMBEST]++; /* unmatched */
}
/* resp refutes move */
void addworstresp(sqr_t move, sqr_t resp) {
int i;
for (i = 0; i < NUMBEST; ++i) {
if (bestresp[move][i] == resp) {
worstcount[move][i]++;
return;
}
}
worstcount[move][NUMBEST]++; /* unmatched */
}
#endif
#ifdef G2DEBUGOUTPUT
// dump generated tactical moves for group at cursorpos.
// tm moves first, winsoko wins the kos
void dumpgenmoves(sqr_t cursorpos, int tm, int winsko) {
group_t g;
int debugtmp;
int efflibs, maxlibs, result, ret;
char buf[100];
g = board[cursorpos];
if (g == NOGROUP)
return;
debugtmp = debug;
debug = 2000+g;
numnn = 0;
ret = livesordies(cursorpos, taclibs[playlevel], tm, S_COLOR(cursorpos), 0, NOGROUP, &efflibs,
&maxlibs, board[cursorpos], 10, &result, winsko, 100);
efflibs = getefflibs(g, 4 ,NOGROUP, &maxlibs);
sprintf(buf, "libs %d, eff libs %d-%d, livesordies: %d, %d\n", grlibs[g], efflibs, maxlibs, ret, result);
outerr(buf);
generatemoves(1, cursorpos, g, tm, NOGROUP, taclibs[playlevel], winsko, mvmost[playlevel], efflibs, maxlibs);
debug = debugtmp;
}
#endif
/* ldrhash are xor of the zobhash for all w or b stones
* added or removed from the board. xor a zob when a stone is played
* and for each stone is captured group. xor again for stones removed
* or groups restored. ldrhash is a 64 bit code for each
* position.
*/
int ldrhash[2];
static int hasht[256][2]; /* hash table of ldrhash values for all white stone changes */
/* 0-lose, 1-win, 5-not set yet */
static char hres[256]; /* result for this position */
static int hkosquare[256]; /* ko square must be the same as well as piece positions */
static int htomove[256]; /* side to move must be the same as well */
/* hash ldrhash down to 8 bits */
int makehash(void) {
int h = ldrhash[0] ^ ldrhash[1];
h = h ^ (h>>16);
h = h ^ (h >> 8);
return (h&0xff);
}
void sethash(int score, int tm) {
int h = makehash(); /* set a hash table entry */
hres[h] = score;
hasht[h][0] = ldrhash[0];
hasht[h][1] = ldrhash[1];
hkosquare[h] = kosquare;
htomove[h] = tm;
}
/* put stone at s, color c and check if group with stone at gs can be captured
* if ctm moves first. Return TRUE if can be captured.with winsko winning all kos
*/
int canbecaptured(sqr_t s, int c, sqr_t gs, int ctm, int winsko, int depth, int size, int libs, listval_t ldrno)
{
int can_cap;
sqr_t tmp;
mvs[msptr] = s;
mvcolor[msptr] = (char)c;
if (lupdate(msptr)) {
#ifdef CHECK
if (debug && showtactics > 1)
outstone(s,"P");
#endif
++msptr;
if (board[gs] != NOGROUP)
can_cap = iscaptured(board[gs], depth, size, libs, connmost[playlevel], ctm, ldrno, &tmp, winsko);
else
can_cap = TRUE;
--msptr;
}
else
can_cap = FALSE;
ldndate(msptr);
return(can_cap);
}
static void zerowins() {}
static void zerowinseye() {}
static void firstwins() {}
static void firstwinseye() {}
static void secondwins() {}
static void secondwinseye() {}
static void thirdwins() {}
static void thirdwinseye() {}
static void fourthwins() {}
static void fourthwinseye() {}
static void morewins() {}
static void morewinseye() {}
static void returnsfalselibs() {}
static void returnsfalselibseye() {}
/* static void returnsfalsenodes() {} */
static void returnsfalse() {}
static void returnsfalseeye() {}
static void returnstrue() {}
static void returnstrueeye() {}
/* iscaptured does a tactical analysis of a group. It returns true if
* the group is captured and false if it can escape. g is the group number
* to be checked. maxply, maxnodes, and maxlibs control the depth, size
* and complexity of the search.
* maxply is the maximum depth of the search
* maxnodes is the maximum number of nodes to look at not counting
* nodes where the move generator only generates one move
* (only counts nodes that branch) MUST BE < 128!
* maxlibs is the maximum number of liberties. if the group gets more
* liberties than maxlibs it has escaped.
* maxbr is the maximum branch factor. Look at maxbr+1 moves for first 3
* ply, maxbr moves up to maxbranch, and one or two moves after maxbranch.
* tm is the color to move first.
* ldrno is the index of this search in the grldr array (or NOGROUP)
* return the move which worked at the first ply in move
* moves made in this search will be linked in to the
* grldr and ldrflag lists unless ldrno is NOGROUP
* winsko is the color that wins any ko (BLACKCOLOR, WHITECOLOR, NOCOLOR)
*/
int iscaptured(group_t g, int maxply, int maxnodes, int maxlibs, int maxbr,
int tm, listval_t ldrno, sqr_t *movethatworks, int winsko) {
int scrply[NUMPLY]; /* score at each ply 1=wins, -1 = loses */
int newnode,done,ply,leaf,color,madeamove;
int h;
sqr_t starts;
int nbptr,ptr,efflibs,maxefflibs;
group_t g2;
#ifdef CHECK
char buf[100];
int oldmaxnply;
#endif
ASSERT(list[EOL] == 0xffff);
*movethatworks = NOSQUARE;
if (grlibs[g] == 1) {
*movethatworks = list[grlbp[g]];
if (tm == 1-grcolor[g] && grsize[g] == 1 && snapback(g, winsko)) {
return FALSE; /* 9/05 can't capture stone in a snapback */
}
if (tm == grcolor[g])
for (ptr = grnbp[g]; ptr != EOL; ptr = link[ptr])
if (grlibs[list[ptr]] == 1 && grsize[list[ptr]] > 1)
*movethatworks = list[grlbp[list[ptr]]];
}
for (ptr = grlbp[g]; ptr != EOL; ptr = link[ptr])
if (lnbn[list[ptr]] == 3)*movethatworks = list[ptr];
#ifdef CHECK
if (ldrno >= NUMGROUPS+NUMCONNS+NUMEYERECS ||
ldrno < NUMGROUPS-1 && !grlv[ldrno]) {
sprintf(buf,"bad ldrno %d in iscaptured\n",ldrno);
outerror(buf);
}
if (g == NOGROUP) {
outerror("iscaptured called with NOGROUP!\n");
return TRUE;
}
#endif
numnn = 0;
if (maxply > NUMPLY - 5)
maxply = NUMPLY - 5;
starts = mvs[grpieces[g]];
/* mark neighboring groups */
if (ldrno != NOGROUP) {
for (nbptr = grnbp[g]; nbptr != EOL; nbptr = link[nbptr]) {
g2 = (group_t)list[nbptr];
if (grlibs[g2] > 4)continue;
if (addlist(ldrno,&ldrflag[mvs[grpieces[g2]]])) {
adflist(mvs[grpieces[g2]],&grldr[ldrno]);
}
}
}
ldrhash[0] = ldrhash[1] = 0; /* clear the hash and hash table. */
memset(hres, 5, 256);
done = FALSE;
color = grcolor[g];
ply = 1;
sply[0] = msptr - 1;
eply[0] = msptr;
sply[1] = eply[0];
scrply[0] = -1;
scrply[1] = -1;
maxnply[1] = maxnodes;
newnode = TRUE;
while(!done) { /* this should be recursive */
leaf = FALSE;
madeamove = FALSE;
if (newnode) { /* first time to this node */
h = makehash();
if (hres[h] != 5 &&
hasht[h][0] == ldrhash[0] &&
hasht[h][1] == ldrhash[1] &&
hkosquare[h] == kosquare &&
htomove[h] == tm) {
scrply[ply] = hres[h];
leaf = TRUE;
#ifdef CHECK
if (debug == 2000+(unsigned int)g && leaf) {
sprintf(buf, "hash table hit found. val %d color %d. Hit enter\n", hres[h], tm);
outerr(buf);
waitaction(); clearerror();
}
#endif
}
else
/* is this end of this branch? */
leaf = livesordies(starts,maxlibs,tm,color,ply,ldrno,&efflibs,&maxefflibs,g,maxply,&scrply[ply],winsko,maxnply[ply]);
#ifdef CHECK
if (debug == 2000+(unsigned int)g && leaf) {
outerr("Leaf found. Hit enter\n"); waitaction(); clearerror();
}
#endif
if (!leaf) { /* not end of branch, generate move list */
generatemoves(ply,starts,g,tm,ldrno,maxlibs,winsko,maxbr,efflibs,maxefflibs);
#ifdef CHECK
if (eply[ply]-sply[ply] > 20)
outerr("too many moves, over 20");
#endif
}
}
#ifdef MOVESORT
if (ply > 2 && !newnode && scrply[ply] == 1) { /* have a winning move (sply[ply-1]) */
addbestresp(mvs[sply[ply-2]], mvs[sply[ply-1]]);
}
if (ply > 2 && !newnode && scrply[ply] == -1) { /* have a winning move (sply[ply-1]) */
addworstresp(mvs[sply[ply-2]], mvs[sply[ply-1]]);
}
#endif
if (!leaf && /* need to make a move to next ply */
scrply[ply] != 1 && /* not beta cutoff */
(newnode || numnn < maxnply[ply])) { /* no new siblings if out of nodes */
#ifdef CHECK
if (debug == 2000+(unsigned int)g) {
sprintf(buf,"ply %d numnn-%d maxnply-%d\n",ply,numnn,maxnply[ply]);
outerr(buf);
}
#endif
madeamove = makeldrmove(ply,starts,g,tm,ldrno,maxlibs,winsko,maxbr);
if (madeamove) {
if (ply == 1) {
*movethatworks = mvs[sply[ply]];
}
++ply;
tm = 1-tm;
scrply[ply] = -1; /* assume not winning at new ply */
newnode = TRUE;
}
}
if (leaf || !madeamove) { /* reached bottom of this line or end of moves */
if (ply == 1) {
done = TRUE;
}
else {
sethash(scrply[ply], tm);
--ply;
#ifdef CHECK
oldmaxnply = maxnply[ply+1];
if (debug == 2000+(unsigned int)g) {
fixsd(sply[ply],FALSE);
}
#endif
ldndate(sply[ply]);
tm = 1 - tm;
++sply[ply];
scrply[ply] = -scrply[ply+1];
if (sply[ply] < eply[ply] && ply <= 2 && maxnply[ply] - numnn < maxnodes/5)
maxnply[ply] = numnn + maxnodes/5; /* 1/03 make sure all moves tried at first two ply */
if (sply[ply] < eply[ply] && /* at least one more move left */
maxnply[ply] - numnn >= 25)
maxnply[ply+1] = numnn + ((maxnply[ply]-numnn)*4)/5;
else
maxnply[ply+1] = maxnply[ply];
newnode = FALSE;
#ifdef CHECK
if (debug == 2000+(unsigned int)g) {
if (leaf)
outerr("Backing up - leaf node");
else if (scrply[ply+1] == 1)
outerr("Backing up - have a win - no need for more moves");
else if (sply[ply+1] >= eply[ply+1])
outerr("Backing up - tried all moves");
else {
sprintf(buf, "backing up - numnn %d too big ( >= %d)", numnn, oldmaxnply);
outerr(buf);
}
waitaction(); clearerror();
}
#endif
}
}
} /* end of while !done loop */
if ((scrply[1] * cfac[tm == color]) == 1) {
returnsfalse();
if (maxlibs == 2)
returnsfalseeye();
if (sply[1] == eply[0]) {
returnsfalselibs();
if (maxlibs == 2)
returnsfalselibseye();
}
return(FALSE);
}
else {
if (sply[1] == eply[0]) {
zerowins();
if (maxlibs == 2)
zerowinseye();
}
else if (sply[1] == eply[0]+1) {
firstwins();
if (maxlibs == 2)
firstwinseye();
}
else if (sply[1] == eply[0]+2) {
secondwins();
if (maxlibs == 2)
secondwinseye();
}
else if (sply[1] == eply[0]+3) {
thirdwins();
if (maxlibs == 2)
thirdwinseye();
}
else if (sply[1] == eply[0]+4) {
fourthwins();
if (maxlibs == 2)
fourthwinseye();
}
else {
morewins();
if (maxlibs == 2)
morewinseye();
}
returnstrue();
if (maxlibs == 2)
returnstrueeye();
return(TRUE);
}
}
/* estimate how many new (not total!) liberties can get for liberty list of g at s */
/* s is liberty of g or captures single liberty adjacent group */
/* can stop early if reach max */
static int newliberties(group_t g, list_t liblist, sqr_t s, int max) {
int c,newlibs = 0;
list_t ptr,ptr2,ptr3,connlist = EOL;
c = grcolor[g];
for (ptr = nbgrp[s][1-c]; ptr != EOL; ptr = link[ptr])
if (grlibs[list[ptr]] == 1) { /* move capures enemy */
newlibs += grsize[list[ptr]];
for (ptr2 = grnbp[list[ptr]]; ptr2 != EOL; ptr2 = link[ptr2])
if (list[ptr2] != g)
newlibs += mrglist(grlbp[list[ptr2]],&connlist);
}
if (connlist == EOL && link[nbgrp[s][c]] == EOL) /* simple case */
return newlist(liblist,nblbp[s]) + newlibs - 1;
mrglist(liblist,&connlist);
newlibs += mrglist(nblbp[s],&connlist) - 1;
for (ptr = nbgrp[s][c]; ptr != EOL; ptr = link[ptr])
if (list[ptr] != g) { /* friendly connection */
newlibs += mrglist(grlbp[list[ptr]],&connlist);
if (newlibs >= max)
break;
for (ptr2 = grlbp[list[ptr]]; ptr2 != EOL; ptr2 = link[ptr2]) {
if ((sqr_t)list[ptr2] == s)continue;
if (lnbn[list[ptr2]] <= 1 && lnbf[list[ptr2]][1-c] == 0)
for (ptr3 = nbgrp[list[ptr2]][c]; ptr3 != EOL; ptr3 = link[ptr3])
if (list[ptr3] != list[ptr])
newlibs += mrglist(grlbp[list[ptr3]],&connlist);
}
}
if (connlist != EOL)killist(&connlist);
return newlibs;
}
#ifdef G2DEBUGOUTPUT
void dumpnewlibs(sqr_t s) {
list_t ptr;
int libs;
char buf[20];
if (board[s] == NOGROUP) {
outerr("Click on a group");
return;
}
for (ptr = grlbp[board[s]]; ptr != EOL; ptr = link[ptr]) {
libs = newliberties(board[s], grlbp[board[s]], list[ptr], 100);
sprintf(buf,"%d",libs);
outstone(list[ptr],buf);
}
}
#endif
/* generate moves */
static void generatemoves(int ply, sqr_t starts, group_t g, int tm, listval_t ldrno, int maxlibs, int winsko, int maxbr, int efflibs, int maxefflibs) {
ASSERT(list[EOL] == 0xffff);
maxnply[ply+1] = maxnply[ply];
/* generate move list for this ply */
if (tm == grcolor[g])
gendefmoves(ply,starts,g,tm,ldrno,maxlibs,winsko,maxbr,efflibs, maxefflibs);
else
genatkmoves(ply,starts,g,tm,ldrno,maxlibs,winsko,maxbr);
#ifdef CHECK
if (eply[ply]-sply[ply] > 6)
outerr("too many moves, over 6");
#endif
if (eply[ply] - sply[ply] == 1)--numnn; /* don't count forced moves */
}
/* make a move from the generated move list */
static int makeldrmove(int ply, sqr_t starts, group_t g, int tm, listval_t ldrno, int maxlibs, int winsko, int maxbr) {
int mademove;
group_t g2;
#ifdef CHECK
char buf[100];
#endif
/* make best move */
mademove = FALSE;
/* if (numnn <= maxnply[ply]) */
while(!mademove && sply[ply] < eply[ply]) {
upldrflags(sply[ply],ldrno);
mademove = lupdate(sply[ply]);
if (kosquare != NOSQUARE && 1-tm == winsko)mademove = FALSE;
if (mvs[sply[ply]] != PASS) {
g2 = board[mvs[sply[ply]]];
if (g2 == board[starts] && grlibs[g2] == 1)
mademove = FALSE;
else if (grlibs[g2] == 1 && grsize[g2] > 4) /* throwins ok */
mademove = FALSE;
}
if (!mademove) {
#ifdef CHECK
if (debug == 2000+(unsigned int)g) {
sprintf(buf,"bad or illegal move, tm %d, winsko %d ",tm,winsko);
outerr(buf);
ssqr(mvs[sply[ply]],buf);
outerr(buf);
}
#endif
ldndate(sply[ply]);
++sply[ply];
}
}
if (mademove) { /* found a move */
++numnn; /* count a node */
++numtacnodes;
#ifdef CHECK
if (debug == 2000+(unsigned int)g) {
fixlm(mvs[sply[ply]], mvcolor[sply[ply]]);
}
#endif
}
return(mademove);
}
/* generate moves to defend one liberty group gs
* rules:
* pull out of atari, if end with two liberties
* by entering open space or connecting
* capture neighbor with one liberty
* make a ko if winsko
*/
static void defonelib(int ply, sqr_t starts, group_t g, int tm, group_t gs, listval_t ldrno, int winsko) {
int mvptr,i,connect,numlibs,twolibs,l1,l2;
sqr_t s, sn;
int c, cancapture = FALSE;
list_t tmplist=EOL, ptr, ptr2;
#ifdef CHECK
char buf[60],buf2[10];
#endif
mvptr = sply[ply];
g = g;
starts = starts;
c = grcolor[gs];
s = list[grlbp[gs]];
/* capture one liberty neighbor */
for (ptr = grnbp[gs]; ptr != EOL; ptr = link[ptr]) {
if (ldrno != NOGROUP && addlist(ldrno,&ldrflag[mvs[grpieces[list[ptr]]]])) {
adflist(mvs[grpieces[list[ptr]]],&grldr[ldrno]);
}
if (grlibs[list[ptr]] == 1 &&
((sqr_t)list[grlbp[list[ptr]]] != kosquare ||
winsko == tm)) {
if (lnbn[s] != 0 || (sqr_t)list[grlbp[list[ptr]]] != s ||
grsize[list[ptr]] > 1) {
mvs[mvptr] = list[grlbp[list[ptr]]];
mvcolor[mvptr++] = (char)tm;
if ((sqr_t)list[grlbp[list[ptr]]] == s)cancapture = TRUE;
#ifdef CHECK
if (debug == 2000+(unsigned int)g) {
outerr("defonelib captures\n");
if (winsko == tm)
outerr("I win kos");
}
#endif
}
}
}
/* pull out of atari */
twolibs = lnbn[s] >= 2;
connect = FALSE;
if (lnbn[s] < 2) { /* can connect or capture? */
numlibs = lnbn[s];
cpylist(nblbp[s],&tmplist);
addlist(s,&tmplist);
for (ptr = nbgrp[s][c]; ptr != EOL; ptr = link[ptr])
if ((group_t)list[ptr] != gs)
numlibs += mrglist(grlbp[list[ptr]],&tmplist);
for (ptr = nbgrp[s][1-c]; ptr != EOL; ptr = link[ptr])
if (grlibs[list[ptr]] == 1) {
numlibs += grsize[list[ptr]];;
if (numlibs < 2)
for (ptr2 = grnbp[list[ptr]]; ptr2 != EOL; ptr2 = link[ptr2])
if (list[ptr2] != gs)
numlibs += mrglist(grlbp[list[ptr2]],&tmplist);
}
if (numlibs > 1)connect = TRUE;
killist(&tmplist);
}
if (lnbn[s] == 2 && lnbf[s][tm] == 1 && !connect) {
l1 = list[nblbp[s]];
l2 = list[link[nblbp[s]]];
if (lnbf[l1][1-c] == 0 && lnbf[l2][1-c] == 0 &&
((lnbn[l1] >= 2 && lnbn[l2] <= 2) ||
(lnbn[l2] >= 2 && lnbn[l1] <= 2)))twolibs = FALSE;
}
if (!cancapture && (twolibs || connect)) {
mvs[mvptr] = s;
mvcolor[mvptr++] = (char)tm;
# ifdef CHECK
if (debug == 2000+(unsigned int)g)outerr("defonelib pulling out of atari\n");
#endif
}
/* make a ko */
if (winsko == tm && grsize[gs] == 1 && lnbf[s][1-c] == 0 && lnbn[s] == 1) {
sn = list[nblbp[s]];
if (lnbn[sn] > 1 || lnbf[sn][c]) {
mvs[mvptr] = sn;
mvcolor[mvptr++] = (char)tm;
# ifdef CHECK
if (debug == 2000+(unsigned int)g)outerr("defonelib trying for ko\n");
#endif
}
}
eply[ply] = mvptr;
for (i = sply[ply]; i < eply[ply]; ++i) {
mvcolor[i] = (char)tm;
#ifdef CHECK
if (debug == 2000+(unsigned int)g) {
sprintf(buf,"defone genning %s",ssqr(mvs[i],buf2));
outerr(buf);
}
#endif
}
}
/* defend nbr of lastmove in atari
* defend if nbr has one liberty
* and can get more by pulling out
* or by capturing
* makes double ataris work
*/
int defatari(int ply, group_t g, int tm, group_t gs, int lastmove, int winsko) {
int mvptr,numlibs,j,i,ldtmp;
sqr_t s,sn;
group_t gn;
list_t ptr2;
#ifdef CHECK
char buf[80],buf2[10];
#endif
g = g;
if (winsko == grcolor[gs])
return FALSE; /* if I win kos also ignore threats to other groups */
if (lastmove == PASS)return(FALSE);
mvptr = sply[ply];
i = fdir[lastmove];
for (ldtmp = ldir[i]; i < ldtmp; ++i) {
s = lastmove+nbr[i];
if (S_COLOR(s) != G_COLOR(gs))continue;
gn = board[s];
if (gn == gs)continue;
if (grlibs[gn] > 1)continue; /* find one liberty groups adjacent to last move */
if (grsize[gn] < grsize[gs] - 2 && /* small group */
grpieces[gn] > sply[0]) { /* created by move during this fight */
continue; /* OK not to answer atari */
}
/* if ((S_ALIVE(s)&31) >= WEAK)continue; ALIVE is not generally valid here */
/* gn is a one liberty group adjacent to the last move */
for (ptr2 = grnbp[gn]; ptr2 != EOL; ptr2 = link[ptr2])
if (grlibs[list[ptr2]] == 1 &&
(grsize[list[ptr2]] > 2 ||
list[ptr2] == board[lastmove])) /* first capture to save it */
mvs[mvptr++] = list[grlbp[list[ptr2]]];
numlibs = 0;
sn = list[grlbp[gn]];
if (inlist(sn,&grlbp[gs]))continue; /* don't fill liberty of group chased */
numlibs += lnbn[sn];
for (ptr2 = nbgrp[sn][grcolor[gs]]; ptr2 != EOL; ptr2 = link[ptr2])
if (grlibs[list[ptr2]] >= 2)
numlibs += newlist(nblbp[sn],grlbp[list[ptr2]])-1;
for (ptr2 = nbgrp[sn][1-grcolor[gs]]; ptr2 != EOL; ptr2 = link[ptr2])
if (grlibs[list[ptr2]] == 1)
numlibs += grsize[list[ptr2]];
if (numlibs > 1) /* gain more liberties by saving, so save group */
mvs[mvptr++] = sn;
for (ptr2 = grnbp[gn]; ptr2 != EOL; ptr2 = link[ptr2])
if (grlibs[list[ptr2]] == 1 &&
(sqr_t)list[grlbp[list[ptr2]]] != kosquare &&
list[grlbp[list[ptr2]]] != list[grlbp[gn]] &&
grsize[list[ptr2]] <= 2 && list[ptr2] != board[lastmove])
mvs[mvptr++] = list[grlbp[list[ptr2]]]; /* capture to save it */
eply[ply] = mvptr;
for (j = sply[ply]; j < eply[ply]; ++j) {
mvcolor[j] = (char)tm;
#ifdef CHECK
if (debug == 2000+(unsigned int)g) {
sprintf(buf,"defatari saving group %s",ssqr(mvs[j],buf2));
outerr(buf);
waitaction();
}
#endif
}
if (eply[ply] != sply[ply])return(TRUE);
}
return(FALSE);
}
/* g is a one liberty, one stone group. Return TRUE if capturing
* g leds to a snapback - play back in point where G is to
* capture group that just played
*
* Three interesting cases:
*
* ko: recapture depends on ko threat return FALSE
*
* * * *
* * + *
* O * O O
* + O * *
*
* real snapback: recapturing stone is safe return TRUE
*
* * * * * *
* * O O O *
* * O + * *
* * O * O O
* * * O O +
*
* * * * * *
* * O O O *
* * O + O *
* * O * O *
* * * O * *
*
*
* short of liberties: recapturing stone can be captured itself
*
* * * *
* * O *
* * + *
* O * O
* O O O
*
*/
int snapback(group_t g, int winsko) {
sqr_t s;
int c;
list_t ptr;
int nbr = FALSE;
if (grlibs[g] > 1) /* Must be one liberty group being captured */
return FALSE;
if (grsize[g] > 1) /* must be one stone group */
return FALSE;
s = (sqr_t)list[grlbp[g]]; /* liberty of group being captured */
c = grcolor[g]; /* color of group being captured */
if (lnbn[s] != 0)return(FALSE); /* will get more than one liberty */
for (ptr = nbgrp[s][c]; ptr != EOL; ptr = link[ptr])
if (list[ptr] != g && grlibs[list[ptr]] == 1)
return(FALSE); /* another capture */
for (ptr = nbgrp[s][1-c]; ptr != EOL; ptr = link[ptr]) {
if (grlibs[list[ptr]] > 1)return(FALSE);
if (inlist(list[ptr], &grnbp[g]))
nbr = TRUE; /* will get at least 2 liberties */
}
if (nbgrp[s][1-c] == EOL && winsko == 1-c) /* ko */
return FALSE;
if (!nbr)
return FALSE; /* case 3 */
return(TRUE);
}
/* return true if g can be captured in a snapback at s1
* s2 is g's other liberty
* already know that g has 2 liberties and no one liberty neighbors
* note: one move earlier than snapback()
*/
static int issnapback(sqr_t s1, sqr_t s2, group_t g, int c) {
list_t ptr;
if (lnbn[s1] != 1 || (sqr_t)list[nblbp[s1]] != s2 || lnbf[s1][1-c] != 0)
return FALSE;
if (lnbn[s2] != 1 || link[nbgrp[s2][c]] != EOL)
return FALSE;
for (ptr = nbgrp[s2][1-c]; ptr != EOL; ptr = link[ptr])
if (grlibs[list[ptr]] == 1)
return FALSE;
return TRUE;
}
/* defender attack a neighboring group grl with fewer or same liberties.
* gs group being defended. grl is the neighboring group to attack.
* grp is the group solidly connected to gs that is adjacent to this group
* grp may be NOGROUP
* g is for debug output.
* libamt is effective libs of gs
* grllibs is effective libs of grl
* winsko is color that wins all kos
* only generate full value for one attacking move to try to kill each nbr
* unless group chased has only two libs
* don't generate attacking move which can be captured (unless it looks like
* a good throwin - captures 1 or 2 stones at a cutting point).
* value each neighboring liberty reduced at 16 and each total liberty at 16 also
*/
#define MAXATK 10
int def_atk_nbr(group_t gs, group_t grp, group_t grl, int mvp, group_t g, int libamt, int maxlibamt, int grllibs, int tm, listval_t ldrno, int winsko) {
#ifdef CHECK
int i;
#endif
int val; /* value of capturing group grl overall */
int j,illegal_move,mvvtmp,mvvmax,mvls=0,next_to,c;
int throwinok; /* it's ok to throwin here - put down stone with one liberty */
int numcuts,iscut,savemvp,iscapture,newlibs,islib,nlibs,clibs, onelibnbr = FALSE;
list_t lblist = EOL, lptr2, ptr, cptr, ptr2, commlibs = EOL;
int connected;
int highest = 0; /* highest liberty of thhis group */
int numexpand = 0;
int nblibs = 0; /* number of liberties can get from neighbor of grl */
sqr_t s, sl;
int passflag = TRUE; /* pass if enemy can't get more liberties, might be a seki */
sqr_t atksqr[MAXATK];
int atkval[MAXATK];
int nextatk = 0;
#ifdef CHECK
char buf[10];
char buf2[80];
#endif
g = g;
c = grcolor[gs];
savemvp = mvp;
val = 16*libamt-16*grllibs; /* 4/01 changed to grllibs and 8 to 16 value of attack of grl depends on number of liberties */
if (val < 0)val = 0;
if (grlibs[grl] == 2) {
if (grsize[grl] > 4)val += 40;
else
val += 8*grsize[grl]; /* attack is sente */
}
if (grlibs[grl] == 1 ) { /* can capture nbr */
passflag = FALSE;
val = 32 * lnbn[list[grlbp[grl]]];
cpylist(nblbp[list[grlbp[grl]]],&lblist);
if (grsize[grl] > 1) {
val += 32*grsize[grl]+8;
}
else if (snapback(grl,winsko))
return(mvp); /* can't capture due to snapback */
else val += 40; /* since might make an eye */
nlibs = 0;
mrglist(grlbp[gs], &lblist);
if (grp != NOGROUP)
mrglist(grlbp[grp],&lblist);
for (j = grnbp[grl]; j != EOL; j = link[j]) {
if (list[j] != gs && list[j] != grp) { /* cutting stones */
andlist(grlbp[gs], grlbp[list[j]], &commlibs);
connected = FALSE;
for (ptr2 = commlibs; ptr2 != EOL; ptr2 = link[ptr2]) {
if (solidconnect(list[ptr2], c, winsko) == 1) {
connected = TRUE;
break;
}
}
killist(&commlibs);
if (!connected)
nlibs += mrglist(grlbp[list[j]],&lblist);
else
mrglist(grlbp[list[j]],&lblist); /* already connected - no new liberties */
if (grlibs[list[j]] == 1)
onelibnbr = TRUE;
}
}
if (onelibnbr)
val += 16; /* capture to save the neighbor */
if (nlibs > 5)
nlibs = 5;
val += 32 * nlibs;
if (lblist != EOL)killist(&lblist);
if (!onelibnbr && lnbn[list[grlbp[grl]]] <= 1 && lnbf[list[grlbp[grl]]][1-c] == 1) {
val -= 32; /* already captured */
if (lnbn[list[grlbp[grl]]] == 1) {
mval[mvp] = val-16;
mmov[mvp] = list[nblbp[list[grlbp[grl]]]]; /* try for eyespace */
mvp++;
}
}
}
else { /* set val (value of capturing) if more than one liberty group */
for (ptr = grlbp[grl]; ptr != EOL; ptr = link[ptr]) {
if (edge[list[ptr]] > highest)
highest = edge[list[ptr]];
if (lnbn[list[ptr]] == 3 || link[nbgrp[list[ptr]][grcolor[grl]]] != EOL)
++numexpand; /* number of points can get more liberties */
}
if (highest > 5)highest = 5;
val += 10-highest*2; /* prefer to attack groups nearer edge */
val += 8 * (1-numexpand);
/* add possible extra liberties for capturing cutting stones */
nblibs = 0;
for (ptr = grnbp[grl]; ptr != EOL; ptr = link[ptr]) {
if (list[ptr] == gs || list[ptr] == grp)continue;
if (comlist(grlbp[list[ptr]], grlbp[gs]))continue;
if (grlibs[list[ptr]] > nblibs)
nblibs = grlibs[list[ptr]];