-
Notifications
You must be signed in to change notification settings - Fork 0
/
goalsearch.d
3892 lines (3761 loc) · 168 KB
/
goalsearch.d
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
import tango.util.log.Trace;
import position;
class GoalSearch
{
Position start;
uint[2] goals_found;
bitix[8][2] rabbit_location;
uint[8][2] goal_depth;
uint[64] board_depth;
Side cur_side;
ulong goal_line;
void set_start(Position pos)
{
if (start !is null)
{
Position.free(start);
}
start = pos.dup;
}
void clear_start()
{
if (start !is null)
{
Position.free(start);
}
}
private ulong expand_forward(ulong bits)
{
ulong expanded;
expanded = (bits & NOT_A_FILE) << 1;
expanded |= (bits & NOT_H_FILE) >> 1;
expanded |= ((bits << 8) >> (cur_side << 4))
| (((bits & RANK_8) >> 8) << ((cur_side^1) << 5));
//expanded |= (cur_side == Side.WHITE) ?
// (bits & NOT_RANK_8) << 8 : (bits & NOT_RANK_1) >> 8;
return bits | expanded;
}
int find_unassisted(ulong rbit, int max_depth, ulong bad_neighbors)
{
ulong friend_neighbors = neighbors_of(start.placement[cur_side] ^ rbit);
ulong good_squares = start.bitBoards[Piece.EMPTY] & ~((TRAPS | bad_neighbors) & ~friend_neighbors);
int depth;
ulong movement = expand_forward(rbit) & good_squares;
for (depth = 1; depth <= max_depth; depth++)
{
if (movement & goal_line)
break;
ulong next = expand_forward(movement) & good_squares;
if (next == movement)
{
depth = max_depth+1;
break;
}
movement = next;
}
return depth;
}
void find_goals(int search_depth)
{
for (Side s=Side.WHITE; s <= Side.BLACK; s++)
{
cur_side = s;
goal_line = RANK_8;
uint piece_offset = 0;
uint enemy_offset = 6;
if (s == Side.BLACK)
{
goal_line = RANK_1;
piece_offset = 6;
enemy_offset = 0;
}
ulong friend_neighbors = neighbors_of(start.placement[cur_side]);
ulong bad_neighbors = neighbors_of(start.placement[cur_side ^1] ^ start.bitBoards[Piece.WRABBIT+enemy_offset]);
ulong good_squares = start.bitBoards[Piece.EMPTY] & ~((TRAPS | bad_neighbors) & ~friend_neighbors);
ulong possible_squares = goal_line & start.bitBoards[Piece.EMPTY];
ulong npossible = possible_squares | (neighbors_of(possible_squares) & good_squares);
int pdepth = 1;
while (pdepth < search_depth && possible_squares != npossible)
{
possible_squares = npossible;
npossible = possible_squares | (neighbors_of(possible_squares) & good_squares);
pdepth++;
}
possible_squares |= neighbors_of(possible_squares) & ~start.frozen;
goals_found[s] = 0;
ulong rabbits = start.bitBoards[Piece.WRABBIT+piece_offset];
while (rabbits)
{
bitix rix = bitindex(rabbits); // using msbindex for white helps partially sort the goals
ulong rbit = 1UL << rix;
rabbits ^= rbit;
int gdepth;
if (rbit & possible_squares)
{
gdepth = find_unassisted(rbit, search_depth, bad_neighbors);
} else {
gdepth = search_depth + 1;
}
board_depth[rix] = gdepth;
if (gdepth <= search_depth)
{
uint cur_goal = (goals_found[s] == 0) ? 0 : 1;
goals_found[s]++;
rabbit_location[s][cur_goal] = rix;
goal_depth[s][cur_goal] = gdepth;
//while (cur_goal > 0
if (cur_goal > 0
&& goal_depth[s][cur_goal] < goal_depth[s][cur_goal-1])
{
rix = rabbit_location[s][cur_goal];
rabbit_location[s][cur_goal] = rabbit_location[s][cur_goal-1];
rabbit_location[s][cur_goal-1] = rix;
gdepth = goal_depth[s][cur_goal];
goal_depth[s][cur_goal] = goal_depth[s][cur_goal-1];
goal_depth[s][cur_goal-1] = gdepth;
//cur_goal--;
}
}
}
}
}
}
class GoalSearchDT
{
const static int NOT_FOUND = 5;
Position start;
int[2] shortest;
ulong goal_squares;
private bool marker = false;
void set_start(Position pos)
{
if (start !is null)
{
Position.free(start);
}
start = pos.dup;
shortest[Side.WHITE] = shortest[Side.BLACK] = NOT_FOUND;
goal_squares = 0;
}
void clear_start()
{
if (start !is null)
{
Position.free(start);
}
start = null;
shortest[Side.WHITE] = shortest[Side.BLACK] = NOT_FOUND;
goal_squares = 0;
}
private ulong backward(ulong bits, Side side)
{
return side ? bits << 8 : bits >> 8;
}
private ulong forward(ulong bits, Side side)
{
return side ? bits >> 8 : bits << 8;
}
private int opponent_goal(ulong gbit, Side side)
{
ulong gneighbors = neighbors_of(gbit);
ulong side_friendlies;
ulong gsides;
Piece myrabbit;
Piece erabbit;
int enemyoffset;
if (gbit & RANK_8)
{
myrabbit = Piece.WRABBIT;
erabbit = Piece.BRABBIT;
enemyoffset = 6;
gsides = gneighbors & RANK_8;
side_friendlies = gsides
& start.placement[Side.WHITE]
& ~start.frozen;
} else {
myrabbit = Piece.BRABBIT;
erabbit = Piece.WRABBIT;
enemyoffset = -6;
gsides = gneighbors & RANK_1;
side_friendlies = gsides
& start.placement[Side.BLACK]
& ~start.frozen;
}
int shortest_goal = NOT_FOUND;
bitix gix = bitindex(gbit);
ulong back_bit = backward(gbit, side);
ulong bneighbors = neighbors_of(back_bit);
ulong side_empties = gsides
& start.bitBoards[Piece.EMPTY]
& (neighbors_of(start.placement[side]
& ~start.bitBoards[myrabbit]
& ~start.frozen)
| (1UL << start.lastfrom));
while (side_empties)
{
ulong se_bit = side_empties & -side_empties;
side_empties ^= se_bit;
bitix seix = bitindex(se_bit);
if ((start.lastfrom == seix)
&& ((start.lastpiece + enemyoffset)
> start.pieces[gix]))
{
if ((back_bit & start.bitBoards[myrabbit])
&& ((back_bit & ~start.frozen)
|| !(bneighbors & start.placement[side^1]
& ~start.bitBoards[erabbit] & ~gbit)))
return 2;
if ((back_bit & start.bitBoards[myrabbit])
&& (neighbors_of(bneighbors
& start.bitBoards[Piece.EMPTY])
& start.placement[side] & ~start.frozen))
return 3;
ulong bn_rabbits = bneighbors & start.bitBoards[myrabbit];
if (bn_rabbits & ~start.frozen)
{
if (!(bneighbors & start.placement[side^1]
& ~start.bitBoards[erabbit]
& ~gbit)
|| popcount(bneighbors
& start.placement[side]) > 1)
return 3;
}
}
if (back_bit & start.bitBoards[myrabbit])
{
if ((start.strongest[side][seix] + enemyoffset
> start.pieces[gix])
&& (start.strongest[side][seix] + enemyoffset
>= start.strongest[side^1][seix]))
{
ulong pullers = neighbors_of(se_bit)
& start.placement[side] & ~start.frozen;
while (pullers)
{
ulong pbit = pullers & -pullers;
pullers ^= pbit;
bitix pix = bitindex(pbit);
if ((start.pieces[pix] + enemyoffset
<= start.pieces[gix])
|| (start.pieces[pix] + enemyoffset
< start.strongest[side^1][seix]))
continue;
if (back_bit & ~start.frozen
|| (neighbors_of(se_bit)
& start.bitBoards[Piece.EMPTY]
& bneighbors))
{
shortest_goal = 4;
break;
}
}
}
}
}
// Check that unfrozen friendlies to the side of the opponent on the
// goal square exist
if (!side_friendlies)
return shortest_goal;
// check that some friendly neighbor of the goal square is stronger
// than it. It's a fast check so can give an early exit.
if (start.pieces[gix] >= start.strongest[side][gix] + enemyoffset)
return shortest_goal;
bool has_sempty = cast(bool)(gsides & start.bitBoards[Piece.EMPTY]);
ulong pullers = 0;
while (side_friendlies)
{
ulong sfbit = side_friendlies & -side_friendlies;
side_friendlies ^= sfbit;
bitix sfix = bitindex(sfbit);
// Is this piece stronger than the goal square piece
if (start.pieces[sfix] + enemyoffset <= start.pieces[gix])
continue;
if ((backward(sfbit, side)
& start.bitBoards[myrabbit])
&& (back_bit
& start.bitBoards[Piece.EMPTY])
&& (start.pieces[sfix] + enemyoffset
>= start.strongest[side^1][sfix]))
{
shortest_goal = 4;
} else if (has_sempty
&& (back_bit & start.bitBoards[myrabbit])
&& ((back_bit & ~start.frozen)
|| !(bneighbors & ~gbit
& start.placement[side^1]
& ~start.bitBoards[erabbit])))
{
shortest_goal = 4;
}
ulong empty_neighbors = neighbors_of(sfbit)
& start.bitBoards[Piece.EMPTY];
// Is there some place for us to move so we can pull the piece
// off the goal square
if (!empty_neighbors)
{
if ((back_bit & start.bitBoards[myrabbit] & ~start.frozen)
&& (start.pieces[sfix] + enemyoffset
>= start.strongest[side^1][sfix]))
{
ulong fn = neighbors_of(sfbit) & start.placement[side]
& neighbors_of(start.bitBoards[Piece.EMPTY]);
if ((fn & ~start.bitBoards[myrabbit])
|| (neighbors_of(fn) & ~backward(fn, side)
& start.bitBoards[Piece.EMPTY]))
shortest_goal = 4;
}
continue;
}
pullers |= sfbit;
}
// Can the piece occupying the goal square be pulled off
if (!pullers)
{
return shortest_goal;
}
// check for a rabbit on the square back from the goal square
if (back_bit & start.bitBoards[myrabbit])
{
// it's unfrozen
if (back_bit & ~start.frozen)
return 3;
ulong empty_neighbors = bneighbors & start.bitBoards[Piece.EMPTY];
// it can be unfrozen by the puller
if (empty_neighbors & neighbors_of(pullers))
return 3;
// there is another piece that can come unfreeze it
if (neighbors_of(empty_neighbors) & ~start.frozen
& start.placement[side])
return 4;
}
else if ((back_bit & start.bitBoards[Piece.EMPTY])
&& (bneighbors
& start.bitBoards[myrabbit]
& ~start.frozen)
&& (!(bneighbors & start.placement[side^1]
& ~start.bitBoards[erabbit])
|| (bneighbors & start.bitBoards[Piece.EMPTY]
& neighbors_of(pullers))
|| popcount(bneighbors & start.placement[side]) > 1))
{
return 4;
}
return shortest_goal;
}
private int friendly_goal(ulong gbit, Side side)
{
const static ulong dist2 = 0x0010387C38100000UL;
bitix gix = bitindex(gbit);
ulong gneighbors = neighbors_of(gbit);
int enemyoffset;
Piece myrabbit;
Piece erabbit;
ulong back_bit;
ulong goal_rank;
ulong rabbit_mask;
if (side == Side.WHITE)
{
myrabbit = Piece.WRABBIT;
erabbit = Piece.BRABBIT;
enemyoffset = 6;
goal_rank = RANK_8;
back_bit = gbit >> 8;
rabbit_mask = dist2 << (gix-44);
} else {
myrabbit = Piece.BRABBIT;
erabbit = Piece.WRABBIT;
enemyoffset = -6;
goal_rank = RANK_1;
back_bit = gbit << 8;
rabbit_mask = dist2 >> (28-gix);
}
// check that there isn't an enemy piece blocking any access
if (back_bit & start.placement[side^1])
return NOT_FOUND;
// check that there is a rabbit within range
if (!(rabbit_mask & start.bitBoards[myrabbit]))
return NOT_FOUND;
ulong side_neighbors = gneighbors & goal_rank;
ulong bneighbors = neighbors_of(back_bit);
ulong empty_sides = side_neighbors & start.bitBoards[Piece.EMPTY];
if (empty_sides)
{
if (back_bit & start.bitBoards[myrabbit])
{
// Will the rabbit still be unfrozen after moving out of the way
if ((bneighbors & start.placement[side] & ~gbit)
|| !(bneighbors & start.placement[side^1]
& ~start.bitBoards[erabbit]))
return 2;
// get any empty side spaces that lead to an empty space next
// to the rabbit
ulong freeze_check = neighbors_of(neighbors_of(empty_sides)
& bneighbors & start.bitBoards[Piece.EMPTY])
& empty_sides;
while (freeze_check)
{
ulong cbit = freeze_check & -freeze_check;
freeze_check ^= cbit;
bitix cix = bitindex(cbit);
if (start.pieces[cix] <= start.pieces[gix] + enemyoffset)
return 3;
}
ulong unfreezers = neighbors_of(bneighbors
& start.bitBoards[Piece.EMPTY]) & start.placement[side]
& ~back_bit;
if (unfreezers & ~start.frozen)
return 3;
int shortest_goal = NOT_FOUND;
// if the unfreezers are all frozen check to see if another
// piece can unfreeze them
if (neighbors_of(unfreezers)
& start.bitBoards[Piece.EMPTY] & ~bneighbors
& (neighbors_of(start.placement[side]
& ~start.bitBoards[myrabbit] & ~start.frozen)
| neighbors_of(start.bitBoards[myrabbit]
& ~start.frozen
& ~neighbors_of(forward(unfreezers, side)))))
{
shortest_goal = 4;
}
ulong pushsq = neighbors_of(bneighbors
& start.placement[side^1]) & empty_sides;
while (pushsq)
{
ulong pbit = pushsq & -pushsq;
pushsq ^= pbit;
bitix pix = bitindex(pbit);
if (start.pieces[gix] + enemyoffset >= start.strongest[side^1][pix])
{
ulong pn = neighbors_of(pbit) & bneighbors & ~gbit;
if (!(neighbors_of(pn) & start.bitBoards[Piece.EMPTY]
& ~pbit))
{
if ((neighbors_of(pbit)
& start.bitBoards[Piece.EMPTY])
&& !(bneighbors & start.placement[side^1]
& ~start.bitBoards[erabbit]
& ~pn)
&& (start.pieces[gix] + enemyoffset >
start.strongest[side^1][pix]))
{
shortest_goal = 4;
}
continue;
}
bitix pnix = bitindex(pn);
if (start.pieces[gix] + enemyoffset
> start.pieces[pnix])
{
shortest_goal = 4;
}
}
}
ulong unfsq = neighbors_of(bneighbors &
start.bitBoards[Piece.EMPTY])
& start.bitBoards[Piece.EMPTY]
& neighbors_of(start.placement[side] & ~start.frozen);
if (unfsq & ~(TRAPS
| neighbors_of(start.placement[side^1]
& ~start.placement[side])))
shortest_goal = 4;
while (unfsq)
{
ulong unfsqb = unfsq & -unfsq;
unfsq ^= unfsqb;
ulong unfsq_neighbors = neighbors_of(unfsqb);
if (popcount(unfsq_neighbors & start.placement[side]) > 1)
shortest_goal = 4;
if (unfsqb & TRAPS)
continue;
bitix unfsqix = bitindex(unfsqb);
if (start.strongest[side][unfsqix] + enemyoffset
>= start.strongest[side^1][unfsqix])
shortest_goal = 4;
}
ulong push_to = start.bitBoards[Piece.EMPTY];
if (popcount(empty_sides) < 2)
push_to &= ~empty_sides;
ulong obn = bneighbors & start.placement[side^1];
bool can_pull = popcount(bneighbors & start.placement[side^1]
& ~start.bitBoards[erabbit]) < 2;
if (can_pull && start.lastpiece != Piece.EMPTY
&& (neighbors_of(obn) & (1UL << start.lastfrom)))
{
ulong freezer = obn & ~start.bitBoards[erabbit];
assert (popcount(freezer) == 1);
ulong fpiece = start.pieces[bitindex(freezer)];
if (start.lastpiece + enemyoffset > fpiece)
{
return 3;
}
}
obn &= neighbors_of(start.placement[side]
& ~start.bitBoards[myrabbit]);
push_to &= neighbors_of(obn);
if (shortest_goal != NOT_FOUND
|| (!can_pull && !push_to))
return shortest_goal;
while (obn)
{
ulong obit = obn & -obn;
obn ^= obit;
bitix oix = bitindex(obit);
if (!(start.pieces[oix] < start.strongest[side][oix]
+ enemyoffset))
continue;
ulong pushers = neighbors_of(obit) & start.placement[side]
& ~start.frozen;
while (pushers)
{
ulong pbit = pushers & -pushers;
pushers ^= pbit;
bitix pix = bitindex(pbit);
if (start.pieces[pix] + enemyoffset
<= start.pieces[oix])
continue;
if ((push_to & neighbors_of(obit))
|| (can_pull && (neighbors_of(pbit)
& start.bitBoards[Piece.EMPTY])))
return 4;
}
}
return NOT_FOUND;
}
else if (back_bit & start.placement[side])
{
if (!(bneighbors & start.bitBoards[Piece.EMPTY]))
return NOT_FOUND;
ulong prabbits = bneighbors & start.bitBoards[myrabbit];
while (prabbits)
{
ulong rbit = prabbits & -prabbits;
prabbits ^= rbit;
ulong rn = neighbors_of(rbit);
if ((rn & start.placement[side] & ~back_bit)
|| ((rbit & ~TRAPS) && !(rn
& start.placement[side^1]
& ~start.bitBoards[erabbit])))
return 4;
}
return NOT_FOUND;
} else { // back_bit is empty
assert (back_bit & start.bitBoards[Piece.EMPTY]);
ulong bn_rabbits = bneighbors & start.bitBoards[myrabbit];
ulong fbn = bneighbors & start.placement[side] & ~gbit;
int fbn_pop = popcount(fbn);
if (bn_rabbits & ~start.frozen)
{
if (!(bneighbors & start.placement[side^1]
& ~start.bitBoards[erabbit])
|| fbn_pop > 1)
return 3;
// one neighbor is the goal, one is the rabbit,
// one is the freezing piece so there can only be one empty
ulong bn_empty = bneighbors & start.bitBoards[Piece.EMPTY];
if ((bn_empty & ~(TRAPS &
~neighbors_of(start.placement[side])))
&& (gbit & ~start.frozen))
{
return 4;
}
if (bn_empty & neighbors_of(empty_sides))
{
return 4;
}
ulong unfreezers = (neighbors_of(bn_empty)
| neighbors_of(bn_rabbits))
& start.placement[side]
& ~start.frozen;
if (!unfreezers)
return NOT_FOUND;
// if there is an unfreezer that is not a current neighbor
// of the rabbit we can goal for sure
ulong rneighbors = neighbors_of(bn_rabbits);
if (unfreezers & ~rneighbors)
{
return 4;
}
// otherwise we have to check that one of them won't freeze
// when the other moves and that the unfreezer won't be
// trapped
if ((bn_empty & neighbors_of(unfreezers))
&& !(rneighbors & start.placement[side^1]
& ~start.bitBoards[erabbit]))
return 4;
if (unfreezers & neighbors_of(start.placement[side]
& ~bn_rabbits))
return 4;
unfreezers &= ~TRAPS;
while (unfreezers)
{
ulong unfb = unfreezers & -unfreezers;
bitix unix = bitindex(unfb);
if (start.strongest[side^1][unix]
<= start.pieces[unix] + enemyoffset)
return 4;
unfreezers ^= unfb;
}
}
bool safe_bb = !(bneighbors & start.placement[side^1]
& ~start.bitBoards[erabbit]);
if (bn_rabbits && (safe_bb || fbn_pop > 1))
{
if (neighbors_of(neighbors_of(bn_rabbits & start.frozen)
& start.bitBoards[Piece.EMPTY])
& start.placement[side] & ~start.frozen
& ~bneighbors)
return 4;
if (safe_bb && (fbn & ~bn_rabbits & ~start.frozen
& neighbors_of(neighbors_of(bn_rabbits)
& start.bitBoards[Piece.EMPTY]
& ~back_bit)))
{
return 4;
}
if (safe_bb && (gbit & ~start.frozen) && (fbn & ~bn_rabbits
& neighbors_of(neighbors_of(bn_rabbits)
& start.bitBoards[Piece.EMPTY]
& ~back_bit)
& neighbors_of(empty_sides)))
{
return 4;
}
}
if (safe_bb || fbn)
{
ulong pempties = bneighbors & start.bitBoards[Piece.EMPTY]
& neighbors_of(start.bitBoards[myrabbit]
& ~start.frozen);
if (pempties & ~(TRAPS
| neighbors_of(start.placement[side^1]
& ~start.bitBoards[erabbit])))
return 4;
while (pempties)
{
ulong pebit = pempties & -pempties;
pempties ^= pebit;
if (popcount(neighbors_of(pebit)
& start.placement[side]) > 1)
return 4;
}
}
return NOT_FOUND;
}
} else {
// check that there is an unfrozen rabbit that can make it to us
ulong bn_rabbits = (bneighbors | back_bit)
& start.bitBoards[myrabbit] & ~start.frozen;
if (!bn_rabbits)
return NOT_FOUND;
// make sure another piece isn't in the way
if (!(back_bit & (start.bitBoards[myrabbit]
| start.bitBoards[Piece.EMPTY])))
return NOT_FOUND;
int shortest_goal = NOT_FOUND;
ulong side_to = neighbors_of(side_neighbors & start.placement[side])
& start.bitBoards[Piece.EMPTY];
if (side_to && ((side_to & bneighbors)
|| !(bneighbors & start.placement[side^1]
& ~start.bitBoards[erabbit])
|| (popcount((bneighbors | back_bit)
& start.placement[side]) > 2)))
{
if (back_bit & start.bitBoards[myrabbit])
return 3;
shortest_goal = 4;
}
int bside_pop = popcount(bneighbors & start.placement[side]);
ulong side_check = side_neighbors & start.placement[side^1];
while (side_check)
{
ulong sideb = side_check & -side_check;
side_check ^= sideb;
bitix sideix = bitindex(sideb);
ulong sideb_neighbors = neighbors_of(sideb);
ulong in_pull = 0;
if (start.lastpiece != Piece.EMPTY
&& ((1UL << start.lastfrom) & sideb_neighbors)
&& start.lastpiece + enemyoffset
> start.pieces[sideix])
{
in_pull = 1UL << start.lastfrom;
}
if (!in_pull && start.strongest[side][sideix] + enemyoffset
<= start.pieces[sideix])
continue;
ulong sideb_empties = sideb_neighbors
& start.bitBoards[Piece.EMPTY];
ulong sideb_friendlies = sideb_neighbors
& start.placement[side]
& ~gbit & ~start.frozen
& neighbors_of(start.bitBoards[Piece.EMPTY]);
if (!in_pull && !sideb_empties && !sideb_friendlies)
continue;
bool gstronger = start.pieces[gix] + enemyoffset
> start.pieces[sideix];
if ((in_pull | sideb_empties | sideb_friendlies) & goal_rank)
{
if (back_bit & start.bitBoards[myrabbit])
{
if (((sideb_empties & goal_rank) && gstronger)
|| (in_pull & goal_rank))
{
if (bside_pop > 1
|| !(bneighbors & start.placement[side^1]
& ~start.bitBoards[erabbit]))
{
return 3;
}
if ((sideb_empties & bneighbors)
|| (neighbors_of(bneighbors
& start.bitBoards[Piece.EMPTY])
& start.placement[side]
& ~start.frozen
& ~back_bit))
{
shortest_goal = 4;
}
} else {
ulong sfg = sideb_friendlies & goal_rank;
bitix sfgix = bitindex(sfg);
if (sfg && (gstronger
|| (start.pieces[sfgix] + enemyoffset
> start.pieces[sideix]))
&& (!(bneighbors & start.placement[side^1]
& ~start.bitBoards[erabbit])
|| (bneighbors
& start.placement[side]
& ~gbit)))
{
shortest_goal = 4;
}
}
}
else if ((((sideb_empties & goal_rank)
&& gstronger)
|| (in_pull & goal_rank))
&& (bside_pop > 2
|| !(bneighbors & start.placement[side^1]
& ~start.bitBoards[erabbit])))
{
shortest_goal = 4;
}
}
sideb_empties &= ~goal_rank;
sideb_friendlies &= ~goal_rank;
if (back_bit & start.bitBoards[myrabbit])
{
if ((sideb_empties && gstronger)
|| (in_pull & ~goal_rank))
{
if (((sideb & start.bitBoards[erabbit])
&& (bside_pop > 1
|| !(bneighbors & start.placement[side^1]
& ~start.bitBoards[erabbit])))
|| popcount(bneighbors
& start.placement[side]) > 1)
return 3;
if (neighbors_of(bneighbors
& start.bitBoards[Piece.EMPTY]
& ~sideb_empties)
& start.placement[side] & ~start.frozen
& ~back_bit)
{
shortest_goal = 4;
continue;
}
}
else if (sideb_friendlies
&& (bside_pop > 2
|| ((sideb & start.bitBoards[erabbit])
&& !(bneighbors & start.placement[side^1]
& ~start.bitBoards[erabbit])))
&& (!(sideb_friendlies & start.bitBoards[myrabbit])
|| (neighbors_of(sideb_friendlies)
& start.bitBoards[Piece.EMPTY]
& ~backward(sideb_friendlies, side))))
{
if (gstronger)
{
shortest_goal = 4;
continue;
}
bitix sfix = bitindex(sideb_friendlies);
if (start.pieces[sfix] + enemyoffset
> start.pieces[sideix])
{
shortest_goal = 4;
continue;
}
}
}
else if (sideb_empties && gstronger && (bside_pop > 2
|| ((sideb & start.bitBoards[erabbit])
&& !(bneighbors & start.placement[side^1]
& ~start.bitBoards[erabbit]))))
{
shortest_goal = 4;
}
else if ((sideb_friendlies & start.bitBoards[myrabbit])
&& gstronger && (!((bneighbors | sideb)
& start.placement[side^1]
& ~start.bitBoards[erabbit])
|| popcount(bneighbors & start.placement[side]) > 2))
{
shortest_goal = 4;
}
}
if (shortest_goal != NOT_FOUND)
return shortest_goal;
if (back_bit & start.bitBoards[myrabbit])
{
ulong side_friendlies = side_neighbors & start.placement[side];
if (neighbors_of(neighbors_of(side_friendlies)
& start.placement[side])
& start.bitBoards[Piece.EMPTY]
&& (back_bit & ~neighbors_of(start.placement[side^1]
& ~start.bitBoards[erabbit])
|| popcount(bneighbors & start.placement[side])
> 1))
{
ulong sff = neighbors_of(side_friendlies)
& start.placement[side]
& neighbors_of(start.bitBoards[Piece.EMPTY]);
if (sff & ~start.bitBoards[myrabbit])
return 4;
while (sff)
{
ulong sffb = sff & -sff;
sff ^= sffb;
if (neighbors_of(sffb)
& start.bitBoards[Piece.EMPTY]
& ~backward(sffb, side))
return 4;
}
}
if ((neighbors_of(side_friendlies)
& start.bitBoards[Piece.EMPTY])
&& (neighbors_of(bneighbors
& start.bitBoards[Piece.EMPTY])
& start.placement[side]
& ~start.frozen & ~back_bit))
{
return 4;
}
ulong pushed = neighbors_of(side_friendlies)
& start.placement[side^1]
& neighbors_of(start.bitBoards[Piece.EMPTY]);
if ((back_bit & neighbors_of(start.placement[side^1]
& ~start.bitBoards[erabbit]))
&& (popcount(bneighbors & start.placement[side]) < 2))
pushed &= bneighbors;
while (pushed)
{
ulong obit = pushed & -pushed;
pushed ^= obit;
bitix oix = bitindex(obit);
if (start.pieces[oix] >= start.strongest[side][oix]
+ enemyoffset)
continue;
ulong pushers = neighbors_of(obit) & side_neighbors
& start.placement[side];
while (pushers)
{
ulong pbit = pushers & -pushers;
pushers ^= pbit;
bitix pix = bitindex(pbit);
if (start.pieces[pix] + enemyoffset
> start.pieces[oix])
return 4;
}
}
} else {
// unable to move or push anything to the side so if we're
// frozen we can't goal
if (gbit & start.frozen)
return NOT_FOUND;
// if there's a place for us to go we can goal
ulong to = bneighbors & start.bitBoards[Piece.EMPTY];
if (to)
{
if (to & ~(TRAPS & ~neighbors_of(start.placement[side])))
return 4;
if (!(bneighbors & start.placement[side^1]
& ~start.bitBoards[erabbit]))
return 4;
}
}
}
return NOT_FOUND;
}
private int empty_goal_emtpy_back_mix_n_empty_bn(Side side, ulong gbit,
bitix gix, int enemyoffset, int myrabbit, int erabbit,
ulong back_bit, ulong rabbit_mask, ulong bneighbors,
ulong empty_bn, int shortest_goal)
{
ulong en = neighbors_of(empty_bn);
ulong rabbits = en & start.bitBoards[myrabbit]
& ~start.frozen;
if (rabbits)
{
if (popcount(en & start.placement[side]) > 1)
return 3;
if (!(empty_bn & TRAPS)
&& !(en & start.placement[side^1]
& ~start.bitBoards[erabbit]))
return 3;
if (shortest_goal < NOT_FOUND)
return shortest_goal;
ulong rn = neighbors_of(rabbits);
ulong unfreezers = (neighbors_of(en
& start.bitBoards[Piece.EMPTY])
| rn)
& start.placement[side] & ~start.frozen
& ~bneighbors;
if (!unfreezers)
return shortest_goal;
if (unfreezers & ~rn)
{
return 4;
}
assert (popcount(rabbits) == 1);
// can an unfreezer move first?
if ((!(rabbits & TRAPS)
|| popcount(rn & start.placement[side]) > 1)
&& (!(rn & start.placement[side^1]
& ~start.bitBoards[erabbit])
|| popcount(rn
& start.placement[side]) > 1)
&& (unfreezers & neighbors_of(en
& start.bitBoards[Piece.EMPTY])))
{
if (unfreezers & neighbors_of(en
& start.bitBoards[Piece.EMPTY]
& ~(TRAPS
& ~neighbors_of(
start.placement[side]
& ~unfreezers))))
return 4;
if (popcount(start.placement[side] & neighbors_of(en
& start.bitBoards[Piece.EMPTY]