public Vector UpdatePosition(bool verbose = false)
        {
            Position = Position.AddVector(Speed);

            if (verbose)
            {
                Console.WriteLine($"--- Start of {Name}, UpdatePosition ---");
                Console.WriteLine($"{Name} position: {Position}");
                Console.WriteLine($"--- End of {Name} ---\n");
            }

            return(Position);
        }
Exemple #2
0
        public bool IsMoveValid(Board board, Vector2 endPosition)
        {
            bool valid = false;

            //Check if endPosition is out of bounds
            if (endPosition.x < 0 || endPosition.x > 7 || endPosition.y < 0 || endPosition.y > 7)
            {
                return(false);                                                                                  // Off board
            }
            //Get the delta between curent position and final position
            Vector2 delta = Position.Delta(endPosition);

            //Check if this delta conforms to a standard move
            var possibleMoves = StandardMoves.Where(x => x.x == delta.x && x.y == delta.y);

            if (possibleMoves.Count() > 0)
            {
                valid = true;
            }
            foreach (var point in possibleMoves)
            {
                var collision = board.Pieces.Where(x => x.Position.isEqual(Position.AddVector(point)));
                if (collision.Count() > 0)
                {
                    if (collision.First().Color == this.Color)
                    {
                        return(false);
                    }
                }
            }

            //Check if king would be attacking or just moving
            var pieceAtDestination = board.GetPieceAt(endPosition);

            if (pieceAtDestination != null && pieceAtDestination.Color == this.Color)
            {
                return(false);
            }

            //Check if final position puts King in check
            //Temp set piece location to endPosition
            if (pieceAtDestination != null)
            {
                board.Pieces.Remove(pieceAtDestination);
            }
            var originalPosition = this.Position;

            this.Position = endPosition;
            if (valid && Color == Color.White && board.WhiteKing != null)
            {
                valid = board.WhiteKing.SafetyCheck(board);
            }
            if (valid && Color == Color.Black && board.BlackKing != null)
            {
                valid = board.BlackKing.SafetyCheck(board);
            }

            this.Position = originalPosition;
            if (pieceAtDestination != null)
            {
                board.Pieces.Add(pieceAtDestination);
            }

            return(valid);
        }
 public void IncrementPosition()
 {
     Position.AddVector(Velocity);
 }
