1818
1919package org .petero .cuckoo .engine .chess ;
2020
21+ import java .util .Arrays ;
22+
2123public class BitBoard {
2224
25+ private BitBoard () {
26+ }
27+
2328 /** Squares attacked by a king on a given square. */
24- public static final long [] kingAttacks ;
25- public static final long [] knightAttacks ;
26- public static final long [] wPawnAttacks , bPawnAttacks ;
29+ protected static final long [] kingAttacks ;
30+ protected static final long [] knightAttacks ;
31+ protected static final long [] wPawnAttacks ;
32+ protected static final long [] bPawnAttacks ;
2733
2834 // Squares preventing a pawn from being a passed pawn, if occupied by enemy pawn
29- static final long [] wPawnBlockerMask , bPawnBlockerMask ;
35+ static final long [] wPawnBlockerMask ;
36+ static final long [] bPawnBlockerMask ;
3037
31- public static final long maskAToGFiles = 0x7F7F7F7F7F7F7F7FL ;
32- public static final long maskBToHFiles = 0xFEFEFEFEFEFEFEFEL ;
33- public static final long maskAToFFiles = 0x3F3F3F3F3F3F3F3FL ;
34- public static final long maskCToHFiles = 0xFCFCFCFCFCFCFCFCL ;
38+ public static final long MASK_A_TO_G_FILES = 0x7F7F7F7F7F7F7F7FL ;
39+ public static final long MASK_B_TO_H_FILES = 0xFEFEFEFEFEFEFEFEL ;
40+ public static final long MASK_A_TO_F_FILES = 0x3F3F3F3F3F3F3F3FL ;
41+ public static final long MASK_C_TO_H_FILES = 0xFCFCFCFCFCFCFCFCL ;
3542
36- public static final long [] maskFile = {
43+ protected static final long [] MASK_FILE = {
3744 0x0101010101010101L ,
3845 0x0202020202020202L ,
3946 0x0404040404040404L ,
@@ -44,29 +51,27 @@ public class BitBoard {
4451 0x8080808080808080L
4552 };
4653
47- public static final long maskRow1 = 0x00000000000000FFL ;
48- public static final long maskRow2 = 0x000000000000FF00L ;
49- public static final long maskRow3 = 0x0000000000FF0000L ;
50- public static final long maskRow4 = 0x00000000FF000000L ;
51- public static final long maskRow5 = 0x000000FF00000000L ;
52- public static final long maskRow6 = 0x0000FF0000000000L ;
53- public static final long maskRow7 = 0x00FF000000000000L ;
54- public static final long maskRow8 = 0xFF00000000000000L ;
55- public static final long maskRow1Row8 = 0xFF000000000000FFL ;
54+ public static final long MASK_ROW_1 = 0x00000000000000FFL ;
55+ public static final long MASK_ROW_2 = 0x000000000000FF00L ;
56+ public static final long MASK_ROW_3 = 0x0000000000FF0000L ;
57+ public static final long MASK_ROW_6 = 0x0000FF0000000000L ;
58+ public static final long MASK_ROW_7 = 0x00FF000000000000L ;
59+ public static final long MASK_ROW_8 = 0xFF00000000000000L ;
60+ public static final long MASK_ROW_1_ROW_8 = 0xFF000000000000FFL ;
5661
57- public static final long maskDarkSq = 0xAA55AA55AA55AA55L ;
58- public static final long maskLightSq = 0x55AA55AA55AA55AAL ;
62+ public static final long MASK_DARK_SQ = 0xAA55AA55AA55AA55L ;
63+ public static final long MASK_LIGHT_SQ = 0x55AA55AA55AA55AAL ;
5964
60- public static final long maskCorners = 0x8100000000000081L ;
65+ public static final long MASK_CORNERS = 0x8100000000000081L ;
6166
6267 static {
6368 // Compute king attacks
6469 kingAttacks = new long [64 ];
6570
6671 for (int sq = 0 ; sq < 64 ; sq ++) {
6772 long m = 1L << sq ;
68- long mask = (((m >>> 1 ) | (m << 7 ) | (m >>> 9 )) & maskAToGFiles ) |
69- (((m << 1 ) | (m << 9 ) | (m >>> 7 )) & maskBToHFiles ) |
73+ long mask = (((m >>> 1 ) | (m << 7 ) | (m >>> 9 )) & MASK_A_TO_G_FILES ) |
74+ (((m << 1 ) | (m << 9 ) | (m >>> 7 )) & MASK_B_TO_H_FILES ) |
7075 (m << 8 ) | (m >>> 8 );
7176 kingAttacks [sq ] = mask ;
7277 }
@@ -75,10 +80,10 @@ public class BitBoard {
7580 knightAttacks = new long [64 ];
7681 for (int sq = 0 ; sq < 64 ; sq ++) {
7782 long m = 1L << sq ;
78- long mask = (((m << 6 ) | (m >>> 10 )) & maskAToFFiles ) |
79- (((m << 15 ) | (m >>> 17 )) & maskAToGFiles ) |
80- (((m << 17 ) | (m >>> 15 )) & maskBToHFiles ) |
81- (((m << 10 ) | (m >>> 6 )) & maskCToHFiles );
83+ long mask = (((m << 6 ) | (m >>> 10 )) & MASK_A_TO_F_FILES ) |
84+ (((m << 15 ) | (m >>> 17 )) & MASK_A_TO_G_FILES ) |
85+ (((m << 17 ) | (m >>> 15 )) & MASK_B_TO_H_FILES ) |
86+ (((m << 10 ) | (m >>> 6 )) & MASK_C_TO_H_FILES );
8287 knightAttacks [sq ] = mask ;
8388 }
8489
@@ -89,41 +94,41 @@ public class BitBoard {
8994 bPawnBlockerMask = new long [64 ];
9095 for (int sq = 0 ; sq < 64 ; sq ++) {
9196 long m = 1L << sq ;
92- long mask = ((m << 7 ) & maskAToGFiles ) | ((m << 9 ) & maskBToHFiles );
97+ long mask = ((m << 7 ) & MASK_A_TO_G_FILES ) | ((m << 9 ) & MASK_B_TO_H_FILES );
9398 wPawnAttacks [sq ] = mask ;
94- mask = ((m >>> 9 ) & maskAToGFiles ) | ((m >>> 7 ) & maskBToHFiles );
99+ mask = ((m >>> 9 ) & MASK_A_TO_G_FILES ) | ((m >>> 7 ) & MASK_B_TO_H_FILES );
95100 bPawnAttacks [sq ] = mask ;
96101
97102 int x = Position .getX (sq );
98103 int y = Position .getY (sq );
99104 m = 0 ;
100105 for (int y2 = y +1 ; y2 < 8 ; y2 ++) {
101106 if (x > 0 ) m |= 1L << Position .getSquare (x -1 , y2 );
102- m |= 1L << Position .getSquare (x , y2 );
107+ m |= 1L << Position .getSquare (x , y2 );
103108 if (x < 7 ) m |= 1L << Position .getSquare (x +1 , y2 );
104109 }
105110 wPawnBlockerMask [sq ] = m ;
106111 m = 0 ;
107112 for (int y2 = y -1 ; y2 >= 0 ; y2 --) {
108113 if (x > 0 ) m |= 1L << Position .getSquare (x -1 , y2 );
109- m |= 1L << Position .getSquare (x , y2 );
114+ m |= 1L << Position .getSquare (x , y2 );
110115 if (x < 7 ) m |= 1L << Position .getSquare (x +1 , y2 );
111116 }
112117 bPawnBlockerMask [sq ] = m ;
113118 }
114119 }
115120
116- private final static long [][] rTables ;
117- private final static long [] rMasks ;
118- private final static int [] rBits = { 12 , 11 , 11 , 11 , 11 , 11 , 11 , 12 ,
121+ private static final long [][] rTables ;
122+ private static final long [] rMasks ;
123+ private static final int [] rBits = { 12 , 11 , 11 , 11 , 11 , 11 , 11 , 12 ,
119124 11 , 10 , 10 , 10 , 10 , 10 , 10 , 11 ,
120125 11 , 10 , 10 , 10 , 10 , 10 , 10 , 11 ,
121126 11 , 10 , 10 , 10 , 10 , 10 , 10 , 11 ,
122127 11 , 10 , 10 , 10 , 10 , 10 , 10 , 11 ,
123128 11 , 10 , 10 , 10 , 10 , 10 , 10 , 11 ,
124129 10 , 9 , 9 , 9 , 9 , 9 , 10 , 10 ,
125130 11 , 10 , 10 , 10 , 10 , 11 , 11 , 11 };
126- private final static long [] rMagics = {
131+ private static final long [] rMagics = {
127132 0x0080011084624000L , 0x1440031000200141L , 0x2080082004801000L , 0x0100040900100020L ,
128133 0x0200020010200408L , 0x0300010008040002L , 0x040024081000a102L , 0x0080003100054680L ,
129134 0x1100800040008024L , 0x8440401000200040L , 0x0432001022008044L , 0x0402002200100840L ,
@@ -141,17 +146,17 @@ public class BitBoard {
141146 0xebffffb9ff9fc526L , 0x61fffeddfeedaeaeL , 0x53bfffedffdeb1a2L , 0x127fffb9ffdfb5f6L ,
142147 0x411fffddffdbf4d6L , 0x0005000208040001L , 0x264038060100d004L , 0x7645fffecbfea79eL ,
143148 };
144- private final static long [][] bTables ;
145- private final static long [] bMasks ;
146- private final static int [] bBits = { 5 , 4 , 5 , 5 , 5 , 5 , 4 , 5 ,
149+ private static final long [][] bTables ;
150+ private static final long [] bMasks ;
151+ private static final int [] bBits = { 5 , 4 , 5 , 5 , 5 , 5 , 4 , 5 ,
147152 4 , 4 , 5 , 5 , 5 , 5 , 4 , 4 ,
148153 4 , 4 , 7 , 7 , 7 , 7 , 4 , 4 ,
149154 5 , 5 , 7 , 9 , 9 , 7 , 5 , 5 ,
150155 5 , 5 , 7 , 9 , 9 , 7 , 5 , 5 ,
151156 4 , 4 , 7 , 7 , 7 , 7 , 4 , 4 ,
152157 4 , 4 , 5 , 5 , 5 , 5 , 4 , 4 ,
153158 5 , 4 , 5 , 5 , 5 , 5 , 4 , 5 };
154- private final static long [] bMagics = {
159+ private static final long [] bMagics = {
155160 0xffedf9fd7cfcffffL , 0xfc0962854a77f576L , 0x9010210041047000L , 0x52242420800c0000L ,
156161 0x884404220480004aL , 0x0002080248000802L , 0xfc0a66c64a7ef576L , 0x7ffdfdfcbd79ffffL ,
157162 0xfc0846a64a34fff6L , 0xfc087a874a3cf7f6L , 0x02000888010a2211L , 0x0040044040801808L ,
@@ -170,7 +175,7 @@ public class BitBoard {
170175 0x9080000412220a00L , 0x0000002020010a42L , 0xfc087e8e4bb2f736L , 0x43ff9e4ef4ca2c89L ,
171176 };
172177
173- private static final long createPattern (int i , long mask ) {
178+ private static long createPattern (int i , long mask ) {
174179 long ret = 0L ;
175180 for (int j = 0 ; ; j ++) {
176181 long nextMask = mask & (mask - 1 );
@@ -184,15 +189,15 @@ private static final long createPattern(int i, long mask) {
184189 return ret ;
185190 }
186191
187- private static final long addRookRays (int x , int y , long occupied , boolean inner ) {
192+ private static long addRookRays (int x , int y , long occupied , boolean inner ) {
188193 long mask = 0 ;
189194 mask = addRay (mask , x , y , 1 , 0 , occupied , inner );
190195 mask = addRay (mask , x , y , -1 , 0 , occupied , inner );
191196 mask = addRay (mask , x , y , 0 , 1 , occupied , inner );
192197 mask = addRay (mask , x , y , 0 , -1 , occupied , inner );
193198 return mask ;
194199 }
195- private static final long addBishopRays (int x , int y , long occupied , boolean inner ) {
200+ private static long addBishopRays (int x , int y , long occupied , boolean inner ) {
196201 long mask = 0 ;
197202 mask = addRay (mask , x , y , 1 , 1 , occupied , inner );
198203 mask = addRay (mask , x , y , -1 , -1 , occupied , inner );
@@ -201,21 +206,24 @@ private static final long addBishopRays(int x, int y, long occupied, boolean inn
201206 return mask ;
202207 }
203208
204- private static final long addRay (long mask , int x , int y , int dx , int dy ,
205- long occupied , boolean inner ) {
209+ private static long addRay (long mask , int x , int y , int dx , int dy ,
210+ long occupied , boolean inner ) {
206211 int lo = inner ? 1 : 0 ;
207212 int hi = inner ? 6 : 7 ;
208213 while (true ) {
209214 if (dx != 0 ) {
210- x += dx ; if ((x < lo ) || (x > hi )) break ;
215+ x += dx ;
216+ if ((x < lo ) || (x > hi )) break ;
211217 }
212218 if (dy != 0 ) {
213- y += dy ; if ((y < lo ) || (y > hi )) break ;
219+ y += dy ;
220+ if ((y < lo ) || (y > hi )) break ;
214221 }
215222 int sq = Position .getSquare (x , y );
216223 mask |= 1L << sq ;
217- if ((occupied & (1L << sq )) != 0 )
224+ if ((occupied & (1L << sq )) != 0 ) {
218225 break ;
226+ }
219227 }
220228 return mask ;
221229 }
@@ -229,7 +237,7 @@ private static final long addRay(long mask, int x, int y, int dx, int dy,
229237 rMasks [sq ] = addRookRays (x , y , 0L , true );
230238 int tableSize = 1 << rBits [sq ];
231239 long [] table = new long [tableSize ];
232- for ( int i = 0 ; i < tableSize ; i ++) table [ i ] = - 1 ;
240+ Arrays . fill ( table , - 1 ) ;
233241 int nPatterns = 1 << Long .bitCount (rMasks [sq ]);
234242 for (int i = 0 ; i < nPatterns ; i ++) {
235243 long p = createPattern (i , rMasks [sq ]);
@@ -238,7 +246,7 @@ private static final long addRay(long mask, int x, int y, int dx, int dy,
238246 if (table [entry ] == -1 ) {
239247 table [entry ] = atks ;
240248 } else if (table [entry ] != atks ) {
241- throw new RuntimeException ();
249+ throw new InitializationException ();
242250 }
243251 }
244252 rTables [sq ] = table ;
@@ -254,7 +262,7 @@ private static final long addRay(long mask, int x, int y, int dx, int dy,
254262 bMasks [sq ] = addBishopRays (x , y , 0L , true );
255263 int tableSize = 1 << bBits [sq ];
256264 long [] table = new long [tableSize ];
257- for ( int i = 0 ; i < tableSize ; i ++) table [ i ] = - 1 ;
265+ Arrays . fill ( table , - 1 ) ;
258266 int nPatterns = 1 << Long .bitCount (bMasks [sq ]);
259267 for (int i = 0 ; i < nPatterns ; i ++) {
260268 long p = createPattern (i , bMasks [sq ]);
@@ -263,22 +271,23 @@ private static final long addRay(long mask, int x, int y, int dx, int dy,
263271 if (table [entry ] == -1 ) {
264272 table [entry ] = atks ;
265273 } else if (table [entry ] != atks ) {
266- throw new RuntimeException ();
274+ throw new InitializationException ();
267275 }
268276 }
269277 bTables [sq ] = table ;
270278 }
271279 }
272280
273- public static final long bishopAttacks (int sq , long occupied ) {
281+ public static long bishopAttacks (int sq , long occupied ) {
274282 return bTables [sq ][(int )(((occupied & bMasks [sq ]) * bMagics [sq ]) >>> (64 - bBits [sq ]))];
275283 }
276284
277- public static final long rookAttacks (int sq , long occupied ) {
285+ public static long rookAttacks (int sq , long occupied ) {
278286 return rTables [sq ][(int )(((occupied & rMasks [sq ]) * rMagics [sq ]) >>> (64 - rBits [sq ]))];
279287 }
280-
281- static public final long [][] squaresBetween ;
288+
289+ protected static final long [][] squaresBetween ;
290+
282291 static {
283292 squaresBetween = new long [64 ][];
284293 for (int sq1 = 0 ; sq1 < 64 ; sq1 ++) {
@@ -305,7 +314,7 @@ public static final long rookAttacks(int sq, long occupied) {
305314 }
306315 }
307316
308- private static final byte dirTable [] = {
317+ private static final byte [] dirTable = {
309318 -9 , 0 , 0 , 0 , 0 , 0 , 0 , -8 , 0 , 0 , 0 , 0 , 0 , 0 , -7 ,
310319 0 , 0 , -9 , 0 , 0 , 0 , 0 , 0 , -8 , 0 , 0 , 0 , 0 , 0 , -7 , 0 ,
311320 0 , 0 , 0 , -9 , 0 , 0 , 0 , 0 , -8 , 0 , 0 , 0 , 0 , -7 , 0 , 0 ,
@@ -323,37 +332,37 @@ public static final long rookAttacks(int sq, long occupied) {
323332 0 , 7 , 0 , 0 , 0 , 0 , 0 , 0 , 8 , 0 , 0 , 0 , 0 , 0 , 0 , 9
324333 };
325334
326- static public final int getDirection (int from , int to ) {
335+ public static int getDirection (int from , int to ) {
327336 int offs = to + (to |7 ) - from - (from |7 ) + 0x77 ;
328337 return dirTable [offs ];
329338 }
330339
331- public static final long southFill (long mask ) {
340+ public static long southFill (long mask ) {
332341 mask |= (mask >>> 8 );
333342 mask |= (mask >>> 16 );
334343 mask |= (mask >>> 32 );
335344 return mask ;
336345 }
337346
338- public static final long northFill (long mask ) {
347+ public static long northFill (long mask ) {
339348 mask |= (mask << 8 );
340349 mask |= (mask << 16 );
341350 mask |= (mask << 32 );
342351 return mask ;
343352 }
344353
345- private static final int trailingZ [] = {
346- 63 , 0 , 58 , 1 , 59 , 47 , 53 , 2 ,
347- 60 , 39 , 48 , 27 , 54 , 33 , 42 , 3 ,
348- 61 , 51 , 37 , 40 , 49 , 18 , 28 , 20 ,
349- 55 , 30 , 34 , 11 , 43 , 14 , 22 , 4 ,
350- 62 , 57 , 46 , 52 , 38 , 26 , 32 , 41 ,
351- 50 , 36 , 17 , 19 , 29 , 10 , 13 , 21 ,
352- 56 , 45 , 25 , 31 , 35 , 16 , 9 , 12 ,
353- 44 , 24 , 15 , 8 , 23 , 7 , 6 , 5
354- };
354+ private static final int [] trailingZ = {
355+ 63 , 0 , 58 , 1 , 59 , 47 , 53 , 2 ,
356+ 60 , 39 , 48 , 27 , 54 , 33 , 42 , 3 ,
357+ 61 , 51 , 37 , 40 , 49 , 18 , 28 , 20 ,
358+ 55 , 30 , 34 , 11 , 43 , 14 , 22 , 4 ,
359+ 62 , 57 , 46 , 52 , 38 , 26 , 32 , 41 ,
360+ 50 , 36 , 17 , 19 , 29 , 10 , 13 , 21 ,
361+ 56 , 45 , 25 , 31 , 35 , 16 , 9 , 12 ,
362+ 44 , 24 , 15 , 8 , 23 , 7 , 6 , 5
363+ };
355364
356- static public final int numberOfTrailingZeros (long mask ) {
365+ public static int numberOfTrailingZeros (long mask ) {
357366 return trailingZ [(int )(((mask & -mask ) * 0x07EDD5E59A4E28C2L ) >>> 58 )];
358367 }
359368}
0 commit comments