示例#1
0
文件: Piece.cs 项目: tha7556/Chess
        /// <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);
        }