Example #1
0
        /// <summary>
        /// Generates the move.
        /// </summary>
        /// <param name="board">The board</param>
        /// <param name="from">The starting square</param>
        /// <param name="to">The ending square</param>
        /// <returns></returns>
        internal override Move GenerateMove(Board board, int from, int to)
        {
            Move move;

            // if it's an en passant capture
            if (IsEnPassantCaptureMove(board, from, to))
            {
                move = new EnPassantCaptureMove(board.Status, from, to, this);

                move.ChangeSideToMove();       // change the side to move
                move.SetEnPassantTarget(null); // reset the en passant target
                move.ResetPly();               // reset the ply
                move.IncrementMoves();         // increment the number of moves

                // we need to verify for check
                move.Make(board);
                var result = !board.BlackKingInCheck();
                move.TakeBack(board);
                return(result ? move : null);
            }

            move = base.GenerateMove(board, from, to);

            if (move != null)
            {
                move.ResetPly();// reset the ply

                // if it's the two-squares move
                if (IsTwoSquaresMove(board, from, to))
                {
                    // set the en passant target
                    move.SetEnPassantTarget(to - Board.SideSquareNo);
                }

                // if it's a promotion
                if (Board.Rank(to) == Board.SideSquareNo - 1)
                {
                    // we don't need to verify for check again
                    // so the promotion delegate will not be triggered
                    // later we will change the promotion type as needed
                    move = new PromotionMove(board.Status, from, to, this);

                    move.ChangeSideToMove();                     // change the side to move
                    move.SetEnPassantTarget(null);               // reset the en passant target
                    move.ResetPly();                             // reset the ply
                    move.IncrementMoves();                       // increment the number of moves
                    ((PromotionMove)move).SetCapture(board[to]); // the capture information is set
                }

                return(move);
            }
            else
            {
                return(null);
            }
        }
Example #2
0
        private string FormatPromotion(PromotionMove move, IChessPieceFormatter pieceFormatter)
        {
            string promotingPiece = pieceFormatter.FormatPiece(Pieces.PieceType.Pawn);
            string newPiece       = pieceFormatter.FormatPiece(move.NewPieceType.Type);

            return(promotingPiece +
                   move.OldPosition.ToString() +
                   (move.PieceCaptured ? " x " : " ") +
                   move.NewPosition.ToString() + " = " +
                   newPiece);
        }
Example #3
0
        public void ZobristUpdateTest_Promotion()
        {
            Move  move         = new PromotionMove(gameState, BitboardUtils.GetBitboard(52), BitboardUtils.GetBitboard(61), PieceType.Queen);
            ulong expectedHash = GetPieceBitstring(0, PieceType.Pawn, 52) ^ GetPieceBitstring(0, PieceType.Queen, 61)
                                 ^ GetPieceBitstring(1, PieceType.Rook, 61) ^ GetBlackTurnBitstring();

            TestMove("r4rk1/1pp1Pppp/pb6/6B1/2B1n1b1/P1N2N2/1Pn1QPPP/R4RK1 w - - 0 5", move, expectedHash);

            move         = new PromotionMove(gameState, BitboardUtils.GetBitboard(9), BitboardUtils.GetBitboard(1), PieceType.Knight);
            expectedHash = GetPieceBitstring(1, PieceType.Pawn, 9) ^ GetPieceBitstring(1, PieceType.Knight, 1) ^ GetBlackTurnBitstring();
            TestMove("r7/1p3kpp/pb6/3N2B1/4Q1b1/P4N2/1pn2PPP/4RRK1 b - - 1 11", move, expectedHash);
        }
Example #4
0
    private int ReplacePromotionMove(List <Move> moveList)
    {
        int replaceCount = 0;

        int lastRow = team == Team.White ? 7 : 0;

        for (int i = 0; i < moveList.Count; i++)
        {
            if (moveList[i].To.y == lastRow && moveList[i].Piece is Pawn)
            {
                replaceCount++;
                if (moveList[i] is CaptureMove)
                {
                    moveList[i] = new PromotionCaptureMove((CaptureMove)moveList[i]);
                }
                else
                {
                    moveList[i] = new PromotionMove(moveList[i]);
                }
            }
        }
        return(replaceCount);
    }
Example #5
0
 /// <summary>
 /// Initializes a new instance of the <see cref="PromotionSelectedEventArgs"/> class.
 /// </summary>
 /// <param name="move">The promotion move.</param>
 public PromotionSelectedEventArgs(PromotionMove move)
 {
     Move = move;
 }
Example #6
0
        public async Task HandleMessageAsync(IPlayer player, IReceivedMessage msg)
        {
            IGameMove chessGameMove = null;

            if (msg is MakeChessMoveMessage)
            {
                var chessMoveMsg = (MakeChessMoveMessage)msg;
                chessGameMove = new ChessGameMove(
                    chessMoveMsg.X_StartPosition, chessMoveMsg.Y_StartPosition,
                    chessMoveMsg.X_FinishedPosition, chessMoveMsg.Y_FinishedPosition);
            }
            else
            {
                var chessMoveMsg = (PawnPromotionMessage)msg;
                chessGameMove = new PromotionMove(chessMoveMsg.PromotionPiece);
            }

            ChessGameSession session;

            try
            {
                session = (ChessGameSession)collections.FindSessionOfAPlayer(player);
            }
            catch (InvalidOperationException)
            {
                var msgText = "This player is not connected to any game session.";
                await messageSender.SendMessageAsync(player.Socket,
                                                     new InvalidStateMessage(msgText));

                return;
            }

            var result = session.Play(player, chessGameMove);

            switch (result)
            {
            case PlayResult.Success:
            case PlayResult.Check:
                await SendToBothAsync(session, player, result);

                break;

            case PlayResult.Draw:
                await SendStalemateAsync(session);

                collections.RemoveSession(session);
                break;

            case PlayResult.YouWin:
                await SendGameFinishedAsync(session, player);

                collections.RemoveSession(session);
                break;

            case PlayResult.PromotionRequired:
                await SendPromotionAsync(player);

                break;

            default:
                await SendErrorMsgToSenderAsync(player, result);

                break;
            }
        }
 public bool Equals(PromotionMove other)
 {
     return(PromotionPiece == other.PromotionPiece);
 }