/// <summary> /// Checks to see if the Pawn can move to the specified location /// </summary> /// <param name="square">The Square the Pawn is moving to</param> /// <returns>True if the move is valid</returns> public override bool CanMoveTo(Square square) { if (square.ColumnLabel == Space.ColumnLabel) //Normal movement { int distance = square.RowNumber - Space.RowNumber; if (distance == 1) { return(true); } else if (distance == -1 && !white) { return(true); } else if (distance == 2 && white && !hasMoved) { return(true); } else if (distance == -2 && !white && !hasMoved) { return(true); } } else if (square.Piece != null && IsDiagonalTo(square)) //Taking Pieces { if (IsWhite) { if (square.Equals(Space.TopLeftOf())) { return(true); } else if (square.Equals(Space.TopRightOf())) { return(true); } } else { if (square.Equals(Space.BottomLeftOf())) { return(true); } else if (square.Equals(Space.BottomRightOf())) { return(true); } } } //En Passant else if ((Space.RightOf().Piece is Pawn || Space.LeftOf().Piece is Pawn) && ((Space.RightOf().Piece != null && Space.RightOf().Piece.IsWhite != IsWhite) || (Space.LeftOf().Piece != null && Space.LeftOf().Piece.IsWhite != IsWhite))) { if (Program.lastMoved.Space.Equals(Space.RightOf()) || Program.lastMoved.Space.Equals(Space.LeftOf())) { if (Math.Abs(Program.lastEndSpace.RowNumber - Program.lastStartSpace.RowNumber) == 2) { if (square.Piece == null && IsDiagonalTo(square)) { if (IsWhite) { if (square.RowNumber > Space.RowNumber) { return(true); } } else { if (square.RowNumber < Space.RowNumber) { return(true); } } } } } } return(false); }
/// <summary> /// MoveTo for Kings, used to implement Castling /// </summary> /// <param name="square">The Square to move to</param> /// <returns>True if the move was successful</returns> public override bool MoveTo(Square square) { bool b = base.MoveTo(square); if (Math.Abs(Board.colLabels.IndexOf(square.ColumnLabel) - Board.colLabels.IndexOf(Program.lastStartSpace.ColumnLabel)) == 2) //Castling { if (Board.colLabels.IndexOf(square.ColumnLabel) - Board.colLabels.IndexOf(Program.lastStartSpace.ColumnLabel) > 0) //Moving right { Program.board[Board.colLabels[Board.colLabels.IndexOf(Program.lastStartSpace.ColumnLabel) + 3] + Program.lastStartSpace.RowNumber.ToString()].Piece.MoveTo(Space.LeftOf(), true); } else //Moving left { Program.board[Board.colLabels[Board.colLabels.IndexOf(Program.lastStartSpace.ColumnLabel) - 4] + Program.lastStartSpace.RowNumber.ToString()].Piece.MoveTo(Space.RightOf(), true); } } return(b); }