Exemple #1
0
 private void OnPromotePawnEvent(PromotionEventArgs e)
 {
     if (context is not null)
     {
         context.Send(delegate { OnPromotePawn(this, e); }, null);
     }
     else
     {
         OnPromotePawn(this, e);
     }
 }
Exemple #2
0
    internal static bool IsValidMove(Move move, ChessBoard board, bool raise, bool checkTurn)
    {
        if (move is null || !move.HasValue)
        {
            throw new ArgumentNullException(nameof(move));
        }

        if (board.pieces[move.OriginalPosition.Y, move.OriginalPosition.X] is null)
        {
            throw new ChessPieceNotFoundException(board, move.OriginalPosition);
        }

        if (checkTurn && board.pieces[move.OriginalPosition.Y, move.OriginalPosition.X].Color != board.Turn)
        {
            return(false);
        }

        if (move.OriginalPosition == move.NewPosition)
        {
            return(false);
        }

        move.Piece         = board.pieces[move.OriginalPosition.Y, move.OriginalPosition.X];
        move.IsCheck       = false;
        move.IsMate        = false;
        move.CapturedPiece = null;
        move.San           = null;

        MovePromotion?promParams = null;

        // Promotion result can be already specified so we dont need to invoke event again to ask for prom result
        if (move.Parameter is MovePromotion p)
        {
            promParams = new MovePromotion(p.PromotionType);
        }

        move.Parameter = null;

        bool isValid = IsValidMove(move, board);
        // If is not valid => don't validate further
        bool isChecked = !isValid || IsKingCheckedValidation(move, move.Piece.Color, board);

        if (!isChecked)
        {
            // Capture
            if (board.pieces[move.NewPosition.Y, move.NewPosition.X] is not null &&
                board.pieces[move.NewPosition.Y, move.NewPosition.X].Color != move.Piece.Color)
            {
                move.CapturedPiece = board.pieces[move.NewPosition.Y, move.NewPosition.X];
            }

            // Promote, Invoke event only when raises == true AND promotion parameters has been not specified yet
            if (move.Parameter is MovePromotion promotion)
            {
                if (promParams != null && promParams.PromotionType != PromotionType.Default)
                {
                    move.Parameter = promParams;
                }
                else if (raise)
                {
                    var args = new PromotionEventArgs(board);
                    board.OnPromotePawnEvent(args);
                    promotion.PromotionType = args.PromotionResult;
                }
            }

            // Check on opposite king
            move.IsCheck = IsKingCheckedValidation(move, move.Piece.Color.OppositeColor(), board);

            // Opposite king is in (stale)mate
            move.IsMate = !PlayerHasMovesValidation(move, move.Piece.Color.OppositeColor(), board);

            return(true);
        }
        else
        {
            if (isValid && raise)
            {
                board.OnInvalidMoveKingCheckedEvent(new CheckEventArgs(board, move.Piece.Color == PieceColor.White ? board.WhiteKing : board.BlackKing, true));
            }
            return(false);
        }
    }
Exemple #3
0
 static void chess_WhitePawnPromoted(object sender, PromotionEventArgs e)
 {
     e.PromotionPiece = promotion;
 }
Exemple #4
0
 private void HandleBlackPawnPromotedEvent(object sender, PromotionEventArgs e)
 {
     e.PromotionPiece = PawnPromotionDialog.ShowPawnPromotionDialog(this);
 }