Exemple #4
0
        public bool IsMoveValid(Board board, Vector2 endPosition)
        {
            bool valid = true;

            IsCastling = false;

            //Check if endPosition is out of bounds
            if (endPosition.x < 0 || endPosition.x > 7 || endPosition.y < 0 || endPosition.y > 7)
            {
                return(false);                                                                                  // Off board
            }
            //Get the delta between curent position and final position
            Vector2 delta = Position.Delta(endPosition);

            if (delta.x == 0 && delta.y == 0)
            {
                return(false);                              //return if stationary
            }
            if (Math.Abs(delta.y) > 1)
            {
                return(false);                       // Too far to move
            }
            //Checks for castling. This should not be in this project, but I wanted it.
            //Castling requires prior knowledge about the board state since it only works if neither the castle nor the king have moved previously
            if (!HasMoved && Math.Abs(delta.x) == 2)
            {
                //Attempting to castle?
                if (delta.x < 0)
                {
                    //Queen castle
                    //Get Queen Rook
                    var R0 = board.GetPieceAt(new Vector2(0, Position.y));
                    if (R0 == null || R0.HasMoved)
                    {
                        return(false);
                    }
                    //Check all spaces between
                    if (board.GetPieceAt(new Vector2(1, Position.y)) != null)
                    {
                        return(false);
                    }
                    if (board.GetPieceAt(new Vector2(2, Position.y)) != null)
                    {
                        return(false);
                    }
                    if (board.GetPieceAt(new Vector2(3, Position.y)) != null)
                    {
                        return(false);
                    }

                    //Loops over all opponent pieces, checking if they have a check on this king, or any of the spaces it will occupy during the move
                    var opponents = board.Pieces.Where(x => x.Color != this.Color);
                    for (var i = opponents.Count() - 1; i >= 0; i--)
                    {
                        var piece = opponents.ElementAt(i);
                        if (piece.IsMoveValid(board, Position))
                        {
                            return(false);
                        }
                        if (piece.IsMoveValid(board, Position.AddVector(-1, 0)))
                        {
                            return(false);
                        }
                        if (piece.IsMoveValid(board, Position.AddVector(-2, 0)))
                        {
                            return(false);
                        }
                    }
                    //R0.position = new Vector2(3, position.y);
                    //position = new Vector2(2, position.y);
                    IsCastling = true;
                    return(true);
                }
                else
                {
                    //King castle
                    var R1 = board.GetPieceAt(new Vector2(7, Position.y));
                    if (R1 == null || R1.HasMoved)
                    {
                        return(false);
                    }

                    //Check all spaces between
                    if (board.GetPieceAt(new Vector2(5, Position.y)) != null)
                    {
                        return(false);
                    }
                    if (board.GetPieceAt(new Vector2(6, Position.y)) != null)
                    {
                        return(false);
                    }

                    //Loops over all opponent pieces, checking if they have a check on this king, or any of the spaces it will occupy during the move
                    var opponents = board.Pieces.Where(x => x.Color != this.Color);
                    for (var i = opponents.Count() - 1; i >= 0; i--)
                    {
                        var piece = opponents.ElementAt(i);
                        if (piece.IsMoveValid(board, Position))
                        {
                            return(false);
                        }
                        if (piece.IsMoveValid(board, Position.AddVector(1, 0)))
                        {
                            return(false);
                        }
                        if (piece.IsMoveValid(board, Position.AddVector(2, 0)))
                        {
                            return(false);
                        }
                    }
                    //R0.position = new Vector2(3, position.y);
                    //position = new Vector2(2, position.y);
                    IsCastling = true;
                    return(true);
                }
            }
            else if (Math.Abs(delta.x) > 1)
            {
                return(false);
            }

            //Check if king would be attacking or just moving
            var pieceAtDestination = board.GetPieceAt(endPosition);

            if (pieceAtDestination != null && pieceAtDestination.Color == this.Color)
            {
                return(false);
            }

            //Check if final position puts King in check
            //Temp set piece location to endPosition
            if (pieceAtDestination != null)
            {
                board.Pieces.Remove(pieceAtDestination);
            }
            var originalPosition = this.Position;

            Position = endPosition;

            valid = SafetyCheck(board);

            Position = originalPosition;
            if (pieceAtDestination != null)
            {
                board.Pieces.Add(pieceAtDestination);
            }


            return(valid);
        }
Exemple #5
0
        public bool IsMoveValid(Board board, Vector2 endPosition)
        {
            bool valid = false;

            //Check if endPosition is out of bounds
            if (endPosition.x < 0 || endPosition.x > 7 || endPosition.y < 0 || endPosition.y > 7)
            {
                return(false);                                                                                  // Off board
            }
            //Get the delta between curent position and final position
            Vector2 delta = Position.Delta(endPosition);

            //Diagonal/Stationary check
            if ((delta.x != 0 && delta.y != 0) || (delta.x == 0 && delta.y == 0))
            {
                return(false);
            }

            //Check if final position is valid
            var pieceAtDestination = board.GetPieceAt(endPosition);

            if (pieceAtDestination == null || pieceAtDestination.Color != this.Color)
            {
                valid = true;
                //Move x towards 0
                delta.x += (delta.x > 0) ? -1 : (delta.x < 0) ? 1 : 0;
                //Move y towards 0
                delta.y += (delta.y > 0) ? -1 : (delta.y < 0) ? 1 : 0;

                //Check if path is clear to arrive at valid end-point
                while (delta.x != 0 || delta.y != 0)
                {
                    //Check for collision
                    var collisionCheck = board.GetPieceAt(Position.AddVector(delta));
                    if (collisionCheck != null)
                    {
                        return(false);
                    }
                    //Move x towards 0
                    delta.x += (delta.x > 0) ? -1 : (delta.x < 0) ? 1 : 0;
                    //Move y towards 0
                    delta.y += (delta.y > 0) ? -1 : (delta.y < 0) ? 1 : 0;
                }
            }

            //Check if king would be attacking or just moving
            if (pieceAtDestination != null && pieceAtDestination.Color == this.Color)
            {
                return(false);
            }

            //Check if final position puts King in check
            //Temp set piece location to endPosition
            if (pieceAtDestination != null)
            {
                board.Pieces.Remove(pieceAtDestination);
            }
            var originalPosition = this.Position;

            this.Position = endPosition;
            if (valid && Color == Color.White && board.WhiteKing != null)
            {
                valid = board.WhiteKing.SafetyCheck(board);
            }
            if (valid && Color == Color.Black && board.BlackKing != null)
            {
                valid = board.BlackKing.SafetyCheck(board);
            }

            this.Position = originalPosition;
            if (pieceAtDestination != null)
            {
                board.Pieces.Add(pieceAtDestination);
            }

            return(valid);
        }
