/// <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); }
/// <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; }
public void Hash(int enPassantFile) => Value ^= Zobrist.GetZobristEnPessant(enPassantFile);