diff --git a/Java/Bitboard.java b/Java/Bitboard.java new file mode 100644 index 0000000..c5c61f5 --- /dev/null +++ b/Java/Bitboard.java @@ -0,0 +1,74 @@ +import java.math.BigInteger; + +public class Bitboard +{ + + public static BigInteger addBit(BigInteger bitboard, int square) + { + return bitboard.setBit(square); + } + + public static void printBigInteger(BigInteger bitboard) + { + for (int rank = 0; rank < 8; rank++) + { + for (int file = 0; file < 8; file++) + { + int square = rank * 8 + file; + processSquare(bitboard, square); + } + System.out.println(); + } + System.out.println("BigInteger: " + bitboard); + } + + static void processSquare(BigInteger bitboard, int square) + { + if (bitboard.testBit(square)) + { + System.out.print("X "); + } + else + { + System.out.print("_ "); + } + } + + static int bitScanForwardSlow(BigInteger bitboard) + { + for (int i = 0; i < 64; i++) + { + if (bitboard.testBit(i)) + { + return i; + } + } + return -1; + } + + static void printLong(long input) + { + for (int rank = 0; rank < 8; rank++) + { + for (int file = 0; file < 8; file++) + { + int square = rank * 8 + file; + processSquareLong(input, square); + } + System.out.println(); + } + System.out.println("long: " + input); + } + + static void processSquareLong(long bitboard, int square) + { + if ((bitboard & MoveConstants.SQUARE_BBS[square]) != 0) + { + System.out.print("X "); + } + else + { + System.out.print("_ "); + } + } +} diff --git a/Java/Board.java b/Java/Board.java new file mode 100644 index 0000000..90d5dd8 --- /dev/null +++ b/Java/Board.java @@ -0,0 +1,142 @@ + +public class Board +{ + public static long[] bitboard_array_global = new long[12]; + public static boolean is_white_global = true; + public static boolean[] castle_rights_global = new boolean[4]; + public static int ep_global; + + static final long BP_STARTING_POSITIONS = 65280L; + static final long WP_STARTING_POSITIONS = 71776119061217280L; + static final long BK_STARTING_POSITION = 16L; + static final long WK_STARTING_POSITION = 1152921504606846976L; + static final long BN_STARTING_POSITIONS = 66L; + static final long WN_STARTING_POSITIONS = 4755801206503243776L; + static final long WR_STARTING_POSITIONS = Long.parseUnsignedLong("9295429630892703744"); + static final long BR_STARTING_POSITIONS = 129L; + static final long BB_STARTING_POSITIONS = 36L; + static final long WB_STARTING_POSITIONS = 2594073385365405696L; + static final long WQ_STARTING_POSITION = 576460752303423488L; + static final long BQ_STARTING_POSITION = 8L; + + + public static void SetTrickyPosition() { + + ep_global = GenConst.NO_SQUARE; + is_white_global = true; + castle_rights_global[0] = true; + castle_rights_global[1] = true; + castle_rights_global[2] = true; + castle_rights_global[3] = true; + + bitboard_array_global[GenConst.WP] = 65020788473856000L; + bitboard_array_global[GenConst.WN] = 4398314946560L; + bitboard_array_global[GenConst.WB] = 6755399441055744L; + bitboard_array_global[GenConst.WR] = Long.parseUnsignedLong("9295429630892703744L"); + bitboard_array_global[GenConst.WQ] = 35184372088832L; + bitboard_array_global[GenConst.WK] = 1152921504606846976L; + bitboard_array_global[GenConst.BP] = 140746083544320L; + bitboard_array_global[GenConst.BN] = 2228224L; + bitboard_array_global[GenConst.BB] = 81920L; + bitboard_array_global[GenConst.BR] = 129L; + bitboard_array_global[GenConst.BQ] = 4096L; + bitboard_array_global[GenConst.BK] = 16L; + } + + public static void setTestPosition() + { + ep_global = GenConst.NO_SQUARE; + is_white_global = false; + castle_rights_global[0] = false; + castle_rights_global[1] = false; + castle_rights_global[2] = false; + castle_rights_global[3] = false; + + bitboard_array_global[GenConst.WP] = 33554432L; + bitboard_array_global[GenConst.WN] = 0; + bitboard_array_global[GenConst.WB] = 0; + bitboard_array_global[GenConst.WR] = 0; + bitboard_array_global[GenConst.WQ] = 0; + bitboard_array_global[GenConst.WK] = 4398046511104L; + bitboard_array_global[GenConst.BP] = 0; + bitboard_array_global[GenConst.BN] = 0; + bitboard_array_global[GenConst.BB] = 0; + bitboard_array_global[GenConst.BR] = 0; + bitboard_array_global[GenConst.BQ] = 0; + bitboard_array_global[GenConst.BK] = 2048L; + } + + public static void setStartingPosition() { + + ep_global = GenConst.NO_SQUARE; + is_white_global = true; + castle_rights_global[0] = true; + castle_rights_global[1] = true; + castle_rights_global[2] = true; + castle_rights_global[3] = true; + + bitboard_array_global[GenConst.WP] = WP_STARTING_POSITIONS; + bitboard_array_global[GenConst.WN] = WN_STARTING_POSITIONS; + bitboard_array_global[GenConst.WB] = WB_STARTING_POSITIONS; + bitboard_array_global[GenConst.WR] = WR_STARTING_POSITIONS; + bitboard_array_global[GenConst.WQ] = WQ_STARTING_POSITION; + bitboard_array_global[GenConst.WK] = WK_STARTING_POSITION; + bitboard_array_global[GenConst.BP] = BP_STARTING_POSITIONS; + bitboard_array_global[GenConst.BN] = BN_STARTING_POSITIONS; + bitboard_array_global[GenConst.BB] = BB_STARTING_POSITIONS; + bitboard_array_global[GenConst.BR] = BR_STARTING_POSITIONS; + bitboard_array_global[GenConst.BQ] = BQ_STARTING_POSITION; + bitboard_array_global[GenConst.BK] = BK_STARTING_POSITION; + } + + static final char[] PieceNames = {'P', 'N', 'B', 'R', 'Q', 'K', 'P', 'N', 'B', 'R', 'Q', 'K', '_'}; + static final char[] PieceColours = {'W', 'W', 'W', 'W', 'W', 'W', 'B', 'B', 'B', 'B', 'B', 'B', '_'}; + +static final int EMPTY = 12; + + //static boolean IsOccupied(long bitboard, int square) { + // return !bitboard.and(MoveConstants.SQUARE_BBS[square]).equals(long.ZERO); + //} + + static int GetOccupiedIndex(int square) { + for (int i = 0; i < 12; i++) { + if (true) {//(IsOccupied(bitboard_array_global[i], square)) { + return i; + } + } + return EMPTY; + } + + static int[] fillBoardArray() { + int[] boardArray = new int[64]; + for (int i = 0; i < 64; i++) { + boardArray[i] = GetOccupiedIndex(i); + } + return boardArray; + } + + public static void printBoard() { + System.out.println("Board:"); + + int[] boardArray = fillBoardArray(); + + for (int rank = 0; rank < 8; rank++) { + System.out.print(" "); + + for (int file = 0; file < 8; file++) { + int square = (rank * 8) + file; + System.out.printf("%c%c ", PieceColours[boardArray[square]], PieceNames[boardArray[square]]); + } + + System.out.println(); + } + System.out.println(); + + System.out.printf("White to play: %b\n", is_white_global); + System.out.printf("Castle: %b %b %b %b\n", castle_rights_global[0], castle_rights_global[1], castle_rights_global[2], castle_rights_global[3]); + System.out.printf("ep: %d\n", ep_global); + System.out.println(); + System.out.println(); + } + +} diff --git a/Java/ChessEngine.java b/Java/ChessEngine.java new file mode 100644 index 0000000..af76a88 --- /dev/null +++ b/Java/ChessEngine.java @@ -0,0 +1,12 @@ + +public class ChessEngine +{ + public static void main(String[] args) + { + Board.setStartingPosition(); + //Board.setTestPosition(); + Perft.PrintBoardGlobal(); + //Perft.runPerftFunctionsDebug(6); + Perft.runPerftFunctions(6); + } +} \ No newline at end of file diff --git a/Java/GenConst.java b/Java/GenConst.java new file mode 100644 index 0000000..6212f4c --- /dev/null +++ b/Java/GenConst.java @@ -0,0 +1,152 @@ +public class GenConst { + public static final int WP = 0; + public static final int WN = 1; + public static final int WB = 2; + public static final int WR = 3; + public static final int WQ = 4; + public static final int WK = 5; + public static final int BP = 6; + public static final int BN = 7; + public static final int BB = 8; + public static final int BR = 9; + public static final int BQ = 10; + public static final int BK = 11; + public static final int EMPTY = 12; + + public static final long RANK_1_BITBOARD = Long.parseUnsignedLong("18374686479671623680"); + public static final long RANK_2_BITBOARD = 71776119061217280L; + public static final long RANK_3_BITBOARD = 280375465082880L; + public static final long RANK_4_BITBOARD = 1095216660480L; + public static final long RANK_5_BITBOARD = 4278190080L; + public static final long RANK_6_BITBOARD = 16711680L; + public static final long RANK_7_BITBOARD = 65280L; + public static final long RANK_8_BITBOARD = 255L; + + public static final long WKS_EMPTY_BITBOARD = 6917529027641081856L; + public static final long WQS_EMPTY_BITBOARD = 1008806316530991104L; + public static final long BKS_EMPTY_BITBOARD = 96L; + public static final long BQS_EMPTY_BITBOARD = 14L; + + public static final long MAX_ULONG = Long.parseUnsignedLong("18446744073709551615"); + + public static final int TAG_NONE = 0; +public static final int TAG_CAPTURE = 1; +public static final int TAG_WHITE_EP = 2; +public static final int TAG_BLACK_EP = 3; +public static final int TAG_W_CASTLE_KS = 4; +public static final int TAG_W_CASTLE_QS = 5; +public static final int TAG_B_CASTLE_KS = 6; +public static final int TAG_B_CASTLE_QS = 7; +public static final int TAG_B_N_PROMOTION = 8; +public static final int TAG_B_B_PROMOTION = 9; +public static final int TAG_B_Q_PROMOTION = 10; +public static final int TAG_B_R_PROMOTION = 11; +public static final int TAG_W_N_PROMOTION = 12; +public static final int TAG_W_B_PROMOTION = 13; +public static final int TAG_W_Q_PROMOTION = 14; +public static final int TAG_W_R_PROMOTION = 15; +public static final int TAG_B_N_PROMOTION_CAP = 16; +public static final int TAG_B_B_PROMOTION_CAP = 17; +public static final int TAG_B_Q_PROMOTION_CAP = 18; +public static final int TAG_B_R_PROMOTION_CAP = 19; +public static final int TAG_W_N_PROMOTION_CAP = 20; +public static final int TAG_W_B_PROMOTION_CAP = 21; +public static final int TAG_W_Q_PROMOTION_CAP = 22; +public static final int TAG_W_R_PROMOTION_CAP = 23; +public static final int TAG_W_P_DOUBLE = 24; +public static final int TAG_B_P_DOUBLE = 25; +public static final int TAG_CHECK = 26; +public static final int TAG_CHECK_CAP = 27; + +public static final int A8 = 0; +public static final int B8 = 1; +public static final int C8 = 2; +public static final int D8 = 3; +public static final int E8 = 4; +public static final int F8 = 5; +public static final int G8 = 6; +public static final int H8 = 7; +public static final int A7 = 8; +public static final int B7 = 9; +public static final int C7 = 10; +public static final int D7 = 11; +public static final int E7 = 12; +public static final int F7 = 13; +public static final int G7 = 14; +public static final int H7 = 15; +public static final int A6 = 16; +public static final int B6 = 17; +public static final int C6 = 18; +public static final int D6 = 19; +public static final int E6 = 20; +public static final int F6 = 21; +public static final int G6 = 22; +public static final int H6 = 23; +public static final int A5 = 24; +public static final int B5 = 25; +public static final int C5 = 26; +public static final int D5 = 27; +public static final int E5 = 28; +public static final int F5 = 29; +public static final int G5 = 30; +public static final int H5 = 31; +public static final int A4 = 32; +public static final int B4 = 33; +public static final int C4 = 34; +public static final int D4 = 35; +public static final int E4 = 36; +public static final int F4 = 37; +public static final int G4 = 38; +public static final int H4 = 39; +public static final int A3 = 40; +public static final int B3 = 41; +public static final int C3 = 42; +public static final int D3 = 43; +public static final int E3 = 44; +public static final int F3 = 45; +public static final int G3 = 46; +public static final int H3 = 47; +public static final int A2 = 48; +public static final int B2 = 49; +public static final int C2 = 50; +public static final int D2 = 51; +public static final int E2 = 52; +public static final int F2 = 53; +public static final int G2 = 54; +public static final int H2 = 55; +public static final int A1 = 56; +public static final int B1 = 57; +public static final int C1 = 58; +public static final int D1 = 59; +public static final int E1 = 60; +public static final int F1 = 61; +public static final int G1 = 62; +public static final int H1 = 63; + +public static final int NO_SQUARE = 64; + +static char[] SQ_CHAR_Y = { + '8', '8', '8', '8', '8', '8', '8', '8', + '7', '7', '7', '7', '7', '7', '7', '7', + '6', '6', '6', '6', '6', '6', '6', '6', + '5', '5', '5', '5', '5', '5', '5', '5', + '4', '4', '4', '4', '4', '4', '4', '4', + '3', '3', '3', '3', '3', '3', '3', '3', + '2', '2', '2', '2', '2', '2', '2', '2', + '1', '1', '1', '1', '1', '1', '1', '1', + 'A' // This seems like an extra value; ensure it is intentional +}; + +static char[] SQ_CHAR_X = { + 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', + 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', + 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', + 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', + 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', + 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', + 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', + 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', + 'N' // This seems like an extra value; ensure it is intentional +}; + +} diff --git a/Java/Inb.java b/Java/Inb.java new file mode 100644 index 0000000..8b192ba --- /dev/null +++ b/Java/Inb.java @@ -0,0 +1,4230 @@ +public class Inb +{ + public static final long[][] INBETWEEN_BITBOARDS = + { + { + 0, + 2, + 6, + 14, + 30, + 62, + 126, + 254, + 256, + 512, + 0, + 0, + 0, + 0, + 0, + 0, + 65792, + 0, + 262656, + 0, + 0, + 0, + 0, + 0, + 16843008, + 0, + 0, + 134480384, + 0, + 0, + 0, + 0, + 4311810304L, + 0, + 0, + 0, + 68853957120L, + 0, + 0, + 0, + 1103823438080L, + 0, + 0, + 0, + 0, + 35253226045952L, + 0, + 0, + 282578800148736L, + 0, + 0, + 0, + 0, + 0, + 18049651735527936L, + 0, + 72340172838076672L, + 0, + 0, + 0, + 0, + 0, + 0, + Long.parseUnsignedLong("9241421688590303744"), + }, + { + 1, + 0, + 4, + 12, + 28, + 60, + 124, + 252, + 256, + 512, + 1024, + 0, + 0, + 0, + 0, + 0, + 0, + 131584, + 0, + 525312, + 0, + 0, + 0, + 0, + 0, + 33686016, + 0, + 0, + 268960768, + 0, + 0, + 0, + 0, + 8623620608L, + 0, + 0, + 0, + 137707914240L, + 0, + 0, + 0, + 2207646876160L, + 0, + 0, + 0, + 0, + 70506452091904L, + 0, + 0, + 565157600297472L, + 0, + 0, + 0, + 0, + 0, + 36099303471055872L, + 0, + 144680345676153344L, + 0, + 0, + 0, + 0, + 0, + 0, + }, + { + 3, + 2, + 0, + 8, + 24, + 56, + 120, + 248, + 0, + 512, + 1024, + 2048, + 0, + 0, + 0, + 0, + 66048, + 0, + 263168, + 0, + 1050624, + 0, + 0, + 0, + 0, + 0, + 67372032, + 0, + 0, + 537921536, + 0, + 0, + 0, + 0, + 17247241216L, + 0, + 0, + 0, + 275415828480L, + 0, + 0, + 0, + 4415293752320L, + 0, + 0, + 0, + 0, + 141012904183808L, + 0, + 0, + 1130315200594944L, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 289360691352306688L, + 0, + 0, + 0, + 0, + 0, + }, + { + 7, + 6, + 4, + 0, + 16, + 48, + 112, + 240, + 0, + 0, + 1024, + 2048, + 4096, + 0, + 0, + 0, + 0, + 132096, + 0, + 526336, + 0, + 2101248, + 0, + 0, + 16909312, + 0, + 0, + 134744064, + 0, + 0, + 1075843072L, + 0, + 0, + 0, + 0, + 34494482432L, + 0, + 0, + 0, + 550831656960L, + 0, + 0, + 0, + 8830587504640L, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 2260630401189888L, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 578721382704613376L, + 0, + 0, + 0, + 0, + }, + { + 15, + 14, + 12, + 8, + 0, + 32, + 96, + 224, + 0, + 0, + 0, + 2048, + 4096, + 8192, + 0, + 0, + 0, + 0, + 264192, + 0, + 1052672, + 0, + 4202496, + 0, + 0, + 33818624, + 0, + 0, + 269488128, + 0, + 0, + 2151686144L, + 4328785920L, + 0, + 0, + 0, + 68988964864L, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 17661175009280L, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 4521260802379776L, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 1157442765409226752L, + 0, + 0, + 0, + }, + { + 31, + 30, + 28, + 24, + 16, + 0, + 64, + 192, + 0, + 0, + 0, + 0, + 4096, + 8192, + 16384, + 0, + 0, + 0, + 0, + 528384, + 0, + 2105344, + 0, + 8404992, + 0, + 0, + 67637248, + 0, + 0, + 538976256, + 0, + 0, + 0, + 8657571840L, + 0, + 0, + 0, + 137977929728L, + 0, + 0, + 1108169199616L, + 0, + 0, + 0, + 0, + 35322350018560L, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 9042521604759552L, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 2314885530818453504L, + 0, + 0, + }, + { + 63, + 62, + 60, + 56, + 48, + 32, + 0, + 128, + 0, + 0, + 0, + 0, + 0, + 8192, + 16384, + 32768, + 0, + 0, + 0, + 0, + 1056768, + 0, + 4210688, + 0, + 0, + 0, + 0, + 135274496, + 0, + 0, + 1077952512L, + 0, + 0, + 0, + 17315143680L, + 0, + 0, + 0, + 275955859456L, + 0, + 0, + 2216338399232L, + 0, + 0, + 0, + 0, + 70644700037120L, + 0, + 283691315109888L, + 0, + 0, + 0, + 0, + 0, + 18085043209519104L, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 4629771061636907008L, + 0, + }, + { + 127, + 126, + 124, + 120, + 112, + 96, + 64, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 16384, + 32768, + 0, + 0, + 0, + 0, + 0, + 2113536, + 0, + 8421376, + 0, + 0, + 0, + 0, + 270548992, + 0, + 0, + 2155905024L, + 0, + 0, + 0, + 34630287360L, + 0, + 0, + 0, + 551911718912L, + 0, + 0, + 4432676798464L, + 0, + 0, + 0, + 0, + 141289400074240L, + 0, + 567382630219776L, + 0, + 0, + 0, + 0, + 0, + 36170086419038208L, + 72624976668147712L, + 0, + 0, + 0, + 0, + 0, + 0, + Long.parseUnsignedLong("9259542123273814016"), + }, + { + 1, + 2, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 512, + 1536, + 3584, + 7680, + 15872, + 32256, + 65024, + 65536, + 131072, + 0, + 0, + 0, + 0, + 0, + 0, + 16842752, + 0, + 67239936, + 0, + 0, + 0, + 0, + 0, + 4311810048L, + 0, + 0, + 34426978304L, + 0, + 0, + 0, + 0, + 1103823437824L, + 0, + 0, + 0, + 17626613022720L, + 0, + 0, + 0, + 282578800148480L, + 0, + 0, + 0, + 0, + 9024825867763712L, + 0, + 0, + 72340172838076416L, + 0, + 0, + 0, + 0, + 0, + 4620710844295151616L, + 0, + }, + { + 1, + 2, + 4, + 0, + 0, + 0, + 0, + 0, + 256, + 0, + 1024, + 3072, + 7168, + 15360, + 31744, + 64512, + 65536, + 131072, + 262144, + 0, + 0, + 0, + 0, + 0, + 0, + 33685504, + 0, + 134479872, + 0, + 0, + 0, + 0, + 0, + 8623620096L, + 0, + 0, + 68853956608L, + 0, + 0, + 0, + 0, + 2207646875648L, + 0, + 0, + 0, + 35253226045440L, + 0, + 0, + 0, + 565157600296960L, + 0, + 0, + 0, + 0, + 18049651735527424L, + 0, + 0, + 144680345676152832L, + 0, + 0, + 0, + 0, + 0, + Long.parseUnsignedLong("9241421688590303232"), + }, + { + 0, + 2, + 4, + 8, + 0, + 0, + 0, + 0, + 768, + 512, + 0, + 2048, + 6144, + 14336, + 30720, + 63488, + 0, + 131072, + 262144, + 524288, + 0, + 0, + 0, + 0, + 16908288, + 0, + 67371008, + 0, + 268959744, + 0, + 0, + 0, + 0, + 0, + 17247240192L, + 0, + 0, + 137707913216L, + 0, + 0, + 0, + 0, + 4415293751296L, + 0, + 0, + 0, + 70506452090880L, + 0, + 0, + 0, + 1130315200593920L, + 0, + 0, + 0, + 0, + 36099303471054848L, + 0, + 0, + 289360691352305664L, + 0, + 0, + 0, + 0, + 0, + }, + { + 36099303471054849L, + 0, + 4, + 8, + 16, + 0, + 0, + 0, + 1792, + 1536, + 1024, + 0, + 4096, + 12288, + 28672, + 61440, + 0, + 0, + 262144, + 524288, + 1048576, + 0, + 0, + 0, + 0, + 33816576, + 0, + 134742016, + 0, + 537919488, + 0, + 0, + 4328783872L, + 0, + 0, + 34494480384L, + 0, + 0, + 275415826432L, + 0, + 0, + 0, + 0, + 8830587502592L, + 0, + 0, + 0, + 141012904181760L, + 0, + 0, + 0, + 2260630401187840L, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 578721382704611328L, + 0, + 0, + 0, + 0, + }, + { + 0, + 0, + 0, + 8, + 16, + 32, + 0, + 0, + 3840, + 3584, + 3072, + 2048, + 0, + 8192, + 24576, + 57344, + 0, + 0, + 0, + 524288, + 1048576, + 2097152, + 0, + 0, + 0, + 0, + 67633152, + 0, + 269484032, + 0, + 1075838976L, + 0, + 0, + 8657567744L, + 0, + 0, + 68988960768L, + 0, + 0, + 550831652864L, + 1108169195520L, + 0, + 0, + 0, + 17661175005184L, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 4521260802375680L, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 1157442765409222656L, + 0, + 0, + 0, + }, + { + 0, + 0, + 0, + 0, + 16, + 32, + 64, + 0, + 7936, + 7680, + 7168, + 6144, + 4096, + 0, + 16384, + 49152, + 0, + 0, + 0, + 0, + 1048576, + 2097152, + 4194304, + 0, + 0, + 0, + 0, + 135266304, + 0, + 538968064, + 0, + 2151677952L, + 0, + 0, + 17315135488L, + 0, + 0, + 137977921536L, + 0, + 0, + 0, + 2216338391040L, + 0, + 0, + 0, + 35322350010368L, + 0, + 0, + 283691315101696L, + 0, + 0, + 0, + 0, + 9042521604751360L, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 2314885530818445312L, + 0, + 0, + }, + { + 0, + 0, + 0, + 0, + 0, + 32, + 64, + 128, + 16128, + 15872, + 15360, + 14336, + 12288, + 8192, + 0, + 32768, + 0, + 0, + 0, + 0, + 0, + 2097152, + 4194304, + 8388608, + 0, + 0, + 0, + 0, + 270532608, + 0, + 1077936128L, + 0, + 0, + 0, + 0, + 34630270976L, + 0, + 0, + 275955843072L, + 0, + 0, + 0, + 4432676782080L, + 0, + 0, + 0, + 70644700020736L, + 0, + 0, + 567382630203392L, + 0, + 0, + 0, + 0, + 18085043209502720L, + 0, + 72624976668131328L, + 0, + 0, + 0, + 0, + 0, + 4629771061636890624L, + 0, + }, + { + 0, + 0, + 0, + 0, + 0, + 0, + 64, + 128, + 32512, + 32256, + 31744, + 30720, + 28672, + 24576, + 16384, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 4194304, + 8388608, + 0, + 0, + 0, + 0, + 0, + 541065216, + 0, + 2155872256L, + 0, + 0, + 0, + 0, + 69260541952L, + 0, + 0, + 551911686144L, + 0, + 0, + 0, + 8865353564160L, + 0, + 0, + 0, + 141289400041472L, + 0, + 0, + 1134765260406784L, + 0, + 0, + 0, + 0, + 36170086419005440L, + 0, + 145249953336262656L, + 0, + 0, + 0, + 0, + 0, + Long.parseUnsignedLong("9259542123273781248"), + }, + { + 257, + 0, + 516, + 0, + 0, + 0, + 0, + 0, + 256, + 512, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 131072, + 393216, + 917504, + 1966080, + 4063232, + 8257536, + 16646144, + 16777216, + 33554432, + 0, + 0, + 0, + 0, + 0, + 0, + 4311744512L, + 0, + 17213423616L, + 0, + 0, + 0, + 0, + 0, + 1103823372288L, + 0, + 0, + 8813306445824L, + 0, + 0, + 0, + 0, + 282578800082944L, + 0, + 0, + 0, + 4512412933816320L, + 0, + 0, + 0, + 72340172838010880L, + 0, + 0, + 0, + 0, + 2310355422147510272L, + 0, + 0, + }, + { + 0, + 514, + 0, + 1032, + 0, + 0, + 0, + 0, + 256, + 512, + 1024, + 0, + 0, + 0, + 0, + 0, + 65536, + 0, + 262144, + 786432, + 1835008, + 3932160, + 8126464, + 16515072, + 16777216, + 33554432, + 67108864, + 0, + 0, + 0, + 0, + 0, + 0, + 8623489024L, + 0, + 34426847232L, + 0, + 0, + 0, + 0, + 0, + 2207646744576L, + 0, + 0, + 17626612891648L, + 0, + 0, + 0, + 0, + 565157600165888L, + 0, + 0, + 0, + 9024825867632640L, + 0, + 0, + 0, + 144680345676021760L, + 0, + 0, + 0, + 0, + 4620710844295020544L, + 0, + }, + { + 513, + 0, + 1028, + 0, + 2064, + 0, + 0, + 0, + 0, + 512, + 1024, + 2048, + 0, + 0, + 0, + 0, + 196608, + 131072, + 0, + 524288, + 1572864, + 3670016, + 7864320, + 16252928, + 0, + 33554432, + 67108864, + 134217728, + 0, + 0, + 0, + 0, + 4328521728L, + 0, + 17246978048L, + 0, + 68853694464L, + 0, + 0, + 0, + 0, + 0, + 4415293489152L, + 0, + 0, + 35253225783296L, + 0, + 0, + 0, + 0, + 1130315200331776L, + 0, + 0, + 0, + 18049651735265280L, + 0, + 0, + 0, + 289360691352043520L, + 0, + 0, + 0, + 0, + Long.parseUnsignedLong("9241421688590041088"), + }, + { + 0, + 1026, + 0, + 2056, + 0, + 4128, + 0, + 0, + 0, + 0, + 1024, + 2048, + 4096, + 0, + 0, + 0, + 458752, + 393216, + 262144, + 0, + 1048576, + 3145728, + 7340032, + 15728640, + 0, + 0, + 67108864, + 134217728, + 268435456, + 0, + 0, + 0, + 0, + 8657043456L, + 0, + 34493956096L, + 0, + 137707388928L, + 0, + 0, + 1108168671232L, + 0, + 0, + 8830586978304L, + 0, + 0, + 70506451566592L, + 0, + 0, + 0, + 0, + 2260630400663552L, + 0, + 0, + 0, + 36099303470530560L, + 0, + 0, + 0, + 578721382704087040L, + 0, + 0, + 0, + 0, + }, + { + 36099303470530561L, + 0, + 2052, + 0, + 4112, + 0, + 8256, + 0, + 0, + 0, + 0, + 2048, + 4096, + 8192, + 0, + 0, + 983040, + 917504, + 786432, + 524288, + 0, + 2097152, + 6291456, + 14680064, + 0, + 0, + 0, + 134217728, + 268435456, + 536870912, + 0, + 0, + 0, + 0, + 17314086912L, + 0, + 68987912192L, + 0, + 275414777856L, + 0, + 0, + 2216337342464L, + 0, + 0, + 17661173956608L, + 0, + 0, + 141012903133184L, + 283691314053120L, + 0, + 0, + 0, + 4521260801327104L, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 1157442765408174080L, + 0, + 0, + 0, + }, + { + 0, + 0, + 0, + 4104, + 0, + 8224, + 0, + 16512, + 0, + 0, + 0, + 0, + 4096, + 8192, + 16384, + 0, + 2031616, + 1966080, + 1835008, + 1572864, + 1048576, + 0, + 4194304, + 12582912, + 0, + 0, + 0, + 0, + 268435456, + 536870912, + 1073741824L, + 0, + 0, + 0, + 0, + 34628173824L, + 0, + 137975824384L, + 0, + 550829555712L, + 0, + 0, + 4432674684928L, + 0, + 0, + 35322347913216L, + 0, + 0, + 0, + 567382628106240L, + 0, + 0, + 0, + 9042521602654208L, + 0, + 0, + 72624976666034176L, + 0, + 0, + 0, + 0, + 2314885530816348160L, + 0, + 0, + }, + { + 0, + 0, + 0, + 0, + 8208, + 0, + 16448, + 0, + 0, + 0, + 0, + 0, + 0, + 8192, + 16384, + 32768, + 4128768, + 4063232, + 3932160, + 3670016, + 3145728, + 2097152, + 0, + 8388608, + 0, + 0, + 0, + 0, + 0, + 536870912, + 1073741824L, + 2147483648L, + 0, + 0, + 0, + 0, + 69256347648L, + 0, + 275951648768L, + 0, + 0, + 0, + 0, + 8865349369856L, + 0, + 0, + 70644695826432L, + 0, + 0, + 0, + 1134765256212480L, + 0, + 0, + 0, + 18085043205308416L, + 0, + 0, + 145249953332068352L, + 0, + 0, + 0, + 0, + 4629771061632696320L, + 0, + }, + { + 0, + 0, + 0, + 0, + 0, + 16416, + 0, + 32896, + 0, + 0, + 0, + 0, + 0, + 0, + 16384, + 32768, + 8323072, + 8257536, + 8126464, + 7864320, + 7340032, + 6291456, + 4194304, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 1073741824L, + 2147483648L, + 0, + 0, + 0, + 0, + 0, + 138512695296L, + 0, + 551903297536L, + 0, + 0, + 0, + 0, + 17730698739712L, + 0, + 0, + 141289391652864L, + 0, + 0, + 0, + 2269530512424960L, + 0, + 0, + 0, + 36170086410616832L, + 0, + 0, + 290499906664136704L, + 0, + 0, + 0, + 0, + Long.parseUnsignedLong("9259542123265392640"), + }, + { + 65793, + 0, + 0, + 132104, + 0, + 0, + 0, + 0, + 65792, + 0, + 132096, + 0, + 0, + 0, + 0, + 0, + 65536, + 131072, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 33554432, + 100663296, + 234881024, + 503316480, + 1040187392L, + 2113929216L, + 4261412864L, + 4294967296L, + 8589934592L, + 0, + 0, + 0, + 0, + 0, + 0, + 1103806595072L, + 0, + 4406636445696L, + 0, + 0, + 0, + 0, + 0, + 282578783305728L, + 0, + 0, + 2256206450130944L, + 0, + 0, + 0, + 0, + 72340172821233664L, + 0, + 0, + 0, + 1155177711056977920L, + 0, + 0, + 0, + }, + { + 0, + 131586, + 0, + 0, + 264208, + 0, + 0, + 0, + 0, + 131584, + 0, + 264192, + 0, + 0, + 0, + 0, + 65536, + 131072, + 262144, + 0, + 0, + 0, + 0, + 0, + 16777216, + 0, + 67108864, + 201326592, + 469762048, + 1006632960L, + 2080374784L, + 4227858432L, + 4294967296L, + 8589934592L, + 17179869184L, + 0, + 0, + 0, + 0, + 0, + 0, + 2207613190144L, + 0, + 8813272891392L, + 0, + 0, + 0, + 0, + 0, + 565157566611456L, + 0, + 0, + 4512412900261888L, + 0, + 0, + 0, + 0, + 144680345642467328L, + 0, + 0, + 0, + 2310355422113955840L, + 0, + 0, + }, + { + 0, + 0, + 263172, + 0, + 0, + 528416, + 0, + 0, + 131328, + 0, + 263168, + 0, + 528384, + 0, + 0, + 0, + 0, + 131072, + 262144, + 524288, + 0, + 0, + 0, + 0, + 50331648, + 33554432, + 0, + 134217728, + 402653184, + 939524096, + 2013265920L, + 4160749568L, + 0, + 8589934592L, + 17179869184L, + 34359738368L, + 0, + 0, + 0, + 0, + 1108101562368L, + 0, + 4415226380288L, + 0, + 17626545782784L, + 0, + 0, + 0, + 0, + 0, + 1130315133222912L, + 0, + 0, + 9024825800523776L, + 0, + 0, + 0, + 0, + 289360691284934656L, + 0, + 0, + 0, + 4620710844227911680L, + 0, + }, + { + 262657, + 0, + 0, + 526344, + 0, + 0, + 1056832, + 0, + 0, + 262656, + 0, + 526336, + 0, + 1056768, + 0, + 0, + 0, + 0, + 262144, + 524288, + 1048576, + 0, + 0, + 0, + 117440512, + 100663296, + 67108864, + 0, + 268435456, + 805306368, + 1879048192L, + 4026531840L, + 0, + 0, + 17179869184L, + 34359738368L, + 68719476736L, + 0, + 0, + 0, + 0, + 2216203124736L, + 0, + 8830452760576L, + 0, + 35253091565568L, + 0, + 0, + 283691179835392L, + 0, + 0, + 2260630266445824L, + 0, + 0, + 18049651601047552L, + 0, + 0, + 0, + 0, + 578721382569869312L, + 0, + 0, + 0, + Long.parseUnsignedLong("9241421688455823360"), + }, + { + 0, + 525314, + 0, + 0, + 1052688, + 0, + 0, + 2113664, + 0, + 0, + 525312, + 0, + 1052672, + 0, + 2113536, + 0, + 0, + 0, + 0, + 524288, + 1048576, + 2097152, + 0, + 0, + 251658240, + 234881024, + 201326592, + 134217728, + 0, + 536870912, + 1610612736L, + 3758096384L, + 0, + 0, + 0, + 34359738368L, + 68719476736L, + 137438953472L, + 0, + 0, + 0, + 0, + 4432406249472L, + 0, + 17660905521152L, + 0, + 70506183131136L, + 0, + 0, + 567382359670784L, + 0, + 0, + 4521260532891648L, + 0, + 0, + 36099303202095104L, + 72624976397598720L, + 0, + 0, + 0, + 1157442765139738624L, + 0, + 0, + 0, + }, + { + 36099303202095105L, + 0, + 1050628, + 0, + 0, + 2105376, + 0, + 0, + 0, + 0, + 0, + 1050624, + 0, + 2105344, + 0, + 4227072, + 0, + 0, + 0, + 0, + 1048576, + 2097152, + 4194304, + 0, + 520093696, + 503316480, + 469762048, + 402653184, + 268435456, + 0, + 1073741824L, + 3221225472L, + 0, + 0, + 0, + 0, + 68719476736L, + 137438953472L, + 274877906944L, + 0, + 0, + 0, + 0, + 8864812498944L, + 0, + 35321811042304L, + 0, + 141012366262272L, + 0, + 0, + 1134764719341568L, + 0, + 0, + 9042521065783296L, + 0, + 0, + 0, + 145249952795197440L, + 0, + 0, + 0, + 2314885530279477248L, + 0, + 0, + }, + { + 0, + 0, + 0, + 2101256, + 0, + 0, + 4210752, + 0, + 0, + 0, + 0, + 0, + 2101248, + 0, + 4210688, + 0, + 0, + 0, + 0, + 0, + 0, + 2097152, + 4194304, + 8388608, + 1056964608L, + 1040187392L, + 1006632960L, + 939524096, + 805306368, + 536870912, + 0, + 2147483648L, + 0, + 0, + 0, + 0, + 0, + 137438953472L, + 274877906944L, + 549755813888L, + 0, + 0, + 0, + 0, + 17729624997888L, + 0, + 70643622084608L, + 0, + 0, + 0, + 0, + 2269529438683136L, + 0, + 0, + 18085042131566592L, + 0, + 0, + 0, + 290499905590394880L, + 0, + 0, + 0, + 4629771060558954496L, + 0, + }, + { + 0, + 0, + 0, + 0, + 4202512, + 0, + 0, + 8421504, + 0, + 0, + 0, + 0, + 0, + 4202496, + 0, + 8421376, + 0, + 0, + 0, + 0, + 0, + 0, + 4194304, + 8388608, + 2130706432L, + 2113929216L, + 2080374784L, + 2013265920L, + 1879048192L, + 1610612736L, + 1073741824L, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 274877906944L, + 549755813888L, + 0, + 0, + 0, + 0, + 0, + 35459249995776L, + 0, + 141287244169216L, + 0, + 0, + 0, + 0, + 4539058877366272L, + 0, + 0, + 36170084263133184L, + 0, + 0, + 0, + 580999811180789760L, + 0, + 0, + 0, + Long.parseUnsignedLong("9259542121117908992"), + }, + { + 16843009, + 0, + 0, + 0, + 33818640, + 0, + 0, + 0, + 16843008, + 0, + 0, + 33818624, + 0, + 0, + 0, + 0, + 16842752, + 0, + 33816576, + 0, + 0, + 0, + 0, + 0, + 16777216, + 33554432, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 8589934592L, + 25769803776L, + 60129542144L, + 128849018880L, + 266287972352L, + 541165879296L, + 1090921693184L, + 1099511627776L, + 2199023255552L, + 0, + 0, + 0, + 0, + 0, + 0, + 282574488338432L, + 0, + 1128098930098176L, + 0, + 0, + 0, + 0, + 0, + 72340168526266368L, + 0, + 0, + 577588851233521664L, + 0, + 0, + 0, + 0, + }, + { + 0, + 33686018, + 0, + 0, + 0, + 67637280, + 0, + 0, + 0, + 33686016, + 0, + 0, + 67637248, + 0, + 0, + 0, + 0, + 33685504, + 0, + 67633152, + 0, + 0, + 0, + 0, + 16777216, + 33554432, + 67108864, + 0, + 0, + 0, + 0, + 0, + 4294967296L, + 0, + 17179869184L, + 51539607552L, + 120259084288L, + 257698037760L, + 532575944704L, + 1082331758592L, + 1099511627776L, + 2199023255552L, + 4398046511104L, + 0, + 0, + 0, + 0, + 0, + 0, + 565148976676864L, + 0, + 2256197860196352L, + 0, + 0, + 0, + 0, + 0, + 144680337052532736L, + 0, + 0, + 1155177702467043328L, + 0, + 0, + 0, + }, + { + 0, + 0, + 67372036, + 0, + 0, + 0, + 135274560, + 0, + 0, + 0, + 67372032, + 0, + 0, + 135274496, + 0, + 0, + 33619968, + 0, + 67371008, + 0, + 135266304, + 0, + 0, + 0, + 0, + 33554432, + 67108864, + 134217728, + 0, + 0, + 0, + 0, + 12884901888L, + 8589934592L, + 0, + 34359738368L, + 103079215104L, + 240518168576L, + 515396075520L, + 1065151889408L, + 0, + 2199023255552L, + 4398046511104L, + 8796093022208L, + 0, + 0, + 0, + 0, + 283673999966208L, + 0, + 1130297953353728L, + 0, + 4512395720392704L, + 0, + 0, + 0, + 0, + 0, + 289360674105065472L, + 0, + 0, + 2310355404934086656L, + 0, + 0, + }, + { + 0, + 0, + 0, + 134744072, + 0, + 0, + 0, + 270549120, + 67240192, + 0, + 0, + 134744064, + 0, + 0, + 270548992, + 0, + 0, + 67239936, + 0, + 134742016, + 0, + 270532608, + 0, + 0, + 0, + 0, + 67108864, + 134217728, + 268435456, + 0, + 0, + 0, + 30064771072L, + 25769803776L, + 17179869184L, + 0, + 68719476736L, + 206158430208L, + 481036337152L, + 1030792151040L, + 0, + 0, + 4398046511104L, + 8796093022208L, + 17592186044416L, + 0, + 0, + 0, + 0, + 567347999932416L, + 0, + 2260595906707456L, + 0, + 9024791440785408L, + 0, + 0, + 72624942037860352L, + 0, + 0, + 578721348210130944L, + 0, + 0, + 4620710809868173312L, + 0, + }, + { + 134480385, + 0, + 0, + 0, + 269488144, + 0, + 0, + 0, + 0, + 134480384, + 0, + 0, + 269488128, + 0, + 0, + 541097984, + 0, + 0, + 134479872, + 0, + 269484032, + 0, + 541065216, + 0, + 0, + 0, + 0, + 134217728, + 268435456, + 536870912, + 0, + 0, + 64424509440L, + 60129542144L, + 51539607552L, + 34359738368L, + 0, + 137438953472L, + 412316860416L, + 962072674304L, + 0, + 0, + 0, + 8796093022208L, + 17592186044416L, + 35184372088832L, + 0, + 0, + 0, + 0, + 1134695999864832L, + 0, + 4521191813414912L, + 0, + 18049582881570816L, + 0, + 0, + 145249884075720704L, + 0, + 0, + 1157442696420261888L, + 0, + 0, + Long.parseUnsignedLong("9241421619736346624"), + }, + { + 0, + 268960770, + 0, + 0, + 0, + 538976288, + 0, + 0, + 0, + 0, + 268960768, + 0, + 0, + 538976256, + 0, + 0, + 0, + 0, + 0, + 268959744, + 0, + 538968064, + 0, + 1082130432L, + 0, + 0, + 0, + 0, + 268435456, + 536870912, + 1073741824L, + 0, + 133143986176L, + 128849018880L, + 120259084288L, + 103079215104L, + 68719476736L, + 0, + 274877906944L, + 824633720832L, + 0, + 0, + 0, + 0, + 17592186044416L, + 35184372088832L, + 70368744177664L, + 0, + 0, + 0, + 0, + 2269391999729664L, + 0, + 9042383626829824L, + 0, + 36099165763141632L, + 0, + 0, + 290499768151441408L, + 0, + 0, + 2314885392840523776L, + 0, + 0, + }, + { + 36099165763141633L, + 0, + 537921540, + 0, + 0, + 0, + 1077952576L, + 0, + 0, + 0, + 0, + 537921536, + 0, + 0, + 1077952512L, + 0, + 0, + 0, + 0, + 0, + 537919488, + 0, + 1077936128L, + 0, + 0, + 0, + 0, + 0, + 0, + 536870912, + 1073741824L, + 2147483648L, + 270582939648L, + 266287972352L, + 257698037760L, + 240518168576L, + 206158430208L, + 137438953472L, + 0, + 549755813888L, + 0, + 0, + 0, + 0, + 0, + 35184372088832L, + 70368744177664L, + 140737488355328L, + 0, + 0, + 0, + 0, + 4538783999459328L, + 0, + 18084767253659648L, + 0, + 0, + 0, + 0, + 580999536302882816L, + 0, + 0, + 4629770785681047552L, + 0, + }, + { + 0, + 0, + 0, + 1075843080L, + 0, + 0, + 0, + 2155905152L, + 0, + 0, + 0, + 0, + 1075843072L, + 0, + 0, + 2155905024L, + 0, + 0, + 0, + 0, + 0, + 1075838976L, + 0, + 2155872256L, + 0, + 0, + 0, + 0, + 0, + 0, + 1073741824L, + 2147483648L, + 545460846592L, + 541165879296L, + 532575944704L, + 515396075520L, + 481036337152L, + 412316860416L, + 274877906944L, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 70368744177664L, + 140737488355328L, + 0, + 0, + 0, + 0, + 0, + 9077567998918656L, + 0, + 36169534507319296L, + 0, + 0, + 0, + 0, + 1161999072605765632L, + 0, + 0, + Long.parseUnsignedLong("9259541571362095104"), + }, + { + 4311810305L, + 0, + 0, + 0, + 0, + 8657571872L, + 0, + 0, + 4311810304L, + 0, + 0, + 0, + 8657571840L, + 0, + 0, + 0, + 4311810048L, + 0, + 0, + 8657567744L, + 0, + 0, + 0, + 0, + 4311744512L, + 0, + 8657043456L, + 0, + 0, + 0, + 0, + 0, + 4294967296L, + 8589934592L, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 2199023255552L, + 6597069766656L, + 15393162788864L, + 32985348833280L, + 68169720922112L, + 138538465099776L, + 279275953455104L, + 281474976710656L, + 562949953421312L, + 0, + 0, + 0, + 0, + 0, + 0, + 72339069014638592L, + 0, + 288793326105133056L, + 0, + 0, + 0, + 0, + 0, + }, + { + 0, + 8623620610L, + 0, + 0, + 0, + 0, + 17315143744L, + 0, + 0, + 8623620608L, + 0, + 0, + 0, + 17315143680L, + 0, + 0, + 0, + 8623620096L, + 0, + 0, + 17315135488L, + 0, + 0, + 0, + 0, + 8623489024L, + 0, + 17314086912L, + 0, + 0, + 0, + 0, + 4294967296L, + 8589934592L, + 17179869184L, + 0, + 0, + 0, + 0, + 0, + 1099511627776L, + 0, + 4398046511104L, + 13194139533312L, + 30786325577728L, + 65970697666560L, + 136339441844224L, + 277076930199552L, + 281474976710656L, + 562949953421312L, + 1125899906842624L, + 0, + 0, + 0, + 0, + 0, + 0, + 144678138029277184L, + 0, + 577586652210266112L, + 0, + 0, + 0, + 0, + }, + { + 0, + 0, + 17247241220L, + 0, + 0, + 0, + 0, + 34630287488L, + 0, + 0, + 17247241216L, + 0, + 0, + 0, + 34630287360L, + 0, + 0, + 0, + 17247240192L, + 0, + 0, + 34630270976L, + 0, + 0, + 8606711808L, + 0, + 17246978048L, + 0, + 34628173824L, + 0, + 0, + 0, + 0, + 8589934592L, + 17179869184L, + 34359738368L, + 0, + 0, + 0, + 0, + 3298534883328L, + 2199023255552L, + 0, + 8796093022208L, + 26388279066624L, + 61572651155456L, + 131941395333120L, + 272678883688448L, + 0, + 562949953421312L, + 1125899906842624L, + 2251799813685248L, + 0, + 0, + 0, + 0, + 72620543991349248L, + 0, + 289356276058554368L, + 0, + 1155173304420532224L, + 0, + 0, + 0, + }, + { + 0, + 0, + 0, + 34494482440L, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 34494482432L, + 0, + 0, + 0, + 69260574720L, + 17213489152L, + 0, + 0, + 34494480384L, + 0, + 0, + 69260541952L, + 0, + 0, + 17213423616L, + 0, + 34493956096L, + 0, + 69256347648L, + 0, + 0, + 0, + 0, + 17179869184L, + 34359738368L, + 68719476736L, + 0, + 0, + 0, + 7696581394432L, + 6597069766656L, + 4398046511104L, + 0, + 17592186044416L, + 52776558133248L, + 123145302310912L, + 263882790666240L, + 0, + 0, + 1125899906842624L, + 2251799813685248L, + 4503599627370496L, + 0, + 0, + 0, + 0, + 145241087982698496L, + 0, + 578712552117108736L, + 0, + 2310346608841064448L, + 0, + 0, + }, + { + 0, + 0, + 0, + 0, + 68988964880L, + 0, + 0, + 0, + 34426978560L, + 0, + 0, + 0, + 68988964864L, + 0, + 0, + 0, + 0, + 34426978304L, + 0, + 0, + 68988960768L, + 0, + 0, + 138521083904L, + 0, + 0, + 34426847232L, + 0, + 68987912192L, + 0, + 138512695296L, + 0, + 0, + 0, + 0, + 34359738368L, + 68719476736L, + 137438953472L, + 0, + 0, + 16492674416640L, + 15393162788864L, + 13194139533312L, + 8796093022208L, + 0, + 35184372088832L, + 105553116266496L, + 246290604621824L, + 0, + 0, + 0, + 2251799813685248L, + 4503599627370496L, + 9007199254740992L, + 0, + 0, + 0, + 0, + 290482175965396992L, + 0, + 1157425104234217472L, + 0, + 4620693217682128896L, + 0, + }, + { + 68853957121L, + 0, + 0, + 0, + 0, + 137977929760L, + 0, + 0, + 0, + 68853957120L, + 0, + 0, + 0, + 137977929728L, + 0, + 0, + 0, + 0, + 68853956608L, + 0, + 0, + 137977921536L, + 0, + 0, + 0, + 0, + 0, + 68853694464L, + 0, + 137975824384L, + 0, + 277025390592L, + 0, + 0, + 0, + 0, + 68719476736L, + 137438953472L, + 274877906944L, + 0, + 34084860461056L, + 32985348833280L, + 30786325577728L, + 26388279066624L, + 17592186044416L, + 0, + 70368744177664L, + 211106232532992L, + 0, + 0, + 0, + 0, + 4503599627370496L, + 9007199254740992L, + 18014398509481984L, + 0, + 0, + 0, + 0, + 580964351930793984L, + 0, + 2314850208468434944L, + 0, + Long.parseUnsignedLong("9241386435364257792"), + }, + { + 0, + 137707914242L, + 0, + 0, + 0, + 0, + 275955859520L, + 0, + 0, + 0, + 137707914240L, + 0, + 0, + 0, + 275955859456L, + 0, + 0, + 0, + 0, + 137707913216L, + 0, + 0, + 275955843072L, + 0, + 0, + 0, + 0, + 0, + 137707388928L, + 0, + 275951648768L, + 0, + 0, + 0, + 0, + 0, + 0, + 137438953472L, + 274877906944L, + 549755813888L, + 69269232549888L, + 68169720922112L, + 65970697666560L, + 61572651155456L, + 52776558133248L, + 35184372088832L, + 0, + 140737488355328L, + 0, + 0, + 0, + 0, + 0, + 9007199254740992L, + 18014398509481984L, + 36028797018963968L, + 0, + 0, + 0, + 0, + 1161928703861587968L, + 0, + 4629700416936869888L, + 0, + }, + { + 36028797018963969L, + 0, + 275415828484L, + 0, + 0, + 0, + 0, + 551911719040L, + 0, + 0, + 0, + 275415828480L, + 0, + 0, + 0, + 551911718912L, + 0, + 0, + 0, + 0, + 275415826432L, + 0, + 0, + 551911686144L, + 0, + 0, + 0, + 0, + 0, + 275414777856L, + 0, + 551903297536L, + 0, + 0, + 0, + 0, + 0, + 0, + 274877906944L, + 549755813888L, + 139637976727552L, + 138538465099776L, + 136339441844224L, + 131941395333120L, + 123145302310912L, + 105553116266496L, + 70368744177664L, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 18014398509481984L, + 36028797018963968L, + 0, + 0, + 0, + 0, + 0, + 2323857407723175936L, + 0, + Long.parseUnsignedLong("9259400833873739776"), + }, + { + 1103823438081L, + 0, + 0, + 0, + 0, + 0, + 2216338399296L, + 0, + 1103823438080L, + 0, + 0, + 0, + 0, + 2216338399232L, + 0, + 0, + 1103823437824L, + 0, + 0, + 0, + 2216338391040L, + 0, + 0, + 0, + 1103823372288L, + 0, + 0, + 2216337342464L, + 0, + 0, + 0, + 0, + 1103806595072L, + 0, + 2216203124736L, + 0, + 0, + 0, + 0, + 0, + 1099511627776L, + 2199023255552L, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 562949953421312L, + 1688849860263936L, + 3940649673949184L, + 8444249301319680L, + 17451448556060672L, + 35465847065542656L, + 71494644084506624L, + 72057594037927936L, + 144115188075855872L, + 0, + 0, + 0, + 0, + 0, + 0, + }, + { + 0, + 2207646876162L, + 0, + 0, + 0, + 0, + 0, + 4432676798592L, + 0, + 2207646876160L, + 0, + 0, + 0, + 0, + 4432676798464L, + 0, + 0, + 2207646875648L, + 0, + 0, + 0, + 4432676782080L, + 0, + 0, + 0, + 2207646744576L, + 0, + 0, + 4432674684928L, + 0, + 0, + 0, + 0, + 2207613190144L, + 0, + 4432406249472L, + 0, + 0, + 0, + 0, + 1099511627776L, + 2199023255552L, + 4398046511104L, + 0, + 0, + 0, + 0, + 0, + 281474976710656L, + 0, + 1125899906842624L, + 3377699720527872L, + 7881299347898368L, + 16888498602639360L, + 34902897112121344L, + 70931694131085312L, + 72057594037927936L, + 144115188075855872L, + 288230376151711744L, + 0, + 0, + 0, + 0, + 0, + }, + { + 0, + 0, + 4415293752324L, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 4415293752320L, + 0, + 0, + 0, + 0, + 8865353596928L, + 0, + 0, + 4415293751296L, + 0, + 0, + 0, + 8865353564160L, + 0, + 0, + 0, + 4415293489152L, + 0, + 0, + 8865349369856L, + 0, + 0, + 2203318222848L, + 0, + 4415226380288L, + 0, + 8864812498944L, + 0, + 0, + 0, + 0, + 2199023255552L, + 4398046511104L, + 8796093022208L, + 0, + 0, + 0, + 0, + 844424930131968L, + 562949953421312L, + 0, + 2251799813685248L, + 6755399441055744L, + 15762598695796736L, + 33776997205278720L, + 69805794224242688L, + 0, + 144115188075855872L, + 288230376151711744L, + 576460752303423488L, + 0, + 0, + 0, + 0, + }, + { + 0, + 0, + 0, + 8830587504648L, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 8830587504640L, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 8830587502592L, + 0, + 0, + 0, + 17730707128320L, + 4406653222912L, + 0, + 0, + 8830586978304L, + 0, + 0, + 17730698739712L, + 0, + 0, + 4406636445696L, + 0, + 8830452760576L, + 0, + 17729624997888L, + 0, + 0, + 0, + 0, + 4398046511104L, + 8796093022208L, + 17592186044416L, + 0, + 0, + 0, + 1970324836974592L, + 1688849860263936L, + 1125899906842624L, + 0, + 4503599627370496L, + 13510798882111488L, + 31525197391593472L, + 67553994410557440L, + 0, + 0, + 288230376151711744L, + 576460752303423488L, + 1152921504606846976L, + 0, + 0, + 0, + }, + { + 0, + 0, + 0, + 0, + 17661175009296L, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 17661175009280L, + 0, + 0, + 0, + 8813306511360L, + 0, + 0, + 0, + 17661175005184L, + 0, + 0, + 0, + 0, + 8813306445824L, + 0, + 0, + 17661173956608L, + 0, + 0, + 35461397479424L, + 0, + 0, + 8813272891392L, + 0, + 17660905521152L, + 0, + 35459249995776L, + 0, + 0, + 0, + 0, + 8796093022208L, + 17592186044416L, + 35184372088832L, + 0, + 0, + 4222124650659840L, + 3940649673949184L, + 3377699720527872L, + 2251799813685248L, + 0, + 9007199254740992L, + 27021597764222976L, + 63050394783186944L, + 0, + 0, + 0, + 576460752303423488L, + 1152921504606846976L, + 2305843009213693952L, + 0, + 0, + }, + { + 0, + 0, + 0, + 0, + 0, + 35322350018592L, + 0, + 0, + 17626613022976L, + 0, + 0, + 0, + 0, + 35322350018560L, + 0, + 0, + 0, + 17626613022720L, + 0, + 0, + 0, + 35322350010368L, + 0, + 0, + 0, + 0, + 17626612891648L, + 0, + 0, + 35322347913216L, + 0, + 0, + 0, + 0, + 0, + 17626545782784L, + 0, + 35321811042304L, + 0, + 70918499991552L, + 0, + 0, + 0, + 0, + 17592186044416L, + 35184372088832L, + 70368744177664L, + 0, + 8725724278030336L, + 8444249301319680L, + 7881299347898368L, + 6755399441055744L, + 4503599627370496L, + 0, + 18014398509481984L, + 54043195528445952L, + 0, + 0, + 0, + 0, + 1152921504606846976L, + 2305843009213693952L, + 4611686018427387904L, + 0, + }, + { + 35253226045953L, + 0, + 0, + 0, + 0, + 0, + 70644700037184L, + 0, + 0, + 35253226045952L, + 0, + 0, + 0, + 0, + 70644700037120L, + 0, + 0, + 0, + 35253226045440L, + 0, + 0, + 0, + 70644700020736L, + 0, + 0, + 0, + 0, + 35253225783296L, + 0, + 0, + 70644695826432L, + 0, + 0, + 0, + 0, + 0, + 35253091565568L, + 0, + 70643622084608L, + 0, + 0, + 0, + 0, + 0, + 0, + 35184372088832L, + 70368744177664L, + 140737488355328L, + 17732923532771328L, + 17451448556060672L, + 16888498602639360L, + 15762598695796736L, + 13510798882111488L, + 9007199254740992L, + 0, + 36028797018963968L, + 0, + 0, + 0, + 0, + 0, + 2305843009213693952L, + 4611686018427387904L, + Long.parseUnsignedLong("9223372036854775808"), + }, + { + 0, + 70506452091906L, + 0, + 0, + 0, + 0, + 0, + 141289400074368L, + 0, + 0, + 70506452091904L, + 0, + 0, + 0, + 0, + 141289400074240L, + 0, + 0, + 0, + 70506452090880L, + 0, + 0, + 0, + 141289400041472L, + 0, + 0, + 0, + 0, + 70506451566592L, + 0, + 0, + 141289391652864L, + 0, + 0, + 0, + 0, + 0, + 70506183131136L, + 0, + 141287244169216L, + 0, + 0, + 0, + 0, + 0, + 0, + 70368744177664L, + 140737488355328L, + 35747322042253312L, + 35465847065542656L, + 34902897112121344L, + 33776997205278720L, + 31525197391593472L, + 27021597764222976L, + 18014398509481984L, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 4611686018427387904L, + Long.parseUnsignedLong("9223372036854775808"), + }, + { + 282578800148737L, + 0, + 0, + 0, + 0, + 0, + 0, + 567382630219904L, + 282578800148736L, + 0, + 0, + 0, + 0, + 0, + 567382630219776L, + 0, + 282578800148480L, + 0, + 0, + 0, + 0, + 567382630203392L, + 0, + 0, + 282578800082944L, + 0, + 0, + 0, + 567382628106240L, + 0, + 0, + 0, + 282578783305728L, + 0, + 0, + 567382359670784L, + 0, + 0, + 0, + 0, + 282574488338432L, + 0, + 567347999932416L, + 0, + 0, + 0, + 0, + 0, + 281474976710656L, + 562949953421312L, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 144115188075855872L, + 432345564227567616L, + 1008806316530991104L, + 2161727821137838080L, + 4467570830351532032L, + Long.parseUnsignedLong("9079256848778919936"), + Long.parseUnsignedLong("18302628885633695744"), + }, + { + 0, + 565157600297474L, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 565157600297472L, + 0, + 0, + 0, + 0, + 0, + 1134765260439552L, + 0, + 565157600296960L, + 0, + 0, + 0, + 0, + 1134765260406784L, + 0, + 0, + 565157600165888L, + 0, + 0, + 0, + 1134765256212480L, + 0, + 0, + 0, + 565157566611456L, + 0, + 0, + 1134764719341568L, + 0, + 0, + 0, + 0, + 565148976676864L, + 0, + 1134695999864832L, + 0, + 0, + 0, + 0, + 281474976710656L, + 562949953421312L, + 1125899906842624L, + 0, + 0, + 0, + 0, + 0, + 72057594037927936L, + 0, + 288230376151711744L, + 864691128455135232L, + 2017612633061982208L, + 4323455642275676160L, + 8935141660703064064L, + Long.parseUnsignedLong("18158513697557839872"), + }, + { + Long.parseUnsignedLong("18158513697557839873"), + 0, + 1130315200594948L, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 1130315200594944L, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 1130315200593920L, + 0, + 0, + 0, + 0, + 2269530520813568L, + 0, + 0, + 1130315200331776L, + 0, + 0, + 0, + 2269530512424960L, + 0, + 0, + 0, + 1130315133222912L, + 0, + 0, + 2269529438683136L, + 0, + 0, + 564049465049088L, + 0, + 1130297953353728L, + 0, + 2269391999729664L, + 0, + 0, + 0, + 0, + 562949953421312L, + 1125899906842624L, + 2251799813685248L, + 0, + 0, + 0, + 0, + 216172782113783808L, + 144115188075855872L, + 0, + 576460752303423488L, + 1729382256910270464L, + 4035225266123964416L, + 8646911284551352320L, + Long.parseUnsignedLong("17870283321406128128"), + }, + { + Long.parseUnsignedLong("17870283321406128129"), + 0, + 0, + 2260630401189896L, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 2260630401189888L, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 2260630401187840L, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 2260630400663552L, + 0, + 0, + 0, + 4539061024849920L, + 1128103225065472L, + 0, + 0, + 2260630266445824L, + 0, + 0, + 4539058877366272L, + 0, + 0, + 1128098930098176L, + 0, + 2260595906707456L, + 0, + 4538783999459328L, + 0, + 0, + 0, + 0, + 1125899906842624L, + 2251799813685248L, + 4503599627370496L, + 0, + 0, + 0, + 504403158265495552L, + 432345564227567616L, + 288230376151711744L, + 0, + 1152921504606846976L, + 3458764513820540928L, + 8070450532247928832L, + Long.parseUnsignedLong("17293822569102704640"), + }, + { + Long.parseUnsignedLong("17293822569102704641"), + 0, + 0, + 0, + 4521260802379792L, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 4521260802379776L, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 4521260802375680L, + 0, + 0, + 0, + 2256206466908160L, + 0, + 0, + 0, + 4521260801327104L, + 0, + 0, + 0, + 0, + 2256206450130944L, + 0, + 0, + 4521260532891648L, + 0, + 0, + 9078117754732544L, + 0, + 0, + 2256197860196352L, + 0, + 4521191813414912L, + 0, + 9077567998918656L, + 0, + 0, + 0, + 0, + 2251799813685248L, + 4503599627370496L, + 9007199254740992L, + 0, + 0, + 1080863910568919040L, + 1008806316530991104L, + 864691128455135232L, + 576460752303423488L, + 0, + 2305843009213693952L, + 6917529027641081856L, + Long.parseUnsignedLong("16140901064495857664"), + }, + { + Long.parseUnsignedLong("16140901064495857665"), + 0, + 0, + 0, + 0, + 9042521604759584L, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 9042521604759552L, + 0, + 0, + 4512412933881856L, + 0, + 0, + 0, + 0, + 9042521604751360L, + 0, + 0, + 0, + 4512412933816320L, + 0, + 0, + 0, + 9042521602654208L, + 0, + 0, + 0, + 0, + 4512412900261888L, + 0, + 0, + 9042521065783296L, + 0, + 0, + 0, + 0, + 0, + 4512395720392704L, + 0, + 9042383626829824L, + 0, + 18155135997837312L, + 0, + 0, + 0, + 0, + 4503599627370496L, + 9007199254740992L, + 18014398509481984L, + 0, + 2233785415175766016L, + 2161727821137838080L, + 2017612633061982208L, + 1729382256910270464L, + 1152921504606846976L, + 0, + 4611686018427387904L, + Long.parseUnsignedLong("13835058055282163712"), + }, + { + Long.parseUnsignedLong("13835058055282163713"), + 0, + 0, + 0, + 0, + 0, + 18085043209519168L, + 0, + 9024825867763968L, + 0, + 0, + 0, + 0, + 0, + 18085043209519104L, + 0, + 0, + 9024825867763712L, + 0, + 0, + 0, + 0, + 18085043209502720L, + 0, + 0, + 0, + 9024825867632640L, + 0, + 0, + 0, + 18085043205308416L, + 0, + 0, + 0, + 0, + 9024825800523776L, + 0, + 0, + 18085042131566592L, + 0, + 0, + 0, + 0, + 0, + 9024791440785408L, + 0, + 18084767253659648L, + 0, + 0, + 0, + 0, + 0, + 0, + 9007199254740992L, + 18014398509481984L, + 36028797018963968L, + 4539628424389459968L, + 4467570830351532032L, + 4323455642275676160L, + 4035225266123964416L, + 3458764513820540928L, + 2305843009213693952L, + 0, + Long.parseUnsignedLong("9223372036854775808"), + }, + { + 18049651735527937L, + 0, + 0, + 0, + 0, + 0, + 0, + 36170086419038336L, + 0, + 18049651735527936L, + 0, + 0, + 0, + 0, + 0, + 36170086419038208L, + 0, + 0, + 18049651735527424L, + 0, + 0, + 0, + 0, + 36170086419005440L, + 0, + 0, + 0, + 18049651735265280L, + 0, + 0, + 0, + 36170086410616832L, + 0, + 0, + 0, + 0, + 18049651601047552L, + 0, + 0, + 36170084263133184L, + 0, + 0, + 0, + 0, + 0, + 18049582881570816L, + 0, + 36169534507319296L, + 0, + 0, + 0, + 0, + 0, + 0, + 18014398509481984L, + 36028797018963968L, + Long.parseUnsignedLong("9151314442816847872"), + Long.parseUnsignedLong("9079256848778919936"), + 8935141660703064064L, + 8646911284551352320L, + 8070450532247928832L, + 6917529027641081856L, + 4611686018427387904L, + 0, + }, +}; +} \ No newline at end of file diff --git a/Java/MoveConstants.java b/Java/MoveConstants.java new file mode 100644 index 0000000..cb73d90 --- /dev/null +++ b/Java/MoveConstants.java @@ -0,0 +1,878 @@ + + +public class MoveConstants { + +//#region + + +public static final long[] KNIGHT_ATTACKS = +{ + 132096, + 329728, + 659712, + 1319424, + 2638848, + 5277696, + 10489856, + 4202496, + 33816580, + 84410376, + 168886289, + 337772578, + 675545156, + 1351090312L, + 2685403152L, + 1075839008L, + 8657044482L, + 21609056261L, + 43234889994L, + 86469779988L, + 172939559976L, + 345879119952L, + 687463207072L, + 275414786112L, + 2216203387392L, + 5531918402816L, + 11068131838464L, + 22136263676928L, + 44272527353856L, + 88545054707712L, + 175990581010432L, + 70506185244672L, + 567348067172352L, + 1416171111120896L, + 2833441750646784L, + 5666883501293568L, + 11333767002587136L, + 22667534005174272L, + 45053588738670592L, + 18049583422636032L, + 145241105196122112L, + 362539804446949376L, + 725361088165576704L, + 1450722176331153408L, + 2901444352662306816L, + 5802888705324613632L, + Long.parseUnsignedLong("11533718717099671552"), + 4620693356194824192L, + 288234782788157440L, + 576469569871282176L, + 1224997833292120064L, + 2449995666584240128L, + 4899991333168480256L, + Long.parseUnsignedLong("9799982666336960512"), + 1152939783987658752L, + 2305878468463689728L, + 1128098930098176L, + 2257297371824128L, + 4796069720358912L, + 9592139440717824L, + 19184278881435648L, + 38368557762871296L, + 4679521487814656L, + 9077567998918656L, +}; +public static final long[][] BISHOP_ATTACKS = { + { + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 1, + 2, + 4, + 8, + 16, + 32, + 64, + 0, + 256, + 513, + 1026, + 2052, + 4104, + 8208, + 16416, + 0, + 65536, + 131328, + 262657, + 525314, + 1050628, + 2101256, + 4202512, + 0, + 16777216, + 33619968, + 67240192, + 134480385, + 268960770, + 537921540, + 1075843080L, + 0, + 4294967296L, + 8606711808L, + 17213489152L, + 34426978560L, + 68853957121L, + 137707914242L, + 275415828484L, + 0, + 1099511627776L, + 2203318222848L, + 4406653222912L, + 8813306511360L, + 17626613022976L, + 35253226045953L, + 70506452091906L, + 0, + 281474976710656L, + 564049465049088L, + 1128103225065472L, + 2256206466908160L, + 4512412933881856L, + 9024825867763968L, + 18049651735527937L, + }, + { + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 2, + 4, + 8, + 16, + 32, + 64, + 128, + 0, + 516, + 1032, + 2064, + 4128, + 8256, + 16512, + 32768, + 0, + 132104, + 264208, + 528416, + 1056832, + 2113664, + 4227072, + 8388608, + 0, + 33818640, + 67637280, + 135274560, + 270549120, + 541097984, + 1082130432L, + 2147483648L, + 0, + 8657571872L, + 17315143744L, + 34630287488L, + 69260574720L, + 138521083904L, + 277025390592L, + 549755813888L, + 0, + 2216338399296L, + 4432676798592L, + 8865353596928L, + 17730707128320L, + 35461397479424L, + 70918499991552L, + 140737488355328L, + 0, + 567382630219904L, + 1134765260439552L, + 2269530520813568L, + 4539061024849920L, + 9078117754732544L, + 18155135997837312L, + 36028797018963968L, + 0, + }, + { + 0, + 256, + 66048, + 16909312, + 4328785920L, + 1108169199616L, + 283691315109888L, + 72624976668147712L, + 0, + 65536, + 16908288, + 4328783872L, + 1108169195520L, + 283691315101696L, + 72624976668131328L, + 145249953336262656L, + 0, + 16777216, + 4328521728L, + 1108168671232L, + 283691314053120L, + 72624976666034176L, + 145249953332068352L, + 290499906664136704L, + 0, + 4294967296L, + 1108101562368L, + 283691179835392L, + 72624976397598720L, + 145249952795197440L, + 290499905590394880L, + 580999811180789760L, + 0, + 1099511627776L, + 283673999966208L, + 72624942037860352L, + 145249884075720704L, + 290499768151441408L, + 580999536302882816L, + 1161999072605765632L, + 0, + 281474976710656L, + 72620543991349248L, + 145241087982698496L, + 290482175965396992L, + 580964351930793984L, + 1161928703861587968L, + 2323857407723175936L, + 0, + 72057594037927936L, + 144115188075855872L, + 288230376151711744L, + 576460752303423488L, + 1152921504606846976L, + 2305843009213693952L, + 4611686018427387904L, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + }, + { + Long.parseUnsignedLong("9241421688590303744"), + 36099303471055872L, + 141012904183808L, + 550831656960L, + 2151686144L, + 8404992, + 32768, + 0, + 4620710844295151616L, + Long.parseUnsignedLong("9241421688590303232"), + 36099303471054848L, + 141012904181760L, + 550831652864L, + 2151677952L, + 8388608, + 0, + 2310355422147510272L, + 4620710844295020544L, + Long.parseUnsignedLong("9241421688590041088"), + 36099303470530560L, + 141012903133184L, + 550829555712L, + 2147483648L, + 0, + 1155177711056977920L, + 2310355422113955840L, + 4620710844227911680L, + Long.parseUnsignedLong("9241421688455823360"), + 36099303202095104L, + 141012366262272L, + 549755813888L, + 0, + 577588851233521664L, + 1155177702467043328L, + 2310355404934086656L, + 4620710809868173312L, + Long.parseUnsignedLong("9241421619736346624"), + 36099165763141632L, + 140737488355328L, + 0, + 288793326105133056L, + 577586652210266112L, + 1155173304420532224L, + 2310346608841064448L, + 4620693217682128896L, + Long.parseUnsignedLong("9241386435364257792"), + 36028797018963968L, + 0, + 144115188075855872L, + 288230376151711744L, + 576460752303423488L, + 1152921504606846976L, + 2305843009213693952L, + 4611686018427387904L, + Long.parseUnsignedLong("9223372036854775808"), + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + + }, +}; +public static final long[][] ROOK_ATTACKS = { + { + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 1, + 2, + 4, + 8, + 16, + 32, + 64, + 128, + 257, + 514, + 1028, + 2056, + 4112, + 8224, + 16448, + 32896, + 65793, + 131586, + 263172, + 526344, + 1052688, + 2105376, + 4210752, + 8421504, + 16843009, + 33686018, + 67372036, + 134744072, + 269488144, + 538976288, + 1077952576L, + 2155905152L, + 4311810305L, + 8623620610L, + 17247241220L, + 34494482440L, + 68988964880L, + 137977929760L, + 275955859520L, + 551911719040L, + 1103823438081L, + 2207646876162L, + 4415293752324L, + 8830587504648L, + 17661175009296L, + 35322350018592L, + 70644700037184L, + 141289400074368L, + 282578800148737L, + 565157600297474L, + 1130315200594948L, + 2260630401189896L, + 4521260802379792L, + 9042521604759584L, + 18085043209519168L, + 36170086419038336L + }, + { + 254, + 252, + 248, + 240, + 224, + 192, + 128, + 0, + 65024, + 64512, + 63488, + 61440, + 57344, + 49152, + 32768, + 0, + 16646144, + 16515072, + 16252928, + 15728640, + 14680064, + 12582912, + 8388608, + 0, + 4261412864L, + 4227858432L, + 4160749568L, + 4026531840L, + 3758096384L, + 3221225472L, + 2147483648L, + 0, + 1090921693184L, + 1082331758592L, + 1065151889408L, + 1030792151040L, + 962072674304L, + 824633720832L, + 549755813888L, + 0, + 279275953455104L, + 277076930199552L, + 272678883688448L, + 263882790666240L, + 246290604621824L, + 211106232532992L, + 140737488355328L, + 0, + 71494644084506624L, + 70931694131085312L, + 69805794224242688L, + 67553994410557440L, + 63050394783186944L, + 54043195528445952L, + 36028797018963968L, + 0, + Long.parseUnsignedLong("18302628885633695744"), + Long.parseUnsignedLong("18158513697557839872"), + Long.parseUnsignedLong("17870283321406128128"), + Long.parseUnsignedLong("17293822569102704640"), + Long.parseUnsignedLong("16140901064495857664"), + Long.parseUnsignedLong("13835058055282163712"), + Long.parseUnsignedLong("9223372036854775808"), + 0 + }, + { + 72340172838076672L, + 144680345676153344L, + 289360691352306688L, + 578721382704613376L, + 1157442765409226752L, + 2314885530818453504L, + 4629771061636907008L, + Long.parseUnsignedLong("9259542123273814016"), + 72340172838076416L, + 144680345676152832L, + 289360691352305664L, + 578721382704611328L, + 1157442765409222656L, + 2314885530818445312L, + 4629771061636890624L, + Long.parseUnsignedLong("9259542123273781248"), + 72340172838010880L, + 144680345676021760L, + 289360691352043520L, + 578721382704087040L, + 1157442765408174080L, + 2314885530816348160L, + 4629771061632696320L, + Long.parseUnsignedLong("9259542123265392640"), + 72340172821233664L, + 144680345642467328L, + 289360691284934656L, + 578721382569869312L, + 1157442765139738624L, + 2314885530279477248L, + 4629771060558954496L, + Long.parseUnsignedLong("9259542121117908992"), + 72340168526266368L, + 144680337052532736L, + 289360674105065472L, + 578721348210130944L, + 1157442696420261888L, + 2314885392840523776L, + 4629770785681047552L, + Long.parseUnsignedLong("9259541571362095104"), + 72339069014638592L, + 144678138029277184L, + 289356276058554368L, + 578712552117108736L, + 1157425104234217472L, + 2314850208468434944L, + 4629700416936869888L, + Long.parseUnsignedLong("9259400833873739776"), + 72057594037927936L, + 144115188075855872L, + 288230376151711744L, + 576460752303423488L, + 1152921504606846976L, + 2305843009213693952L, + 4611686018427387904L, + Long.parseUnsignedLong("9223372036854775808"), + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0 + }, + { + 0, + 1, + 3, + 7, + 15, + 31, + 63, + 127, + 0, + 256, + 768, + 1792, + 3840, + 7936, + 16128, + 32512, + 0, + 65536, + 196608, + 458752, + 983040, + 2031616, + 4128768, + 8323072, + 0, + 16777216, + 50331648, + 117440512, + 251658240, + 520093696, + 1056964608L, + 2130706432L, + 0, + 4294967296L, + 12884901888L, + 30064771072L, + 64424509440L, + 133143986176L, + 270582939648L, + 545460846592L, + 0, + 1099511627776L, + 3298534883328L, + 7696581394432L, + 16492674416640L, + 34084860461056L, + 69269232549888L, + 139637976727552L, + 0, + 281474976710656L, + 844424930131968L, + 1970324836974592L, + 4222124650659840L, + 8725724278030336L, + 17732923532771328L, + 35747322042253312L, + 0, + 72057594037927936L, + 216172782113783808L, + 504403158265495552L, + 1080863910568919040L, + 2233785415175766016L, + 4539628424389459968L, + 9151314442816847872L + } +}; + +public static final long[] WHITE_PAWN_ATTACKS = { + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 2, + 5, + 10, + 20, + 40, + 80, + 160, + 64, + 512, + 1280, + 2560, + 5120, + 10240, + 20480, + 40960, + 16384, + 131072, + 327680, + 655360, + 1310720, + 2621440, + 5242880, + 10485760, + 4194304, + 33554432, + 83886080, + 167772160L, + 335544320L, + 671088640L, + 1342177280L, + 2684354560L, + 1073741824L, + 8589934592L, + 21474836480L, + 42949672960L, + 85899345920L, + 171798691840L, + 343597383680L, + 687194767360L, + 274877906944L, + 2199023255552L, + 5497558138880L, + 10995116277760L, + 21990232555520L, + 43980465111040L, + 87960930222080L, + 175921860444160L, + 70368744177664L, + 562949953421312L, + 1407374883553280L, + 2814749767106560L, + 5629499534213120L, + 11258999068426240L, + 22517998136852480L, + 45035996273704960L, + 18014398509481984L +}; +public static final long[] BLACK_PAWN_ATTACKS = { + 512, + 1280, + 2560, + 5120, + 10240, + 20480, + 40960, + 16384, + 131072, + 327680, + 655360, + 1310720, + 2621440, + 5242880, + 10485760, + 4194304, + 33554432, + 83886080, + 167772160, + 335544320, + 671088640, + 1342177280, + 2684354560L, + 1073741824, + 8589934592L, + 21474836480L, + 42949672960L, + 85899345920L, + 171798691840L, + 343597383680L, + 687194767360L, + 274877906944L, + 2199023255552L, + 5497558138880L, + 10995116277760L, + 21990232555520L, + 43980465111040L, + 87960930222080L, + 175921860444160L, + 70368744177664L, + 562949953421312L, + 1407374883553280L, + 2814749767106560L, + 5629499534213120L, + 11258999068426240L, + 22517998136852480L, + 45035996273704960L, + 18014398509481984L, + 144115188075855872L, + 360287970189639680L, + 720575940379279360L, + 1441151880758558720L, + 2882303761517117440L, + 5764607523034234880L, + Long.parseUnsignedLong("11529215046068469760"), + 4611686018427387904L, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0 +}; + +public static final long[] KING_ATTACKS = { + 770, + 1797, + 3594, + 7188, + 14376, + 28752, + 57504, + 49216, + 197123, + 460039, + 920078, + 1840156, + 3680312, + 7360624, + 14721248, + 12599488, + 50463488, + 117769984, + 235539968, + 471079936, + 942159872, + 1884319744L, + 3768639488L, + 3225468928L, + 12918652928L, + 30149115904L, + 60298231808L, + 120596463616L, + 241192927232L, + 482385854464L, + 964771708928L, + 825720045568L, + 3307175149568L, + 7718173671424L, + 15436347342848L, + 30872694685696L, + 61745389371392L, + 123490778742784L, + 246981557485568L, + 211384331665408L, + 846636838289408L, + 1975852459884544L, + 3951704919769088L, + 7903409839538176L, + 15806819679076352L, + 31613639358152704L, + 63227278716305408L, + 54114388906344448L, + 216739030602088448L, + 505818229730443264L, + 1011636459460886528L, + 2023272918921773056L, + 4046545837843546112L, + 8093091675687092224L, + Long.parseUnsignedLong("16186183351374184448"), + Long.parseUnsignedLong("13853283560024178688"), + 144959613005987840L, + 362258295026614272L, + 724516590053228544L, + 1449033180106457088L, + 2898066360212914176L, + 5796132720425828352L, + Long.parseUnsignedLong("11592265440851656704"), + Long.parseUnsignedLong("4665729213955833856") +}; + +public static final long[] SQUARE_BBS = { + 1, + 2, + 4, + 8, + 16, + 32, + 64, + 128, + 256, + 512, + 1024, + 2048, + 4096, + 8192, + 16384, + 32768, + 65536, + 131072, + 262144, + 524288, + 1048576, + 2097152, + 4194304, + 8388608, + 16777216, + 33554432, + 67108864, + 134217728, + 268435456, + 536870912, + 1073741824L, + 2147483648L, + 4294967296L, + 8589934592L, + 17179869184L, + 34359738368L, + 68719476736L, + 137438953472L, + 274877906944L, + 549755813888L, + 1099511627776L, + 2199023255552L, + 4398046511104L, + 8796093022208L, + 17592186044416L, + 35184372088832L, + 70368744177664L, + 140737488355328L, + 281474976710656L, + 562949953421312L, + 1125899906842624L, + 2251799813685248L, + 4503599627370496L, + 9007199254740992L, + 18014398509481984L, + 36028797018963968L, + 72057594037927936L, + 144115188075855872L, + 288230376151711744L, + 576460752303423488L, + 1152921504606846976L, + 2305843009213693952L, + 4611686018427387904L, + Long.parseUnsignedLong("9223372036854775808"), +}; + +//#endregion + +} \ No newline at end of file diff --git a/Java/MoveUtils.java b/Java/MoveUtils.java new file mode 100644 index 0000000..69b7076 --- /dev/null +++ b/Java/MoveUtils.java @@ -0,0 +1,236 @@ + +public class MoveUtils +{ + + static final int BISHOP_UP_LEFT = 0; + static final int BISHOP_UP_RIGHT = 1; + static final int BISHOP_DOWN_LEFT = 2; + static final int BISHOP_DOWN_RIGHT = 3; + + static final int ROOK_UP = 0; + static final int ROOK_DOWN = 2; + static final int ROOK_LEFT = 3; + static final int ROOK_RIGHT = 1; + + + public static long getRookMovesSeparate(long combined_occ, int square) + { + long combinedAttacks = 0; + + long rookAttackUp = MoveConstants.ROOK_ATTACKS[ROOK_UP][square]; + long rookAndOccs = rookAttackUp & combined_occ; + if (rookAndOccs != 0) + { + long lastValue = rookAndOccs; + for (int i = 0; i < 8; i++) + { + rookAndOccs = rookAndOccs & rookAndOccs - 1; + if (rookAndOccs == 0) + { + int endSquare = Perft.bitScanForward(lastValue); // Implement this method + combinedAttacks |= Inb.INBETWEEN_BITBOARDS[square][endSquare]; + break; + } + lastValue = rookAndOccs; + } + } + else + { + combinedAttacks |= rookAttackUp; + } + // Pr.println("Rook up"); + //Pr.printlongLn(combinedAttacks); + + long rookAttackLeft = MoveConstants.ROOK_ATTACKS[ROOK_LEFT][square]; + rookAndOccs = rookAttackLeft & combined_occ; + if (rookAndOccs != 0) + { + long lastValue = rookAndOccs; + for (int i = 0; i < 8; i++) + { + rookAndOccs = rookAndOccs & rookAndOccs - 1; + if (rookAndOccs == 0) + { + int endSquare = Perft.bitScanForward(lastValue); // Implement this method + combinedAttacks |= Inb.INBETWEEN_BITBOARDS[square][endSquare]; + break; + } + lastValue = rookAndOccs; + } + } + else + { + combinedAttacks |= rookAttackLeft; + } + //Pr.println("Rook left"); + //Pr.printlongLn(combinedAttacks); + + long rookAttackDown = MoveConstants.ROOK_ATTACKS[ROOK_DOWN][square]; + rookAndOccs = rookAttackDown & combined_occ; + if (rookAndOccs != 0) + { + int endSquare = Perft.bitScanForward(rookAndOccs); // Implement this method + combinedAttacks |= Inb.INBETWEEN_BITBOARDS[square][endSquare]; + } + else + { + combinedAttacks |= rookAttackDown; + } + // Pr.println("Rook down"); + // Pr.printlongLn(combinedAttacks); + + long rookAttackRight = MoveConstants.ROOK_ATTACKS[ROOK_RIGHT][square]; + rookAndOccs = rookAttackRight & combined_occ; + if (rookAndOccs != 0) + { + int endSquare = Perft.bitScanForward(rookAndOccs); // Implement this method + combinedAttacks |= Inb.INBETWEEN_BITBOARDS[square][endSquare]; + } + else + { + combinedAttacks |= rookAttackRight; + } + // Pr.println("Rook right"); + // Pr.printlongLn(combinedAttacks); + + return combinedAttacks; + } + + public static long getBishopMovesSeparate(long combined_occ, int square) + { + + //Pr.printIntLn(square); + //Pr.printSquareLn(square); + + long combinedAttacks = 0; + + long bishopAttackUpLeft = MoveConstants.BISHOP_ATTACKS[BISHOP_UP_LEFT][square]; + long bishopAndOccs = bishopAttackUpLeft & combined_occ; + if (bishopAndOccs != 0) { + long lastValue = bishopAndOccs; + for (int i = 0; i < 8; i++) { + bishopAndOccs &= bishopAndOccs - 1; + if (bishopAndOccs == 0) { + int endSquare = Perft.bitScanForward(lastValue); // Implement this method + combinedAttacks |= Inb.INBETWEEN_BITBOARDS[square][endSquare]; + break; + } + lastValue = bishopAndOccs; + } + } else { + combinedAttacks |= bishopAttackUpLeft; + } + // Pr.println("Bishop up left"); + //Pr.printlongLn(combinedAttacks); + + long bishopAttackUpRight = MoveConstants.BISHOP_ATTACKS[BISHOP_UP_RIGHT][square]; + bishopAndOccs = bishopAttackUpRight & combined_occ; + if (bishopAndOccs != 0) { + long lastValue = bishopAndOccs; + for (int i = 0; i < 8; i++) { + bishopAndOccs &= bishopAndOccs - 1; + if (bishopAndOccs == 0) { + int endSquare = Perft.bitScanForward(lastValue); // Implement this method + combinedAttacks |= Inb.INBETWEEN_BITBOARDS[square][endSquare]; + break; + } + lastValue = bishopAndOccs; + } + } else { + combinedAttacks |= bishopAttackUpRight; + } + // Pr.println("Bishop up right"); + // Pr.printlongLn(combinedAttacks); + + long bishopAttackDownLeft = MoveConstants.BISHOP_ATTACKS[BISHOP_DOWN_LEFT][square]; + + // Pr.println("down left bitboard"); + // Pr.printlongLn(bishopAttackDownLeft); + // Pr.println("__________"); + + bishopAndOccs = bishopAttackDownLeft & combined_occ; + if (bishopAndOccs != 0) { + int endSquare = Perft.bitScanForward(bishopAndOccs); // Implement this method + combinedAttacks |= Inb.INBETWEEN_BITBOARDS[square][endSquare]; + } else { + combinedAttacks |= bishopAttackDownLeft; + } + // Pr.println("Bishop down left"); + // Pr.printlongLn(combinedAttacks); + + long bishopAttackDownRight = MoveConstants.BISHOP_ATTACKS[BISHOP_DOWN_RIGHT][square]; + bishopAndOccs = bishopAttackDownRight & combined_occ; + if (bishopAndOccs != 0) { + int endSquare = Perft.bitScanForward(bishopAndOccs); // Implement this method + combinedAttacks |= Inb.INBETWEEN_BITBOARDS[square][endSquare]; + } else { + combinedAttacks |= bishopAttackDownRight; + } + // Pr.println("Bishop down right"); + // Pr.printlongLn(combinedAttacks); + return combinedAttacks; + } + + public static Boolean Is_Square_Attacked_By_Black_Global(int square, long occupancy) + { + + // if (Board.bitboard_array_global[GenConst.BP] & MoveConstants.WHITE_PAWN_ATTACKS[square]).signum() != 0) { + //return true; + //} + //if (Board.bitboard_array_global[GenConst.BN] & MoveConstants.KNIGHT_ATTACKS[square]).signum() != 0) { + //return true; + //} + //if (Board.bitboard_array_global[GenConst.BK] & MoveConstants.KING_ATTACKS[square]).signum() != 0) { + /// return true; + //} + //long bishopAttacks = MoveUtils.GetBishopMovesSeparate(occupancy, square); + //if (Board.bitboard_array_global[GenConst.BB] & bishopAttacks).signum() != 0) { + /// return true; + //} + //if (Board.bitboard_array_global[GenConst.BQ] & bishopAttacks).signum() != 0) { + /// return true; + //} + //B/igInteger rookAttacks = MoveUtils.GetRookMovesSeparate(occupancy, square); + //if (Board.bitboard_array_global[GenConst.BR] & rookAttacks).signum() != 0) { + //return true; + //} + //if (Board.bitboard_array_global[GenConst.BQ] & rookAttacks).signum() != 0) { + //return true; + //} + + return false; + } + + + public static Boolean Is_Square_Attacked_By_White_Global(int square, long occupancy) + { + + // if (Board.bitboard_array_global[GenConst.WP] & MoveConstants.BLACK_PAWN_ATTACKS[square]).signum() != 0) { + //return true; + //} + //if (Board.bitboard_array_global[GenConst.WN] & MoveConstants.KNIGHT_ATTACKS[square]).signum() != 0) { + //return true; + //} + //if (Board.bitboard_array_global[GenConst.WK] & MoveConstants.KING_ATTACKS[square]).signum() != 0) { + //return true; + //} + //long bishopAttacks = MoveUtils.GetBishopMovesSeparate(occupancy, square); + //if (Board.bitboard_array_global[GenConst.WB] & bishopAttacks).signum() != 0) { + //return true; + //} + //if (Board.bitboard_array_global[GenConst.WQ] & bishopAttacks).signum() != 0) { + //return true; + //} + //long rookAttacks = MoveUtils.GetRookMovesSeparate(occupancy, square); + //if (Board.bitboard_array_global[GenConst.WR] & rookAttacks).signum() != 0) { + //return true; + //} + //if (Board.bitboard_array_global[GenConst.WQ] & rookAttacks).signum() != 0) { + //return true; + //} + + return false; + } + + +} diff --git a/Java/Perft.java b/Java/Perft.java new file mode 100644 index 0000000..d314007 --- /dev/null +++ b/Java/Perft.java @@ -0,0 +1,2378 @@ + +import java.math.BigInteger; + + +class ErrorInt +{ + private Error error; + private int value; + + // No-argument constructor + public ErrorInt(Error _error, int _value) { + this.error = _error; // or any default Error value + this.value = _value; // default value + } + + public static void printError(Error error) { + System.out.println(error); + } + + // Getters for error and value + public Error getError() { + return error; + } + + public int getValue() { + return value; + } + + // Setter for value + public void setValue(int value) { + this.value = value; + } +} + + +class DebugInfo +{ + public int CallCount; + public int LastStarting; + public int LastTarget; + public int LastTag; + public int lastPiece; + public boolean PromotionExpected; +} + + +enum Error +{ + White_King_Captured, + Black_King_Captured, + Too_Many_Moves, + White_King_In_Check_On_BlackMove, + Black_King_In_Check_On_White_Move, + Copy_Boards_Not_Same, + Invalid_Starting_Square, + Invalid_Target_Square, + Invalid_Tag, + Invalid_Piece, + Capture_Index_Invalid, + Capture_Piece_Not_Found, + Starting_Depth_Too_High, + Starting_Depth_Zero, + Depth_Less_Than_Zero, + Starting_Square_And_Target_Square_The_Same, + Ep_Not_Target_Square, + Promotion_When_Not_Expected, + Side_Not_Changed, + Side_Not_Changed_Back, +} + +public class Perft +{ + + static Boolean OutOfBounds(int move) { + + if (move < 0) { + return true; + } + if (move > 63) { + return true; + } + return false; + } + + static void PrintMoveNoNL(int starting, int target_square, int tag) { //starting + + // Print starting + if (OutOfBounds(starting)) { + System.out.printf("%d", starting); + } else { + System.out.printf("%c%c", GenConst.SQ_CHAR_X[starting], GenConst.SQ_CHAR_Y[starting]); + } + + // Print target + if (OutOfBounds(target_square)) { + System.out.printf("%d", target_square); + } else { + System.out.printf("%c%c", GenConst.SQ_CHAR_X[target_square], GenConst.SQ_CHAR_Y[target_square]); + } + + // Print promotion tag + switch (tag) { + case GenConst.TAG_B_N_PROMOTION: + case GenConst.TAG_B_N_PROMOTION_CAP: + case GenConst.TAG_W_N_PROMOTION: + case GenConst.TAG_W_N_PROMOTION_CAP: + System.out.printf("n"); + break; + case GenConst.TAG_B_R_PROMOTION: + case GenConst.TAG_B_R_PROMOTION_CAP: + case GenConst.TAG_W_R_PROMOTION: + case GenConst.TAG_W_R_PROMOTION_CAP: + System.out.printf("r"); + break; + case GenConst.TAG_B_B_PROMOTION: + case GenConst.TAG_B_B_PROMOTION_CAP: + case GenConst.TAG_W_B_PROMOTION: + case GenConst.TAG_W_B_PROMOTION_CAP: + System.out.printf("b"); + break; + case GenConst.TAG_B_Q_PROMOTION: + case GenConst.TAG_B_Q_PROMOTION_CAP: + case GenConst.TAG_W_Q_PROMOTION: + case GenConst.TAG_W_Q_PROMOTION_CAP: + System.out.printf("q"); + break; + default: + break; + } + } + + static final int MOVE_STARTING = 0; + static final int MOVE_TARGET = 1; + static final int MOVE_PIECE = 2; + static final int MOVE_TAG = 3; + static final int NO_SQUARE = 64; + + static final int WKS_CASTLE_RIGHTS = 0; + static final int WQS_CASTLE_RIGHTS = 1; + static final int BKS_CASTLE_RIGHTS = 2; + static final int BQS_CASTLE_RIGHTS = 3; + + static final int PINNED_SQUARE_INDEX = 0; + static final int PINNING_PIECE_INDEX = 1; + + static final BigInteger WKS_EMPTY_BITBOARD = new BigInteger("6917529027641081856"); + static final BigInteger WQS_EMPTY_BITBOARD = new BigInteger("1008806316530991104"); + static final BigInteger BKS_EMPTY_BITBOARD = new BigInteger("96"); + static final BigInteger BQS_EMPTY_BITBOARD = new BigInteger("14"); + + static final BigInteger RANK_1_BITBOARD = new BigInteger( "18374686479671623680"); + static final BigInteger RANK_2_BITBOARD =new BigInteger( "71776119061217280"); + static final BigInteger RANK_3_BITBOARD =new BigInteger( "280375465082880"); + static final BigInteger RANK_4_BITBOARD =new BigInteger( "1095216660480"); + static final BigInteger RANK_5_BITBOARD =new BigInteger( "4278190080"); + static final BigInteger RANK_6_BITBOARD =new BigInteger( "16711680"); + static final BigInteger RANK_7_BITBOARD =new BigInteger( "65280"); + static final BigInteger RANK_8_BITBOARD =new BigInteger( "255"); + + static final BigInteger MAX_long = new BigInteger("18446744073709551615"); + + static boolean Is_Square_Attacked_By_Black_Global(int square, long occupancy) + { + if ((Board.bitboard_array_global[GenConst.BP] & MoveConstants.WHITE_PAWN_ATTACKS[square]) != 0) + { + return true; + } + if ((Board.bitboard_array_global[GenConst.BN] & MoveConstants.KNIGHT_ATTACKS[square]) != 0) + { + return true; + } + if ((Board.bitboard_array_global[GenConst.BK] & MoveConstants.KING_ATTACKS[square]) != 0) + { + return true; + } + long bishopAttacks = MoveUtils.getBishopMovesSeparate(occupancy, square); + if ((Board.bitboard_array_global[GenConst.BB] & bishopAttacks) != 0) + { + return true; + } + if ((Board.bitboard_array_global[GenConst.BQ] & bishopAttacks) != 0) + { + return true; + } + long rookAttacks = MoveUtils.getRookMovesSeparate(occupancy, square); + if ((Board.bitboard_array_global[GenConst.BR] & rookAttacks) != 0) + { + return true; + } + if ((Board.bitboard_array_global[GenConst.BQ] & rookAttacks) != 0) + { + return true; + } + return false; + } + + static boolean Is_Square_Attacked_By_White_Global(int square, long occupancy) + { + if ((Board.bitboard_array_global[GenConst.WP] & MoveConstants.BLACK_PAWN_ATTACKS[square]) != 0) + { + return true; + } + if ((Board.bitboard_array_global[GenConst.WN] & MoveConstants.KNIGHT_ATTACKS[square]) != 0) + { + return true; + } + if ((Board.bitboard_array_global[GenConst.WK] & MoveConstants.KING_ATTACKS[square]) != 0) + { + return true; + } + long bishopAttacks = MoveUtils.getBishopMovesSeparate(occupancy, square); + if ((Board.bitboard_array_global[GenConst.WB] & bishopAttacks) != 0) + { + return true; + } + if ((Board.bitboard_array_global[GenConst.WQ] & bishopAttacks) != 0) + { + return true; + } + long rookAttacks = MoveUtils.getRookMovesSeparate(occupancy, square); + if ((Board.bitboard_array_global[GenConst.WR] & rookAttacks) != 0) + { + return true; + } + if ((Board.bitboard_array_global[GenConst.WQ] & rookAttacks) != 0) + { + return true; + } + return false; + } + + + static final long MAGIC = 285870213051386505L; + + static final int[] DEBRUIJN64 = { + 0, 47, 1, 56, 48, 27, 2, 60, + 57, 49, 41, 37, 28, 16, 3, 61, + 54, 58, 35, 52, 50, 42, 21, 44, + 38, 32, 29, 23, 17, 11, 4, 62, + 46, 55, 26, 59, 40, 36, 15, 53, + 34, 51, 20, 43, 31, 22, 10, 45, + 25, 39, 14, 33, 19, 30, 9, 24, + 13, 18, 8, 12, 7, 6, 5, 63 + }; + + + static int bitScanForward(long tempBitboard) + { + long debruijnIndex = ((MAGIC * (tempBitboard ^ (tempBitboard - 1))) >>> 58); + //PrintBitboard(debruijnIndex); + int indexAsInteger = (int)debruijnIndex; + return (DEBRUIJN64[indexAsInteger]); + } + + + static boolean isNotZero(BigInteger bitboard) { + return bitboard.signum() != 0; + } + + static boolean isZero(BigInteger bitboard) { + return bitboard.signum() == 0; + } + + static BigInteger removeBit(BigInteger bitboard) { + return bitboard.and(bitboard.subtract(BigInteger.ONE)); + } + + static void printMoveInfo(int startingSquare, int targetSquare, int tag, int piece) + { + Pr.println("\n Move Info:"); + Pr.print(" Starting Square: "); + Pr.printInt(startingSquare); + Pr.print(" "); + Pr.printSquareLn(startingSquare); + Pr.print(" Target Square: "); + Pr.printInt(targetSquare); + Pr.print(" "); + Pr.printSquareLn(targetSquare); + Pr.print(" Tag: "); + Pr.printIntLn(tag); + Pr.print(" Piece: "); + Pr.printIntLn(piece); + } + + static ErrorInt errorIntFromValue(int value) + { + return new ErrorInt(null, value); + } + + static ErrorInt errorIntFromError(Error error) + { + return new ErrorInt(error, 0); + } + + static long CombineBitboardsGlobal() + { + return Board.bitboard_array_global[0] | + Board.bitboard_array_global[1] | + Board.bitboard_array_global[2] | + Board.bitboard_array_global[3] | + Board.bitboard_array_global[4] | + Board.bitboard_array_global[5] | + Board.bitboard_array_global[6] | + Board.bitboard_array_global[7] | + Board.bitboard_array_global[8] | + Board.bitboard_array_global[9] | + Board.bitboard_array_global[10] | + Board.bitboard_array_global[11]; + } + + static void PrintAllDebug(int startingSquare, int targetSquare, int piece, int tag) + { + PrintPiece(piece); + PrintMoveNoNL(startingSquare, targetSquare, tag); + System.out.println(); + PrintBoardGlobal(); + } + + + static final char[] SQ_CHAR_Y = { + '8','8','8','8','8','8','8','8', + '7','7','7','7','7','7','7','7', + '6','6','6','6','6','6','6','6', + '5','5','5','5','5','5','5','5', + '4','4','4','4','4','4','4','4', + '3','3','3','3','3','3','3','3', + '2','2','2','2','2','2','2','2', + '1','1','1','1','1','1','1','1','A' + }; + + static final char[] SQ_CHAR_X = { + 'a','b','c','d','e','f','g','h', + 'a','b','c','d','e','f','g','h', + 'a','b','c','d','e','f','g','h', + 'a','b','c','d','e','f','g','h', + 'a','b','c','d','e','f','g','h', + 'a','b','c','d','e','f','g','h', + 'a','b','c','d','e','f','g','h', + 'a','b','c','d','e','f','g','h','N' + }; + + + static void PrintBoardGlobal() + { + char[] yCoordinates = { '8', '7', '6', '5', '4', '3', '2', '1' }; + char[] castleChars = { 'K', 'Q', 'k', 'q' }; + + for (int y = 0; y < 8; y++) + { + System.out.print(String.format(" %c ", yCoordinates[y])); + for (int x = 0; x < 8; x++) + { + int square = (y * 8) + x; + boolean pieceFound = false; + + for (int pieceIndex = 0; pieceIndex < 12; pieceIndex++) + { + //Pr.print("piece index: "); Pr.printIntLn(pieceIndex); + if ((Board.bitboard_array_global[pieceIndex] & MoveConstants.SQUARE_BBS[square]) != 0) + { + // Pr.print("not zero, anded together:"); + //PrintBitboard(Board.bitboard_array_global[pieceIndex] & MoveConstants.SQUARE_BBS[square]); + System.out.print(PIECE_COLOURS[pieceIndex]); + System.out.print(PIECE_CHARS[pieceIndex]); + System.out.print(' '); + pieceFound = true; + break; + } + } + + if (pieceFound == false) + { + System.out.print("-- "); + } + } + System.out.print('\n'); + } + System.out.println(" A B C D E F G H\n\n"); + if (Board.is_white_global == true) + { + System.out.println("Side: White To Play\n"); + } + else + { + System.out.println("Side: Black To Play\n"); + } + System.out.print("castle: "); + for (int i = 0; i < 4; i++) + { + if (Board.castle_rights_global[i] == true) + { + System.out.print(castleChars[i]); + } + else + { + System.out.print("-"); + } + } + System.out.print("\nEP: "); + if (Board.ep_global > 63 || Board.ep_global < 0) + { + System.out.println(Board.ep_global); + } + else + { + System.out.println(SQ_CHAR_X[Board.ep_global]); + System.out.println(SQ_CHAR_Y[Board.ep_global]); + } + + System.out.print("\n\n"); + } + + static final char[] PIECE_COLOURS = { 'W','W','W','W','W','W','b','b','b','b','b','b' }; + static final char[] PIECE_CHARS = { 'P','N','B','R','Q','K','p','n','b','r','q','k' }; + + static void PrintPiece(int piece) + { + System.out.print(PIECE_COLOURS[piece]); + System.out.print(PIECE_CHARS[piece]); + } + static void PrintPieceLn(int piece) + { + System.out.print(PIECE_COLOURS[piece]); + System.out.println(PIECE_CHARS[piece]); + } + + + enum CaptureType + { + None, White, Black + } + + private static int FindCaptureBlack(int targetSquare, int captureIndex) + { + for (int i = GenConst.BP; i <= GenConst.BK; ++i) + { + if ((Board.bitboard_array_global[i] & MoveConstants.SQUARE_BBS[targetSquare]) != 0) + { + captureIndex = i; + break; + } + } + Board.bitboard_array_global[captureIndex] &= ~MoveConstants.SQUARE_BBS[targetSquare]; + return captureIndex; + } + + private static void MovePiecePromote(int startingSquare, int targetSquare, int piece, int promotionPiece) + { + Board.bitboard_array_global[promotionPiece] |= MoveConstants.SQUARE_BBS[targetSquare]; + Board.bitboard_array_global[piece] &= ~MoveConstants.SQUARE_BBS[startingSquare]; + } + + private static void RemovePiece(int piece, int targetSquare) + { + Board.bitboard_array_global[piece] &= ~MoveConstants.SQUARE_BBS[targetSquare]; + } + + private static void MovePiece(int startingSquare, int targetSquare, int piece) + { + Board.bitboard_array_global[piece] |= MoveConstants.SQUARE_BBS[targetSquare]; + Board.bitboard_array_global[piece] &= ~MoveConstants.SQUARE_BBS[startingSquare]; + } + + private static int FindCaptureWhite(int targetSquareCopy, int captureIndex) + { + for (int i = GenConst.WP; i <= GenConst.WK; ++i) + { + if ((Board.bitboard_array_global[i] & MoveConstants.SQUARE_BBS[targetSquareCopy]) != 0) + { + captureIndex = i; + break; + } + } + Board.bitboard_array_global[captureIndex] &= ~MoveConstants.SQUARE_BBS[targetSquareCopy]; + return captureIndex; + } + + private static void UnmakeMove(int startingSquareCopy, int targetSquareCopy, int piece, int tag, int captureIndex) + { + Board.is_white_global = !Board.is_white_global; + switch (tag) + { + case 0: //none + case 26: //check + Board.bitboard_array_global[piece] |= MoveConstants.SQUARE_BBS[startingSquareCopy]; + Board.bitboard_array_global[piece] &= ~MoveConstants.SQUARE_BBS[targetSquareCopy]; + + break; + case 1: //capture + case 27: //check cap + Board.bitboard_array_global[piece] |= MoveConstants.SQUARE_BBS[startingSquareCopy]; + Board.bitboard_array_global[piece] &= ~MoveConstants.SQUARE_BBS[targetSquareCopy]; + if (piece >= GenConst.WP && piece <= GenConst.WK) + { + Board.bitboard_array_global[captureIndex] |= MoveConstants.SQUARE_BBS[targetSquareCopy]; + } + else //is black + { + Board.bitboard_array_global[captureIndex] |= MoveConstants.SQUARE_BBS[targetSquareCopy]; + } + + break; + case 2: //white ep + Board.bitboard_array_global[GenConst.WP] |= MoveConstants.SQUARE_BBS[startingSquareCopy]; + Board.bitboard_array_global[GenConst.WP] &= ~MoveConstants.SQUARE_BBS[targetSquareCopy]; + Board.bitboard_array_global[GenConst.BP] |= MoveConstants.SQUARE_BBS[targetSquareCopy + 8]; + + break; + case 3: //black ep + Board.bitboard_array_global[GenConst.BP] |= MoveConstants.SQUARE_BBS[startingSquareCopy]; + Board.bitboard_array_global[GenConst.BP] &= ~MoveConstants.SQUARE_BBS[targetSquareCopy]; + Board.bitboard_array_global[GenConst.WP] |= MoveConstants.SQUARE_BBS[targetSquareCopy - 8]; + + break; + case 4: //WKS + //white king + Board.bitboard_array_global[GenConst.WK] |= MoveConstants.SQUARE_BBS[GenConst.E1]; + Board.bitboard_array_global[GenConst.WK] &= ~MoveConstants.SQUARE_BBS[GenConst.G1]; + //white rook + Board.bitboard_array_global[GenConst.WR] |= MoveConstants.SQUARE_BBS[GenConst.H1]; + Board.bitboard_array_global[GenConst.WR] &= ~MoveConstants.SQUARE_BBS[GenConst.F1]; + break; + case 5: //WQS + //white king + Board.bitboard_array_global[GenConst.WK] |= MoveConstants.SQUARE_BBS[GenConst.E1]; + Board.bitboard_array_global[GenConst.WK] &= ~MoveConstants.SQUARE_BBS[GenConst.C1]; + //white rook + Board.bitboard_array_global[GenConst.WR] |= MoveConstants.SQUARE_BBS[GenConst.A1]; + Board.bitboard_array_global[GenConst.WR] &= ~MoveConstants.SQUARE_BBS[GenConst.D1]; + break; + case 6: //BKS + //white king + Board.bitboard_array_global[GenConst.BK] |= MoveConstants.SQUARE_BBS[GenConst.E8]; + Board.bitboard_array_global[GenConst.BK] &= ~MoveConstants.SQUARE_BBS[GenConst.G8]; + //white rook + Board.bitboard_array_global[GenConst.BR] |= MoveConstants.SQUARE_BBS[GenConst.H8]; + Board.bitboard_array_global[GenConst.BR] &= ~MoveConstants.SQUARE_BBS[GenConst.F8]; + break; + case 7: //BQS + //white king + Board.bitboard_array_global[GenConst.BK] |= MoveConstants.SQUARE_BBS[GenConst.E8]; + Board.bitboard_array_global[GenConst.BK] &= ~MoveConstants.SQUARE_BBS[GenConst.C8]; + //white rook + Board.bitboard_array_global[GenConst.BR] |= MoveConstants.SQUARE_BBS[GenConst.A8]; + Board.bitboard_array_global[GenConst.BR] &= ~MoveConstants.SQUARE_BBS[GenConst.D8]; + + break; + + //#region Promotion Unmakemove + case 8: //BNPr + Board.bitboard_array_global[GenConst.BP] |= MoveConstants.SQUARE_BBS[startingSquareCopy]; + Board.bitboard_array_global[GenConst.BN] &= ~MoveConstants.SQUARE_BBS[targetSquareCopy]; + break; + case 9: //BBPr + Board.bitboard_array_global[GenConst.BP] |= MoveConstants.SQUARE_BBS[startingSquareCopy]; + Board.bitboard_array_global[GenConst.BB] &= ~MoveConstants.SQUARE_BBS[targetSquareCopy]; + break; + case 10: //BQPr + Board.bitboard_array_global[GenConst.BP] |= MoveConstants.SQUARE_BBS[startingSquareCopy]; + Board.bitboard_array_global[GenConst.BQ] &= ~MoveConstants.SQUARE_BBS[targetSquareCopy]; + break; + case 11: //BRPr + Board.bitboard_array_global[GenConst.BP] |= MoveConstants.SQUARE_BBS[startingSquareCopy]; + Board.bitboard_array_global[GenConst.BR] &= ~MoveConstants.SQUARE_BBS[targetSquareCopy]; + break; + case 12: //WNPr + Board.bitboard_array_global[GenConst.WP] |= MoveConstants.SQUARE_BBS[startingSquareCopy]; + Board.bitboard_array_global[GenConst.WN] &= ~MoveConstants.SQUARE_BBS[targetSquareCopy]; + break; + case 13: //WBPr + Board.bitboard_array_global[GenConst.WP] |= MoveConstants.SQUARE_BBS[startingSquareCopy]; + Board.bitboard_array_global[GenConst.WB] &= ~MoveConstants.SQUARE_BBS[targetSquareCopy]; + break; + case 14: //WQPr + Board.bitboard_array_global[GenConst.WP] |= MoveConstants.SQUARE_BBS[startingSquareCopy]; + Board.bitboard_array_global[GenConst.WQ] &= ~MoveConstants.SQUARE_BBS[targetSquareCopy]; + break; + case 15: //WRPr + Board.bitboard_array_global[GenConst.WP] |= MoveConstants.SQUARE_BBS[startingSquareCopy]; + Board.bitboard_array_global[GenConst.WR] &= ~MoveConstants.SQUARE_BBS[targetSquareCopy]; + break; + case 16: //BNPrCAP + Board.bitboard_array_global[GenConst.BP] |= MoveConstants.SQUARE_BBS[startingSquareCopy]; + Board.bitboard_array_global[GenConst.BN] &= ~MoveConstants.SQUARE_BBS[targetSquareCopy]; + + Board.bitboard_array_global[captureIndex] |= MoveConstants.SQUARE_BBS[targetSquareCopy]; + break; + case 17: //BBPrCAP + Board.bitboard_array_global[GenConst.BP] |= MoveConstants.SQUARE_BBS[startingSquareCopy]; + Board.bitboard_array_global[GenConst.BB] &= ~MoveConstants.SQUARE_BBS[targetSquareCopy]; + + Board.bitboard_array_global[captureIndex] |= MoveConstants.SQUARE_BBS[targetSquareCopy]; + + break; + case 18: //BQPrCAP + Board.bitboard_array_global[GenConst.BP] |= MoveConstants.SQUARE_BBS[startingSquareCopy]; + Board.bitboard_array_global[GenConst.BQ] &= ~MoveConstants.SQUARE_BBS[targetSquareCopy]; + + Board.bitboard_array_global[captureIndex] |= MoveConstants.SQUARE_BBS[targetSquareCopy]; + break; + case 19: //BRPrCAP + Board.bitboard_array_global[GenConst.BP] |= MoveConstants.SQUARE_BBS[startingSquareCopy]; + Board.bitboard_array_global[GenConst.BR] &= ~MoveConstants.SQUARE_BBS[targetSquareCopy]; + + Board.bitboard_array_global[captureIndex] |= MoveConstants.SQUARE_BBS[targetSquareCopy]; + break; + case 20: //WNPrCAP + Board.bitboard_array_global[GenConst.WP] |= MoveConstants.SQUARE_BBS[startingSquareCopy]; + Board.bitboard_array_global[GenConst.WN] &= ~MoveConstants.SQUARE_BBS[targetSquareCopy]; + + Board.bitboard_array_global[captureIndex] |= MoveConstants.SQUARE_BBS[targetSquareCopy]; + break; + case 21: //WBPrCAP + Board.bitboard_array_global[GenConst.WP] |= MoveConstants.SQUARE_BBS[startingSquareCopy]; + Board.bitboard_array_global[GenConst.WB] &= ~MoveConstants.SQUARE_BBS[targetSquareCopy]; + + Board.bitboard_array_global[captureIndex] |= MoveConstants.SQUARE_BBS[targetSquareCopy]; + break; + case 22: //WQPrCAP + Board.bitboard_array_global[GenConst.WP] |= MoveConstants.SQUARE_BBS[startingSquareCopy]; + Board.bitboard_array_global[GenConst.WQ] &= ~MoveConstants.SQUARE_BBS[targetSquareCopy]; + + Board.bitboard_array_global[captureIndex] |= MoveConstants.SQUARE_BBS[targetSquareCopy]; + break; + case 23: //WRPrCAP + Board.bitboard_array_global[GenConst.WP] |= MoveConstants.SQUARE_BBS[startingSquareCopy]; + Board.bitboard_array_global[GenConst.WR] &= ~MoveConstants.SQUARE_BBS[targetSquareCopy]; + + Board.bitboard_array_global[captureIndex] |= MoveConstants.SQUARE_BBS[targetSquareCopy]; + break; + + //#endregion + + case 24: //WDouble + Board.bitboard_array_global[GenConst.WP] |= MoveConstants.SQUARE_BBS[startingSquareCopy]; + Board.bitboard_array_global[GenConst.WP] &= ~MoveConstants.SQUARE_BBS[targetSquareCopy]; + break; + case 25: //BDouble + Board.bitboard_array_global[GenConst.BP] |= MoveConstants.SQUARE_BBS[startingSquareCopy]; + Board.bitboard_array_global[GenConst.BP] &= ~MoveConstants.SQUARE_BBS[targetSquareCopy]; + break; + } + } + + + public static void PrintBitboard(long input) + { + if (input == 0) + { + System.out.print(String.format(" bitboard: %d", input)); + return; + } + for (int y = 0; y < 8; y++) + { + System.out.print(" "); + for (int x = 0; x < 8; x++) + { + int square = (y * 8) + x; + if ((input & MoveConstants.SQUARE_BBS[square]) != 0) + { + System.out.print("X "); + } + else + { + System.out.print("- "); + } + } + System.out.println(); + } + + System.out.println(String.format("\nbitboard: %d", input)); + } + + + + public static void PrintAllBitboards() + { + for (int i = 0; i < 12; i++) + { + PrintBitboard(Board.bitboard_array_global[i]); + } + } + + + static int MakeMove(int startingSquare, int targetSquare, int tag, int piece) + { + int captureIndex = -1; + + Board.is_white_global = !Board.is_white_global; + Board.ep_global = NO_SQUARE; + CaptureType captureType = CaptureType.None; + + switch (tag) + { + case GenConst.TAG_NONE: //none + case GenConst.TAG_CHECK: //check + MovePiece(startingSquare, targetSquare, piece); + break; + case GenConst.TAG_CHECK_CAP: //capture + case GenConst.TAG_CAPTURE: //check cap + MovePiece(startingSquare, targetSquare, piece); + if (Board.is_white_global == true) { + captureType = CaptureType.White; + } + else { + captureType = CaptureType.Black; + } + break; + case GenConst.TAG_WHITE_EP: //white ep + MovePiece(startingSquare, targetSquare, GenConst.WP); + RemovePiece(GenConst.BP, targetSquare + 8); + break; + case GenConst.TAG_BLACK_EP: + MovePiece(startingSquare, targetSquare, GenConst.BP); + RemovePiece(GenConst.WP, targetSquare - 8); + break; + + //#region Castling + + case GenConst.TAG_W_CASTLE_KS: + MovePiece(GenConst.E1, GenConst.G1, GenConst.WK); + MovePiece(GenConst.H1, GenConst.F1, GenConst.WR); + Board.castle_rights_global[WKS_CASTLE_RIGHTS] = false; + Board.castle_rights_global[WQS_CASTLE_RIGHTS] = false; + break; + case GenConst.TAG_W_CASTLE_QS: + MovePiece(GenConst.E1, GenConst.C1, GenConst.WK); + MovePiece(GenConst.A1, GenConst.D1, GenConst.WR); + Board.castle_rights_global[WKS_CASTLE_RIGHTS] = false; + Board.castle_rights_global[WQS_CASTLE_RIGHTS] = false; + break; + case GenConst.TAG_B_CASTLE_KS: + MovePiece(GenConst.E8, GenConst.G8, GenConst.BK); + MovePiece(GenConst.H8, GenConst.F8, GenConst.BR); + Board.castle_rights_global[BKS_CASTLE_RIGHTS] = false; + Board.castle_rights_global[BQS_CASTLE_RIGHTS] = false; + break; + case GenConst.TAG_B_CASTLE_QS: //BQS + MovePiece(GenConst.E8, GenConst.C8, GenConst.BK); + MovePiece(GenConst.A8, GenConst.D8, GenConst.BR); + Board.castle_rights_global[BKS_CASTLE_RIGHTS] = false; + Board.castle_rights_global[BQS_CASTLE_RIGHTS] = false; + break; + + //#endregion + + //#region Promotion makemove + + case GenConst.TAG_B_N_PROMOTION: + MovePiecePromote(startingSquare, targetSquare, piece, GenConst.BN); + break; + case GenConst.TAG_B_B_PROMOTION: + MovePiecePromote(startingSquare, targetSquare, piece, GenConst.BB); + break; + case GenConst.TAG_B_Q_PROMOTION: + MovePiecePromote(startingSquare, targetSquare, piece, GenConst.BQ); + break; + case GenConst.TAG_B_R_PROMOTION: + MovePiecePromote(startingSquare, targetSquare, piece, GenConst.BR); + break; + case GenConst.TAG_W_N_PROMOTION: + MovePiecePromote(startingSquare, targetSquare, piece, GenConst.WN); + break; + case GenConst.TAG_W_B_PROMOTION: + MovePiecePromote(startingSquare, targetSquare, piece, GenConst.WB); + break; + case GenConst.TAG_W_Q_PROMOTION: + MovePiecePromote(startingSquare, targetSquare, piece, GenConst.WQ); + break; + case GenConst.TAG_W_R_PROMOTION: + MovePiecePromote(startingSquare, targetSquare, piece, GenConst.WR); + break; + case GenConst.TAG_B_N_PROMOTION_CAP: + MovePiecePromote(startingSquare, targetSquare, piece, GenConst.BN); + captureType = CaptureType.White; + break; + case GenConst.TAG_B_B_PROMOTION_CAP: + MovePiecePromote(startingSquare, targetSquare, piece, GenConst.BB); + captureType = CaptureType.White; + break; + case GenConst.TAG_B_Q_PROMOTION_CAP: + MovePiecePromote(startingSquare, targetSquare, piece, GenConst.BQ); + captureType = CaptureType.White; + break; + case GenConst.TAG_B_R_PROMOTION_CAP: + MovePiecePromote(startingSquare, targetSquare, piece, GenConst.BR); + captureType = CaptureType.White; + break; + case GenConst.TAG_W_N_PROMOTION_CAP: + MovePiecePromote(startingSquare, targetSquare, piece, GenConst.WN); + captureType = CaptureType.Black; + break; + case GenConst.TAG_W_B_PROMOTION_CAP: + MovePiecePromote(startingSquare, targetSquare, piece, GenConst.WB); + captureType = CaptureType.Black; + break; + case GenConst.TAG_W_Q_PROMOTION_CAP: + MovePiecePromote(startingSquare, targetSquare, piece, GenConst.WQ); + captureType = CaptureType.Black; + break; + case GenConst.TAG_W_R_PROMOTION_CAP: + MovePiecePromote(startingSquare, targetSquare, piece, GenConst.WR); + captureType = CaptureType.Black; + break; + //#endregion + + case GenConst.TAG_W_P_DOUBLE: + MovePiece(startingSquare, targetSquare, GenConst.WP); + Board.ep_global = targetSquare + 8; + break; + case GenConst.TAG_B_P_DOUBLE: + MovePiece(startingSquare, targetSquare, GenConst.BP); + Board.ep_global = targetSquare - 8; + break; + } + + switch (captureType) + { + case CaptureType.None: + break; + case CaptureType.White: + captureIndex = FindCaptureWhite(targetSquare, captureIndex); + break; + case CaptureType.Black: + captureIndex = FindCaptureBlack(targetSquare, captureIndex); + break; + } + + if (piece == GenConst.WK) + { + Board.castle_rights_global[WKS_CASTLE_RIGHTS] = false; + Board.castle_rights_global[WQS_CASTLE_RIGHTS] = false; + } + else if (piece == GenConst.BK) + { + Board.castle_rights_global[BKS_CASTLE_RIGHTS] = false; + Board.castle_rights_global[BQS_CASTLE_RIGHTS] = false; + } + else if (piece == GenConst.WR) + { + if (Board.castle_rights_global[WKS_CASTLE_RIGHTS] == true) + { + if ((Board.bitboard_array_global[GenConst.WR] & MoveConstants.SQUARE_BBS[GenConst.H1]) == 0) + { + Board.castle_rights_global[WKS_CASTLE_RIGHTS] = false; + } + } + if (Board.castle_rights_global[WQS_CASTLE_RIGHTS] == true) + { + if ((Board.bitboard_array_global[GenConst.WR] & MoveConstants.SQUARE_BBS[GenConst.A1]) == 0) + { + Board.castle_rights_global[WQS_CASTLE_RIGHTS] = false; + } + } + } + else if (piece == GenConst.BR) + { + if (Board.castle_rights_global[BKS_CASTLE_RIGHTS] == true) + { + if ((Board.bitboard_array_global[GenConst.BR] & MoveConstants.SQUARE_BBS[GenConst.H8]) == 0) + { + Board.castle_rights_global[BKS_CASTLE_RIGHTS] = false; + } + } + if (Board.castle_rights_global[BQS_CASTLE_RIGHTS] == true) + { + if ((Board.bitboard_array_global[GenConst.BR] & MoveConstants.SQUARE_BBS[GenConst.A8]) == 0) + { + Board.castle_rights_global[BQS_CASTLE_RIGHTS] = false; + } + } + } + + return captureIndex; + } + + + static int getMoves(int[] startingSquares, int[] targetSquares, int[] tags, int[] pieces) + { + int moveCount = 0; + + //Move generating variables + long WHITE_OCCUPANCIES_LOCAL = Board.bitboard_array_global[0] | Board.bitboard_array_global[1] | Board.bitboard_array_global[2] | Board.bitboard_array_global[3] | Board.bitboard_array_global[4] | Board.bitboard_array_global[5]; + long BLACK_OCCUPANCIES_LOCAL = Board.bitboard_array_global[6] | Board.bitboard_array_global[7] | Board.bitboard_array_global[8] | Board.bitboard_array_global[9] | Board.bitboard_array_global[10] | Board.bitboard_array_global[11]; + long COMBINED_OCCUPANCIES_LOCAL = WHITE_OCCUPANCIES_LOCAL | BLACK_OCCUPANCIES_LOCAL; + long EMPTY_OCCUPANCIES = ~COMBINED_OCCUPANCIES_LOCAL; + long tempBitboard; + long checkBitboard = 0L; + long tempPinBitboard, tempAttack, tempEmpty, tempCaptures; + int startingSquare = NO_SQUARE, targetSquare = NO_SQUARE; + int kingPosition; + int checkCount; + long enemy_occupancies; + int bishop_index; + int rook_index; + int queen_index; + int knight_index; + + int[] pinArrayPiece = { + -1,-1,-1,-1,-1,-1,-1,-1 + }; + int[] pinArraySquare = { + -1,-1,-1,-1,-1,-1,-1,-1 + }; + + int pinNumber = 0; + + if (Board.is_white_global == true) + { + checkCount = 0; + kingPosition = bitScanForward(Board.bitboard_array_global[GenConst.WK]); + + rook_index = GenConst.WR; + queen_index = GenConst.WQ; + knight_index = GenConst.WN; + bishop_index = GenConst.WB; + + enemy_occupancies = BLACK_OCCUPANCIES_LOCAL; + + //#region white pins and check + + //pawns + tempBitboard = Board.bitboard_array_global[GenConst.BP] & MoveConstants.WHITE_PAWN_ATTACKS[kingPosition]; + if (tempBitboard != 0) + { + int pawn_square = (DEBRUIJN64[(int)((MAGIC * (tempBitboard ^ (tempBitboard - 1))) >>> 58)]); + + checkBitboard |= MoveConstants.SQUARE_BBS[pawn_square]; + + checkCount++; + } + + //knights + tempBitboard = Board.bitboard_array_global[GenConst.BN] & MoveConstants.KNIGHT_ATTACKS[kingPosition]; + if (tempBitboard != 0) + { + int knight_square = DEBRUIJN64[(int)((MAGIC * (tempBitboard ^ (tempBitboard - 1))) >>> 58)]; + + checkBitboard |= MoveConstants.SQUARE_BBS[knight_square]; + + checkCount++; + } + + //bishops + long bishopAttacksChecks = MoveUtils.getBishopMovesSeparate(BLACK_OCCUPANCIES_LOCAL, kingPosition); + tempBitboard = Board.bitboard_array_global[GenConst.BB] & bishopAttacksChecks; + while (tempBitboard != 0) + { + int piece_square = DEBRUIJN64[(int)((MAGIC * (tempBitboard ^ (tempBitboard - 1))) >>> 58)]; + + tempPinBitboard = Inb.INBETWEEN_BITBOARDS[kingPosition][piece_square] & WHITE_OCCUPANCIES_LOCAL; + + if (tempPinBitboard == 0) + { + + checkBitboard |= Inb.INBETWEEN_BITBOARDS[kingPosition][piece_square]; + + checkCount++; + } + else + { + int pinned_square = (DEBRUIJN64[(int)((MAGIC * (tempPinBitboard ^ (tempPinBitboard - 1))) >>> 58)]); + tempPinBitboard &= tempPinBitboard - 1; + + if (tempPinBitboard == 0) + { + pinArraySquare[pinNumber] = pinned_square; + pinArrayPiece[pinNumber] = piece_square; + pinNumber++; + } + } + tempBitboard &= tempBitboard - 1; + } + + //queen + tempBitboard = Board.bitboard_array_global[GenConst.BQ] & bishopAttacksChecks; + while (tempBitboard != 0) + { + int piece_square = DEBRUIJN64[(int)((MAGIC * (tempBitboard ^ (tempBitboard - 1))) >>> 58)]; + + tempPinBitboard = Inb.INBETWEEN_BITBOARDS[kingPosition][piece_square] & WHITE_OCCUPANCIES_LOCAL; + + if (tempPinBitboard == 0) + { + + checkBitboard |= Inb.INBETWEEN_BITBOARDS[kingPosition][piece_square]; + + checkCount++; + } + else + { + int pinned_square = (DEBRUIJN64[(int)((MAGIC * (tempPinBitboard ^ (tempPinBitboard - 1))) >>> 58)]); + tempPinBitboard &= tempPinBitboard - 1; + + if (tempPinBitboard == 0) + { + pinArraySquare[pinNumber] = pinned_square; + pinArrayPiece[pinNumber] = piece_square; + pinNumber++; + } + } + tempBitboard &= tempBitboard - 1; + } + + //rook + long rook_attacks = MoveUtils.getRookMovesSeparate(BLACK_OCCUPANCIES_LOCAL, kingPosition); + tempBitboard = Board.bitboard_array_global[GenConst.BR] & rook_attacks; + while (tempBitboard != 0) + { + int piece_square = DEBRUIJN64[(int)((MAGIC * (tempBitboard ^ (tempBitboard - 1))) >>> 58)]; + + tempPinBitboard = Inb.INBETWEEN_BITBOARDS[kingPosition][piece_square] & WHITE_OCCUPANCIES_LOCAL; + + if (tempPinBitboard == 0) + { + + checkBitboard |= Inb.INBETWEEN_BITBOARDS[kingPosition][piece_square]; + + checkCount++; + } + else + { + int pinned_square = (DEBRUIJN64[(int)((MAGIC * (tempPinBitboard ^ (tempPinBitboard - 1))) >>> 58)]); + tempPinBitboard &= tempPinBitboard - 1; + + if (tempPinBitboard == 0) + { + pinArraySquare[pinNumber] = pinned_square; + pinArrayPiece[pinNumber] = piece_square; + pinNumber++; + } + } + tempBitboard &= tempBitboard - 1; + } + + //queen + tempBitboard = Board.bitboard_array_global[GenConst.BQ] & rook_attacks; + while (tempBitboard != 0) + { + int piece_square = (DEBRUIJN64[(int)((MAGIC * (tempBitboard ^ (tempBitboard - 1))) >>> 58)]); + + tempPinBitboard = Inb.INBETWEEN_BITBOARDS[kingPosition][piece_square] & WHITE_OCCUPANCIES_LOCAL; + + if (tempPinBitboard == 0) + { + checkBitboard |= Inb.INBETWEEN_BITBOARDS[kingPosition][piece_square]; + + checkCount++; + } + else + { + int pinned_square = (DEBRUIJN64[(int)((MAGIC * (tempPinBitboard ^ (tempPinBitboard - 1))) >>> 58)]); + tempPinBitboard &= tempPinBitboard - 1; + + if (tempPinBitboard == 0) + { + pinArraySquare[pinNumber] = pinned_square; + pinArrayPiece[pinNumber] = piece_square; + pinNumber++; + } + } + tempBitboard &= tempBitboard - 1; + } + + //#endregion + + //#region White king + + long occupanciesWithoutWhiteKing = COMBINED_OCCUPANCIES_LOCAL & (~Board.bitboard_array_global[GenConst.WK]); + tempAttack = MoveConstants.KING_ATTACKS[kingPosition]; + tempEmpty = tempAttack & EMPTY_OCCUPANCIES; + while (tempEmpty != 0) + { + targetSquare = bitScanForward(tempEmpty); + tempEmpty &= tempEmpty - 1; + + if ((Board.bitboard_array_global[GenConst.BP] & MoveConstants.WHITE_PAWN_ATTACKS[targetSquare]) != 0) + { + continue; + } + if ((Board.bitboard_array_global[GenConst.BN] & MoveConstants.KNIGHT_ATTACKS[targetSquare]) != 0) + { + continue; + } + if ((Board.bitboard_array_global[GenConst.BK] & MoveConstants.KING_ATTACKS[targetSquare]) != 0) + { + continue; + } + long bishopAttacks = MoveUtils.getBishopMovesSeparate(occupanciesWithoutWhiteKing, targetSquare); + if ((Board.bitboard_array_global[GenConst.BB] & bishopAttacks) != 0) + { + continue; + } + if ((Board.bitboard_array_global[GenConst.BQ] & bishopAttacks) != 0) + { + continue; + } + long rookAttacks = MoveUtils.getRookMovesSeparate(occupanciesWithoutWhiteKing, targetSquare); + if ((Board.bitboard_array_global[GenConst.BR] & rookAttacks) != 0) + { + continue; + } + if ((Board.bitboard_array_global[GenConst.BQ] & rookAttacks) != 0) + { + continue; + } + + startingSquares[moveCount] = kingPosition; + targetSquares[moveCount] = targetSquare; + tags[moveCount] = GenConst.TAG_NONE; + pieces[moveCount] = GenConst.WK; + moveCount++; + } + + //captures + tempCaptures = tempAttack & BLACK_OCCUPANCIES_LOCAL; + while (tempCaptures != 0) + { + targetSquare = bitScanForward(tempCaptures); + tempCaptures &= tempCaptures - 1; + + if ((Board.bitboard_array_global[GenConst.BP] & MoveConstants.WHITE_PAWN_ATTACKS[targetSquare]) != 0) + { + continue; + } + if ((Board.bitboard_array_global[GenConst.BN] & MoveConstants.KNIGHT_ATTACKS[targetSquare]) != 0) + { + continue; + } + if ((Board.bitboard_array_global[GenConst.BK] & MoveConstants.KING_ATTACKS[targetSquare]) != 0) + { + continue; + } + long bishopAttacks = MoveUtils.getBishopMovesSeparate(occupanciesWithoutWhiteKing, targetSquare); + if ((Board.bitboard_array_global[GenConst.BB] & bishopAttacks) != 0) + { + continue; + } + if ((Board.bitboard_array_global[GenConst.BQ] & bishopAttacks) != 0) + { + continue; + } + long rookAttacks = MoveUtils.getRookMovesSeparate(occupanciesWithoutWhiteKing, targetSquare); + if ((Board.bitboard_array_global[GenConst.BR] & rookAttacks) != 0) + { + continue; + } + if ((Board.bitboard_array_global[GenConst.BQ] & rookAttacks) != 0) + { + continue; + } + + startingSquares[moveCount] = kingPosition; + targetSquares[moveCount] = targetSquare; + tags[moveCount] = GenConst.TAG_CAPTURE; + pieces[moveCount] = GenConst.WK; + moveCount++; + } + + //#endregion + + if (checkCount < 2) { + + if (checkCount == 0) + { + checkBitboard = GenConst.MAX_ULONG; + + if (Board.castle_rights_global[WKS_CASTLE_RIGHTS] == true) + { + if (kingPosition == GenConst.E1) //king on e1 + { + if ((GenConst.WKS_EMPTY_BITBOARD & COMBINED_OCCUPANCIES_LOCAL) == 0) //f1 and g1 empty + { + if ((Board.bitboard_array_global[GenConst.WR] & MoveConstants.SQUARE_BBS[GenConst.H1]) != 0) //rook on h1 + { + if (Is_Square_Attacked_By_Black_Global(GenConst.F1, COMBINED_OCCUPANCIES_LOCAL) == false) + { + if (Is_Square_Attacked_By_Black_Global(GenConst.G1, COMBINED_OCCUPANCIES_LOCAL) == false) + { + startingSquares[moveCount] = GenConst.E1; + targetSquares[moveCount] = GenConst.G1; + tags[moveCount] = GenConst.TAG_W_CASTLE_KS; + pieces[moveCount] = GenConst.WK; + moveCount++; + } + } + } + } + } + } + if (Board.castle_rights_global[WQS_CASTLE_RIGHTS] == true) + { + if (kingPosition == GenConst.E1) //king on e1 + { + if ((GenConst.WQS_EMPTY_BITBOARD & COMBINED_OCCUPANCIES_LOCAL) == 0) //f1 and g1 empty + { + if ((Board.bitboard_array_global[GenConst.WR] & MoveConstants.SQUARE_BBS[GenConst.A1]) != 0) //rook on h1 + { + if (Is_Square_Attacked_By_Black_Global(GenConst.C1, COMBINED_OCCUPANCIES_LOCAL) == false) + { + if (Is_Square_Attacked_By_Black_Global(GenConst.D1, COMBINED_OCCUPANCIES_LOCAL) == false) + { + startingSquares[moveCount] = GenConst.E1; + targetSquares[moveCount] = GenConst.C1; + tags[moveCount] = GenConst.TAG_W_CASTLE_QS; + pieces[moveCount] = GenConst.WK; + moveCount++; + } + } + } + } + } + } + + } + + //#region White pawn + + tempBitboard = Board.bitboard_array_global[GenConst.WP]; + + while (tempBitboard != 0) + { + startingSquare = DEBRUIJN64[(int)((MAGIC * (tempBitboard ^ (tempBitboard - 1))) >>> 58)]; + tempBitboard &= tempBitboard - 1; + + tempPinBitboard = GenConst.MAX_ULONG; + if (pinNumber != 0) + { + for (int i = 0; i < pinNumber; i++) + { + if (pinArraySquare[i] == startingSquare) + { + tempPinBitboard = Inb.INBETWEEN_BITBOARDS[kingPosition][pinArrayPiece[i]]; + } + } + } + + //#region Pawn forward + + if ((MoveConstants.SQUARE_BBS[startingSquare - 8] & COMBINED_OCCUPANCIES_LOCAL) == 0) //if up one square is empty + { + if (((MoveConstants.SQUARE_BBS[startingSquare - 8] & checkBitboard) & tempPinBitboard) != 0) + { + if ((MoveConstants.SQUARE_BBS[startingSquare] & GenConst.RANK_7_BITBOARD) != 0) //if promotion + { + + startingSquares[moveCount] = startingSquare; + targetSquares[moveCount] = startingSquare - 8; + tags[moveCount] = GenConst.TAG_W_Q_PROMOTION; + pieces[moveCount] = GenConst.WP; + moveCount++; + + startingSquares[moveCount] = startingSquare; + targetSquares[moveCount] = startingSquare - 8; + tags[moveCount] = GenConst.TAG_W_R_PROMOTION; + pieces[moveCount] = GenConst.WP; + moveCount++; + + startingSquares[moveCount] = startingSquare; + targetSquares[moveCount] = startingSquare - 8; + tags[moveCount] = GenConst.TAG_W_B_PROMOTION; + pieces[moveCount] = GenConst.WP; + moveCount++; + + startingSquares[moveCount] = startingSquare; + targetSquares[moveCount] = startingSquare - 8; + tags[moveCount] = GenConst.TAG_W_N_PROMOTION; + pieces[moveCount] = GenConst.WP; + moveCount++; + + } + else + { + startingSquares[moveCount] = startingSquare; + targetSquares[moveCount] = startingSquare - 8; + tags[moveCount] = GenConst.TAG_NONE; + pieces[moveCount] = GenConst.WP; + moveCount++; + } + } + + if ((MoveConstants.SQUARE_BBS[startingSquare] & GenConst.RANK_2_BITBOARD) != 0) //if on rank 2 + { + if (((MoveConstants.SQUARE_BBS[startingSquare - 16] & checkBitboard) & tempPinBitboard) != 0) //if not pinned or + { + if (((MoveConstants.SQUARE_BBS[startingSquare - 16]) & COMBINED_OCCUPANCIES_LOCAL) == 0) //if up two squares and one square are empty + { + startingSquares[moveCount] = startingSquare; + targetSquares[moveCount] = startingSquare - 16; + tags[moveCount] = GenConst.TAG_W_P_DOUBLE; + pieces[moveCount] = GenConst.WP; + moveCount++; + } + } + } + } + + //#endregion + + //#region Pawn captures + + tempAttack = ((MoveConstants.WHITE_PAWN_ATTACKS[startingSquare] & BLACK_OCCUPANCIES_LOCAL) & checkBitboard) & tempPinBitboard; //if black piece diagonal to pawn + + while (tempAttack != 0) + { + targetSquare = (DEBRUIJN64[(int)((MAGIC * (tempAttack ^ (tempAttack - 1))) >>> 58)]); + tempAttack &= tempAttack - 1; + + if ((MoveConstants.SQUARE_BBS[startingSquare] & GenConst.RANK_7_BITBOARD) != 0) //if promotion + { + startingSquares[moveCount] = startingSquare; + targetSquares[moveCount] = targetSquare; + tags[moveCount] = GenConst.TAG_W_Q_PROMOTION_CAP; + pieces[moveCount] = GenConst.WP; + moveCount++; + + startingSquares[moveCount] = startingSquare; + targetSquares[moveCount] = targetSquare; + tags[moveCount] = GenConst.TAG_W_R_PROMOTION_CAP; + pieces[moveCount] = GenConst.WP; + moveCount++; + + startingSquares[moveCount] = startingSquare; + targetSquares[moveCount] = targetSquare; + tags[moveCount] = GenConst.TAG_W_B_PROMOTION_CAP; + pieces[moveCount] = GenConst.WP; + moveCount++; + + startingSquares[moveCount] = startingSquare; + targetSquares[moveCount] = targetSquare; + tags[moveCount] = GenConst.TAG_W_N_PROMOTION_CAP; + pieces[moveCount] = GenConst.WP; + moveCount++; + + } + else + { + startingSquares[moveCount] = startingSquare; + targetSquares[moveCount] = targetSquare; + tags[moveCount] = GenConst.TAG_CAPTURE; + pieces[moveCount] = GenConst.WP; + moveCount++; + } + } + + if ((MoveConstants.SQUARE_BBS[startingSquare] & GenConst.RANK_5_BITBOARD) != 0) //check rank for ep + { + if (Board.ep_global != NO_SQUARE) + { + if ((((MoveConstants.WHITE_PAWN_ATTACKS[startingSquare] & MoveConstants.SQUARE_BBS[Board.ep_global]) & checkBitboard) & tempPinBitboard) != 0) + { + if ((Board.bitboard_array_global[GenConst.WK] & GenConst.RANK_5_BITBOARD) == 0) //if no king on rank 5 + { + startingSquares[moveCount] = startingSquare; + targetSquares[moveCount] = Board.ep_global; + tags[moveCount] = GenConst.TAG_WHITE_EP; + pieces[moveCount] = GenConst.WP; + moveCount++; + } + else if ((Board.bitboard_array_global[GenConst.BR] & GenConst.RANK_5_BITBOARD) == 0 && (Board.bitboard_array_global[GenConst.BQ] & GenConst.RANK_5_BITBOARD) == 0) // if no b rook or queen on rank 5 + { + startingSquares[moveCount] = startingSquare; + targetSquares[moveCount] = Board.ep_global; + tags[moveCount] = GenConst.TAG_WHITE_EP; + pieces[moveCount] = GenConst.WP; + moveCount++; + } + else //wk and br or bq on rank 5 + { + long occupancyWithoutEPPawns = COMBINED_OCCUPANCIES_LOCAL & ~MoveConstants.SQUARE_BBS[startingSquare]; + occupancyWithoutEPPawns &= ~MoveConstants.SQUARE_BBS[Board.ep_global + 8]; + + long rookAttacksFromKing = MoveUtils.getRookMovesSeparate(occupancyWithoutEPPawns, kingPosition); + + if ((rookAttacksFromKing & Board.bitboard_array_global[GenConst.BR]) == 0) + { + if ((rookAttacksFromKing & Board.bitboard_array_global[GenConst.BQ]) == 0) + { + startingSquares[moveCount] = startingSquare; + targetSquares[moveCount] = Board.ep_global; + tags[moveCount] = GenConst.TAG_WHITE_EP; + pieces[moveCount] = GenConst.WP; + moveCount++; + } + } + } + } + } + } + + //#endregion + } + + //#endregion + } + + } + else //black move + { + checkCount = 0; + kingPosition = bitScanForward(Board.bitboard_array_global[GenConst.BK]); + + rook_index = GenConst.BR; + queen_index = GenConst.BQ; + knight_index = GenConst.BN; + bishop_index = GenConst.BB; + + enemy_occupancies = WHITE_OCCUPANCIES_LOCAL; + + //#region pins and check + + //pawns + tempBitboard = Board.bitboard_array_global[GenConst.WP] & MoveConstants.BLACK_PAWN_ATTACKS[kingPosition]; + if (tempBitboard != 0) + { + int pawn_square = DEBRUIJN64[(int)((MAGIC * (tempBitboard ^ (tempBitboard - 1))) >>> 58)]; + + checkBitboard |= MoveConstants.SQUARE_BBS[pawn_square]; + + checkCount++; + } + + //knights + tempBitboard = Board.bitboard_array_global[GenConst.WN] & MoveConstants.KNIGHT_ATTACKS[kingPosition]; + if (tempBitboard != 0) + { + int knight_square = DEBRUIJN64[(int)((MAGIC * (tempBitboard ^ (tempBitboard - 1))) >>> 58)]; + + checkBitboard |= MoveConstants.SQUARE_BBS[knight_square]; + + checkCount++; + } + + //bishops + long bishopAttacksChecks = MoveUtils.getBishopMovesSeparate(WHITE_OCCUPANCIES_LOCAL, kingPosition); + tempBitboard = Board.bitboard_array_global[GenConst.WB] & bishopAttacksChecks; + while (tempBitboard != 0) + { + int piece_square = DEBRUIJN64[(int)((MAGIC * (tempBitboard ^ (tempBitboard - 1))) >>> 58)]; + + tempPinBitboard = Inb.INBETWEEN_BITBOARDS[kingPosition][piece_square] & BLACK_OCCUPANCIES_LOCAL; + + if (tempPinBitboard == 0) + { + + checkBitboard |= Inb.INBETWEEN_BITBOARDS[kingPosition][piece_square]; + + checkCount++; + } + else + { + int pinned_square = (DEBRUIJN64[(int)((MAGIC * (tempPinBitboard ^ (tempPinBitboard - 1))) >>> 58)]); + tempPinBitboard &= tempPinBitboard - 1; + + if (tempPinBitboard == 0) + { + pinArraySquare[pinNumber] = pinned_square; + pinArrayPiece[pinNumber] = piece_square; + pinNumber++; + } + } + tempBitboard &= tempBitboard - 1; + } + + //queen + tempBitboard = Board.bitboard_array_global[GenConst.WQ] & bishopAttacksChecks; + while (tempBitboard != 0) + { + int piece_square = DEBRUIJN64[(int)((MAGIC * (tempBitboard ^ (tempBitboard - 1))) >>> 58)]; + + tempPinBitboard = Inb.INBETWEEN_BITBOARDS[kingPosition][piece_square] & BLACK_OCCUPANCIES_LOCAL; + + if (tempPinBitboard == 0) + { + checkBitboard |= Inb.INBETWEEN_BITBOARDS[kingPosition][piece_square]; + + checkCount++; + } + else + { + int pinned_square = (DEBRUIJN64[(int)((MAGIC * (tempPinBitboard ^ (tempPinBitboard - 1))) >>> 58)]); + tempPinBitboard &= tempPinBitboard - 1; + + if (tempPinBitboard == 0) + { + pinArraySquare[pinNumber] = pinned_square; + pinArrayPiece[pinNumber] = piece_square; + pinNumber++; + } + } + tempBitboard &= tempBitboard - 1; + } + + //rook + long rook_attacks = MoveUtils.getRookMovesSeparate(WHITE_OCCUPANCIES_LOCAL, kingPosition); + tempBitboard = Board.bitboard_array_global[GenConst.WR] & rook_attacks; + while (tempBitboard != 0) + { + int piece_square = DEBRUIJN64[(int)((MAGIC * (tempBitboard ^ (tempBitboard - 1))) >>> 58)]; + + tempPinBitboard = Inb.INBETWEEN_BITBOARDS[kingPosition][piece_square] & BLACK_OCCUPANCIES_LOCAL; + + if (tempPinBitboard == 0) + { + checkBitboard |= Inb.INBETWEEN_BITBOARDS[kingPosition][piece_square]; + + checkCount++; + } + else + { + int pinned_square = (DEBRUIJN64[(int)((MAGIC * (tempPinBitboard ^ (tempPinBitboard - 1))) >>> 58)]); + tempPinBitboard &= tempPinBitboard - 1; + + if (tempPinBitboard == 0) + { + pinArraySquare[pinNumber] = pinned_square; + pinArrayPiece[pinNumber] = piece_square; + pinNumber++; + } + } + tempBitboard &= tempBitboard - 1; + } + + //queen + tempBitboard = Board.bitboard_array_global[GenConst.WQ] & rook_attacks; + while (tempBitboard != 0) + { + int piece_square = DEBRUIJN64[(int)((MAGIC * (tempBitboard ^ (tempBitboard - 1))) >>> 58)]; + + tempPinBitboard = Inb.INBETWEEN_BITBOARDS[kingPosition][piece_square] & BLACK_OCCUPANCIES_LOCAL; + + if (tempPinBitboard == 0) + { + checkBitboard |= Inb.INBETWEEN_BITBOARDS[kingPosition][piece_square]; + + checkCount++; + } + else + { + int pinned_square = (DEBRUIJN64[(int)((MAGIC * (tempPinBitboard ^ (tempPinBitboard - 1))) >>> 58)]); + tempPinBitboard &= tempPinBitboard - 1; + + if (tempPinBitboard == 0) + { + pinArraySquare[pinNumber] = pinned_square; + pinArrayPiece[pinNumber] = piece_square; + pinNumber++; + } + } + tempBitboard &= tempBitboard - 1; + } + + //#endregion + + //#region Black king + long occupancyWithoutBlackKing = COMBINED_OCCUPANCIES_LOCAL & (~Board.bitboard_array_global[GenConst.BK]); + + tempAttack = MoveConstants.KING_ATTACKS[kingPosition] & WHITE_OCCUPANCIES_LOCAL; + + while (tempAttack != 0) + { + targetSquare = DEBRUIJN64[(int)((MAGIC * (tempAttack ^ (tempAttack - 1))) >>> 58)]; + tempAttack &= tempAttack - 1; + + if ((Board.bitboard_array_global[GenConst.WP] & MoveConstants.BLACK_PAWN_ATTACKS[targetSquare]) != 0) + { + continue; + } + if ((Board.bitboard_array_global[GenConst.WN] & MoveConstants.KNIGHT_ATTACKS[targetSquare]) != 0) + { + continue; + } + if ((Board.bitboard_array_global[GenConst.WK] & MoveConstants.KING_ATTACKS[targetSquare]) != 0) + { + continue; + } + long bishopAttacks = MoveUtils.getBishopMovesSeparate(occupancyWithoutBlackKing, targetSquare); + if ((Board.bitboard_array_global[GenConst.WB] & bishopAttacks) != 0) + { + continue; + } + if ((Board.bitboard_array_global[GenConst.WQ] & bishopAttacks) != 0) + { + continue; + } + long rookAttacks = MoveUtils.getRookMovesSeparate(occupancyWithoutBlackKing, targetSquare); + if ((Board.bitboard_array_global[GenConst.WR] & rookAttacks) != 0) + { + continue; + } + if ((Board.bitboard_array_global[GenConst.WQ] & rookAttacks) != 0) + { + continue; + } + + startingSquares[moveCount] = kingPosition; + targetSquares[moveCount] = targetSquare; + tags[moveCount] = GenConst.TAG_CAPTURE; + pieces[moveCount] = GenConst.BK; + moveCount++; + + } + + tempAttack = MoveConstants.KING_ATTACKS[kingPosition] & EMPTY_OCCUPANCIES; + + while (tempAttack != 0) + { + targetSquare = DEBRUIJN64[(int)((MAGIC * (tempAttack ^ (tempAttack - 1))) >>> 58)]; + tempAttack &= tempAttack - 1; + + if ((Board.bitboard_array_global[GenConst.WP] & MoveConstants.BLACK_PAWN_ATTACKS[targetSquare]) != 0) + { + continue; + } + if ((Board.bitboard_array_global[GenConst.WN] & MoveConstants.KNIGHT_ATTACKS[targetSquare]) != 0) + { + continue; + } + if ((Board.bitboard_array_global[GenConst.WK] & MoveConstants.KING_ATTACKS[targetSquare]) != 0) + { + continue; + } + long bishopAttacks = MoveUtils.getBishopMovesSeparate(occupancyWithoutBlackKing, targetSquare); + if ((Board.bitboard_array_global[GenConst.WB] & bishopAttacks) != 0) + { + continue; + } + if ((Board.bitboard_array_global[GenConst.WQ] & bishopAttacks) != 0) + { + continue; + } + long rookAttacks = MoveUtils.getRookMovesSeparate(occupancyWithoutBlackKing, targetSquare); + if ((Board.bitboard_array_global[GenConst.WR] & rookAttacks) != 0) + { + continue; + } + if ((Board.bitboard_array_global[GenConst.WQ] & rookAttacks) != 0) + { + continue; + } + + startingSquares[moveCount] = kingPosition; + targetSquares[moveCount] = targetSquare; + tags[moveCount] = GenConst.TAG_NONE; + pieces[moveCount] = GenConst.BK; + moveCount++; + + } + //#endregion + + if (checkCount < 2) { + + if (checkCount == 0) + { + checkBitboard = GenConst.MAX_ULONG; + + if (Board.castle_rights_global[BKS_CASTLE_RIGHTS] == true) + { + if (kingPosition == GenConst.E8) //king on e1 + { + if ((GenConst.BKS_EMPTY_BITBOARD & COMBINED_OCCUPANCIES_LOCAL) == 0) //f1 and g1 empty + { + if ((Board.bitboard_array_global[GenConst.BR] & MoveConstants.SQUARE_BBS[GenConst.H8]) != 0) //rook on h1 + { + if (Is_Square_Attacked_By_White_Global(GenConst.F8, COMBINED_OCCUPANCIES_LOCAL) == false) + { + if (Is_Square_Attacked_By_White_Global(GenConst.G8, COMBINED_OCCUPANCIES_LOCAL) == false) + { + startingSquares[moveCount] = GenConst.E8; + targetSquares[moveCount] = GenConst.G8; + tags[moveCount] = GenConst.TAG_B_CASTLE_KS; + pieces[moveCount] = GenConst.BK; + moveCount++; + + } + } + } + } + } + } + if (Board.castle_rights_global[BQS_CASTLE_RIGHTS] == true) + { + if (kingPosition == GenConst.E8) //king on e1 + { + if ((GenConst.BQS_EMPTY_BITBOARD & COMBINED_OCCUPANCIES_LOCAL) == 0) //f1 and g1 empty + { + if ((Board.bitboard_array_global[GenConst.BR] & MoveConstants.SQUARE_BBS[GenConst.A8]) != 0) //rook on h1 + { + if (Is_Square_Attacked_By_White_Global(GenConst.C8, COMBINED_OCCUPANCIES_LOCAL) == false) + { + if (Is_Square_Attacked_By_White_Global(GenConst.D8, COMBINED_OCCUPANCIES_LOCAL) == false) + { + startingSquares[moveCount] = GenConst.E8; + targetSquares[moveCount] = GenConst.C8; + tags[moveCount] = GenConst.TAG_B_CASTLE_QS; + pieces[moveCount] = GenConst.BK; + moveCount++; + } + } + } + } + } + } + + } + + //#region Black pawns + + tempBitboard = Board.bitboard_array_global[GenConst.BP]; + + while (tempBitboard != 0) + { + startingSquare = DEBRUIJN64[(int)((MAGIC * (tempBitboard ^ (tempBitboard - 1))) >>> 58)]; + tempBitboard &= tempBitboard - 1; + + tempPinBitboard = GenConst.MAX_ULONG; + if (pinNumber != 0) + { + for (int i = 0; i < pinNumber; i++) + { + if (pinArraySquare[i] == startingSquare) + { + tempPinBitboard = Inb.INBETWEEN_BITBOARDS[kingPosition][pinArrayPiece[i]]; + } + } + } + + //#region Pawn forward + + if ((MoveConstants.SQUARE_BBS[startingSquare + 8] & COMBINED_OCCUPANCIES_LOCAL) == 0) //if up one square is empty + { + if (((MoveConstants.SQUARE_BBS[startingSquare + 8] & checkBitboard) & tempPinBitboard) != 0) + { + if ((MoveConstants.SQUARE_BBS[startingSquare] & GenConst.RANK_2_BITBOARD) != 0) //if promotion + { + startingSquares[moveCount] = startingSquare; + targetSquares[moveCount] = startingSquare + 8; + tags[moveCount] = GenConst.TAG_B_B_PROMOTION; + pieces[moveCount] = GenConst.BP; + moveCount++; + + startingSquares[moveCount] = startingSquare; + targetSquares[moveCount] = startingSquare + 8; + tags[moveCount] = GenConst.TAG_B_N_PROMOTION; + pieces[moveCount] = GenConst.BP; + moveCount++; + + startingSquares[moveCount] = startingSquare; + targetSquares[moveCount] = startingSquare + 8; + tags[moveCount] = GenConst.TAG_B_R_PROMOTION; + pieces[moveCount] = GenConst.BP; + moveCount++; + + startingSquares[moveCount] = startingSquare; + targetSquares[moveCount] = startingSquare + 8; + tags[moveCount] = GenConst.TAG_B_Q_PROMOTION; + pieces[moveCount] = GenConst.BP; + moveCount++; + } + else + { + startingSquares[moveCount] = startingSquare; + targetSquares[moveCount] = startingSquare + 8; + tags[moveCount] = GenConst.TAG_NONE; + pieces[moveCount] = GenConst.BP; + moveCount++; + } + } + + if ((MoveConstants.SQUARE_BBS[startingSquare] & GenConst.RANK_7_BITBOARD) != 0) //if on rank 2 + { + if (((MoveConstants.SQUARE_BBS[startingSquare + 16] & checkBitboard) & tempPinBitboard) != 0) + { + if (((MoveConstants.SQUARE_BBS[startingSquare + 16]) & COMBINED_OCCUPANCIES_LOCAL) == 0) //if up two squares and one square are empty + { + startingSquares[moveCount] = startingSquare; + targetSquares[moveCount] = startingSquare + 16; + tags[moveCount] = GenConst.TAG_B_P_DOUBLE; + pieces[moveCount] = GenConst.BP; + moveCount++; + } + } + } + } + + //#endregion + + //#region region Pawn captures + + tempAttack = ((MoveConstants.BLACK_PAWN_ATTACKS[startingSquare] & WHITE_OCCUPANCIES_LOCAL) & checkBitboard) & tempPinBitboard; //if black piece diagonal to pawn + + while (tempAttack != 0) + { + targetSquare = DEBRUIJN64[(int)((MAGIC * (tempAttack ^ (tempAttack - 1))) >>> 58)]; //find the bit + tempAttack &= tempAttack - 1; + + if ((MoveConstants.SQUARE_BBS[startingSquare] & GenConst.RANK_2_BITBOARD) != 0) //if promotion + { + startingSquares[moveCount] = startingSquare; + targetSquares[moveCount] = targetSquare; + tags[moveCount] = GenConst.TAG_B_B_PROMOTION_CAP; + pieces[moveCount] = GenConst.BP; + moveCount++; + + startingSquares[moveCount] = startingSquare; + targetSquares[moveCount] = targetSquare; + tags[moveCount] = GenConst.TAG_B_N_PROMOTION_CAP; + pieces[moveCount] = GenConst.BP; + moveCount++; + + startingSquares[moveCount] = startingSquare; + targetSquares[moveCount] = targetSquare; + tags[moveCount] = GenConst.TAG_B_R_PROMOTION_CAP; + pieces[moveCount] = GenConst.BP; + moveCount++; + + startingSquares[moveCount] = startingSquare; + targetSquares[moveCount] = targetSquare; + tags[moveCount] = GenConst.TAG_B_Q_PROMOTION_CAP; + pieces[moveCount] = GenConst.BP; + moveCount++; + + } + else + { + startingSquares[moveCount] = startingSquare; + targetSquares[moveCount] = targetSquare; + tags[moveCount] = GenConst.TAG_CAPTURE; + pieces[moveCount] = GenConst.BP; + moveCount++; + } + } + + if ((MoveConstants.SQUARE_BBS[startingSquare] & GenConst.RANK_4_BITBOARD) != 0) //check rank for ep + { + if (Board.ep_global != NO_SQUARE) + { + if ((((MoveConstants.BLACK_PAWN_ATTACKS[startingSquare] & MoveConstants.SQUARE_BBS[Board.ep_global]) & checkBitboard) & tempPinBitboard) != 0) + { + if ((Board.bitboard_array_global[GenConst.BK] & GenConst.RANK_4_BITBOARD) == 0) //if no king on rank 5 + { + startingSquares[moveCount] = startingSquare; + targetSquares[moveCount] = Board.ep_global; + tags[moveCount] = GenConst.TAG_BLACK_EP; + pieces[moveCount] = GenConst.BP; + moveCount++; + } + else if ((Board.bitboard_array_global[GenConst.WR] & GenConst.RANK_4_BITBOARD) == 0 && (Board.bitboard_array_global[GenConst.WQ] & GenConst.RANK_4_BITBOARD) == 0) // if no b rook or queen on rank 5 + { + startingSquares[moveCount] = startingSquare; + targetSquares[moveCount] = Board.ep_global; + tags[moveCount] = GenConst.TAG_BLACK_EP; + pieces[moveCount] = GenConst.BP; + moveCount++; + } + else //wk and br or bq on rank 5 + { + long occupancyWithoutEPPawns = COMBINED_OCCUPANCIES_LOCAL & ~MoveConstants.SQUARE_BBS[startingSquare]; + occupancyWithoutEPPawns &= ~MoveConstants.SQUARE_BBS[Board.ep_global - 8]; + + long rookAttacksFromKing = MoveUtils.getRookMovesSeparate(occupancyWithoutEPPawns, kingPosition); + + if ((rookAttacksFromKing & Board.bitboard_array_global[GenConst.WR]) == 0) + { + if ((rookAttacksFromKing & Board.bitboard_array_global[GenConst.WQ]) == 0) + { + startingSquares[moveCount] = startingSquare; + targetSquares[moveCount] = Board.ep_global; + tags[moveCount] = GenConst.TAG_BLACK_EP; + pieces[moveCount] = GenConst.BP; + moveCount++; + } + } + } + } + } + } + + //#endregion + } + //#endregion + + } + + } + + if (checkCount > 1) { + return moveCount; + } + + //#region knight + + tempBitboard = Board.bitboard_array_global[knight_index]; + + while (tempBitboard != 0) + { + startingSquare = DEBRUIJN64[(int)((MAGIC * (tempBitboard ^ (tempBitboard - 1))) >>> 58)]; + tempBitboard &= tempBitboard - 1; //removes the knight from that square to not infinitely loop + + tempPinBitboard = GenConst.MAX_ULONG; + if (pinNumber != 0) + { + for (int i = 0; i < pinNumber; i++) + { + if (pinArraySquare[i] == startingSquare) + { + tempPinBitboard = Inb.INBETWEEN_BITBOARDS[kingPosition][pinArrayPiece[i]]; + } + } + } + + tempAttack = ((MoveConstants.KNIGHT_ATTACKS[startingSquare] & enemy_occupancies) & checkBitboard) & tempPinBitboard; //gets knight captures + while (tempAttack != 0) + { + targetSquare = (DEBRUIJN64[(int)((MAGIC * (tempAttack ^ (tempAttack - 1))) >>> 58)]); + tempAttack &= tempAttack - 1; + + + startingSquares[moveCount] = startingSquare; + targetSquares[moveCount] = targetSquare; + tags[moveCount] = GenConst.TAG_CAPTURE; + pieces[moveCount] = knight_index; + moveCount++; + + } + + tempAttack = ((MoveConstants.KNIGHT_ATTACKS[startingSquare] & EMPTY_OCCUPANCIES) & checkBitboard) & tempPinBitboard; + + while (tempAttack != 0) + { + targetSquare = DEBRUIJN64[(int)((MAGIC * (tempAttack ^ (tempAttack - 1))) >>> 58)]; + tempAttack &= tempAttack - 1; + + startingSquares[moveCount] = startingSquare; + targetSquares[moveCount] = targetSquare; + tags[moveCount] = GenConst.TAG_NONE; + pieces[moveCount] = knight_index; + moveCount++; + + } + } + //#endregion + + //#region Rook + + tempBitboard = Board.bitboard_array_global[rook_index]; + while (tempBitboard != 0) + { + startingSquare = DEBRUIJN64[(int)((MAGIC * (tempBitboard ^ (tempBitboard - 1))) >>> 58)]; + tempBitboard &= tempBitboard - 1; + + tempPinBitboard = GenConst.MAX_ULONG; + if (pinNumber != 0) + { + for (int i = 0; i < pinNumber; i++) + { + if (pinArraySquare[i] == startingSquare) + { + tempPinBitboard = Inb.INBETWEEN_BITBOARDS[kingPosition][pinArrayPiece[i]]; + } + } + } + + long rookAttacks = MoveUtils.getRookMovesSeparate(COMBINED_OCCUPANCIES_LOCAL, startingSquare); + + tempAttack = ((rookAttacks & enemy_occupancies) & checkBitboard) & tempPinBitboard; + while (tempAttack != 0) + { + targetSquare = (DEBRUIJN64[(int)((MAGIC * (tempAttack ^ (tempAttack - 1))) >>> 58)]); + tempAttack &= tempAttack - 1; + + startingSquares[moveCount] = startingSquare; + targetSquares[moveCount] = targetSquare; + tags[moveCount] = GenConst.TAG_CAPTURE; + pieces[moveCount] = rook_index; + moveCount++; + } + + tempAttack = ((rookAttacks & EMPTY_OCCUPANCIES) & checkBitboard) & tempPinBitboard; + while (tempAttack != 0) + { + targetSquare = (DEBRUIJN64[(int)((MAGIC * (tempAttack ^ (tempAttack - 1))) >>> 58)]); + tempAttack &= tempAttack - 1; + + startingSquares[moveCount] = startingSquare; + targetSquares[moveCount] = targetSquare; + tags[moveCount] = GenConst.TAG_NONE; + pieces[moveCount] = rook_index; + moveCount++; + + } + } + //#endregion + + //#region bishop + + tempBitboard = Board.bitboard_array_global[bishop_index]; + while (tempBitboard != 0) + { + startingSquare = DEBRUIJN64[(int)((MAGIC * (tempBitboard ^ (tempBitboard - 1))) >>> 58)]; + tempBitboard &= tempBitboard - 1; + + tempPinBitboard = GenConst.MAX_ULONG; + if (pinNumber != 0) + { + for (int i = 0; i < pinNumber; i++) + { + if (pinArraySquare[i] == startingSquare) + { + tempPinBitboard = Inb.INBETWEEN_BITBOARDS[kingPosition][pinArrayPiece[i]]; + } + } + } + + long bishopAttacks = MoveUtils.getBishopMovesSeparate(COMBINED_OCCUPANCIES_LOCAL, startingSquare); + + tempAttack = ((bishopAttacks & enemy_occupancies) & checkBitboard) & tempPinBitboard; + while (tempAttack != 0) + { + targetSquare = DEBRUIJN64[(int)((MAGIC * (tempAttack ^ (tempAttack - 1))) >>> 58)]; + tempAttack &= tempAttack - 1; + + startingSquares[moveCount] = startingSquare; + targetSquares[moveCount] = targetSquare; + tags[moveCount] = GenConst.TAG_CAPTURE; + pieces[moveCount] = bishop_index; + moveCount++; + + } + + tempAttack = ((bishopAttacks & EMPTY_OCCUPANCIES) & checkBitboard) & tempPinBitboard; + while (tempAttack != 0) + { + targetSquare = DEBRUIJN64[(int)((MAGIC * (tempAttack ^ (tempAttack - 1))) >>> 58)]; + tempAttack &= tempAttack - 1; + + + startingSquares[moveCount] = startingSquare; + targetSquares[moveCount] = targetSquare; + tags[moveCount] = GenConst.TAG_NONE; + pieces[moveCount] = bishop_index; + moveCount++; + + } + } + //#endregion + + //#region Queen + + tempBitboard = Board.bitboard_array_global[queen_index]; + while (tempBitboard != 0) + { + startingSquare = DEBRUIJN64[(int)((MAGIC * (tempBitboard ^ (tempBitboard - 1))) >>> 58)]; + tempBitboard &= tempBitboard - 1; + + tempPinBitboard = GenConst.MAX_ULONG; + if (pinNumber != 0) + { + for (int i = 0; i < pinNumber; i++) + { + if (pinArraySquare[i] == startingSquare) + { + tempPinBitboard = Inb.INBETWEEN_BITBOARDS[kingPosition][pinArrayPiece[i]]; + } + } + } + + long queenAttacks = MoveUtils.getRookMovesSeparate(COMBINED_OCCUPANCIES_LOCAL, startingSquare); + queenAttacks |= MoveUtils.getBishopMovesSeparate(COMBINED_OCCUPANCIES_LOCAL, startingSquare); + + tempAttack = ((queenAttacks & enemy_occupancies) & checkBitboard) & tempPinBitboard; + + while (tempAttack != 0) + { + targetSquare = DEBRUIJN64[(int)((MAGIC * (tempAttack ^ (tempAttack - 1))) >>> 58)]; + tempAttack &= tempAttack - 1; + + startingSquares[moveCount] = startingSquare; + targetSquares[moveCount] = targetSquare; + tags[moveCount] = GenConst.TAG_CAPTURE; + pieces[moveCount] = queen_index; + moveCount++; + + } + + tempAttack = ((queenAttacks & EMPTY_OCCUPANCIES) & checkBitboard) & tempPinBitboard; + while (tempAttack != 0) + { + targetSquare = DEBRUIJN64[(int)((MAGIC * (tempAttack ^ (tempAttack - 1))) >>> 58)]; + tempAttack &= tempAttack - 1; + + + startingSquares[moveCount] = startingSquare; + targetSquares[moveCount] = targetSquare; + tags[moveCount] = GenConst.TAG_NONE; + pieces[moveCount] = queen_index; + moveCount++; + + } + } + //#endregion + + return moveCount; + + } + + + static ErrorInt PerftFunctionsDebug(int depth, int ply, DebugInfo debugInfo) + { + if (depth < 0) { + return errorIntFromError(Error.Depth_Less_Than_Zero); + } + if (Board.bitboard_array_global[GenConst.WK] == 0) { + return errorIntFromError(Error.White_King_Captured); + } + if (Board.bitboard_array_global[GenConst.BK] == 0) { + return errorIntFromError(Error.Black_King_Captured); + } + long COMBINED_OCCUPANCIES = CombineBitboardsGlobal(); + if (Board.is_white_global == true) { + int kingPosition = bitScanForward(Board.bitboard_array_global[GenConst.BK]); + if (Is_Square_Attacked_By_White_Global(kingPosition, COMBINED_OCCUPANCIES) == true) { + PrintAllBitboards(); + PrintAllDebug(debugInfo.LastStarting, debugInfo.LastTarget, debugInfo.lastPiece, debugInfo.LastTag); + return errorIntFromError(Error.Black_King_In_Check_On_White_Move); + } + } else { + int kingPosition = bitScanForward(Board.bitboard_array_global[GenConst.WK]); + if (Is_Square_Attacked_By_Black_Global(kingPosition, COMBINED_OCCUPANCIES) == true) { + + return errorIntFromError(Error.White_King_In_Check_On_BlackMove); + } + } + debugInfo.CallCount += 1; + if (depth == 0) { + return errorIntFromValue(1); + } + + //Same as move list, span to avoid heap allocation + int[] startingSquares = new int[50]; + int[] targetSquares = new int[50]; + int[] tags = new int[50]; + int[] pieces = new int[50]; + + int moveCount = getMoves(startingSquares, targetSquares, tags, pieces); + + ErrorInt nodes = errorIntFromValue(0); + + int copyEp = Board.ep_global; + boolean[] copy_castle = { + Board.castle_rights_global[0], + Board.castle_rights_global[1], + Board.castle_rights_global[2], + Board.castle_rights_global[3], + }; + + long[] bitboardCopy = + { + Board.bitboard_array_global[0], + Board.bitboard_array_global[1], + Board.bitboard_array_global[2], + Board.bitboard_array_global[3], + Board.bitboard_array_global[4], + Board.bitboard_array_global[5], + Board.bitboard_array_global[6], + Board.bitboard_array_global[7], + Board.bitboard_array_global[8], + Board.bitboard_array_global[9], + Board.bitboard_array_global[10], + Board.bitboard_array_global[11], + }; + + for (int move_index = 0; move_index < moveCount; ++move_index) + { + int startingSquareCopy = startingSquares[move_index]; + int targetSquareCopy = targetSquares[move_index]; + int piece = pieces[move_index]; + int tag = tags[move_index]; + + if (startingSquareCopy < 0 || startingSquareCopy > 63) { + return errorIntFromError(Error.Invalid_Starting_Square); + } + if (targetSquareCopy < 0 || targetSquareCopy > 63) { + return errorIntFromError(Error.Invalid_Target_Square); + } + if (piece < 0 || piece > 11) { + return errorIntFromError(Error.Invalid_Piece); + } + if (tag < 0 || tag > 27) { + return errorIntFromError(Error.Invalid_Tag); + } + if (startingSquareCopy == targetSquareCopy) { + return errorIntFromError(Error.Starting_Square_And_Target_Square_The_Same); + } + if (tag == GenConst.TAG_WHITE_EP || tag == GenConst.TAG_BLACK_EP) { + if (Board.ep_global != targetSquareCopy) { + return errorIntFromError(Error.Ep_Not_Target_Square); + } + } + if (debugInfo.PromotionExpected == false) { + if (tag >= GenConst.TAG_B_N_PROMOTION && tag <= GenConst.TAG_W_R_PROMOTION_CAP) { + PrintAllDebug(startingSquareCopy, targetSquareCopy, piece, tag); + return errorIntFromError(Error.Promotion_When_Not_Expected); + } + } + + boolean sideBefore = Board.is_white_global; + + int captureIndex = MakeMove(startingSquareCopy, targetSquareCopy, tag, piece); + + if (Board.is_white_global == sideBefore) { + return errorIntFromError(Error.Side_Not_Changed); + } + + debugInfo.lastPiece = piece; + debugInfo.LastStarting = startingSquareCopy; + debugInfo.LastTarget = targetSquareCopy; + debugInfo.LastTag = tag; + + ErrorInt priorNodes = errorIntFromValue(nodes.getValue()); + ErrorInt nodesToAdd = PerftFunctionsDebug(depth - 1, ply + 1, debugInfo); + if (nodesToAdd.getError() != null) { + return nodesToAdd; + } + nodes.setValue(nodes.getValue() + nodesToAdd.getValue()); + + boolean newSide = Board.is_white_global; + + UnmakeMove(startingSquareCopy, targetSquareCopy, piece, tag, captureIndex); + + if (Board.is_white_global == newSide) { + return errorIntFromError(Error.Side_Not_Changed_Back); + } + + Board.castle_rights_global[0] = copy_castle[0]; + Board.castle_rights_global[1] = copy_castle[1]; + Board.castle_rights_global[2] = copy_castle[2]; + Board.castle_rights_global[3] = copy_castle[3]; + Board.ep_global = copyEp; + + for (int i = 0; i < 12; i++) { + if (Board.bitboard_array_global[i] != bitboardCopy[i]) { + PrintAllBitboards(); + System.out.println(String.format("global %d", i)); + PrintBitboard(Board.bitboard_array_global[i]); + System.out.println(String.format("copy %d", i)); + PrintBitboard(bitboardCopy[i]); + PrintAllDebug(startingSquareCopy, targetSquareCopy, piece, tag); + return errorIntFromError(Error.Copy_Boards_Not_Same); + } + } + + if (ply == 0) + { + PrintMoveNoNL(startingSquareCopy, targetSquareCopy, tag); + System.out.println(String.format(": %d", nodes.getValue() - priorNodes.getValue())); + } + } + + return nodes; + } + + static void runPerftFunctionsDebug(int depth) + { + long startTime = System.currentTimeMillis(); + + DebugInfo debugInfo = new DebugInfo(); + debugInfo.CallCount = 0; + debugInfo.LastStarting = 0; + + ErrorInt nodes = PerftFunctionsDebug(depth, 0, debugInfo); + if (nodes.getError() != null) { + ErrorInt.printError(nodes.getError()); + } + + long endTime = System.currentTimeMillis(); + + long elapsedTime = endTime - startTime; + + String nodeString = String.format("Nodes: %d\n", nodes.getValue()); + String timeString = String.format("Time taken: %d ms\n", elapsedTime); + + Pr.print(nodeString); + Pr.print(timeString); + } + + //int[] startingSquares = new int[50]; + + static int PerftFunctions(int depth, int ply) + { + int[] startingSquares = new int[50]; + int[] targetSquares = new int[50]; + int[] tags = new int[50]; + int[] pieces = new int[50]; + + int moveCount = getMoves(startingSquares, targetSquares, tags, pieces); + + if (depth == 1){ + return moveCount; + } + + int nodes = 0; + + int copyEp = Board.ep_global; + boolean[] copy_castle = { + Board.castle_rights_global[0], + Board.castle_rights_global[1], + Board.castle_rights_global[2], + Board.castle_rights_global[3], + }; + + for (int move_index = 0; move_index < moveCount; ++move_index) + { + int startingSquareCopy = startingSquares[move_index]; + int targetSquareCopy = targetSquares[move_index]; + int piece = pieces[move_index]; + int tag = tags[move_index]; + + int captureIndex = MakeMove(startingSquareCopy, targetSquareCopy, tag, piece); + + int priorNodes = nodes; + nodes += PerftFunctions(depth - 1, ply + 1); + + UnmakeMove(startingSquareCopy, targetSquareCopy, piece, tag, captureIndex); + + Board.castle_rights_global[0] = copy_castle[0]; + Board.castle_rights_global[1] = copy_castle[1]; + Board.castle_rights_global[2] = copy_castle[2]; + Board.castle_rights_global[3] = copy_castle[3]; + Board.ep_global = copyEp; + + //if (ply == 0) + //{ + //PrintMoveNoNL(startingSquareCopy, targetSquareCopy, tag); + //System.out.println(String.format(": %d", nodes - priorNodes)); + //} + } + + return nodes; + } + + static void runPerftFunctions(int depth) + { + long startTime = System.currentTimeMillis(); + + int nodes = PerftFunctions(depth, 0); + + long endTime = System.currentTimeMillis(); + + long elapsedTime = endTime - startTime; + + String nodeString = String.format("Nodes: %d\n", nodes); + String timeString = String.format("Time taken: %d ms\n", elapsedTime); + + Pr.print(nodeString); + Pr.print(timeString); + } + + + + + static void callPerft() + { + long startTime = System.currentTimeMillis(); + + //int nodes = PerftInlineDebug(0, 0); + + long endTime = System.currentTimeMillis(); + + long elapsedTime = endTime - startTime; + + //String nodeString = String.format("Nodes: %d\n", nodes); + String timeString = String.format("Time taken: %d ms\n", elapsedTime); + + //Pr.print(nodeString); + Pr.print(timeString); + } +} diff --git a/Java/Pr.java b/Java/Pr.java new file mode 100644 index 0000000..6fdb393 --- /dev/null +++ b/Java/Pr.java @@ -0,0 +1,39 @@ +import java.math.BigInteger; + +public class Pr { + + public static void print(String input) + { + System.out.print(input); + } + public static void println(String input) + { + System.out.println(input); + } + public static void printInt(int input) + { + System.out.print(input); + } + public static void printIntLn(int input) + { + System.out.println(input); + } + public static void printBigInteger(BigInteger input) + { + System.out.print(input); + } + public static void printBigIntegerLn(BigInteger input) + { + System.out.println(input); + } + + + + public static void printSquareLn(int input) + { + assert input >= 0 && input < 64 : "Invalid square: " + input; + + System.out.print(GenConst.SQ_CHAR_X[input]); + System.out.println(GenConst.SQ_CHAR_Y[input]); + } +} diff --git a/Java/Testing.java b/Java/Testing.java new file mode 100644 index 0000000..1e1aa20 --- /dev/null +++ b/Java/Testing.java @@ -0,0 +1,92 @@ +import java.math.BigInteger; + +public class Testing +{ + + public static void testRookMoves() + { + int rookSquare = 36; + + long WP_STARTING_POSITIONS = 71776119061217280L; + long BP_STARTING_POSITIONS = 65280L; + long COMBINED_OCC = WP_STARTING_POSITIONS | BP_STARTING_POSITIONS; + + long rookMoves = MoveUtils.getRookMovesSeparate(COMBINED_OCC, rookSquare); + //Bitboard.printBigInteger(rookMoves); + } + + public static void TestKnightMoves() + { + BigInteger MAX_BIGINT = new BigInteger("18446744073709551615"); // Max u64 value + BigInteger knightBitboard = BigInteger.ZERO; + knightBitboard = knightBitboard.setBit(45); + knightBitboard = knightBitboard.setBit(42); + + Bitboard.printBigInteger(knightBitboard); + Bitboard.printBigInteger(MAX_BIGINT); + + BigInteger tempBitboard = knightBitboard; + + while (!tempBitboard.equals(BigInteger.ZERO)) + { + int knightSquare = Bitboard.bitScanForwardSlow(tempBitboard); + tempBitboard = tempBitboard.clearBit(knightSquare); + + Pr.println("Knight square: " + knightSquare); + + if (knightSquare == -1) + { + Pr.println("Error knight sq -1"); + break; + } + + //BigInteger knightAttacks = MoveConstants.KNIGHT_ATTACKS[knightSquare]; + Pr.println("Knight on square " + knightSquare); + //Bitboard.printBigInteger(knightAttacks); + } + } + + public static void printRookMoves() + { + for (int direction = 0; direction < 4; direction++) + { + Pr.println("direction: " + direction); + for (int sq = 0; sq < 64; sq++) + { + // Bitboard.printBigInteger(MoveConstants.ROOK_ATTACKS[direction][sq]); + } + } + } + + public static void printBishopMoves() + { + for (int direction = 0; direction < 4; direction++) + { + Pr.println("direction: " + direction); + for (int sq = 0; sq < 64; sq++) + { + // Bitboard.printBigInteger(MoveConstants.BISHOP_ATTACKS[direction][sq]); + } + } + } + + public static void testLong() + { + long knight_bitboard = 0L; + knight_bitboard |= MoveConstants.SQUARE_BBS[63]; + Bitboard.printLong(knight_bitboard); + } + + public static void bitScanTest() + { + for (int i = 0; i < 64; i++) + { + int square = Perft.bitScanForward(MoveConstants.SQUARE_BBS[i]); + Pr.print("i: "); System.out.println(i); Pr.print("sq: "); System.out.println(square); + } + } + + + + +}