Exemple #6
0
        public bool IsMoveValid(Board board, Vector2 endPosition)
        {
            bool valid = false;

            //Check if endPosition is out of bounds
            if (endPosition.x < 0 || endPosition.x > 7 || endPosition.y < 0 || endPosition.y > 7)
            {
                return(false);                                                                                  // Off board
            }
            //Get the delta between curent position and final position
            Vector2 delta = Position.Delta(endPosition);

            //Based on the delta, determine if move is legal
            //Check if pawn is sliding 2 spaces vertically and pawn is in starting position
            if (delta.x == 0 && ((delta.y == 2 && Color == Color.Black && Position.y == 1) || (delta.y == -2 && Color == Color.White && Position.y == 6)))
            {
                var possibleSlides = InitialSlides[Color].Where(x => x.First().x == delta.x && x.First().y == delta.y);
                if (possibleSlides.Count() > 0)
                {
                    valid = true;
                }
                foreach (var path in possibleSlides)
                {
                    foreach (var point in path)
                    {
                        var relativePosition = Position.AddVector(point);
                        var conflicts        = board.Pieces.Where(x => x.Position.isEqual(relativePosition));
                        if (conflicts.Count() > 0)
                        {
                            return(false);
                        }
                    }
                }
            }
            else
            {
                //Check if this delta conforms to a standard move
                var possibleMoves = StandardMoves[Color].Where(x => x.x == delta.x && x.y == delta.y);
                if (possibleMoves.Count() > 0)
                {
                    valid = true;
                }
                foreach (var point in possibleMoves)
                {
                    if (board.Pieces.Where(x => x.Position.isEqual(Position.AddVector(point))).Count() > 0)
                    {
                        return(false);
                    }
                }

                //Check if this delta conforms to a standard attack
                var possibleAttacks = StandardAttacks[Color].Where(x => x.x == delta.x && x.y == delta.y);
                if (possibleAttacks.Count() > 0)
                {
                    valid = true;
                }
                //Check if there are enemies present at the attack location
                foreach (var point in possibleAttacks)
                {
                    if (board.Pieces.Where(x => x.Position.isEqual(Position.AddVector(point)) && x.Color == Color).Count() > 0)
                    {
                        return(false);
                    }
                }
            }


            //Check if king would be attacking or just moving
            var pieceAtDestination = board.GetPieceAt(endPosition);

            if (pieceAtDestination != null && pieceAtDestination.Color == this.Color)
            {
                return(false);
            }

            //Check if final position puts King in check
            //Temp set piece location to endPosition
            if (pieceAtDestination != null)
            {
                board.Pieces.Remove(pieceAtDestination);
            }
            var originalPosition = this.Position;

            this.Position = endPosition;
            if (valid && Color == Color.White && board.WhiteKing != null)
            {
                valid = board.WhiteKing.SafetyCheck(board);
            }
            if (valid && Color == Color.Black && board.BlackKing != null)
            {
                valid = board.BlackKing.SafetyCheck(board);
            }

            this.Position = originalPosition;
            if (pieceAtDestination != null)
            {
                board.Pieces.Add(pieceAtDestination);
            }

            return(valid);
        }