-
Notifications
You must be signed in to change notification settings - Fork 1
/
playout.c
2164 lines (1982 loc) · 58.9 KB
/
playout.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
/*
* playout.c
*
* playout a game for UCT-MC
*/
//#include <malloc.h>
#include <stdlib.h>
#include <memory.h>
#include <assert.h>
#include <stdio.h>
#include <math.h>
#include <time.h>
#ifdef WIN32
#include <process.h>
#endif
#include "ui2g2.h"
#include "playout.h"
#include "learn.h"
#pragma warning(disable : 4996)
#define PO_BITS
/*
* board layout is 19x19 embedded in a 20x21 array, with 1 extra on end for lower right diagaonal of last point
* left point in each row is off the board. 1 more is added to make the size even so arrays of shorts are on int boundaries
*/
#define PO_UP_BIT 1
#define PO_LEFT_BIT 2
#define PO_RIGHT_BIT 4
#define PO_DOWN_BIT 8
#define PO_NONE 0
#define PO_TM_HASH 400
#define PO_KO_HASH 500
static int po_edge[PO_BSIZE]; /* distance to edge of the board (0 corner or off board, 1 edge, 2, ... to center */
extern const __int64 zobhash64[500][2];
/* how many bits are set? */
const char po_count_bits[16] = {
0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4
};
/* offset to the first empty point if one bit is set */
const int po_bit_offs1[16] = {
0, -PO_SZ, -1, -PO_SZ, 1, -PO_SZ, -1, -PO_SZ, PO_SZ, -PO_SZ, -1, -PO_SZ, 1, -PO_SZ, -1, -PO_SZ
};
/* offset to the second empty point if two bits are set */
const int po_bit_offs2[16] = {
0, 0, 0, -1, 0, 1, 1, -1, 0, PO_SZ, PO_SZ, -1, PO_SZ, 1, 1, -1
};
/* offsets for each bit set in a bit map */
const int po_bit_offs[16][4] =
{
0, 0, 0, 0,
-PO_SZ, 0, 0, 0,
-1, 0, 0, 0,
-PO_SZ, -1, 0, 0,
1, 0, 0, 0,
-PO_SZ, 1, 0, 0,
-1, 1, 0, 0,
-PO_SZ, -1, 1, 0,
PO_SZ, 0, 0, 0,
-PO_SZ, PO_SZ, 0, 0,
-1, PO_SZ, 0, 0,
-PO_SZ, -1, PO_SZ, 0,
1, PO_SZ, 0, 0,
-PO_SZ, 1, PO_SZ, 0,
-1, 1, PO_SZ, 0,
-PO_SZ, -1, 1, PO_SZ,
};
/* does a move make a superko position? */
int po_superko(struct po_board *b)
{
int m;
for (m = b->mnum - 2; m >= 0; m--) {
if (b->hash[m] == b->hash[b->mnum]) { /* superko */
return TRUE;
}
}
return FALSE;
}
/*
* convert sqr_t to playout move
*/
int po_to_po(sqr_t s, int size)
{
int x = s % size;
int y = s / size;
if (s == PASS)
return PO_PASS;
return PO_SZ * (y + 1) + x + 1;
}
/*
* convert playout move to sqr_t
*/
sqr_t po_from_po(int s, int size)
{
int x = s % (PO_SZ) - 1;
int y = s / (PO_SZ) - 1;
if (s == PO_PASS)
return PASS;
return size * y + x;
}
short *po_getmoves(struct po_board *b, short *mnum)
{
*mnum = b->mnum;
return b->moves;
}
/*
* random number from 0 to range - 1
*/
int po_random(struct po_board *b, int range)
{
#if PO_RANDOM_64
b->seed = b->seed * 2862933555777941757 + 3037000493;
return ((unsigned int)(b->seed >> 16)) % range;
#else
b->seed = ((unsigned int)b->seed) * 392314069 + 1;
return (((unsigned int)(b->seed) >> 16) % range); /* upper bits are more random */
#endif
}
/*
* one time initialize the playout system
*/
void po_init_all(void)
{
po_pattern_init();
}
void po_init_edge(int size)
{
int i;
for (i = 0; i < PO_BSIZE; ++i) {
po_edge[i] = i % PO_SZ;
if (i / PO_SZ < po_edge[i]) {
po_edge[i] = i / PO_SZ;
}
if (size + 1 - (i % PO_SZ) < po_edge[i]) {
po_edge[i] = size + 1 - (i % PO_SZ);
}
if (size + 1 - (i / PO_SZ) < po_edge[i]) {
po_edge[i] = size + 1 - (i / PO_SZ);
}
if (i == PO_SZ + 1 || i == PO_SZ + size || i == PO_SZ * size + 1 || i == PO_SZ * size + size) {
po_edge[i] = 0;
}
if (i <= PO_SZ || i % PO_SZ == 0 || i % PO_SZ > size || i >= PO_SZ * (size + 1)) {
po_edge[i] = 0;
}
}
}
/*
* initialize a new empty board
*/
void po_init(struct po_board *b, int handicap, int komi, int size, __int64 seed)
{
int i;
#if PO_RANDOM_INIT
int r;
#endif
b->hash[0] = 0;
b->empty_num = 0;
b->po_offs[1] = -PO_SZ;
b->po_offs[2] = -1;
b->po_offs[4] = 1;
b->po_offs[8] = PO_SZ;
b->one_lib_num[0] = b->one_lib_num[1] = 0;
b->captured_num = 0;
b->passcount[PO_BLACK] = 0;
b->passcount[PO_WHITE] = 0;
b->seed = seed;
for (i = 0; i < PO_BSIZE; ++i) {
b->p_first[i] = PO_EMPTY;
b->p_g[i] = PO_NONE;
b->p_pat[i] = 0;
if (i <= PO_SZ || i % PO_SZ == 0 || i % PO_SZ > size || i >= PO_SZ * (size + 1)) {
b->p_c[i] = PO_NO_COLOR;
b->po_onboard[i] = 0;
} else {
b->p_c[i] = PO_EMPTY;
#if PO_RANDOM_INIT
if (b->empty_num) {
r = po_random(b, b->empty_num);
b->empty_idx[b->empty_num] = b->empty_idx[r];
b->p_idx[b->empty_idx[r]] = b->empty_num;
b->empty_idx[r] = i;
b->p_idx[i] = r;
b->empty_num++;
} else {
b->p_idx[i] = 0;
b->empty_idx[b->empty_num++] = i;
}
#else
b->p_idx[i] = b->empty_num;
b->empty_idx[b->empty_num++] = i;
#endif
b->po_onboard[i] = 0xff; /* all bits set */
}
b->p_next_g[i] = PO_NONE;
b->g_last_g[i] = PO_NONE;
b->g_libs[i] = 0;
b->g_size[i] = 0;
b->p_nbr_num[i][0] = b->p_nbr_num[i][1] = 0;
}
for (i = 0; i < PO_BSIZE; ++i) {
b->p_empty_bits[i] = 0;
#ifdef PO_LIBS
b->p_lib_bits[i] = 0;
#endif
if (b->p_c[i] == PO_NO_COLOR) {
continue;
}
if (b->p_c[i - PO_SZ] != PO_NO_COLOR) {
b->p_empty_bits[i] |= PO_UP_BIT;
}
if (b->p_c[i - 1] != PO_NO_COLOR) {
b->p_empty_bits[i] |= PO_LEFT_BIT;
}
if (b->p_c[i + 1] != PO_NO_COLOR) {
b->p_empty_bits[i] |= PO_RIGHT_BIT;
}
if (b->p_c[i + PO_SZ] != PO_NO_COLOR) {
b->p_empty_bits[i] |= PO_DOWN_BIT;
}
}
b->stones[0] = b->stones[1] = 0;
b->ko = PO_NONE;
b->tm = PO_BLACK;
b->komi = komi;
b->boardsize = size;
b->mnum = 0;
}
/*
* alloc a new empty board
* thread safe
*/
struct po_board *po_alloc(int komi, int size, __int64 seed)
{
struct po_board *b = (struct po_board *)malloc(sizeof(struct po_board));
if (!b) {
return b;
}
po_init(b, 0, komi, size, seed);
return b;
}
/*
* copy a board, preserving the random seed
*/
void po_copy(struct po_board *dst, struct po_board *src)
{
__int64 seed = dst->seed;
memcpy(dst, src, sizeof(struct po_board));
dst->seed = seed;
}
void po_free(struct po_board *b)
{
free(b);
}
/*
* make a psss move during a playout
* since playout must take stones off the board it must count chinese style and there is no Japanese bonus for passing
* passes by one side may be forced because there are no legal moves otherwise, while the other side is capturing
*/
static void po_makepass(struct po_board *b)
{
b->reason[b->mnum] = PO_REASON_PO_PASS;
b->moves[b->mnum] = (PO_PASS << 1) + b->tm;
/* b->passcount[b->tm]++; DO NOT put this back!!!! Chinese score is not changed by a pass in the playout, so they are irrelevant */
b->mnum++;
b->tm = 1 - b->tm;
b->ko = PO_NONE;
}
/*
* count liberties of g, just for debug
*/
#ifdef PO_LIBS
static int po_lib_count(struct po_board *b, int g)
{
int p = g;
int libs = 0;
do {
libs += po_count_bits[b->p_lib_bits[p]];
} while ((p = b->p_next_g[p]) != PO_NONE);
return libs;
}
#endif
/*
* make a move m (not a pass and not a suicide) on board b
* must be thread safe, so no references to globals allowed
* rave to record first player for rave updates
*/
static void po_make_move(struct po_board *b, int m, int c, int rave)
{
int i, j, n, p, p2, g, g1, g2, n1, n2, n3, n4;
int capsize;
int found, found_g2;
int *pat;
assert(b->p_g[m] == PO_NONE);
assert(m >= 0 && m <= (PO_SZ) * (PO_SZ) && b->p_c[m] != PO_NO_COLOR);
assert(m != b->ko);
assert(c == 0 || c == 1);
/* put the stone on the board */
b->p_c[m] = c;
b->stones[c]++;
b->captured_num = 0;
b->empty_idx[b->p_idx[m]] = b->empty_idx[--b->empty_num];
b->p_idx[b->empty_idx[b->empty_num]] = b->p_idx[m];
b->moves[b->mnum] = (m << 1) + c;
/* for RAVE */
if (rave && b->p_first[m] == PO_EMPTY && b->mnum < b->rave_moves) {
b->p_first[m] = c;
b->p_first_pat[m] = b->p_pat[m];
}
/* fix up patterns */
/* from center empty point pattern index p_pat[] gets 0 or 1 for stone to right, 3 or 6 to upper-right, then continues counter-clockwise */
pat = po_pat_index[c]; /* constants */
b->p_pat[m]++; /* just to make it nonzero so pat value zero means 3x3 empty points */
b->p_pat[m - PO_SZ - 1] += *pat++;
b->p_pat[m - PO_SZ] += *pat++;
b->p_pat[m - PO_SZ + 1] += *pat++;
b->p_pat[m + 1] += *pat++;
b->p_pat[m + PO_SZ + 1] += *pat++;
b->p_pat[m + PO_SZ] += *pat++;
b->p_pat[m + PO_SZ - 1] += *pat++;
b->p_pat[m - 1] += *pat++;
/* remove empty point and liberty from adjacent points */
b->p_empty_bits[m - PO_SZ] &= ~PO_DOWN_BIT;
b->p_empty_bits[m - 1] &= ~PO_RIGHT_BIT;
b->p_empty_bits[m + 1] &= ~PO_LEFT_BIT;
b->p_empty_bits[m + PO_SZ] &= ~PO_UP_BIT;
#ifdef PO_LIBS
b->p_lib_bits[m - PO_SZ] &= ~PO_DOWN_BIT;
b->p_lib_bits[m - 1] &= ~PO_RIGHT_BIT;
b->p_lib_bits[m + 1] &= ~PO_LEFT_BIT;
b->p_lib_bits[m + PO_SZ] &= ~PO_UP_BIT;
#endif
/* adjust groups for new stone */
if (b->p_nbr_num[m][c] == 0) { /* new single stone group */
assert(m != b->ko);
b->p_g[m] = m;
b->p_next_g[m] = PO_NONE;
b->g_last_g[m] = m;
b->g_libs[m] = po_count_bits[b->p_empty_bits[m]];
#ifdef PO_LIBS
b->p_lib_bits[m] = b->p_empty_bits[m]; /* new group all empty nbrs are liberties */
#endif
b->g_size[m] = 1;
/* existing enemy neighbors are groups, and are already set up. There are no existing friendly neighbors */
if (b->p_empty_bits[m] & PO_UP_BIT) {
p = m - PO_SZ;
b->p_nbr[p][c][b->p_nbr_num[p][c]++] = m;
}
if (b->p_empty_bits[m] & PO_LEFT_BIT) {
p = m - 1;
b->p_nbr[p][c][b->p_nbr_num[p][c]++] = m;
}
if (b->p_empty_bits[m] & PO_RIGHT_BIT) {
p = m + 1;
b->p_nbr[p][c][b->p_nbr_num[p][c]++] = m;
}
if (b->p_empty_bits[m] & PO_DOWN_BIT) {
p = m + PO_SZ;
b->p_nbr[p][c][b->p_nbr_num[p][c]++] = m;
}
} else { /* add stone to existing group */
g = b->p_nbr[m][c][0];
b->p_g[m] = g;
b->p_next_g[b->g_last_g[g]] = m;
b->g_last_g[g] = m;
b->p_next_g[m] = PO_NONE;
b->g_libs[g]--;
for (i = 1; i < 16; i <<= 1) {
if (!(b->p_empty_bits[m] & i)) {
continue;
}
p = m + b->po_offs[i];
found = 0;
for (j = 0; j < b->p_nbr_num[p][c]; ++j) {
if (b->p_nbr[p][c][j] == g) {
found = 1;
break;
}
}
if (!found) {
b->p_nbr[p][c][b->p_nbr_num[p][c]++] = g;
b->g_libs[g]++;
#ifdef PO_LIBS
b->p_lib_bits[m] |= i;
#endif
}
}
b->g_size[g]++;
for (n = 1; n < b->p_nbr_num[m][c]; ++n) { /* merge up to 3 more groups */
g1 = b->p_g[m];
g2 = b->p_nbr[m][c][n];
/* merge smaller into larger (g2 into g1) */
if (b->g_size[g2] > b->g_size[g1]) {
g = g1;
g1 = g2;
g2 = g;
b->g_libs[g1]--; /* need accurate libs since this group will stick around */
}
p2 = g2;
do {
b->p_g[p2] = g1;
#ifdef PO_LIBS
b->p_lib_bits[p2] = 0;
#endif
for (i = 1; i < 16; i <<= 1) {
if (!(b->p_empty_bits[p2] & i)) {
continue;
}
p = p2 + b->po_offs[i];
found = 0;
found_g2 = -1;
for (j = 0; j < b->p_nbr_num[p][c]; ++j) {
if (b->p_nbr[p][c][j] == g2) {
found_g2 = j;
}
if (b->p_nbr[p][c][j] == g1) {
found = 1;
}
}
if (found) { /* already a libery of g1 */
if (found_g2 != -1) {
b->p_nbr[p][c][found_g2] = b->p_nbr[p][c][--b->p_nbr_num[p][c]]; /* remove g2 */
}
} else {
assert(found_g2 != -1);
b->p_nbr[p][c][found_g2] = g1;
b->g_libs[g1]++;
#ifdef PO_LIBS
b->p_lib_bits[p2] |= i;
#endif
}
}
} while ((p2 = b->p_next_g[p2]) != PO_NONE); /* inf loop here once in 9x9 game with 60 min time limits */
b->p_next_g[b->g_last_g[g1]] = g2;
b->g_last_g[g1] = b->g_last_g[g2];
b->g_size[g1] += b->g_size[g2];
}
}
/* reduce liberty count and capture enemy neighbors */
capsize = 0;
for (i = 0; i < b->p_nbr_num[m][1 - c]; ++i) {
if (--b->g_libs[b->p_nbr[m][1 - c][i]] != 0) {
#if PO_CAPTURE
/* record one liberty group - no need to record when I make myslef one liberty since there is already code to capture last one-lib group */
g = b->p_nbr[m][1 - c][i];
if (b->g_libs[g] == 1 && b->one_lib_num[1-c] < PO_BSIZE) {
b->one_lib[1-c][b->one_lib_num[1-c]++] = g;
}
#endif
continue;
}
p = b->p_nbr[m][1 - c][i];
capsize += b->g_size[p];
b->stones[b->p_c[p]] -= b->g_size[p];
assert(b->stones[b->p_c[p]] >= 0);
do {
assert(p != m); /* can't capture myself */
b->p_g[p] = PO_NONE;
b->p_c[p] = PO_EMPTY;
b->empty_idx[b->empty_num] = p;
b->p_idx[p] = b->empty_num++;
b->captured[b->captured_num++] = p;
/* add to neighbor's empty neighbors */
b->p_empty_bits[p - PO_SZ] |= PO_DOWN_BIT;
b->p_empty_bits[p - PO_SZ] &= b->po_onboard[p - PO_SZ];
b->p_empty_bits[p + PO_SZ] |= PO_UP_BIT;
b->p_empty_bits[p + PO_SZ] &= b->po_onboard[p + PO_SZ];
b->p_empty_bits[p - 1] |= PO_RIGHT_BIT;
b->p_empty_bits[p - 1] &= b->po_onboard[p - 1];
b->p_empty_bits[p + 1] |= PO_LEFT_BIT;
b->p_empty_bits[p + 1] &= b->po_onboard[p + 1];
/* fix up patterns */
pat = po_pat_index[1 - c];
b->p_pat[p]--;
b->p_pat[p - PO_SZ - 1] -= *pat++;
b->p_pat[p - PO_SZ] -= *pat++;
b->p_pat[p - PO_SZ + 1] -= *pat++;
b->p_pat[p + 1] -= *pat++;
b->p_pat[p + PO_SZ + 1] -= *pat++;
b->p_pat[p + PO_SZ] -= *pat++;
b->p_pat[p + PO_SZ - 1] -= *pat++;
b->p_pat[p - 1] -= *pat++;
/* neighbors for new empty point and liberties of adjacent groups */
b->p_nbr_num[p][1-c] = 0;
b->p_nbr_num[p][c] = 0;
n1 = b->p_g[p - PO_SZ];
n2 = b->p_g[p + PO_SZ];
n3 = b->p_g[p - 1];
n4 = b->p_g[p + 1];
if (b->p_c[p - PO_SZ] == c) {
b->p_nbr[p][c][b->p_nbr_num[p][c]++] = n1;
b->g_libs[n1]++;
#ifdef PO_LIBS
b->p_lib_bits[p - PO_SZ] |= PO_DOWN_BIT;
#endif
}
if (n2 != n1 && b->p_c[p + PO_SZ] == c) {
b->p_nbr[p][c][b->p_nbr_num[p][c]++] = n2;
b->g_libs[n2]++;
#ifdef PO_LIBS
b->p_lib_bits[p + PO_SZ] |= PO_UP_BIT;
#endif
}
if (n3 != n2 && n3 != n1 && b->p_c[p - 1] == c) {
b->p_nbr[p][c][b->p_nbr_num[p][c]++] = n3;
b->g_libs[n3]++;
#ifdef PO_LIBS
b->p_lib_bits[p - 1] |= PO_RIGHT_BIT;
#endif
}
if (n4 != n3 && n4 != n2 && n4 != n1 && b->p_c[p + 1] == c) {
b->p_nbr[p][c][b->p_nbr_num[p][c]++] = n4;
b->g_libs[n4]++;
#ifdef PO_LIBS
b->p_lib_bits[p + 1] |= PO_LEFT_BIT;
#endif
}
} while ((p = b->p_next_g[p]) != PO_NONE);
}
/* no more nbr groups since this point is no longer empty */
b->p_nbr_num[m][0] = b->p_nbr_num[m][1] = 0;
/* set ko */
if (capsize == 1 && b->g_size[b->p_g[m]] == 1 && b->g_libs[b->p_g[m]] == 1) {
for (i = 1; i < 16; i <<= 1) {
if (b->p_empty_bits[m] & i) {
b->ko = m + b->po_offs[i];
break;
}
}
assert(b->p_empty_bits[b->ko] == 0);
} else {
b->ko = PO_NONE;
}
b->tm = 1 - c;
b->mnum++;
#ifdef PO_LIBS
assert(po_lib_count(b, b->p_g[m]) == b->g_libs[b->p_g[m]]);
#endif
assert(b->stones[0] + b->stones[1] < b->boardsize * b->boardsize);
assert(b->p_nbr_num[m][c] == 0 && b->p_nbr_num[m][1 - c] <= 4 && b->p_nbr_num[m][1 - c] >= 0);
assert(b->g_libs[b->p_g[m]] > 0); /* suicide not allowed */
assert(b->g_libs[b->p_g[m]] <= b->g_size[b->p_g[m]] * 2 + 2);
}
/*
* update the hash value and make the move or pass
*/
void po_make_uct_move(struct po_board *b, int m, int c)
{
int i, p;
/* pass */
if (m == PO_PASS) {
b->moves[b->mnum] = (PO_PASS << 1) + b->tm;
b->mnum++;
b->hash[b->mnum] = b->hash[b->mnum - 1];
b->passcount[b->tm]++;
b->tm = 1 - c;
b->ko = PO_NONE;
b->reason[b->mnum - 1] = PO_REASON_UCT;
return;
}
/* move */
b->hash[b->mnum + 1] = b->hash[b->mnum] ^ zobhash64[m][c];
for (i = 0; i < b->p_nbr_num[m][1 - c]; ++i) {
if (b->g_libs[b->p_nbr[m][1 - c][i]] != 1) {
continue;
}
p = b->p_nbr[m][1 - c][i];
do {
b->hash[b->mnum + 1] ^= zobhash64[p][1 - c];
} while ((p = b->p_next_g[p]) != PO_NONE);
}
b->reason[b->mnum] = PO_REASON_UCT;
po_make_move(b, m, c, FALSE);
#if UCT_LEARN
learn_hash_move(b->mnum);
learn_hash_stone(po_from_po(m, b->boardsize), c, b->boardsize, b->mnum);
for (i = 0; i < b->captured_num; ++i) {
learn_hash_stone(po_from_po(b->captured[i], b->boardsize), 1 - c, b->boardsize, b->mnum);
}
#endif
}
/***************************************************************************************************
* Move generation
**************************************************************************************************/
static int po_add_group(struct po_board *b, int m, int c, int count, int *groups)
{
int i;
if (b->p_c[m] == c) {
for (i = 0; i < count; ++i) {
if (groups[i] == b->p_g[m])break;
}
if (i == count) {
groups[count++] = b->p_g[m];
}
}
return count;
}
/* find the enemy neighbor groups adjacent to group at point m (or group m)
* return the number of groups, and an array of the group numbers
*/
static int po_enemy_nbrs(struct po_board *b, int m, int *groups)
{
int count = 0;
int p;
int c = 1 - b->p_c[m];
for (p = b->p_g[m]; p != PO_NONE; p = b->p_next_g[p]) {
count = po_add_group(b, p + 1, c, count, groups);
count = po_add_group(b, p - 1, c, count, groups);
count = po_add_group(b, p + PO_SZ, c, count, groups);
count = po_add_group(b, p - PO_SZ, c, count, groups);
}
return count;
}
/*
* capture a one liberty group g. Return the move to capture it, or PO_PASS
*/
static int po_capture(struct po_board *b, int g)
{
int p, p2;
for (p = g; p != PO_NONE; p = b->p_next_g[p]) {
if (b->p_empty_bits[p] == 0) {
continue;
}
p2 = p + po_bit_offs1[b->p_empty_bits[p]];
if (b->ko == p2) {
return PO_PASS;
}
return p2;
#if 0
/* replace with more efficient one above */
if (b->p_empty_bits[p] & PO_UP_BIT && b->ko != p - PO_SZ) {
return p - PO_SZ;
}
if (b->p_empty_bits[p] & PO_LEFT_BIT && b->ko != p - 1) {
return p - 1;
}
if (b->p_empty_bits[p] & PO_RIGHT_BIT && b->ko != p + 1) {
return p + 1;
}
if (b->p_empty_bits[p] & PO_DOWN_BIT && b->ko != p + PO_SZ) {
return p + PO_SZ;
}
#endif
}
return PO_PASS;
}
/*
* is m (a move to a point with zero or one adjacent empty point) a self atari move
*/
static int po_self_atari(struct po_board *b, int m)
{
int i, j, g, p, p2, empty_nbrs, two_lib_groups = 0, groups[4];
int newlibs = 0, liberties[3]; /* liberties of the new group */
empty_nbrs = po_count_bits[b->p_empty_bits[m]]; /* 0 or 1 */
assert(empty_nbrs <= 1);
/* ok if we capture an enemy nbr (TODO and it is not a snapback) */
for (i = 0; i < b->p_nbr_num[m][1 - b->tm]; ++i) {
g = b->p_nbr[m][1 - b->tm][i];
if (b->g_libs[g] == 1) {
if ((empty_nbrs == 1 || b->g_size[g] > 1 || newlibs)) {
return FALSE;
}
newlibs++; /* get a liberty when we capture */
}
}
/* ok if adjacent friend has more than two liberties */
/* handle easy cases */
for (i = 0; i < b->p_nbr_num[m][b->tm]; ++i) {
g = b->p_nbr[m][b->tm][i];
if (b->g_libs[g] >= 3) { /* must have at leat 2 liberties */
return FALSE;
}
if (b->g_libs[g] == 2) {
groups[two_lib_groups++] = g;
}
}
if (two_lib_groups && newlibs) {
return FALSE; /* one liberty and one capture */
}
/* must have only one liberty */
if (!two_lib_groups || !empty_nbrs && !newlibs && two_lib_groups == 1) {
return TRUE;
}
/* find all the new liberties */
if (empty_nbrs) {
liberties[0] = m + po_bit_offs1[b->p_empty_bits[m]];
newlibs++;
}
for (i = 0; i < two_lib_groups; ++i) {
p = groups[i];
do {
if (!b->p_empty_bits[p]) {
continue;
}
p2 = p + po_bit_offs1[b->p_empty_bits[p]];
liberties[newlibs++] = p2;
for (j = 0; j < newlibs-1; ++j) {
if (liberties[j] == p2) {
newlibs--;
break;
}
}
if (newlibs > 2) { /* 2 - one for the empty filled by this move, and one for the single liberty */
return FALSE;
}
p2 = p + po_bit_offs2[b->p_empty_bits[p]];
if (p2 == p) {
continue;
}
liberties[newlibs++] = p2;
for (j = 0; j < newlibs-1; ++j) {
if (liberties[j] == p2) {
newlibs--;
break;
}
}
if (newlibs > 2) { /* 2 - one for the empty filled by this move, and one for the single liberty */
return FALSE;
}
} while ((p = b->p_next_g[p]) != PO_NONE);
}
return TRUE;
}
/*
* return the number of pointy ends for the shape made by a move at m,
* or return 10 if there is a connection to a friendly group, so no nakade
*/
static int count_ends(struct po_board *b, int m)
{
int corner = FALSE;
int ends = 0;
int c = b->tm;
int p, p2, i;
int nbr = 0;
int liberty = 0; /* the liberty of the nakade */
int groups[4];
int next_group = 0;
if (b->p_c[m + 1] == c) nbr++;
if (b->p_c[m - 1] == c) nbr++;
if (b->p_c[m + PO_SZ] == c) nbr++;
if (b->p_c[m - PO_SZ] == c) nbr++;
if (nbr == 1) ends++;
if (b->p_empty_bits[m])
liberty = m + po_bit_offs1[b->p_empty_bits[m]];
for (i = 0; i < b->p_nbr_num[m][c]; ++i) {
groups[next_group++] = b->p_nbr[m][c][i];
for (p = b->p_nbr[m][c][i]; p != PO_NONE; p = b->p_next_g[p]) {
nbr = 0;
if (b->p_c[p + 1] == c || p + 1 == m) nbr++;
if (b->p_c[p - 1] == c || p - 1 == m) nbr++;
if (b->p_c[p + PO_SZ] == c || p + PO_SZ == m) nbr++;
if (b->p_c[p - PO_SZ] == c || p - PO_SZ == m) nbr++;
if (nbr == 1) {
ends++;
} else if (po_edge[p] == 0) {
corner = TRUE;
}
if (!liberty && b->p_empty_bits[p]) {
if (p + po_bit_offs1[b->p_empty_bits[p]] != m)
liberty = p + po_bit_offs1[b->p_empty_bits[p]];
else if (po_count_bits[b->p_empty_bits[p]] > 1 &&
p + po_bit_offs2[b->p_empty_bits[p]] != m)
liberty = p + po_bit_offs2[b->p_empty_bits[p]];
}
}
}
if (corner && ends == 2) {
ends++; /* to recognize bent 4 in the corner */
}
/* does liberty have a neighbor that is not in groups? if so there is a connection, and this is not a nakade */
if (liberty) {
for (p = 0; p < b->p_nbr_num[liberty][c]; ++p) {
p2 = b->p_nbr[liberty][c][p];
for (i = 0; i < next_group; ++i) {
if (groups[i] == p2)
break;
}
if (i == next_group) {
return 10; /* there is a connection avaiable, so not nakade */
}
}
}
return ends;
}
/*
* does move m, a self-atari, make a nakade shape? If TRUE, we can move here. If FALSE, prune this move
* we know it is a self-atari, so we know there is 0 or 1 empty adjaacent point, and at least one enemy nbr
* and no capturable enemy nbr
*/
static int po_nakade_self_atari(struct po_board *b, int m)
{
int i, size, ends, nbrs;
int g; /* one of the friendly nbr grops */
int groups[PO_BSIZE];
/* only self atari if it makes nakade shape. Otherwise it might be a seki. 1,2, or 3 stones are usually ok. */
size = 1; /* number of my stones that will be capturable after my move */
nbrs = b->p_nbr_num[m][b->tm];
for (i = 0; i < nbrs; ++i) {
g = b->p_nbr[m][b->tm][i];
size += b->g_size[g];
}
if (size >= 6) {
return FALSE;
}
switch(size) {
case 5:
ends = count_ends(b, m);
if (ends != 4 && ends != 1) {
return FALSE;
}
break;
case 4:
ends = count_ends(b, m);
if (ends == 10 || ends == 2) {
return FALSE;
}
break;
case 3:
if (count_ends(b, m) == 10) {
return FALSE;
}
if (nbrs >= 2) {
return TRUE; /* connect to make the nakade */
}
if (po_enemy_nbrs(b, g, groups) > 1) {
return FALSE; /* stil an outside liberty, or maybe a seki, or he is already alive */
}
break;
case 2:
if (b->p_nbr_num[m][1 - b->tm] == 1 && b->g_libs[b->p_nbr[m][1 - b->tm][0]] > 2) { /* no atari or capture or cut, just a waste */
return FALSE;
}
break;
case 1:
if (b->p_nbr_num[m][1 - b->tm] == 1 && b->g_libs[b->p_nbr[m][1 - b->tm][0]] > 2) { /* no atari or capture or cut, just a waste */
return FALSE;
}
break;
}
#if 0
else if (size > 1) { /* always allow small throwins */
ends = count_ends(b, m);
if (ends == 10)
return TRUE;
}
#endif
return TRUE;
}
/*
* add moves to attack g which has a small number of liberties
*/
static int po_attack(struct po_board *b, int g, short *movelist, int *move_value, int *move_reason, int nextmove, int val)
{
int p, p2, i, count, count2;
for (p = g; p != PO_NONE; p = b->p_next_g[p]) {
count = po_count_bits[b->p_lib_bits[p]];
if (count == 0) {
continue;
}
for (i = 0; i < count; ++i) {
p2 = p + po_bit_offs[b->p_lib_bits[p]][i];
assert(b->p_c[p2] == PO_EMPTY);
count2 = po_count_bits[b->p_empty_bits[p2]];
if (count2 == 0) { /* illegal? */
continue;
}
if (count2 > 1 || !po_self_atari(b, p2)
// || po_nakade_self_atari(b, p2)
) {
movelist[nextmove] = p2;
move_value[nextmove] = val + PO_ATTACK_VAL;
move_reason[nextmove] = PO_REASON_PO_ATTACK_NBR;
nextmove++;
}
}
}
return nextmove;
}
/*
* gen a move to save group g, which has one liberty
*/
static int po_gen_save(struct po_board *b, short g, short *move_list, int *move_value, int *move_reason, int nextmove, int for_uct)
{
int p, p2, empty;
int val = PO_SAVE_VAL + b->g_size[g] * PO_SAVE_STONE_VAL;
#if PO_SAVE_CAPTURE
/* capture neighbor group with one liberty - weaker */
int c = 1 - b->p_c[g];
for (p = g; p != PO_NONE; p = b->p_next_g[p]) {
if (b->p_c[p + 1] == c && b->g_libs[b->p_g[p + 1]] == 1) {
move_list[nextmove] = po_capture(b, b->p_g[p + 1]);
if (move_list[nextmove] != PO_PASS) {
move_value[nextmove] = val + b->g_size[b->p_g[p + 1]] * PO_SAVE_CAP_VAL;
move_reason[nextmove] = PO_REASON_PO_SAVE;
nextmove++;
}
}
if (b->p_c[p - 1] == c && b->g_libs[b->p_g[p - 1]] == 1) {
move_list[nextmove] = po_capture(b, b->p_g[p - 1]);
if (move_list[nextmove] != PO_PASS) {
move_value[nextmove] = val + b->g_size[b->p_g[p + 1]] * PO_SAVE_CAP_VAL;
move_reason[nextmove] = PO_REASON_PO_SAVE;
nextmove++;
}
}
if (b->p_c[p + PO_SZ] == c && b->g_libs[b->p_g[p + PO_SZ]] == 1) {
move_list[nextmove] = po_capture(b, b->p_g[p + PO_SZ]);
if (move_list[nextmove] != PO_PASS) {
move_value[nextmove] = val + b->g_size[b->p_g[p + 1]] * PO_SAVE_CAP_VAL;
move_reason[nextmove] = PO_REASON_PO_SAVE;
nextmove++;
}
}
if (b->p_c[p - PO_SZ] == c && b->g_libs[b->p_g[p - PO_SZ]] == 1) {
move_list[nextmove] = po_capture(b, b->p_g[p - PO_SZ]);
if (move_list[nextmove] != PO_PASS) {
move_value[nextmove] = val + b->g_size[b->p_g[p + 1]] * PO_SAVE_CAP_VAL;
move_reason[nextmove] = PO_REASON_PO_SAVE;
nextmove++;
}
}