public static void DoTurn(PlanetWars pw)
 {
     foreach (Planet source in pw.MyPlanets())
     {
         if (source.NumShips() < 10 * source.GrowthRate())
         {
             continue;
         }
         Planet dest         = null;
         int    bestDistance = 999999;
         foreach (Planet p in pw.EnemyPlanets())
         {
             int dist = pw.Distance(source.PlanetID(), p.PlanetID());
             if (dist < bestDistance)
             {
                 bestDistance = dist;
                 dest         = p;
             }
         }
         if (dest != null)
         {
             pw.IssueOrder(source, dest, source.NumShips());
         }
     }
 }
Exemple #2
0
    private static Planet GetToAttack(PlanetWars pw, Planet source)
    {
        // (3) Find the weakest enemy or neutral planet.
        Planet dest = null;

        log.WriteLine("========== Enemy planets ====");
        double destScore = Double.MinValue;

        foreach (Planet p in pw.NotMyPlanets())
        {
            int    requiredShips = GetPlannedShips(pw, p);
            var    distance      = pw.Distance(source.PlanetID, p.PlanetID);
            var    rate          = p.GrowthRate;
            double score         = (double)rate / (double)(requiredShips * distance);

            log.WriteLine("Planet ID={0} Score={6} Owner{1} Ships={2} GrowthRate={3} Dist={4} requiredShips={5}",
                          p.PlanetID, p.Owner, p.NumShips, rate, distance, requiredShips, score);

            if (score > destScore)
            {
                destScore = score;
                dest      = p;
            }
        }
        return(dest);
    }
Exemple #3
0
    public static void DoTurn(PlanetWars pw)
    {
        if (CurrentTurn == 0 && pw.Distance(pw.MyPlanets().First().PlanetID, pw.EnemyPlanets().First().PlanetID) <= 8)
            IsKamikazi = true;

        if (IsKamikazi)
        {
            foreach (var planet in pw.MyPlanets())
            {
                var enemy = pw.EnemyPlanets().FirstOrDefault();
                int shipsHeaded = pw.Fleets().Where(f => f.DestinationPlanet == planet.PlanetID).Sum(f => f.NumShips);
                if (enemy != null)
                {
                    if (planet.NumShips - shipsHeaded - 1 > 0)
                        pw.IssueOrder(planet, enemy, planet.NumShips - shipsHeaded - 1);
                }
                else
                {
                    foreach (int dest in pw.EnemyFleets().Select(f => f.DestinationPlanet).Distinct())
                    {
                        if (dest == planet.PlanetID)
                            break;

                        int headedTo = pw.EnemyFleets().Where(f => f.DestinationPlanet == dest).Sum(f => f.NumShips);
                        int numToSend = ( (planet.NumShips - shipsHeaded) > headedTo) ? headedTo + 1 : (planet.NumShips - shipsHeaded);

                        if (numToSend > 0)
                            pw.IssueOrder(planet, pw.GetPlanet(dest), numToSend);
                    }
                }
            }
        }
        else
        {
            foreach (var planet in pw.MyPlanets())
            {
                int available = AvailableFleets(planet, pw);
                var enemy = pw.NotMyPlanets().Where(p => CanSend(planet, p, pw));
                enemy = enemy.OrderBy(p => GetScore(planet, p, pw));
                int skip = 0;

                while (available > 0)
                {
                    var bestEnemy = enemy.Skip(skip).FirstOrDefault();
                    skip++;

                    if (bestEnemy != null)
                    {
                        int numToSend = GetNumFleetsToSend(planet, bestEnemy, pw);
                        if (available > numToSend)
                        {

                            pw.IssueOrder(planet, bestEnemy, numToSend);
                            available = available - numToSend;
                        }
                    }
                    else
                    {
                        available = 0;
                    }
                }
            }
        }
    }
Exemple #4
0
    // The DoTurn function is where your code goes. The PlanetWars object
    // contains the state of the game, including information about all planets
    // and fleets that currently exist. Inside this function, you issue orders
    // using the pw.IssueOrder() function. For example, to send 10 ships from
    // planet 3 to planet 8, you would say pw.IssueOrder(3, 8, 10).
    //
    // There is already a basic strategy in place here. You can use it as a
    // starting point, or you can throw it out entirely and replace it with
    // your own. Check out the tutorials and articles on the contest website at
    // http://www.ai-contest.com/resources.
    private static double GetScore(Planet source, Planet dest, PlanetWars pw)
    {
        // on the first turn, don't attack the enemy -- take as many neutral planets as possible
        if (CurrentTurn == 0 && dest.Owner == 2)
            return double.MaxValue;

        double score = dest.NumShips / 1 + dest.GrowthRate;
        if (dest.Owner != 0)
            score = score * 1.1;

        int distance = pw.Distance(source.PlanetID, dest.PlanetID);
        score = score * (double)(distance / 2);
        return score;
    }
