コード例 #1
0
ファイル: v10.cs プロジェクト: Daenyas/codingame
        private static void ComputeFirstStrategy()
        {
            foreach (Buster buster in Game.MyBusters)
            {
                if (buster.State == 2)
                {
                    // Stunned :/
                    // TODO : nothing to do ? // warn intercepted or something ?
                    buster.Decision.Mission = Mission.None;
                    List <Buster> mySupporters = Game.MyBusters.Where(b => b.Decision.Mission == Mission.SupportHome &&
                                                                      b.Decision.Target.Id == buster.Id      // Supporting me
                                                                      ).ToList();
                    List <Buster> ennemyAround = Game.EnnemyBusters.Where(b => b.DistanceTo(buster) < GameConstants.VisionRange + GameConstants.BustMax && b.LastUpdateTurn >= Game.Turn - 1).ToList();
                    if (ennemyAround != null)
                    {
                        Buster ennemy = Buster.GetClosest(ennemyAround, buster);
                        if (buster != null)
                        {
                            mySupporters.ForEach(sup =>
                            {
                                sup.Decision.Mission = Mission.Fight;
                                sup.Decision.Target  = ennemy;
                            });
                        }
                    }
                }
                else if (buster.State == 1)
                {
                    // Carrying ghost
                    buster.Decision.Mission = Mission.BringGhostHome;
                    buster.FirstStrategy    = true;

                    if (Game.MyScore == Game.GhostCount / 2)
                    {
                        // Warn all to secure victory
                        List <Buster> myFriends = Game.MyBusters.Where(b => b.Id != buster.Id).ToList();
                        myFriends.ForEach(f =>
                        {
                            f.Decision.Mission = Mission.SupportHome;
                            f.Decision.Target  = buster;
                            f.FirstStrategy    = true;
                        }
                                          );
                    }

                    //If not in base and ennemy around, ask for support, once.
                    if (buster.DistanceTo(ImportantCoordinates.MyBase) > GameConstants.BaseRadius)
                    {
                        List <Buster> ennemies = Game.EnnemyBusters.Where(b => b.LastUpdateTurn == Game.Turn &&
                                                                          b.State != 2 && //Maybe do something better here
                                                                          b.DistanceTo(buster) < GameConstants.VisionRange
                                                                          ).ToList();

                        List <Buster> mySupporters = Game.MyBusters.Where(b => b.Decision.Mission == Mission.SupportHome &&
                                                                          b.Decision.Target.Id == buster.Id   // Supporting me
                                                                          ).ToList();

                        if (ennemies.Count == 0 && Game.MyScore < Game.GhostCount / 2) //TODO maybe -1 ?
                        {
                            //No one around anymore, let it go
                            mySupporters.ForEach(s => s.Decision.Mission = Mission.None);
                        }
                        if (mySupporters.Count < ennemies.Count)
                        {
                            for (int i = 0; i < ennemies.Count - mySupporters.Count; ++i)
                            {
                                List <Buster> notOccupiedFriend = Game.MyBusters.Where(b => b.State != 1 &&                         // not carrying
                                                                                       b.Decision.Mission != Mission.SupportHome && // not covering someone
                                                                                       b.State != 2                                 // not stunned / TODO : beware
                                                                                       ).ToList();
                                if (notOccupiedFriend.Count > 0)
                                {
                                    Buster chosenFriend = notOccupiedFriend.First();
                                    chosenFriend.Decision.Mission = Mission.SupportHome;
                                    chosenFriend.Decision.Target  = buster;
                                    chosenFriend.FirstStrategy    = true;
                                }
                                else
                                {
                                    // Try with luck
                                    break;
                                }
                            }
                        }
                    }

                    // if around base and noone can stun him, i do it !
                    if (buster.DistanceTo(ImportantCoordinates.MyBase) < 2 * GameConstants.BaseRadius)
                    {
                        List <Buster> ennemiesAround = Game.EnnemyBusters.Where(b => b.LastUpdateTurn == Game.Turn && b.State != 2 && b.DistanceTo(buster) < GameConstants.BustMax).ToList();
                        if (ennemiesAround.Count > 0)
                        {
                            Buster preventingTarget = null;
                            foreach (Buster ennemy in ennemiesAround)
                            {
                                bool yes = true;
                                foreach (Buster b in Game.MyBusters)
                                {
                                    if ((b.Decision.Mission == Mission.Steal || b.Decision.Mission == Mission.Fight) && b.Decision.Target.Id == ennemy.Id)
                                    {
                                        yes = false;
                                        break;
                                    }
                                }
                                if (yes)
                                {
                                    preventingTarget = ennemy;
                                    break;
                                }
                            }
                            if (preventingTarget != null)
                            {
                                // If ennemy on range, get his ghost
                                if (buster.Weapon == 0)
                                {
                                    //Can Stun
                                    buster.Decision.Mission = Mission.Fight;
                                    buster.Decision.Target  = preventingTarget;
                                    continue;
                                }
                                // else force the way
                            }
                        }
                    }
                }
                else
                {
                    // Do it in second strategy cause of warnings
                }
            }
        }
