static Cuckoo() { // initialize cuckoo tables CuckooKeys = new HashKey[8192]; CuckooMoves = new Move[8192]; Span <Piece> pieces = stackalloc Piece[] { Enums.Pieces.WhitePawn, Enums.Pieces.WhiteKnight, Enums.Pieces.WhiteBishop, Enums.Pieces.WhiteRook, Enums.Pieces.WhiteQueen, Enums.Pieces.WhiteKing, Enums.Pieces.BlackPawn, Enums.Pieces.BlackKnight, Enums.Pieces.BlackBishop, Enums.Pieces.BlackRook, Enums.Pieces.BlackQueen, Enums.Pieces.BlackKing }; var count = 0; foreach (var pc in pieces) { foreach (var sq1 in BitBoards.AllSquares) { for (var sq2 = sq1 + 1; sq2 <= Enums.Squares.h8; ++sq2) { if ((pc.Type().PseudoAttacks(sq1) & sq2).IsEmpty) { continue; } var move = Move.Create(sq1, sq2); HashKey key = pc.GetZobristPst(sq1) ^ pc.GetZobristPst(sq2) ^ Zobrist.GetZobristSide(); var i = CuckooHashOne(key); while (true) { (CuckooKeys[i], key) = (key, CuckooKeys[i].Key);
/// <summary> /// Updates the hash key depending on a move /// </summary> /// <param name="move">The move the hash key is depending on</param> private void UpdateKey(Move move) { // TODO : Merge with MakeMove to avoid duplicate ifs var pawnKey = State.PawnStructureKey; var key = State.Key ^ pawnKey; pawnKey ^= Zobrist.GetZobristSide(); if (_stateList[PositionIndex - 1].EnPassantSquare != ESquare.none) { key ^= _stateList[PositionIndex - 1].EnPassantSquare.File().GetZobristEnPessant(); } if (State.EnPassantSquare != ESquare.none) { key ^= State.EnPassantSquare.File().GetZobristEnPessant(); } if (move.IsNullMove()) { State.Key = key ^ pawnKey; State.PawnStructureKey = pawnKey; return; } var pawnPiece = move.GetMovingPieceType() == EPieceType.Pawn; var squareTo = move.GetToSquare(); if (move.IsPromotionMove()) { key ^= move.GetPromotedPiece().GetZobristPst(squareTo); } else { if (pawnPiece) { pawnKey ^= move.GetMovingPiece().GetZobristPst(squareTo); } else { key ^= move.GetMovingPiece().GetZobristPst(squareTo); } } if (pawnPiece) { pawnKey ^= move.GetMovingPiece().GetZobristPst(move.GetFromSquare()); if (move.IsEnPassantMove()) { pawnKey ^= move.GetCapturedPiece().GetZobristPst(squareTo + State.SideToMove.PawnPushDistance()); } else if (move.IsCaptureMove()) { pawnKey ^= move.GetCapturedPiece().GetZobristPst(squareTo); } } else { key ^= move.GetMovingPiece().GetZobristPst(move.GetFromSquare()); if (move.IsCaptureMove()) { key ^= move.GetCapturedPiece().GetZobristPst(squareTo); } else if (move.IsCastlelingMove()) { var piece = EPieceType.Rook.MakePiece(Position.State.SideToMove); key ^= piece.GetZobristPst(Position.GetRookCastleFrom(squareTo)); key ^= piece.GetZobristPst(squareTo.GetRookCastleTo()); } } // castling rights if (State.CastlelingRights != _stateList[PositionIndex - 1].CastlelingRights) { key ^= _stateList[PositionIndex - 1].CastlelingRights.GetZobristCastleling(); key ^= State.CastlelingRights.GetZobristCastleling(); } State.Key = key ^ pawnKey; State.PawnStructureKey = pawnKey; }
/// <summary> /// Apply a FEN string board setup to the board structure. /// </summary> /// <param name="fenString">The string to set</param> /// <param name="validate">If true, the fen string is validated, otherwise not</param> /// <returns> /// 0 = all ok. /// -1 = Error in piece file layout parsing /// -2 = Error in piece rank layout parsing /// -3 = Unknown piece detected /// -4 = Error while parsing moving side /// -5 = Error while parsing castleling /// -6 = Error while parsing en-passant square /// -9 = FEN length exceeding maximum /// </returns> public FenError SetFen(FenData fen, bool validate = false) { if (validate) { Fen.Fen.Validate(fen.Fen.ToString()); } // correctly clear all pieces and invoke possible notification(s) var bb = Occupied; while (bb) { var square = bb.Lsb(); Position.RemovePiece(square, Position.BoardLayout[square.AsInt()]); BitBoards.ResetLsb(ref bb); } var chunk = fen.Chunk(); if (chunk.IsEmpty) { return(new FenError()); } var f = 1; // file (column) var r = 8; // rank (row) Player player; foreach (var c in chunk) { if (char.IsDigit(c)) { f += c - '0'; if (f > 9) { return(new FenError(-1, fen.Index)); } } else if (c == '/') { if (f != 9) { return(new FenError(-2, fen.Index)); } r--; f = 1; } else { var pieceIndex = PieceExtensions.PieceChars.IndexOf(c); if (pieceIndex == -1) { return(new FenError(-3, fen.Index)); } player = char.IsLower(PieceExtensions.PieceChars[pieceIndex]); var square = new Square(r - 1, f - 1); AddPiece(square, player, (EPieceType)pieceIndex); f++; } } // player chunk = fen.Chunk(); if (chunk.IsEmpty || chunk.Length != 1) { return(new FenError(-3, fen.Index)); } player = (chunk[0] != 'w').ToInt(); // castleling chunk = fen.Chunk(); if (!SetupCastleling(chunk)) { return(new FenError(-5, fen.Index)); } // en-passant chunk = fen.Chunk(); if (chunk.Length == 1 || chunk[0] == '-' || !chunk[0].InBetween('a', 'h')) { State.EnPassantSquare = ESquare.none; } else { State.EnPassantSquare = chunk[1].InBetween('3', '6') ? ESquare.none : new Square(chunk[1] - '1', chunk[0] - 'a').Value; } // move number chunk = fen.Chunk(); var moveNum = 0; var halfMoveNum = 0; if (!chunk.IsEmpty) { chunk.ToIntegral(out halfMoveNum); // half move number chunk = fen.Chunk(); chunk.ToIntegral(out moveNum); if (moveNum > 0) { moveNum--; } } PositionIndex = PositionStart = moveNum; Position.State = _stateList[PositionIndex]; State.ReversibleHalfMoveCount = halfMoveNum; State.SideToMove = player; if (player.IsBlack()) { State.Key ^= Zobrist.GetZobristSide(); State.PawnStructureKey ^= Zobrist.GetZobristSide(); } State.Key ^= State.CastlelingRights.GetZobristCastleling(); if (State.EnPassantSquare != ESquare.none) { State.Key ^= State.EnPassantSquare.File().GetZobristEnPessant(); } Position.InCheck = Position.IsAttacked(Position.GetPieceSquare(EPieceType.King, player), ~player); return(0); }
/// <summary> /// Updates the hash key depending on a move /// </summary> /// <param name="move">The move the hash key is depending on</param> private void UpdateKey(Move move) { // TODO : Merge with MakeMove to avoid duplicate ifs var pawnKey = State.PawnStructureKey; var key = State.Key ^ pawnKey; pawnKey ^= Zobrist.GetZobristSide(); if (_stateList[PositionIndex - 1].EnPassantSquare != ESquare.none) { key ^= Zobrist.GetZobristEnPessant(_stateList[PositionIndex - 1].EnPassantSquare.File().AsInt()); } if (State.EnPassantSquare != ESquare.none) { key ^= Zobrist.GetZobristEnPessant(State.EnPassantSquare.File().AsInt()); } if (move.IsNullMove()) { key ^= pawnKey; State.Key = key; State.PawnStructureKey = pawnKey; return; } var pawnPiece = move.GetMovingPieceType() == EPieceType.Pawn; if (pawnPiece) { pawnKey ^= Zobrist.GetZobristPst(move.GetMovingPiece(), move.GetFromSquare()); } else { key ^= Zobrist.GetZobristPst(move.GetMovingPiece(), move.GetFromSquare()); } var squareTo = move.GetToSquare(); if (move.IsPromotionMove()) { key ^= Zobrist.GetZobristPst(move.GetPromotedPiece(), squareTo); } else { if (pawnPiece) { pawnKey ^= Zobrist.GetZobristPst(move.GetMovingPiece(), squareTo); } else { key ^= Zobrist.GetZobristPst(move.GetMovingPiece(), squareTo); } } if (pawnPiece && move.IsEnPassantMove()) { pawnKey ^= Zobrist.GetZobristPst(move.GetCapturedPiece().Type() + (State.SideToMove << 3), squareTo + (State.SideToMove.Side == 0 ? 8 : -8)); } else if (move.IsCaptureMove()) { if (pawnPiece) { pawnKey ^= Zobrist.GetZobristPst(move.GetCapturedPiece(), squareTo); } else { key ^= Zobrist.GetZobristPst(move.GetCapturedPiece(), squareTo); } } else if (move.IsCastlelingMove()) { var piece = (EPieces)(EPieceType.Rook + move.GetSideMask()); key ^= Zobrist.GetZobristPst(piece, Position.GetRookCastleFrom(squareTo)); key ^= Zobrist.GetZobristPst(piece, squareTo.GetRookCastleTo()); } // castleling // castling rights if (State.CastlelingRights != _stateList[PositionIndex - 1].CastlelingRights) { key ^= Zobrist.GetZobristCastleling(_stateList[PositionIndex - 1].CastlelingRights); key ^= Zobrist.GetZobristCastleling(State.CastlelingRights); } key ^= pawnKey; State.Key = key; State.PawnStructureKey = pawnKey; }
/// <summary> /// Apply a FEN string board setup to the board structure. /// *EXCEPTION FREE FUNCTION* /// </summary> /// <param name="fenString">The string to set</param> /// <param name="validate">If true, the fen string is validated, otherwise not</param> /// <returns> /// 0 = all ok. /// -1 = Error in piece file layout parsing /// -2 = Error in piece rank layout parsing /// -3 = Unknown piece detected /// -4 = Error while parsing moving side /// -5 = Error while parsing castleling /// -6 = Error while parsing en-passant square /// -9 = FEN length exceeding maximum /// </returns> public FenError SetFen(string fenString, bool validate = false) { // TODO : Replace with stream at some point // basic validation, catches format errors if (validate) { Fen.Fen.Validate(fenString); } foreach (var square in Occupied) { Position.RemovePiece(square, Position.BoardLayout[square.AsInt()]); } //for (var i = 0; i <= PositionIndex; i++) // _stateList[i].Clear(); Position.Clear(); var fen = new FenData(fenString); Player player; var c = fen.GetAdvance; var f = 1; // file (column) var r = 8; // rank (row) // map pieces to data structure while (c != 0 && !(f == 9 && r == 1)) { if (char.IsDigit(c)) { f += c - '0'; if (f > 9) { return(new FenError(-1, fen.Index)); } c = fen.GetAdvance; continue; } if (c == '/') { if (f != 9) { return(new FenError(-2, fen.Index)); } r--; f = 1; c = fen.GetAdvance; continue; } var pieceIndex = PieceExtensions.PieceChars.IndexOf(c); if (pieceIndex == -1) { return(new FenError(-3, fen.Index)); } player = char.IsLower(PieceExtensions.PieceChars[pieceIndex]); var square = new Square(r - 1, f - 1); AddPiece(square, player, (EPieceType)pieceIndex); c = fen.GetAdvance; f++; } if (!Fen.Fen.IsDelimiter(c)) { return(new FenError(-4, fen.Index)); } c = fen.GetAdvance; player = c == 'w' ? 0 : 1; if (player == -1) { return(new FenError(-4, fen.Index)); } if (!Fen.Fen.IsDelimiter(fen.GetAdvance)) { return(new FenError(-5, fen.Index)); } if (!SetupCastleling(fen)) { return(new FenError(-5, fen.Index)); } if (!Fen.Fen.IsDelimiter(fen.GetAdvance)) { return(new FenError(-6, fen.Index)); } State.EnPassantSquare = fen.GetEpSquare(); // temporary.. the whole method should be using this, but this will do for now. var moveCounters = fen.Fen.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); var first = moveCounters[moveCounters.Length - 2]; var second = moveCounters[moveCounters.Length - 1]; second.ToIntegral(out int number); if (number > 0) { number -= 1; } PositionIndex = PositionStart = number; first.ToIntegral(out number); State = Position.State = _stateList[PositionIndex]; State.ReversibleHalfMoveCount = number; State.SideToMove = player; if (player.IsBlack()) { var zobristSide = Zobrist.GetZobristSide(); State.Key ^= zobristSide; State.PawnStructureKey ^= zobristSide; } State.Key ^= Zobrist.GetZobristCastleling(State.CastlelingRights); if (State.EnPassantSquare != ESquare.none) { State.Key ^= Zobrist.GetZobristEnPessant(State.EnPassantSquare.File().AsInt()); } Position.InCheck = Position.IsAttacked(Position.GetPieceSquare(EPieceType.King, State.SideToMove), ~State.SideToMove); //State.GenerateMoves(true); return(0); }
public void HashSide() => Value ^= Zobrist.GetZobristSide();