private void OnSearchUpdate(object sender, SearchStatistics stats) { var score = FormatScore(stats.Score); var principalVariation = FormatPrincipalVariation(stats.PrincipalVariation, stats.PrincipalVariationMovesCount); Send($"info depth {stats.Depth} seldepth {stats.SelectiveDepth} time {stats.SearchTime} " + $"score {score} nodes {stats.TotalNodes} nps {stats.TotalNodesPerSecond} pv {principalVariation}"); if (_debugMode) { var evaluationStatistics = new EvaluationStatistics(); var openingPhase = stats.Board.GetPhaseRatio(); var endingPhase = BoardConstants.PhaseResolution - openingPhase; var fieldsAttackedByWhite = 0ul; var fieldsAttackedByBlack = 0ul; var materialEvaluation = MaterialEvaluator.Evaluate(stats.Board); var castlingEvaluation = CastlingEvaluator.Evaluate(stats.Board, openingPhase, endingPhase); var positionEvaluation = PositionEvaluator.Evaluate(stats.Board, openingPhase, endingPhase); var pawnStructureEvaluation = PawnStructureEvaluator.Evaluate(stats.Board, evaluationStatistics, openingPhase, endingPhase); var mobility = MobilityEvaluator.Evaluate(stats.Board, openingPhase, endingPhase, ref fieldsAttackedByWhite, ref fieldsAttackedByBlack); var kingSafety = KingSafetyEvaluator.Evaluate(stats.Board, openingPhase, endingPhase, fieldsAttackedByWhite, fieldsAttackedByBlack); var pieces = PiecesEvaluator.Evaluate(stats.Board, openingPhase, endingPhase); var fianchetto = FianchettoEvaluator.Evaluate(stats.Board, openingPhase, endingPhase); var total = materialEvaluation + castlingEvaluation + positionEvaluation + pawnStructureEvaluation + mobility + kingSafety + pieces; Send($"info string evaluation {total} phase {openingPhase} material {materialEvaluation} castling {castlingEvaluation} " + $"position {positionEvaluation} pawns {pawnStructureEvaluation} mobility {mobility} ksafety {kingSafety} " + $"pieces {pieces} fianchetto {fianchetto} irrmoves {stats.Board.IrreversibleMovesCount}"); } }
private void CheckForCastlingAndMove(TurnLog turnLog, PieceMovement pieceMovement) { CastlingMovementSpecification castlingMovementSpecification = CastlingMovementSpecification.Create(this); if (castlingMovementSpecification.IsSatisfied(pieceMovement)) { CastlingEvaluationResult castlingEvaluationResult = CastlingEvaluator.EvaluateCastling(this, pieceMovement); MovePiece(turnLog, PieceMovement.Create(castlingEvaluationResult.Rook, castlingEvaluationResult.From, castlingEvaluationResult.To)); } }
public void Castling_To_Left_Rook_Should_Return_Rook_Movement() { Board board = Board.Create(); Piece whiteKing = Piece.Create(PieceType.King, PieceColor.White); board.AddPiece(whiteKing, "E1"); board.AddPiece(Piece.Create(PieceType.Rook, PieceColor.White), "A1"); CastlingEvaluationResult result = CastlingEvaluator.EvaluateCastling(board, PieceMovement.Create(whiteKing, Position.Create("E1"), Position.Create("C1"))); Assert.IsNotNull(result.Rook); Assert.AreEqual("A1", result.From.Id); Assert.AreEqual("D1", result.To.Id); }
public bool IsSatisfied(PieceMovement candidate) { string[] allowedDestinations = candidate.Piece.Color == PieceColor.White ? new[] { "C1", "G1" } : new[] { "C8", "G8" }; if (candidate.Piece.Type == PieceType.King && !candidate.Piece.HasMoved && allowedDestinations.Contains(candidate.To.Id)) { Square rookSquare = CastlingEvaluator.FindRookSquare(_board, candidate); return(!rookSquare.IsEmpty && rookSquare.Piece.Type == PieceType.Rook && !rookSquare.Piece.HasMoved); } return(false); }
public void Run(params string[] parameters) { var fen = string.Join(' ', parameters); var boardState = FenToBoard.Parse(fen); var evaluationStatistics = new EvaluationStatistics(); var openingPhase = boardState.GetPhaseRatio(); var endingPhase = BoardConstants.PhaseResolution - openingPhase; var fieldsAttackedByWhite = 0ul; var fieldsAttackedByBlack = 0ul; var materialEvaluation = MaterialEvaluator.Evaluate(boardState); var castlingEvaluation = CastlingEvaluator.Evaluate(boardState, openingPhase, endingPhase); var positionEvaluation = PositionEvaluator.Evaluate(boardState, openingPhase, endingPhase); var pawnStructureEvaluation = PawnStructureEvaluator.Evaluate(boardState, evaluationStatistics, openingPhase, endingPhase); var mobility = MobilityEvaluator.Evaluate(boardState, openingPhase, endingPhase, ref fieldsAttackedByWhite, ref fieldsAttackedByBlack); var kingSafety = KingSafetyEvaluator.Evaluate(boardState, openingPhase, endingPhase, fieldsAttackedByWhite, fieldsAttackedByBlack); var pieces = PiecesEvaluator.Evaluate(boardState, openingPhase, endingPhase); var fianchetto = FianchettoEvaluator.Evaluate(boardState, openingPhase, endingPhase); var total = materialEvaluation + castlingEvaluation + positionEvaluation + pawnStructureEvaluation + mobility + kingSafety + pieces + fianchetto; _interactiveConsole.WriteLine($"Evaluation for board with hash {boardState.Hash} (phase {openingPhase}, " + $"{boardState.IrreversibleMovesCount} irreversible moves)"); _interactiveConsole.WriteLine($" = Material: {materialEvaluation}"); _interactiveConsole.WriteLine($" = Castling: {castlingEvaluation}"); _interactiveConsole.WriteLine($" = Position: {positionEvaluation}"); _interactiveConsole.WriteLine($" = Pawns: {pawnStructureEvaluation}"); _interactiveConsole.WriteLine($" = Mobility: {mobility}"); _interactiveConsole.WriteLine($" = King safety: {kingSafety}"); _interactiveConsole.WriteLine($" = Pieces evaluation: {pieces}"); _interactiveConsole.WriteLine($" = Fianchetto evaluation: {fianchetto}"); _interactiveConsole.WriteLine(); _interactiveConsole.WriteLine($" = Total: {total}"); }
public static int Evaluate(BoardState board, EvaluationStatistics statistics) { var openingPhase = board.GetPhaseRatio(); var endingPhase = BoardConstants.PhaseResolution - openingPhase; var result = MaterialEvaluator.Evaluate(board); result += PositionEvaluator.Evaluate(board, openingPhase, endingPhase); result += PawnStructureEvaluator.Evaluate(board, statistics, openingPhase, endingPhase); if (endingPhase != BoardConstants.PhaseResolution) { var fieldsAttackedByWhite = 0ul; var fieldsAttackedByBlack = 0ul; result += MobilityEvaluator.Evaluate(board, openingPhase, endingPhase, ref fieldsAttackedByWhite, ref fieldsAttackedByBlack); result += KingSafetyEvaluator.Evaluate(board, openingPhase, endingPhase, fieldsAttackedByWhite, fieldsAttackedByBlack); result += CastlingEvaluator.Evaluate(board, openingPhase, endingPhase); result += FianchettoEvaluator.Evaluate(board, openingPhase, endingPhase); result += PiecesEvaluator.Evaluate(board, openingPhase, endingPhase); } return(board.ColorToMove == Color.White ? result : -result); }