Esempio n. 1
0
        /// <summary>
        /// Move a peice from one location on the board to another.
        /// </summary>
        /// <param name="from">The location of the piece that is moving.</param>
        /// <param name="to">The location to move to.</param>
        /// <returns>Returns true on success or false on failure.</returns>
        public bool ActionPiece(Point from, Point to, bool bypassValidaiton = false)
        {
            if (bypassValidaiton || PieceActions(from).Contains(to))
            {
                ChessPiece movingPeice = boardArray[from.x, from.y];
                if (movingPeice is Pawn)
                {
                    Pawn pawn = (Pawn)movingPeice;
                    // If this was a double jump, check enpassant
                    if (Math.Abs(from.y - to.y) == 2)
                    {
                        int adjasentX = to.x - 1;
                        if (adjasentX > -1 &&
                            boardArray[adjasentX, to.y] != null &&
                            boardArray[adjasentX, to.y].Player != movingPeice.Player &&
                            boardArray[adjasentX, to.y] is Pawn)
                        {
                            if (!bypassValidaiton)
                            {
                                ((Pawn)boardArray[adjasentX, to.y]).CanEnPassantRight = true;
                            }
                        }
                        adjasentX += 2;
                        if (adjasentX < COLUMNS &&
                            boardArray[adjasentX, to.y] != null &&
                            boardArray[adjasentX, to.y].Player != movingPeice.Player &&
                            boardArray[adjasentX, to.y] is Pawn)
                        {
                            if (!bypassValidaiton)
                            {
                                ((Pawn)boardArray[adjasentX, to.y]).CanEnPassantLeft = true;
                            }
                        }
                    }
                    // If this was a sideways jump to null, it was enpassant!
                    if (from.x != to.x && boardArray[to.x, to.y] == null)
                    {
                        boardArray[to.x, from.y] = null;
                    }

                    if (!bypassValidaiton) // Pawns can't double jump after they move.
                    {
                        pawn.CanDoubleJump = false;
                    }
                }
                if (movingPeice is CastlePiece)
                {
                    CastlePiece rookOrKing = (CastlePiece)movingPeice;
                    if (!bypassValidaiton) // Castling can't be done after moving
                    {
                        rookOrKing.CanCastle = false;
                    }
                }
                if (movingPeice is King)
                {
                    King king = (King)movingPeice;
                    if (from.x - to.x == 2)
                    {   // Move rook for Queenside castle
                        boardArray[to.x + 1, from.y] = boardArray[0, from.y];
                        boardArray[0, from.y]        = null;
                    }
                    if (from.x - to.x == -2)
                    {   // Move rook for Kingside castle
                        boardArray[to.x - 1, from.y]    = boardArray[COLUMNS - 1, from.y];
                        boardArray[COLUMNS - 1, from.y] = null;
                    }
                }
                movingPeice.CalculateMoves();
                boardArray[from.x, from.y] = null;
                boardArray[to.x, to.y]     = movingPeice;
                return(true);
            }
            return(false);
        }