Exemplo n.º 1
0
        public bool IsShortMove(IChessPiece chessPieceMoved, int finalColumnPosition, int finalRowPosition)
        {
            //Check if it actually moves
            if (chessPieceMoved.Position.ColumnPosition == finalColumnPosition &&
                chessPieceMoved.Position.RowPosition == finalRowPosition)
            {
                return(false);
            }

            ClosestChessPieceComparer closestChessPieceComparer = new ClosestChessPieceComparer(chessPieceMoved);
            //Check distance between current location and destination
            var distance = closestChessPieceComparer.FloatCompare(new King(0, finalColumnPosition, finalRowPosition), chessPieceMoved);

            //Return false if distance of a move is bigger than 1 horizontal + 1 vertical = sqrt(1^2 + 1^2)
            return(distance > Math.Sqrt(2) ? false : true);
        }
Exemplo n.º 2
0
        public bool Verify(IChessPiece chessPieceMoved, int finalColumnPosition, int finalRowPosition, List <IChessPiece> chessPiecesList = null)
        {
            var closestPieceComparer = new ClosestChessPieceComparer(chessPieceMoved);
            var moveDistance         = closestPieceComparer.FloatCompare(new Knight(0, finalColumnPosition, finalRowPosition), chessPieceMoved);

            if (moveDistance >= Math.Sqrt(8) || moveDistance <= 2f)
            {
                return(false);
            }
            if (chessPiecesList == null || chessPiecesList.Count < 1)
            {
                return(true);
            }
            IChessPiece pieceAtDestination = chessPiecesList.Where(cp => cp.Position.ColumnPosition == finalColumnPosition && cp.Position.RowPosition == finalRowPosition).FirstOrDefault();

            if (pieceAtDestination == null)
            {
                return(true);
            }

            return(pieceAtDestination.Color != chessPieceMoved.Color);
        }
Exemplo n.º 3
0
        private bool IsOtherPieceBlockingMove(IChessPiece chessPieceMoved,
                                              int finalColumnPosition, int finalRowPosition,
                                              List <IChessPiece> chessPiecesList)
        {
            //Check if there are pieces on board
            if (chessPiecesList.Count < 1)
            {
                return(false);
            }

            //Filter all the ones not in the way
            var PiecesInTheWay = chessPiecesList.Where(cp => IsBishopMovement(chessPieceMoved, cp.Position.ColumnPosition, cp.Position.RowPosition));

            //Check for direction
            //Check for vertical movement
            if (chessPieceMoved.Position.RowPosition < finalRowPosition)
            {
                PiecesInTheWay = PiecesInTheWay.Where(cp => chessPieceMoved.Position.RowPosition < cp.Position.RowPosition);
            }
            else
            {
                PiecesInTheWay = PiecesInTheWay.Where(cp => chessPieceMoved.Position.RowPosition > cp.Position.RowPosition);
            }

            //Check for horizontal movement
            if (chessPieceMoved.Position.ColumnPosition < finalColumnPosition)
            {
                PiecesInTheWay = PiecesInTheWay.Where(cp => chessPieceMoved.Position.ColumnPosition < cp.Position.ColumnPosition);
            }
            else
            {
                PiecesInTheWay = PiecesInTheWay.Where(cp => chessPieceMoved.Position.ColumnPosition > cp.Position.ColumnPosition);
            }
            //Check if any pieces remain
            if (PiecesInTheWay.ToList().Count < 1)
            {
                return(false);
            }

            //Take first one
            var         closestPieceComparer = new ClosestChessPieceComparer(chessPieceMoved);
            IChessPiece closestPieceInTheWay;

            closestPieceInTheWay = PiecesInTheWay.OrderBy(cp => cp, closestPieceComparer).First();

            var distanceBetweenDestinationAndClosets = closestPieceComparer.FloatCompare(new Bishop(0, finalColumnPosition, finalRowPosition), closestPieceInTheWay);

            //Check if it blocks the move to final destination or is too far to interrupt movement
            bool isPieceInTheWay = distanceBetweenDestinationAndClosets >= 0;

            //Check if final destination and closest piece are on the same tile and different colors (Main piece tries to attack)
            bool isMainAttacking = distanceBetweenDestinationAndClosets == 0 &&
                                   closestPieceInTheWay.Color != chessPieceMoved.Color;

            //If there is no piece in the way or main piece attacks the other allow move
            if (!isPieceInTheWay || isMainAttacking)
            {
                return(false);
            }
            return(true);
        }
Exemplo n.º 4
0
        private bool IsOtherPieceBlockingMove(IChessPiece chessPieceMoved,
                                              int finalColumnPosition, int finalRowPosition,
                                              List <IChessPiece> chessPiecesList)
        {
            //Check if there are other pieces on board
            if (chessPiecesList.Count < 1)
            {
                return(false);
            }

            //Filter away all pieces that rook could not even touch
            var PiecesInTheWay = chessPiecesList.Where(cp => IsRookMovement(chessPieceMoved, cp.Position.ColumnPosition, cp.Position.RowPosition));

            //Check for direction of movement
            RookMovementDirection?direction = null;

            if (chessPieceMoved.Position.ColumnPosition < finalColumnPosition)
            {
                direction = RookMovementDirection.Right;
            }
            else if (chessPieceMoved.Position.ColumnPosition > finalColumnPosition)
            {
                direction = RookMovementDirection.Left;
            }
            else if (chessPieceMoved.Position.RowPosition < finalRowPosition)
            {
                direction = RookMovementDirection.Up;
            }
            else if (chessPieceMoved.Position.RowPosition > finalRowPosition)
            {
                direction = RookMovementDirection.Down;
            }

            switch (direction)
            {
            case RookMovementDirection.Up:
                PiecesInTheWay = PiecesInTheWay.Where(cp => chessPieceMoved.Position.RowPosition < cp.Position.RowPosition);
                break;

            case RookMovementDirection.Right:
                PiecesInTheWay = PiecesInTheWay.Where(cp => chessPieceMoved.Position.ColumnPosition < cp.Position.ColumnPosition);
                break;

            case RookMovementDirection.Down:
                PiecesInTheWay = PiecesInTheWay.Where(cp => chessPieceMoved.Position.RowPosition > cp.Position.RowPosition);
                break;

            case RookMovementDirection.Left:
                PiecesInTheWay = PiecesInTheWay.Where(cp => chessPieceMoved.Position.ColumnPosition > cp.Position.ColumnPosition);
                break;
            }

            if (PiecesInTheWay.ToList().Count < 1)
            {
                return(false);
            }

            var         closestPieceComparer = new ClosestChessPieceComparer(chessPieceMoved);
            IChessPiece closestPieceInTheWay = PiecesInTheWay.OrderBy(cp => cp, closestPieceComparer).First();

            var distanceBetweenDestinationAndClosets = closestPieceComparer.FloatCompare(new Rook(0, finalColumnPosition, finalRowPosition), closestPieceInTheWay);

            //Check if it blocks the move to final destination or is too far to interrupt movement
            bool isPieceInTheWay = distanceBetweenDestinationAndClosets >= 0;

            //Check if final destination and closest piece are on the same tile and different colors (Main piece tries to attack)
            bool isMainAttacking = distanceBetweenDestinationAndClosets == 0 &&
                                   closestPieceInTheWay.Color != chessPieceMoved.Color;

            //If there is no piece in the way or main piece attacks the other allow move
            if (!isPieceInTheWay || isMainAttacking)
            {
                return(false);
            }
            return(true);
        }