Exemplo n.º 1
0
 /// <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);
 }
Exemplo n.º 2
0
        /// <summary>
        /// Checks if there are pieces in the way before moving
        /// </summary>
        /// <param name="square">The Square that the Piece is moving to</param>
        /// <returns>True if there are no other pieces in the way</returns>
        public bool PathIsClear(Square square)
        {
            if (this is Knight)
            {
                return(true);
            }
            Square current = Space;
            int    xDir    = Board.colLabels.IndexOf(square.ColumnLabel) - Board.colLabels.IndexOf(current.ColumnLabel);
            int    yDir    = square.RowNumber - current.RowNumber;

            while (!current.Equals(square))
            {
                if (xDir != 0 && yDir == 0) //Moving on just x axis
                {
                    if (xDir > 0)           // moving right
                    {
                        current = current.RightOf();
                    }
                    else //moving left
                    {
                        current = current.LeftOf();
                    }
                }
                else if (xDir == 0 && yDir != 0) //Moving on just y axis
                {
                    if (yDir > 0)                //moving up
                    {
                        current = current.TopOf();
                    }
                    else //moving down
                    {
                        current = current.BottomOf();
                    }
                }
                else                  //Diagonal
                {
                    if (yDir > 0)     //moving up
                    {
                        if (xDir > 0) //moving right
                        {
                            current = current.TopRightOf();
                        }
                        else //moving left
                        {
                            current = current.TopLeftOf();
                        }
                    }
                    else              //moving down
                    {
                        if (xDir > 0) //moving right
                        {
                            current = current.BottomRightOf();
                        }
                        else //moving left
                        {
                            current = current.BottomLeftOf();
                        }
                    }
                }
                if (!current.Equals(square) && current.Piece != null)
                {
                    return(false);
                }
            }
            return(true);
        }