-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAmatriciana.java
1338 lines (1338 loc) · 31.5 KB
/
Amatriciana.java
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 java.util.List;
import java.io.BufferedReader;
import java.util.Arrays;
import java.io.PrintStream;
import java.io.IOException;
import java.util.ArrayList;
import java.io.InputStreamReader;
import java.util.Random;
public class Amatriciana {
public static void main(String[]args)throws IOException{
Player.TIMING=true;
Player.DEBUG=false;
Player.SCORE=true;
Player p=getPlayer();
p.play(new BufferedReader(new InputStreamReader(System.in)),System.out);
}
public static Player getPlayer(){
return new LimitedValueUCTPlayer(new AllMoves(),new EqualTurnTime(29.5),1000,7.0,34.0,new XoRoShiRo128PlusRandom());
}
}
class Move {
private static final int LOCATION_MASK=0b00_111111;
private static final int TILE_MASK =0b11_000000;
private Move(){
}
public static final int location(final int row,final int column){
return 7*row+column;
}
public static final int row(final int location){
return location/7;
}
public static final int column(final int location){
return location %7;
}
public static final int getLocation(final int move){
return move&LOCATION_MASK;
}
public static final int getRow(final int move){
return row(getLocation(move));
}
public static final int getColumn(final int move){
return column(getLocation(move));
}
public static final int getTile(final int move){
return(move&TILE_MASK)>>6;
}
public static final int setTile(int move,int tile){
return(move&~TILE_MASK)|(tile<<6);
}
public static final int fromLocationTile(final int location,final int tile){
return(tile<<6)|location;
}
public static final int fromLocation(final int location){
return location;
}
public static final int fromRowColumnTile(final int row,final int column,final int tile){
return fromLocationTile(location(row,column),tile);
}
private static final char[]TILE_CHARS=new char[]{'s','l','r'};
public static String toString(int move){
char row=(char)(getRow(move)+'a');
char col=(char)(getColumn(move)+'a');
char tile=TILE_CHARS[getTile(move)];
return new StringBuilder().append(row).append(col).append(tile).toString();
}
public static int fromString(String move){
int row=move.charAt(0)-'a';
int col=move.charAt(1)-'a';
int tile=tileFromChar(move.charAt(2));
return fromRowColumnTile(row,col,tile);
}
private static int tileFromChar(char tile){
switch(tile){
case 's':
return Board.STRAIGHT;
case 'l':
return Board.LEFT;
default:
return Board.RIGHT;
}
}
public static int[]arrayFromList(List<Integer>moves){
int[]result=new int[moves.size()];
int i=0;
for(int move:moves){
result[i]=move;
i++;
}
return result;
}
public static void shuffle(int[]moves,Random rand){
for(int i=moves.length-1;i>0;i--){
int newIndex=rand.nextInt(i+1);
int temp=moves[newIndex];
moves[newIndex]=moves[i];
moves[i]=temp;
}
}
}
abstract class Board {
public static final byte EMPTY=-1;
public static final byte STRAIGHT=0;
public static final byte LEFT=1;
public static final byte RIGHT=2;
private int turn=-2;
private int scoreBlue=50;
private int scoreRed=50;
public abstract byte get(int loc);
public byte get(int row,int col){
return get(Move.location(row,col));
}
public boolean isEmpty(int loc){
return get(loc)==EMPTY;
}
public boolean isEmpty(int row,int col){
return isEmpty(Move.location(row,col));
}
protected abstract int applyRegularMove(int move);
protected abstract int undoRegularMove(int move);
public int getTurn(){
return turn;
}
public int getNumFreeSpaces(){
return 61-turn;
}
public boolean isGameOver(){
return turn==61;
}
public boolean isLegalMove(int move){
return isEmpty(Move.getLocation(move));
}
public void applyMove(int move){
updateScore(applyRegularMove(move));
turn++;
}
public void undoMove(int move){
turn--;
updateScore(undoRegularMove(move));
}
private void updateScore(int scoreDelta){
if(isCurrentPlayerBlue()){
scoreBlue+=scoreDelta;
}else{
scoreRed+=scoreDelta;
}
}
public int getScore(boolean blue){
return blue?scoreBlue:scoreRed;
}
public boolean isCurrentPlayerBlue(){
return turn %2==0;
}
public int scoreAfterMove(int move,boolean blue){
applyMove(move);
int score=getScore(blue);
undoMove(move);
return score;
}
protected void initialize(Board board){
turn=board.getTurn();
scoreBlue=board.getScore(true);
scoreRed=board.getScore(false);
}
protected void initializeTurn(){
int nTiles=0;
for(int loc=0;loc<63;loc++){
if(!isEmpty(loc)){
nTiles++;
}
}
turn=nTiles-2;
}
private static final int LEFT_TILE=Move.fromLocationTile(0,LEFT);
private static final int RIGHT_TILE=Move.fromLocationTile(0,RIGHT);
public int[]possibleMoves(){
int[]moves=new int[3*getNumFreeSpaces()];
int index=0;
for(int loc=0;loc<63;loc++){
if(isEmpty(loc)){
moves[index]=loc;
moves[index+1]=loc|LEFT_TILE;
moves[index+2]=loc|RIGHT_TILE;
index+=3;
}
}
return moves;
}
public int[]emptySpaces(){
int[]moves=new int[getNumFreeSpaces()];
int index=0;
for(int loc=0;loc<63;loc++){
if(isEmpty(loc)){
moves[index]=loc;
index++;
}
}
return moves;
}
public abstract int[]connectingMoves();
public abstract int[]nonNegativeMoves();
public void print(){
System.err.println(" abcdefg");
for(int row=0;row<9;row++){
System.err.print((char)(row+'a'));
for(int col=0;col<7;col++){
System.err.print(charForValue(get(row,col)));
}
System.err.println();
}
System.err.printf("B:%3d R:%3d%n",scoreBlue,scoreRed);
}
private char charForValue(int boardValue){
switch(boardValue){
case EMPTY:
return ' ';
case Board.STRAIGHT:
return 'S';
case Board.LEFT:
return 'L';
case Board.RIGHT:
return 'R';
default:
throw new IllegalArgumentException();
}
}
public void printMoves(){
for(int row=0;row<9;row++){
for(int col=0;col<7;col++){
if(!isEmpty(row,col)){
System.err.println(Move.toString(Move.fromRowColumnTile(row,col,get(row,col))));
}
}
}
}
}
class Paths {
public enum Edge{
FIRST,
SECOND;
}
public enum Side{
TOP,LEFT,BOTTOM,RIGHT;
}
private final int[]endpoint=new int[142];
private final int[]length=new int[142];
public Paths(){
for(int i=0;i<142;i++){
endpoint[i]=i;
}
}
public Paths(Paths paths){
System.arraycopy(paths.endpoint,0,endpoint,0,142);
System.arraycopy(paths.length,0,length,0,142);
}
public Paths(Board board){
this();
for(int loc=0;loc<63;loc++){
if(!board.isEmpty(loc)){
int move=Move.fromLocationTile(loc,board.get(loc));
addEdge(move,Edge.FIRST,true);
addEdge(move,Edge.SECOND,true);
}
}
}
public int[]getPossibleEndpointLocations(int midpoint){
int end=endpoint[midpoint];
if(onBoundary(end)){
return null;
}
return getSquares(end);
}
public boolean anyPathEndsAtRightSide(final int row,final int col){
final int top=15*row+col;
final int left=top+7;
final int right=top+8;
final int bottom=top+15;
return rightBoundary(endpoint[top])
||rightBoundary(endpoint[left])
||rightBoundary(endpoint[right])
||rightBoundary(endpoint[bottom]);
}
public boolean couldBeNegative(final int row,final int col){
final int top=15*row+col;
final int left=top+7;
final int right=top+8;
final int bottom=top+15;
final int endTop=endpoint[top];
final int endLeft=endpoint[left];
final int endRight=endpoint[right];
final int endBottom=endpoint[bottom];
if(endTop==left||endTop==right||endTop==bottom
||endLeft==right||endLeft==bottom
||endRight==bottom){
return true;
}
int nLeft=0;
if(leftBoundary(endTop)){
nLeft++;
}
if(leftBoundary(endLeft)){
nLeft++;
}
if(leftBoundary(endRight)){
nLeft++;
}
if(leftBoundary(endBottom)){
nLeft++;
}
if(nLeft>1){
return true;
}
int nRight=0;
if(rightBoundary(endTop)){
nRight++;
}
if(rightBoundary(endLeft)){
nRight++;
}
if(rightBoundary(endRight)){
nRight++;
}
if(rightBoundary(endBottom)){
nRight++;
}
return nRight>1;
}
private int getMidpoint(final int row,final int col,final Side side){
switch(side){
case TOP:
return 15*row+col;
case LEFT:
return 15*row+col+7;
case BOTTOM:
return 15*row+col+15;
default:
return 15*row+col+8;
}
}
private int[]getSquares(int midpoint){
int row=(midpoint-7)/15;
int withinRow=midpoint-15*row;
if(withinRow<15){
int col=withinRow-8;
return new int[]{
row,col,
row,col+1,};
}else{
int col=withinRow-15;
return new int[]{
row,col,
row+1,col
};
}
}
private boolean leftBoundary(int midpoint){
return midpoint %15==7;
}
private boolean rightBoundary(int midpoint){
return midpoint %15==14;
}
private boolean topBoundary(int midpoint){
return midpoint<7;
}
private boolean bottomBoundary(int midpoint){
return midpoint>134;
}
private boolean onBoundary(int midpoint){
return midpoint<7
||midpoint>134
||midpoint %15==7
||midpoint %15==14;
}
private static final Side[][]FIRST_SIDE_FOR=new Side[][]{
new Side[]{Side.LEFT,Side.BOTTOM},
new Side[]{Side.LEFT,Side.BOTTOM},
new Side[]{Side.LEFT,Side.TOP}
};
private static final Side[][]SECOND_SIDE_FOR=new Side[][]{
new Side[]{Side.RIGHT,Side.TOP},
new Side[]{Side.TOP,Side.RIGHT},
new Side[]{Side.BOTTOM,Side.RIGHT}
};
final public int addEdge(final int move,final Edge edge,final boolean blue){
final int row=Move.getRow(move);
final int col=Move.getColumn(move);
final int tile=Move.getTile(move);
final int mid1=getMidpoint(row,col,FIRST_SIDE_FOR[tile][edge.ordinal()]);
final int mid2=getMidpoint(row,col,SECOND_SIDE_FOR[tile][edge.ordinal()]);
final int end1=endpoint[mid1];
final int end2=endpoint[mid2];
if(end1==mid2){
return-5;
}
final boolean left1=leftBoundary(end1);
final boolean left2=leftBoundary(end2);
final boolean right1=rightBoundary(end1);
final boolean right2=rightBoundary(end2);
int score=0;
if((left1&&left2)||(right1&&right2)){
score=-3;
}else if(left1&&right2){
score=1+(blue?length[end1]:length[end2]);
}else if(right1&&left2){
score=1+(blue?length[end2]:length[end1]);
}
endpoint[end1]=end2;
endpoint[end2]=end1;
final int newLength=length[end1]+length[end2]+1;
length[end1]=newLength;
length[end2]=newLength;
return score;
}
final public int removeEdge(final int move,final Edge edge,final boolean blue){
final int row=Move.getRow(move);
final int col=Move.getColumn(move);
final int tile=Move.getTile(move);
final int mid1=getMidpoint(row,col,FIRST_SIDE_FOR[tile][edge.ordinal()]);
final int mid2=getMidpoint(row,col,SECOND_SIDE_FOR[tile][edge.ordinal()]);
final int end1=endpoint[mid1];
final int end2=endpoint[mid2];
if(length[end1]==1){
endpoint[mid1]=mid1;
endpoint[mid2]=mid2;
length[mid1]=0;
length[mid2]=0;
}else if(end1==end2){
if(length[mid1]>length[mid2]){
endpoint[end1]=mid2;
length[end1]=length[mid2];
endpoint[mid1]=mid1;
length[mid1]=0;
}else{
endpoint[end1]=mid1;
length[end1]=length[mid1];
endpoint[mid2]=mid2;
length[mid2]=0;
}
}else{
endpoint[end1]=mid1;
endpoint[end2]=mid2;
length[end1]=length[mid1];
length[end2]=length[mid2];
}
int score=0;
final int preEnd1=endpoint[mid1];
final int preEnd2=endpoint[mid2];
if(preEnd1==mid2){
score=5;
}else{
final boolean left1=leftBoundary(preEnd1);
final boolean left2=leftBoundary(preEnd2);
final boolean right1=rightBoundary(preEnd1);
final boolean right2=rightBoundary(preEnd2);
if((left1&&left2)||(right1&&right2)){
score=3;
}else if(left1&&right2){
score=-1-(blue?length[preEnd1]:length[preEnd2]);
}else if(right1&&left2){
score=-1-(blue?length[preEnd2]:length[preEnd1]);
}
}
return score;
}
public void checkPaths(int row,int col){
for(Side side:Side.values()){
checkPathFrom(getMidpoint(row,col,side));
}
}
public void printDebug(){
System.err.println("Endpoints:");
System.err.println(Arrays.toString(endpoint));
System.err.println("Lengths:");
System.err.println(Arrays.toString(length));
}
private void checkPathFrom(int midpoint){
int end=endpoint[midpoint];
if(endpoint[end]!=midpoint){
printDebug();
throw new IllegalStateException(
String.format("Incorrect path. Midpoint %d has endpoint %d. Expected midpoint %d to have endpoint %d,but was %d",
midpoint,end,end,midpoint,endpoint[end])
);
}
if(length[end]!=length[midpoint]){
printDebug();
throw new IllegalStateException(
String.format("Length mismatch. Midpoint %d has endpoint %d and length %d,but midpoint %d has length %d",
midpoint,end,length[midpoint],end,length[end])
);
}
if(length[end]<0||length[end]>126){
printDebug();
throw new IllegalStateException(
String.format("Illegal length value. Midpoint %d and its endpoint %d have length %d",
midpoint,end,length[end])
);
}
}
}
class ArrayBoard extends Board {
final byte[]board=new byte[63];
final Paths paths;
public ArrayBoard(){
Arrays.fill(board,EMPTY);
paths=new Paths();
}
public ArrayBoard(byte[][]grid){
for(int i=0;i<9;i++){
System.arraycopy(grid[i],0,this.board,7*i,7);
}
initializeTurn();
paths=new Paths(this);
}
public ArrayBoard(Board board){
if(board instanceof ArrayBoard){
ArrayBoard arrayBoard=(ArrayBoard)board;
System.arraycopy(arrayBoard.board,0,this.board,0,63);
paths=new Paths(arrayBoard.paths);
}else{
for(int loc=0;loc<63;loc++){
this.board[loc]=board.get(loc);
}
paths=new Paths(board);
}
initialize(board);
}
@Override
public byte get(int loc){
return board[loc];
}
@Override
protected int applyRegularMove(int move){
int loc=Move.getLocation(move);
board[loc]=(byte)Move.getTile(move);
return scoreDelta(move,isCurrentPlayerBlue());
}
@Override
protected int undoRegularMove(int move){
int loc=Move.getLocation(move);
board[loc]=EMPTY;
return undoScoreDelta(move,isCurrentPlayerBlue());
}
private int scoreDelta(int move,boolean blue){
return paths.addEdge(move,Paths.Edge.FIRST,blue)+paths.addEdge(move,Paths.Edge.SECOND,blue);
}
private int undoScoreDelta(int move,boolean blue){
return paths.removeEdge(move,Paths.Edge.SECOND,blue)+paths.removeEdge(move,Paths.Edge.FIRST,blue);
}
@Override
public int[]connectingMoves(){
int[]leftSideMidpoints=new int[]{7,22,37,52,67,82,97,112,127};
List<Integer>connectingMoves=new ArrayList<>();
for(int left:leftSideMidpoints){
int[]endSquares=paths.getPossibleEndpointLocations(left);
if(endSquares==null){
continue;
}
int row,col;
if(isEmpty(endSquares[0],endSquares[1])){
row=endSquares[0];
col=endSquares[1];
}else{
row=endSquares[2];
col=endSquares[3];
}
if(paths.anyPathEndsAtRightSide(row,col)){
connectingMoves.add(Move.fromRowColumnTile(row,col,Board.STRAIGHT));
connectingMoves.add(Move.fromRowColumnTile(row,col,Board.LEFT));
connectingMoves.add(Move.fromRowColumnTile(row,col,Board.RIGHT));
}
}
return Move.arrayFromList(connectingMoves);
}
@Override
public int[]nonNegativeMoves(){
int[]moves=new int[3*getNumFreeSpaces()];
int index=0;
for(int row=0;row<9;row++){
for(int col=0;col<7;col++){
if(isEmpty(row,col)&&!paths.couldBeNegative(row,col)){
moves[index]=Move.fromRowColumnTile(row,col,Board.STRAIGHT);
moves[index+1]=Move.fromRowColumnTile(row,col,Board.LEFT);
moves[index+2]=Move.fromRowColumnTile(row,col,Board.RIGHT);
index+=3;
}
}
}
return Arrays.copyOf(moves,index);
}
}
class BitBoard extends Board {
final Paths paths;
long empty=Long.MAX_VALUE;
long straight=0;
long left=0;
long right=0;
public BitBoard(){
paths=new Paths();
}
public BitBoard(byte[][]grid){
for(int row=0;row<9;row++){
for(int col=0;col<7;col++){
set(Move.location(row,col),grid[row][col]);
}
}
initializeTurn();
paths=new Paths(this);
}
public BitBoard(Board board){
if(board instanceof BitBoard){
BitBoard bitBoard=(BitBoard)board;
this.empty=bitBoard.empty;
this.straight=bitBoard.straight;
this.left=bitBoard.left;
this.right=bitBoard.right;
paths=new Paths(bitBoard.paths);
}else{
for(int loc=0;loc<63;loc++){
set(loc,board.get(loc));
}
paths=new Paths(board);
}
initialize(board);
}
@Override
public byte get(final int loc){
final long locBit=1L<<loc;
if((empty&locBit)>0){
return EMPTY;
}
if((straight&locBit)>0){
return STRAIGHT;
}
if((left&locBit)>0){
return LEFT;
}
return RIGHT;
}
@Override
public boolean isEmpty(int loc){
return(empty&(1L<<loc))>0;
}
private String debugString(long state){
StringBuilder sb=new StringBuilder(64);
sb.append(Long.toString(state,2));
while(sb.length()<64){
sb.insert(0,'0');
}
return sb.toString();
}
private void set(final int loc,final int tile){
final long locBit=1L<<loc;
switch(tile){
case EMPTY:
empty|=locBit;
straight&=~locBit;
left&=~locBit;
right&=~locBit;
break;
case STRAIGHT:
empty&=~locBit;
straight|=locBit;
left&=~locBit;
right&=~locBit;
break;
case LEFT:
empty&=~locBit;
straight&=~locBit;
left|=locBit;
right&=~locBit;
break;
default:
empty&=~locBit;
straight&=~locBit;
left&=~locBit;
right|=locBit;
break;
}
}
@Override
protected int applyRegularMove(int move){
int loc=Move.getLocation(move);
set(loc,Move.getTile(move));
return scoreDelta(move,isCurrentPlayerBlue());
}
@Override
protected int undoRegularMove(int move){
int loc=Move.getLocation(move);
set(loc,EMPTY);
return undoScoreDelta(move,isCurrentPlayerBlue());
}
private int scoreDelta(int move,boolean blue){
return paths.addEdge(move,Paths.Edge.FIRST,blue)+paths.addEdge(move,Paths.Edge.SECOND,blue);
}
private int undoScoreDelta(int move,boolean blue){
return paths.removeEdge(move,Paths.Edge.SECOND,blue)+paths.removeEdge(move,Paths.Edge.FIRST,blue);
}
@Override
public int[]emptySpaces(){
final int n=getNumFreeSpaces();
final int[]locations=new int[n];
long tempEmpty=empty;
for(int i=0;i<n;i++){
locations[i]=Long.numberOfTrailingZeros(tempEmpty);
tempEmpty&=~Long.lowestOneBit(tempEmpty);
}
return locations;
}
private static final int LEFT_TILE=Move.fromLocationTile(0,LEFT);
private static final int RIGHT_TILE=Move.fromLocationTile(0,RIGHT);
@Override
public int[]possibleMoves(){
final int n=3*getNumFreeSpaces();
final int[]moves=new int[n];
long tempEmpty=empty;
for(int i=0;i<n;i+=3){
int loc=Long.numberOfTrailingZeros(tempEmpty);
moves[i]=loc;
moves[i+1]=loc|LEFT_TILE;
moves[i+2]=loc|RIGHT_TILE;
tempEmpty&=~Long.lowestOneBit(tempEmpty);
}
return moves;
}
@Override
public int[]connectingMoves(){
int[]leftSideMidpoints=new int[]{7,22,37,52,67,82,97,112,127};
List<Integer>connectingMoves=new ArrayList<>();
for(int left:leftSideMidpoints){
int[]endSquares=paths.getPossibleEndpointLocations(left);
if(endSquares==null){
continue;
}
int row,col;
if(isEmpty(endSquares[0],endSquares[1])){
row=endSquares[0];
col=endSquares[1];
}else{
row=endSquares[2];
col=endSquares[3];
}
if(paths.anyPathEndsAtRightSide(row,col)){
connectingMoves.add(Move.fromRowColumnTile(row,col,Board.STRAIGHT));
connectingMoves.add(Move.fromRowColumnTile(row,col,Board.LEFT));
connectingMoves.add(Move.fromRowColumnTile(row,col,Board.RIGHT));
}
}
return Move.arrayFromList(connectingMoves);
}
@Override
public int[]nonNegativeMoves(){
int[]moves=new int[3*getNumFreeSpaces()];
int index=0;
for(int row=0;row<9;row++){
for(int col=0;col<7;col++){
if(isEmpty(row,col)&&!paths.couldBeNegative(row,col)){
moves[index]=Move.fromRowColumnTile(row,col,Board.STRAIGHT);
moves[index+1]=Move.fromRowColumnTile(row,col,Board.LEFT);
moves[index+2]=Move.fromRowColumnTile(row,col,Board.RIGHT);
index+=3;
}
}
}
return Arrays.copyOf(moves,index);
}
}
class RolloutBoard extends Board {
private final Paths paths;
private long empty=Long.MAX_VALUE;
public RolloutBoard(){
paths=new Paths();
}
public RolloutBoard(byte[][]grid){
for(int row=0;row<9;row++){
for(int col=0;col<7;col++){
set(Move.location(row,col),grid[row][col]);
}
}
initializeTurn();
paths=new Paths(this);
}
public RolloutBoard(Board board){
if(board instanceof RolloutBoard){
RolloutBoard rolloutBoard=(RolloutBoard)board;
this.empty=rolloutBoard.empty;
paths=new Paths(rolloutBoard.paths);
}else if(board instanceof BitBoard){
BitBoard bitBoard=(BitBoard)board;
this.empty=bitBoard.empty;
paths=new Paths(bitBoard.paths);
}else if(board instanceof ArrayBoard){
ArrayBoard arrayBoard=(ArrayBoard)board;
for(int loc=0;loc<63;loc++){
set(loc,board.get(loc));
}
paths=new Paths(arrayBoard.paths);
}else{
for(int loc=0;loc<63;loc++){
set(loc,board.get(loc));
}
paths=new Paths(board);
}
initialize(board);
}
@Override
public byte get(final int loc){
throw new UnsupportedOperationException();
}
@Override
public boolean isEmpty(int loc){
return(empty&(1L<<loc))>0;
}
private void set(final int loc,final int tile){
if(tile!=EMPTY){
empty&=~(1L<<loc);
}
}
@Override
protected int applyRegularMove(int move){
int loc=Move.getLocation(move);
set(loc,Move.getTile(move));
return scoreDelta(move,isCurrentPlayerBlue());
}
@Override
protected int undoRegularMove(int move){
throw new UnsupportedOperationException();
}
private int scoreDelta(int move,boolean blue){
return paths.addEdge(move,Paths.Edge.FIRST,blue)+paths.addEdge(move,Paths.Edge.SECOND,blue);
}
@Override
public int[]emptySpaces(){
final int n=getNumFreeSpaces();
final int[]locations=new int[n];
long tempEmpty=empty;
for(int i=0;i<n;i++){
locations[i]=Long.numberOfTrailingZeros(tempEmpty);
tempEmpty&=~Long.lowestOneBit(tempEmpty);
}
return locations;
}
private static final int LEFT_TILE=Move.fromLocationTile(0,LEFT);
private static final int RIGHT_TILE=Move.fromLocationTile(0,RIGHT);
@Override
public int[]possibleMoves(){
final int n=3*getNumFreeSpaces();
final int[]moves=new int[n];
long tempEmpty=empty;
for(int i=0;i<n;i+=3){
int loc=Long.numberOfTrailingZeros(tempEmpty);
moves[i]=loc;
moves[i+1]=loc|LEFT_TILE;
moves[i+2]=loc|RIGHT_TILE;
tempEmpty&=~Long.lowestOneBit(tempEmpty);
}
return moves;
}
@Override
public int[]connectingMoves(){
throw new UnsupportedOperationException();
}
@Override
public int[]nonNegativeMoves(){
throw new UnsupportedOperationException();
}
}
class XoRoShiRo128PlusRandom extends Random {
private static final long serialVersionUID=1L;
private long s0,s1;
protected XoRoShiRo128PlusRandom(final long s0,final long s1){
this.s0=s0;
this.s1=s1;
}
public XoRoShiRo128PlusRandom(final long seed){
setSeed(seed);
}
public XoRoShiRo128PlusRandom(){
this(randomSeed());
}
private static final XoRoShiRo128PlusRandom seedUniquifier=new XoRoShiRo128PlusRandom(System.nanoTime());
private static long randomSeed(){
final long x;
synchronized(seedUniquifier){
x=seedUniquifier.nextLong();
}
return x^System.nanoTime();
}
public XoRoShiRo128PlusRandom copy(){
return new XoRoShiRo128PlusRandom(s0,s1);
}
@Override
public long nextLong(){
final long s0=this.s0;
long s1=this.s1;
final long result=s0+s1;
s1^=s0;
this.s0=Long.rotateLeft(s0,24)^s1^s1<<16;
this.s1=Long.rotateLeft(s1,37);
return result;
}
@Override
public int nextInt(){
return(int)(nextLong()>>>32);
}
@Override
public int nextInt(final int n){
return(int)nextLong(n);
}
public long nextLong(final long n){
if(n<=0){
throw new IllegalArgumentException("illegal bound "+n+"(must be positive)");
}
long t=nextLong();
final long nMinus1=n-1;
if((n&nMinus1)==0){
return(t>>>Long.numberOfLeadingZeros(nMinus1))&nMinus1;
}
for(long u=t>>>1;u+nMinus1-(t=u %n)<0;u=nextLong()>>>1);
return t;
}
@Override
public double nextDouble(){
return(nextLong()>>>11)*0x1.0p-53;
}
public double nextDoubleFast(){
return Double.longBitsToDouble(0x3FFL<<52|nextLong()>>>12)-1.0;
}
@Override
public float nextFloat(){
return(nextLong()>>>40)*0x1.0p-24f;
}
@Override
public boolean nextBoolean(){
return nextLong()<0;
}
@Override
public void nextBytes(final byte[]bytes){
int i=bytes.length,n=0;
while(i!=0){
n=Math.min(i,8);
for(long bits=nextLong();n--!=0;bits>>=8){
bytes[--i]=(byte)bits;
}
}
}
protected XoRoShiRo128PlusRandom jump(final long[]jump){
long s0=0;
long s1=0;
for(final long element:jump){
for(int b=0;b<64;b++){
if((element&1L<<b)!=0){
s0^=this.s0;
s1^=this.s1;
}
nextLong();
}
}
this.s0=s0;
this.s1=s1;
return this;
}
private static final long[]JUMP={0xdf900294d8f554a5L,0x170865df4b3201fcL};
public XoRoShiRo128PlusRandom jump(){
return jump(JUMP);
}
private static final long[]LONG_JUMP={0xd2a98b26625eee7bL,0xdddf9b1090aa7ac1L};
public XoRoShiRo128PlusRandom longJump(){
return jump(LONG_JUMP);
}
public XoRoShiRo128PlusRandom split(){
nextLong();
final XoRoShiRo128PlusRandom split=copy();
long h0=s0;
long h1=s1;
long h2=s0+0x55a650a4c1dac3e9L;
long h3=s1+0xb39ae98dfa439b73L;
h2=Long.rotateLeft(h2,50);
h2+=h3;
h0^=h2;
h3=Long.rotateLeft(h3,52);
h3+=h0;
h1^=h3;
h0=Long.rotateLeft(h0,30);
h0+=h1;
h2^=h0;
h1=Long.rotateLeft(h1,41);
h1+=h2;
h3^=h1;
h2=Long.rotateLeft(h2,54);
h2+=h3;
h0^=h2;
h3=Long.rotateLeft(h3,48);
h3+=h0;
h1^=h3;
h0=Long.rotateLeft(h0,38);
h0+=h1;
h2^=h0;
h1=Long.rotateLeft(h1,37);
h1+=h2;
h3^=h1;