Exemplo n.º 1
0
        private bool isPathObstructed(Position origin, Position destination)
        {
            Figure figure = this.boardState[origin.x][origin.y];

            if (figure == null)
            {
                return(false);
            }

            if (figure is Knight)
            {
                return(false);
            }

            // Bishop same-row move
            if (origin.x == destination.x && origin.x - destination.x > 1)
            {
                int smallerOfTheTwo = origin.y > destination.y ? destination.y : origin.y;
                int largerOfTheTwo  = origin.y > destination.y ? origin.y : destination.y;
                for (int i = smallerOfTheTwo; i < largerOfTheTwo; i++)
                {
                    if (this.boardState[i][origin.y] != null)
                    {
                        // There is a piece that is in between therefore in this board state it is illegal.
                        return(true);
                    }
                }
            }
            else if (origin.y == destination.y && origin.y - destination.y > 1)
            {
                int smallerOfTheTwo = origin.x > destination.x ? destination.x : origin.x;
                int largerOfTheTwo  = origin.x > destination.x ? origin.x : destination.x;
                for (int i = smallerOfTheTwo; i < largerOfTheTwo; i++)
                {
                    if (this.boardState[origin.x][i] != null)
                    {
                        // There is a piece that is in between therefore in this board state it is illegal.
                        return(true);
                    }
                }
            }
            else // Must be either a bishop or knight specific move
                 // Out of those, only a bishop move can be obstructed
            {
                int xDelta;
                if (destination.x > origin.x)
                {
                    xDelta = -1;
                }
                else
                {
                    xDelta = 1;
                }

                int yDelta;
                if (destination.y > origin.y)
                {
                    yDelta = -1;
                }
                else
                {
                    yDelta = 1;
                }

                Position startPosition = origin;
                while (startPosition.IsValidPosition(this.BoardSize) && startPosition.x != destination.x && startPosition.y != destination.y)
                {
                    if (this.boardState[startPosition.x][startPosition.y] != null)
                    {
                        // This diagonal is obstructed and this move is illegal
                        return(true);
                    }
                    startPosition.x += xDelta;
                    startPosition.y += yDelta;
                }
            }



            return(false);
        }