public void Update()// Update method is called each tick of the timer in the form. This contains all the necessary code for the game to funcion with each tick.
        {
            for (int i = 0; i < map.Units.Count; i++)
            {
                if (map.Units[i] is MeleeUnit)
                {
                    for (int j = 0; j < map.Buildings.Count; j++)
                    {
                        MeleeUnit mu = (MeleeUnit)map.Units[i];
                        if (map.Buildings[j] is ResourceBuilding)
                        {
                            ResourceBuilding rb = (ResourceBuilding)map.Buildings[j];



                            if (mu.Health <= mu.MaxHealth * 0.25)// if the unit is below 25% hp, the unit flees in a random direction.
                            {
                                mu.Move(r.Next(0, 4));
                            }
                            else
                            {
                                (Unit closest, int distanceTo) = mu.Closest(map.Units);



                                if (distanceTo <= mu.AttackRange)
                                {
                                    mu.IsAttacking = true; //The unit atacks other units
                                    mu.Combat(closest);    // The unit can be attacked
                                    rb.Combat(closest);    //Allows for the builds to be attacked
                                }
                                else
                                {
                                    if (closest is MeleeUnit) // The unit moves towards the closest unit
                                    {
                                        MeleeUnit closestMu = (MeleeUnit)closest;
                                        if (mu.XPos > closestMu.XPos)
                                        {
                                            mu.Move(0);
                                        }
                                        else if (mu.XPos < closestMu.XPos)
                                        {
                                            mu.Move(2);
                                        }
                                        else if (mu.YPos > closestMu.YPos)
                                        {
                                            mu.Move(3);
                                        }
                                        else if (mu.YPos < closestMu.YPos)
                                        {
                                            mu.Move(1);
                                        }
                                    }
                                    else if (closest is RangedUnit)
                                    {
                                        RangedUnit closestRu = (RangedUnit)closest;
                                        if (mu.XPos > closestRu.XPos)
                                        {
                                            mu.Move(0);
                                        }
                                        else if (mu.XPos < closestRu.XPos)
                                        {
                                            mu.Move(2);
                                        }
                                        else if (mu.YPos > closestRu.YPos)
                                        {
                                            mu.Move(3);
                                        }
                                        else if (mu.YPos < closestRu.YPos)
                                        {
                                            mu.Move(1);
                                        }

                                        else if (closest is WizardUnit)
                                        {
                                            WizardUnit closestWu = (WizardUnit)closest;
                                            if (mu.XPos > closestWu.XPos)
                                            {
                                                mu.Move(0);
                                            }
                                            else if (mu.XPos < closestWu.XPos)
                                            {
                                                mu.Move(2);
                                            }
                                            else if (mu.YPos > closestWu.YPos)
                                            {
                                                mu.Move(3);
                                            }
                                            else if (mu.YPos < closestWu.YPos)
                                            {
                                                mu.Move(1);
                                            }
                                        }
                                    }
                                }
                            }
                        }

                        else if (map.Buildings[j] is FactoryBuilding)
                        {
                            FactoryBuilding fb = (FactoryBuilding)map.Buildings[j];



                            if (mu.Health <= mu.MaxHealth * 0.25) // if the unit is below 25% hp, the unit flees in a random direction.
                            {
                                mu.Move(r.Next(0, 4));
                            }
                            else
                            {
                                (Unit closest, int distanceTo) = mu.Closest(map.Units);



                                if (distanceTo <= mu.AttackRange)
                                {
                                    mu.IsAttacking = true; //The unit atacks other units
                                    mu.Combat(closest);    // The unit can be attacked
                                    fb.Combat(closest);    //Allows for the builds to be attacked
                                }
                                else
                                {
                                    if (closest is MeleeUnit)
                                    {
                                        MeleeUnit closestMu = (MeleeUnit)closest;
                                        if (mu.XPos > closestMu.XPos)
                                        {
                                            mu.Move(0);
                                        }
                                        else if (mu.XPos < closestMu.XPos)
                                        {
                                            mu.Move(2);
                                        }
                                        else if (mu.YPos > closestMu.YPos)
                                        {
                                            mu.Move(3);
                                        }
                                        else if (mu.YPos < closestMu.YPos)
                                        {
                                            mu.Move(1);
                                        }
                                    }
                                    else if (closest is RangedUnit)
                                    {
                                        RangedUnit closestRu = (RangedUnit)closest;
                                        if (mu.XPos > closestRu.XPos)
                                        {
                                            mu.Move(0);
                                        }
                                        else if (mu.XPos < closestRu.XPos)
                                        {
                                            mu.Move(2);
                                        }
                                        else if (mu.YPos > closestRu.YPos)
                                        {
                                            mu.Move(3);
                                        }
                                        else if (mu.YPos < closestRu.YPos)
                                        {
                                            mu.Move(1);
                                        }

                                        else if (closest is WizardUnit)
                                        {
                                            WizardUnit closestWu = (WizardUnit)closest;
                                            if (mu.XPos > closestWu.XPos)
                                            {
                                                mu.Move(0);
                                            }
                                            else if (mu.XPos < closestWu.XPos)
                                            {
                                                mu.Move(2);
                                            }
                                            else if (mu.YPos > closestWu.YPos)
                                            {
                                                mu.Move(3);
                                            }
                                            else if (mu.YPos < closestWu.YPos)
                                            {
                                                mu.Move(1);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }

                else if (map.Units[i] is RangedUnit)
                {
                    for (int j = 0; j < map.Buildings.Count; j++)
                    {
                        RangedUnit ru = (RangedUnit)map.Units[i];
                        if (map.Buildings[j] is ResourceBuilding)
                        {
                            ResourceBuilding rb = (ResourceBuilding)map.Buildings[j];



                            (Unit closest, int distanceTo) = ru.Closest(map.Units);



                            if (distanceTo <= ru.AttackRange) // Checks to see which unit is closest to attack
                            {
                                ru.IsAttacking = true;        //the unit attacks other units in range
                                ru.Combat(closest);           // the unit can be attacked
                                rb.Combat(closest);           //Allows for the builds to be attacked
                            }
                            else
                            {
                                if (closest is MeleeUnit)
                                {
                                    MeleeUnit closestMu = (MeleeUnit)closest;
                                    if (ru.XPos > closestMu.XPos)
                                    {
                                        ru.Move(0);
                                    }
                                    else if (ru.XPos < closestMu.XPos)
                                    {
                                        ru.Move(2);
                                    }
                                    else if (ru.YPos > closestMu.YPos)
                                    {
                                        ru.Move(3);
                                    }
                                    else if (ru.YPos < closestMu.YPos)
                                    {
                                        ru.Move(1);
                                    }
                                }
                                else if (closest is RangedUnit)
                                {
                                    RangedUnit closestRu = (RangedUnit)closest;
                                    if (ru.XPos > closestRu.XPos)
                                    {
                                        ru.Move(0);
                                    }
                                    else if (ru.XPos < closestRu.XPos)
                                    {
                                        ru.Move(2);
                                    }
                                    else if (ru.YPos > closestRu.YPos)
                                    {
                                        ru.Move(3);
                                    }
                                    else if (ru.YPos < closestRu.YPos)
                                    {
                                        ru.Move(1);
                                    }
                                }

                                else if (closest is WizardUnit)
                                {
                                    WizardUnit closestWu = (WizardUnit)closest;
                                    if (ru.XPos > closestWu.XPos)
                                    {
                                        ru.Move(0);
                                    }
                                    else if (ru.XPos < closestWu.XPos)
                                    {
                                        ru.Move(2);
                                    }
                                    else if (ru.YPos > closestWu.YPos)
                                    {
                                        ru.Move(3);
                                    }
                                    else if (ru.YPos < closestWu.YPos)
                                    {
                                        ru.Move(1);
                                    }
                                }
                            }
                        }


                        else if (map.Buildings[j] is FactoryBuilding)
                        {
                            FactoryBuilding fb = (FactoryBuilding)map.Buildings[j];



                            (Unit closest, int distanceTo) = ru.Closest(map.Units);



                            if (distanceTo <= ru.AttackRange) // Checks to see which unit is closest to attack
                            {
                                ru.IsAttacking = true;        // allows the unit to attack others
                                ru.Combat(closest);           // Allows for the unnnit to be attacked
                                fb.Combat(closest);           //Allows for the builds to be attacked
                            }
                            else
                            {
                                if (closest is MeleeUnit)
                                {
                                    MeleeUnit closestMu = (MeleeUnit)closest;
                                    if (ru.XPos > closestMu.XPos)
                                    {
                                        ru.Move(0);
                                    }
                                    else if (ru.XPos < closestMu.XPos)
                                    {
                                        ru.Move(2);
                                    }
                                    else if (ru.YPos > closestMu.YPos)
                                    {
                                        ru.Move(3);
                                    }
                                    else if (ru.YPos < closestMu.YPos)
                                    {
                                        ru.Move(1);
                                    }
                                }
                                else if (closest is RangedUnit)
                                {
                                    RangedUnit closestRu = (RangedUnit)closest;
                                    if (ru.XPos > closestRu.XPos)
                                    {
                                        ru.Move(0);
                                    }
                                    else if (ru.XPos < closestRu.XPos)
                                    {
                                        ru.Move(2);
                                    }
                                    else if (ru.YPos > closestRu.YPos)
                                    {
                                        ru.Move(3);
                                    }
                                    else if (ru.YPos < closestRu.YPos)
                                    {
                                        ru.Move(1);
                                    }
                                }

                                else if (closest is WizardUnit)
                                {
                                    WizardUnit closestWu = (WizardUnit)closest;
                                    if (ru.XPos > closestWu.XPos)
                                    {
                                        ru.Move(0);
                                    }
                                    else if (ru.XPos < closestWu.XPos)
                                    {
                                        ru.Move(2);
                                    }
                                    else if (ru.YPos > closestWu.YPos)
                                    {
                                        ru.Move(3);
                                    }
                                    else if (ru.YPos < closestWu.YPos)
                                    {
                                        ru.Move(1);
                                    }
                                }
                            }
                        }



                        else if (map.Units[i] is WizardUnit)
                        {
                            WizardUnit wu = (WizardUnit)map.Units[i];



                            if (wu.Health <= wu.MaxHealth * 0.50)// Wizard flees if they are below 50% hp
                            {
                                wu.Move(r.Next(0, 4));
                            }
                            else
                            {
                                (Unit closest, int distanceTo) = wu.Closest(map.Units);



                                if (distanceTo <= wu.AttackRange)
                                {
                                    wu.IsAttacking = true; //allows for the uit to attack other units
                                    wu.Combat(closest);    //Allows for the unit to be attacked
                                }
                                else
                                {
                                    if (closest is MeleeUnit)
                                    {
                                        MeleeUnit closestMu = (MeleeUnit)closest;
                                        if (wu.XPos > closestMu.XPos)
                                        {
                                            wu.Move(0);
                                        }
                                        else if (wu.XPos < closestMu.XPos)
                                        {
                                            wu.Move(2);
                                        }
                                        else if (wu.YPos > closestMu.YPos)
                                        {
                                            wu.Move(3);
                                        }
                                        else if (wu.YPos < closestMu.YPos)
                                        {
                                            wu.Move(1);
                                        }
                                    }
                                    else if (closest is RangedUnit)
                                    {
                                        RangedUnit closestRu = (RangedUnit)closest;
                                        if (wu.XPos > closestRu.XPos)
                                        {
                                            wu.Move(0);
                                        }
                                        else if (wu.XPos < closestRu.XPos)
                                        {
                                            wu.Move(2);
                                        }
                                        else if (wu.YPos > closestRu.YPos)
                                        {
                                            wu.Move(3);
                                        }
                                        else if (wu.YPos < closestRu.YPos)
                                        {
                                            wu.Move(1);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }



            currUnits.Text = "Number of units on the map: " + map.count + "";
            map.Display(grpMap);
            Round++;
        }