コード例 #2
0
ファイル: v10.cs プロジェクト: Daenyas/codingame
        private static void ComputeSecondStrategy()
        {
            List <Buster> secondList = Game.MyBusters.Where(b => b.FirstStrategy == false).ToList();

            foreach (Buster buster in secondList)
            {
                // Doing nothing or busting ghost
                List <Buster> carryingEnnemies = Game.EnnemyBusters.Where(b => b.LastUpdateTurn == Game.Turn && b.State == 1 && b.DistanceTo(buster) < GameConstants.BustMax).ToList();
                Buster        carryingTarget   = null;
                foreach (Buster ennemy in carryingEnnemies)
                {
                    bool yes = true;
                    foreach (Buster b in Game.MyBusters)
                    {
                        if (b.Decision.Mission == Mission.Steal && b.Decision.Target.Id == ennemy.Id)
                        {
                            yes = false;
                            break;
                        }
                    }
                    if (yes)
                    {
                        carryingTarget = ennemy;
                        break;
                    }
                }
                if (carryingTarget != null)
                {
                    // If ennemy on range, get his ghost
                    if (buster.Weapon == 0)
                    {
                        //Can Stun
                        buster.Decision.Mission = Mission.Steal;
                        buster.Decision.Target  = carryingTarget;
                        continue;
                    }
                    else
                    {
                        double ennemyDistanceToBase = carryingTarget.DistanceTo(ImportantCoordinates.EnnemyBase);
                        if ((ennemyDistanceToBase - GameConstants.BaseRadius) / GameConstants.BusterMove < buster.Weapon)
                        {
                            //Chase
                            buster.Decision.Mission = Mission.Steal;
                            buster.Decision.Target  = carryingTarget;
                            continue;
                        }
                    }
                }

                // If get here, nothing to stun
                if (buster.Decision.Mission == Mission.SupportHome)
                {
                    // Keep support mission or find one to do
                    continue;
                }

                if (buster.Decision.Mission == Mission.GiveVision)
                {
                    List <Buster> targets = Game.EnnemyBusters.Where(b => b.DistanceTo(buster) < GameConstants.VisionRange).ToList();
                    Buster        target  = null;
                    foreach (Buster ennemy in targets)
                    {
                        bool yes = true;
                        foreach (Buster b in Game.MyBusters)
                        {
                            if (b.Decision.Mission == Mission.Fight && b.Decision.Target.Id == ennemy.Id)
                            {
                                yes = false;
                                break;
                            }
                        }
                        if (yes)
                        {
                            target = ennemy;
                            break;
                        }
                    }
                    if (target != null)
                    {
                        buster.Decision.Mission = Mission.Fight;
                        buster.Decision.Target  = target;
                        continue;
                    }
                }

                if (buster.Decision.Mission == Mission.Fight && Game.Turn - buster.Decision.Target.LastUpdateTurn < 5 && buster.Decision.Target.State != 2)
                {
                    // Keep Fighting mission or find one to do
                    continue;
                }

                // If chasing and still have a ghost and i haven't been stunned, keep on doing so
                if (buster.Decision.Mission == Mission.Steal && buster.Decision.Target.State == 1 && buster.State != 2)
                {
                    continue;
                }

                // If we were targetting a ghost, look if we know how took it : if ennemy, try stun strategy.
                if (buster.Decision.Mission == Mission.Bust && buster.Decision.Target.LastUpdateTurn != Game.Turn)
                {
                    // can be used for generic interception ?
                    Buster carryingEnnemy = Buster.GetClosest(Game.EnnemyBusters.Where(b => b.LastUpdateTurn == Game.Turn && b.State == 1 && b.Value == buster.Decision.Target.Id).ToList(), buster);
                    if (carryingEnnemy != null)
                    {
                        if (buster.Weapon == 0 && buster.DistanceTo(carryingEnnemy) < GameConstants.BustMax)
                        {
                            //Can Stun
                            buster.Decision.Mission = Mission.Steal;
                            buster.Decision.Target  = carryingEnnemy;
                            continue;
                        }
                        else
                        {
                            double ennemyDistanceToBase = carryingEnnemy.DistanceTo(ImportantCoordinates.EnnemyBase);
                            int    ennemyTurnsToRelease = (int)Math.Round((ennemyDistanceToBase - GameConstants.BaseRadius) / GameConstants.BusterMove);
                            double myDistanceToBase     = buster.DistanceTo(ImportantCoordinates.EnnemyBase);
                            int    myTurnsToReachBase   = (int)Math.Round((myDistanceToBase - GameConstants.BaseRadius) / GameConstants.BusterMove);

                            if (myTurnsToReachBase < ennemyTurnsToRelease && myDistanceToBase < buster.Weapon)
                            {
                                //Intercept
                                buster.Decision.Mission = Mission.Steal;
                                buster.Decision.Target  = carryingEnnemy;
                                continue;
                            }
                        }
                    }
                }

                // Bust : check ennemyCount to know how to fight
                List <Ghost> visibleGhosts = Game.Ghosts.Where(g => g.LastUpdateTurn == Game.Turn &&
                                                               g.DistanceTo(buster) < GameConstants.VisionRange).ToList();
                Ghost weakest = Ghost.GetWeakest(visibleGhosts);

                List <Ghost> allVisibleGhosts = Game.Ghosts.Where(g => g.LastUpdateTurn == Game.Turn).ToList();
                Ghost        allWeakest       = Ghost.GetWeakest(allVisibleGhosts);

                if (allWeakest != null && allWeakest.DistanceTo(buster) < 5 * GameConstants.BusterMove)
                {
                    // Maybe look at time to get there
                    buster.Decision.Mission = Mission.Bust;
                    buster.Decision.Target  = allWeakest;
                    continue;
                }

                if (weakest != null)
                {
                    buster.Decision.Mission = Mission.Bust;
                    buster.Decision.Target  = weakest;
                    continue;
                }

                //No ghost around, join friend

                if (allWeakest != null)
                {
                    buster.Decision.Mission = Mission.Bust;
                    buster.Decision.Target  = allWeakest;
                    continue;
                }

                //Nothing at all... Explore !
                if (buster.Decision.Mission == Mission.Explore)
                {
                    Area targettedArea = Game.Areas.Where(a => buster.Decision.Place.X == a.Middle.X && buster.Decision.Place.Y == a.Middle.Y).FirstOrDefault();
                    if (targettedArea != null)
                    {
                        if (targettedArea.IsInside(buster.Position))
                        {
                            buster.Decision.Place = buster.FindAreaToExplore();
                        }
                        else
                        {
                            // keep doing the same
                            continue;
                        }
                    }
                }

                buster.Decision.Mission = Mission.Explore;
                buster.Decision.Place   = buster.FindAreaToExplore();
            }
        }