Exemplo n.º 1
0
        // ToDo: Handle promotion of pawns
        public override bool DidMove(
            Player player, Player opponent, Location origin, Location destination)
        {
            // Can move up to 2 rows toward opponent on 1st move
            bool isInitialMove = false;
            var  piece         = player.Pieces.Find(p => p.CurrentLocation == origin);

            if (piece.PreviousLocation == null)
            {
                isInitialMove = true;
            }

            int limit = 1;

            if (isInitialMove)
            {
                limit = 2;
            }

            bool isLegalMoveThroughRows =
                MoveAssistant.IsLegalMoveThroughRows(player, opponent, origin, destination, limit, false);

            // If is capture, can move 1 square diagonally toward opponent
            // ToDo: Handle case of en passant capture!!!
            bool isCapture             = MoveAssistant.IsCapture(opponent, destination);
            bool isLegalMoveDiagonally = false;

            if (isCapture)
            {
                isLegalMoveDiagonally =
                    MoveAssistant.IsLegalMoveDiagonally(player, opponent, origin, destination, 1, false);
            }

            // If legal through rows or diagonally, return True
            if (isLegalMoveThroughRows || isLegalMoveDiagonally)
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Tries to execute the move.
        /// </summary>
        /// <param name="playerIndex">Index of player making the move.</param>
        /// <param name="origin">The origin Location</param>
        /// <param name="destination">The destination Location</param>
        /// <param name="message">Either an empty string or a message containing UI feedback.</param>
        /// <returns>True, if successful; else false</returns>
        internal bool DidExecuteMove(
            int playerIndex, Location origin, Location destination, out string message)
        {
            int otherPlayerIndex = GetOtherPlayerIndex(playerIndex);

            Player     player   = Players[playerIndex];
            Player     opponent = Players[otherPlayerIndex];
            ChessPiece piece    = Players[playerIndex].Pieces
                                  .SingleOrDefault(p => p.CurrentLocation.Coordinate == origin.Coordinate);

            if (DidMovePiece(player, opponent, origin, destination, piece))
            {
                // Update state of captured piece
                bool isCaptured = MoveAssistant.IsCapture(opponent, destination);
                if (isCaptured)
                {
                    ChessPiece capturedPiece =
                        opponent.Pieces.SingleOrDefault(p => p.CurrentLocation == destination);
                    capturedPiece.PreviousLocation = capturedPiece.CurrentLocation;
                    capturedPiece.CurrentLocation  = null;
                    capturedPiece.IsCaptured       = true;

                    if (player.CapturedPieces == null)
                    {
                        player.CapturedPieces = new List <ChessPiece>();
                    }
                    player.CapturedPieces.Add(capturedPiece);
                    opponent.Pieces.Remove(capturedPiece);
                }

                piece.PreviousLocation = piece.CurrentLocation;
                piece.CurrentLocation  = destination;

                message = $"Moved {piece.Text} from {piece.PreviousLocation.Coordinate} to {piece.CurrentLocation.Coordinate}";
                return(true);
            }
            else
            {
                message = "Your move is not legal. Please try a different move.";
            }

            return(false);
        }