public void ValidateMove(IFigure figure, IBoard board, Move move)
        {
            var from = move.From;
            var to   = move.To;

            var horizontalMovement = Math.Abs(from.Row - to.Row);
            var verticalMovement   = Math.Abs(from.Col - to.Col);

            if (horizontalMovement == 0 || verticalMovement == 0)
            {
                if (horizontalMovement == 0)
                {
                    char colIndex     = from.Col;
                    char colDirection = (char)(from.Col < to.Col ? 1 : -1);

                    while (true)
                    {
                        colIndex += colDirection;

                        if (to.Col == colIndex)
                        {
                            MovementValidator.CheckForFigureOnTheWay(figure, board, to);
                            return;
                        }

                        var position = Position.FromChessCordinates(move.From.Row, colIndex);
                        MovementValidator.CheckForFigureOnTheWay(figure, board, position);
                    }
                }
                else
                {
                    int rowIndex     = from.Row;
                    int rowDirection = from.Row < to.Row ? 1 : -1;

                    while (true)
                    {
                        rowIndex += rowDirection;

                        if (to.Row == rowIndex)
                        {
                            MovementValidator.CheckForFigureOnTheWay(figure, board, to);
                            return;
                        }

                        var position = Position.FromChessCordinates(rowIndex, move.From.Col);
                        MovementValidator.CheckForFigureOnTheWay(figure, board, position);
                    }
                }
            }
            else
            {
                if (figure.GetType().Name != "Queen")
                {
                    throw new InvalidOperationException($"{figure.GetType().Name} cannot move that way!");
                }
            }
        }
Exemplo n.º 2
0
        public void ValidateMove(IFigure figure, IBoard board, Move move)
        {
            var rowDistance = Math.Abs(move.From.Row - move.To.Row);
            var colDistance = Math.Abs(move.From.Col - move.To.Col);

            // TODO: extract to method
            var other = figure.Color == ChessColor.White ? ChessColor.Black : ChessColor.White;

            if (rowDistance != colDistance)
            {
                throw new InvalidOperationException($"{figure.GetType().Name} cannot move this way!");
            }

            var from = move.From;
            var to   = move.To;

            int  rowIndex = from.Row;
            char colIndex = from.Col;


            int  rowDirection = from.Row < to.Row ? 1 : -1;
            char colDirection = (char)(from.Col < to.Col ? 1 : -1);


            while (true)
            {
                rowIndex += rowDirection;
                colIndex += colDirection;

                if (to.Row == rowIndex && to.Col == colIndex)
                {
                    MovementValidator.CheckForFigureOnTheWay(figure, board, to);
                    return;
                }

                var position = Position.FromChessCordinates(rowIndex, colIndex);
                MovementValidator.CheckForFigureOnTheWay(figure, board, position);
            }
        }
Exemplo n.º 3
0
        public void ValidateMove(IFigure figure, IBoard board, Move move)
        {
            var color            = figure.Color;
            var other            = figure.Color == ChessColor.White ? ChessColor.Black : ChessColor.White;
            var from             = move.From;
            var to               = move.To;
            var figureAtPosition = board.GetFigureAtPosition(to);

            if (color == ChessColor.White && to.Row < from.Row)
            {
                throw new InvalidOperationException(PawnBackwardsErrorMessage);
            }

            if (color == ChessColor.Black && to.Row > from.Row)
            {
                throw new InvalidOperationException(PawnBackwardsErrorMessage);
            }

            if (color == ChessColor.White)
            {
                if (from.Row + 1 == to.Row && MovementValidator.IsItADiagonalMove(from, to))
                {
                    if (MovementValidator.IsItValidOponentFigure(board, to, other))
                    {
                        return;
                    }
                }
                if (from.Row == GlobalConstants.WhitePawnsStartingRow && !MovementValidator.IsItADiagonalMove(from, to))
                {
                    if (from.Row + 2 == to.Row && figureAtPosition == null)
                    {
                        return;
                    }
                }
                if (from.Row + 1 == to.Row && !MovementValidator.IsItADiagonalMove(from, to))
                {
                    if (figureAtPosition == null)
                    {
                        return;
                    }
                }
            }
            else if (color == ChessColor.Black)
            {
                if (from.Row - 1 == to.Row && MovementValidator.IsItADiagonalMove(from, to))
                {
                    if (MovementValidator.IsItValidOponentFigure(board, to, other))
                    {
                        return;
                    }
                }
                if (from.Row == GlobalConstants.BlackPawnsStartingRow && !MovementValidator.IsItADiagonalMove(from, to))
                {
                    if (from.Row - 2 == to.Row && figureAtPosition == null)
                    {
                        return;
                    }
                }
                if (from.Row - 1 == to.Row && !MovementValidator.IsItADiagonalMove(from, to))
                {
                    if (figureAtPosition == null)
                    {
                        return;
                    }
                }
            }

            throw new InvalidOperationException(PawnInvalidMove);
        }