Exemple #5
0
    private static int GetNumFleetsToSend(Planet source, Planet dest, PlanetWars pw)
    {
        int distance = pw.Distance(source.PlanetID, dest.PlanetID);
        int inFlight = pw.MyFleets().Where(f => f.DestinationPlanet == dest.PlanetID && f.TurnsRemaining < distance)
                                    .Sum(f => f.NumShips);

        int shipsOnArrival = (dest.Owner == 0) ? dest.NumShips : dest.NumShips + (distance * dest.GrowthRate);
        return (shipsOnArrival  + 1) - inFlight;
    }
Exemple #6
0
    public static void DoTurn(PlanetWars pw)
    {
        /*
         * basically I need to find out if I need to do something to maintain my advantage, by either attacking, reinforcing, or waiting
         */
        //double x = 0, y = 0;
        List<Planet> blue = pw.MyPlanets();
        List<Planet> opfor = pw.EnemyPlanets();
        List<Planet> targets = pw.NotMyPlanets();

        int opfor_army = 0;
        int opfor_production = 0;
        int blue_army = 0;
        int blue_production = 0;
        foreach (Planet p in opfor)
        {
            opfor_army += p.NumShips();
            opfor_production += p.GrowthRate();
        }
        foreach (Fleet f in pw.EnemyFleets())
        {
            opfor_army += f.NumShips();
        }
        foreach (Planet p in blue)
        {
            blue_army += p.NumShips();
            blue_production += p.GrowthRate();
        }
        foreach (Fleet f in pw.MyFleets())
        {
            blue_army += f.NumShips();
        }
        if (blue_army > opfor_army && blue_production > opfor_production)
        {
            // I win, attack
            foreach (Planet planet in blue)
            {
                targetSort = planet;
                opfor.Sort(SortTarget);
                foreach (Planet target in opfor)
                {
                    int shipsToSend = (int)Math.Floor(planet.NumShips()*0.5);
                    pw.IssueOrder(planet, target, shipsToSend);
                    planet.NumShips(planet.NumShips() - shipsToSend);
                }

            }
        }
        targets.Sort(ComparePlanetValue);
        foreach (Planet planet in blue)
        {
            foreach (Planet target in targets)
            {
                int turns = pw.Distance(planet.PlanetID(), target.PlanetID());
                int shipsToSend = target.NumShips() + (turns * target.GrowthRate()) + 1;
                int shipsSent = 0;
                foreach (Fleet fleet in pw.MyFleets())
                {
                    if (fleet.SourcePlanet() == planet.PlanetID())
                    {
                        shipsSent += fleet.NumShips();
                    }
                }
                if (planet.NumShips() > shipsToSend && shipsSent <= shipsToSend)
                {
                    int order = shipsToSend - shipsSent;
                    pw.IssueOrder(planet, target, order);
                    planet.NumShips(planet.NumShips() - order);
                }
            }

        }
        for (int i = 0; i < 3; i++)
        {
            Planet target = targets[i];
            targetSort = target;
            if (blue.Count > 1)
            {
            //    blue.Sort(SortTarget);
            }

            //pw.IssueOrder(pw.GetPlanet(blue[0].PlanetID()), target, pw.GetPlanet(blue[0].PlanetID()).NumShips() - 5);
            /*for (int j = 0; j < blue.Count; j++)
            {
                pw.IssueOrder(blue[j], target, 1);
                //blue[j].NumShips(0);
            }*/

        }
        /*foreach (Planet planet in blue)
        {
            if (planet.Owner() == 1)
            {
                foreach (Planet target in targets)
                {
                    int shipsExtra = 0;
                    int shipsSent = 0;
                    int shipsNeeded = 0;
                    shipsNeeded = target.NumShips() + 1;
                    int takeover = shipsExtra + shipsNeeded;
                    if (target.Owner() == 2)
                    {
                        int turns = pw.Distance(planet.PlanetID(), target.PlanetID());
                        takeover += turns * target.GrowthRate();
                    }
                    if (shipsSent < takeover)
                    {
                        if (planet.NumShips() <= shipsNeeded && planet.NumShips() > planet.GrowthRate()*5)
                        {
                          //  int shipsToSend = planet.NumShips();
                          //  shipsSent += shipsToSend;
                         //   pw.IssueOrder(planet, target, shipsToSend);
                         //   planet.NumShips(planet.NumShips() - shipsToSend);
                        }
                        else if (planet.NumShips() > takeover)
                        {
                            int shipsToSend = takeover;
                            shipsSent += shipsToSend;
                            pw.IssueOrder(planet, target, shipsToSend);
                            planet.NumShips(planet.NumShips() - shipsToSend);
                        }
                    }
                }
            }

        }*/

        /*

        else
        {
            if (closest != null)
            {
                int shipCount = 0;
                foreach (Planet p in blue)
                {
                    shipCount += p.NumShips();
                }
                foreach (Planet p in blue)
                {
                    if (shipCount > closest.NumShips())
                        pw.IssueOrder(p, closest, (int)Math.Floor(p.NumShips() * 0.5));
                }
            }
            if (next_closest != null)
            {
                foreach (Planet p in pw.MyPlanets())
                {
                    if (p.NumShips() > next_closest.NumShips())
                        pw.IssueOrder(p, next_closest, (int)Math.Floor(p.NumShips() * 0.5));
                }
            }
        }*/
    }