示例#1
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);

            if (rowDistance != colDistance)
            {
                throw new InvalidOperationException(BishopInvalidMove);
            }

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

            int rowIndex = from.Row;
            char colIndex = from.Col;
            var other = figure.Color == ChessColor.White ? ChessColor.Black : ChessColor.White;

            // top-right
            while (true)
            {
                rowIndex++;
                colIndex++;
                if (to.Row == rowIndex && to.Col == colIndex)
                {
                    var figureAtPosition = board.GetFigureAtPosition(to);
                    if (figureAtPosition != null && figureAtPosition.Color == figure.Color)
                    {
                        throw new InvalidOperationException("There is a figure on the way!");
                    }
                    else
                    {
                        return;
                    }
                }

                var position = Position.FromChessCoordinates(rowIndex, colIndex);
                var figAtPosition = board.GetFigureAtPosition(position);
                if (figAtPosition != null)
                {
                    throw new InvalidOperationException("There is a figure on the way!");
                }
            }
        }
示例#2
0
        public void ValidateMove(IFigure figure, IBoard board, Move move)
        {
            var other = figure.Color == ChessColor.White ? ChessColor.White : ChessColor.Black;
            var from = move.From;
            var to = move.To;

            if ((from.Row == to.Row + 1 && from.Col == to.Col + 1) || // top right
                (from.Row == to.Row + 1 && from.Col == to.Col) || // top center
                (from.Row == to.Row + 1 && from.Col == to.Col - 1) || // top left
                (from.Row == to.Row && from.Col == to.Col - 1) || // left
                (from.Row == to.Row && from.Col == to.Col + 1) || // right
                (from.Row == to.Row - 1 && from.Col == to.Col + 1) || // bottom right)
               (from.Row == to.Row - 1 && from.Col == to.Col) || // bottom center
                (from.Row == to.Row - 1 && from.Col == to.Col - 1)) // bottom left;
            {
                if (this.CheckOtherFigureIfValid(board, to, other))
                {
                    return;
                }
            }

            throw new InvalidOperationException(KingInvalidMove);
        }
示例#3
0
        public void ValidateMove(IFigure figure, IBoard board, Move move)
        {
            var color = figure.Color;
            var other = figure.Color == ChessColor.White ? ChessColor.White : ChessColor.Black;
            var from = move.From;
            var to = move.To;

            // TODO: [Bug] е7-а6 is a valid move
            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 && this.CheckDiagonalMove(from, to))
                {
                    if (this.CheckOtherFigureIfValid(board, to, other))
                    {
                        return;
                    }
                }
            }
            else if (color == ChessColor.Black )
            {
                if (from.Row - 1 == to.Row && this.CheckDiagonalMove(from, to))
                {
                    if (this.CheckOtherFigureIfValid(board, to, other))
                    {
                        return;
                    }
                }
            }

            // TODO: remove 2 magic number (const)
            if (from.Row == 2 && color == ChessColor.White)
            {
                if (from.Row + 2 == to.Row &&
                    this.CheckOtherFigureIfValid(board, to, other))
                {
                    return;
                }
            }
            else if (from.Row == 7 && color == ChessColor.Black)
            {
                if (from.Row - 2 == to.Row &&
                    this.CheckOtherFigureIfValid(board, to, other))
                {
                    return;
                }
            }

            if (from.Row + 1 == to.Row && color == ChessColor.White)
            {
                if (this.CheckOtherFigureIfValid(board, to, other))
                {
                    return;
                }
            }
            else if (from.Row - 1 == to.Row && color == ChessColor.Black)
            {
                if (this.CheckOtherFigureIfValid(board, to, other))
                {
                    return;
                }
            }

            throw new InvalidOperationException(PawnInvalidMove);
        }