Пример #1
0
        private Location powerup(Location s, Location l, IPirateGame game)
        {
            List <Powerup> conts = new List <Powerup>();

            foreach (Powerup p in game.Powerups())
            {
                int max = Math.Max(s.Row, l.Row);
                int mix = Math.Min(s.Row, l.Row);
                int may = Math.Max(s.Col, l.Col);
                int miy = Math.Min(s.Col, l.Col);

                if (max - mix >= p.Location.Row - mix && p.Location.Row - mix >= 0 &&             // rows contains
                    may - miy >= p.Location.Col - miy && p.Location.Col - miy >= 0)                       // cols contains
                {
                    conts.Add(p);
                }
            }

            if (conts.Count == 0)
            {
                return(null);
            }

            int[] ds = new int[conts.Count];
            for (int i = 0; i < conts.Count; i++)
            {
                ds[i] = game.Distance(s, conts[i].Location);
            }

            return(conts[sortInto(ds)[0]].Location);
        }
Пример #2
0
        // this is the actual turn
        public void DoTurn(IPirateGame game)
        {
            int remaining = game.GetActionsPerTurn();

            if (game.GetTurn() == 1)
            {
                game.Debug("moves per turn: " + remaining);
            }

            try
            {
                #region init
                if (game.GetTurn() == 1)
                {
                    chooseMap(game);
                }
                nowhere = inNowhereMode(game);
                if (nowhere)
                {
                    game.Debug("ACTIVATING NOWHERE MODE");
                }
                panic = inPanicMode(game);
                if (panic)
                {
                    game.Debug("ACTIVATING PANIC MODE");
                }

                PirateContainer.init(game);
                QueuedAttack.init();
                QueuedMotion.init();
                int ships            = game.AllMyPirates().Count;
                PirateContainer[] ps = new PirateContainer[ships];
                int[]             ds = new int[ships];
                Treasure[]        ts = new Treasure[ships];
                #endregion

                calcBestTreasure(game, ships, ref ps, ref ds, ref ts);  // calculate the closest treasure to ps[i]
                BringBackTreasure(game, ref remaining);                 // move Pirates that have treasures towards the base
                calcKamikazes(ps, ref ts, ref ds, game);                // control the kamikazes

                //BringBackTreasure(game, ref remaining); // move Pirates that have treasures towards the base
                //Powerup pt = new SpeedPowerup(-1, new Location(0, 0), 0, 0, 0);

                #region power up calculations
                Powerup[] pu = (from l in game.Powerups()
                                where l.Type == "Speed"
                                select l).ToArray();

                if (pu.Count() > 0)
                {
                    Powerup[] puu = new Powerup[ships];
                    for (int i = pu.Count() - 1; i >= 0; i--)
                    {
                        puu[i] = pu[i];
                    }
                    game.Debug("A speed powerup was found");
                    int[] dis = new int[ships];



                    locatePowerups(game, ships, ref ps, ref dis, ref puu);

                    int chosen = -1;
                    for (int i = 0, min = int.MaxValue; i < ships; ++i)
                    {
                        if (dis[i] < min && ps[i].AVALIBLE && dis[i] != 0)                        //&& !ps[i].P.HasTreasure
                        {
                            min    = dis[i];
                            chosen = i;
                        }
                    }
                    game.Debug("Moving pirate " + chosen + " towards powerup");

                    if (chosen != -1)
                    {
                        game.Debug("Distance of chosen powerup - " + dis[chosen]);
                        if (puu[chosen] == null)
                        {
                            game.Debug("we found the problem");
                        }

                        remaining -= move(ps[chosen], puu[chosen].Location, remaining, game);
                    }
                }
                #endregion

                #region panic mode
                Pirate k = null, tar = null;
                if (panic)
                {
                    search_n_destroy(game, ref tar, ref k);                     // search and destroy, TODO: prioritise this!!!
                }
                #endregion

                List <int> dss = sortInto(ds);               // sort the ds into the dss


                attack(game);
                QueuedAttack.doAttacks(game, deadMap);

                #region move
                // move
                for (int j = 0; j < ships; j++)
                {
                    int i = dss[j];
                    if (ps[i].S == PirateContainer.State.none && ps[i].AVALIBLE && !ps[i].P.HasTreasure)
                    {
                        if (game.Treasures().Count > 0)                        // use typical motion
                        {
                            Location l = powerup(ps[i].P.Location, ts[i].Location, game);
                            int      mv;
                            if (l != null)
                            {
                                mv = move(ps[i], l, remaining, game);
                            }
                            else
                            {
                                mv = move(ps[i], ts[i].Location, remaining, game);
                            }

                            if (mv > 0)
                            {
                                remaining -= mv;
                                continue;
                            }
                        }
                        if (game.EnemyPiratesWithTreasures().Count > 0 && ps[i].P == k)                        // activate search and destroy
                        {
                            remaining -= move(ps[i], tar.Location, remaining, game);
                        }
                    }
                }
                #endregion
            }
            catch (Exception e)
            {
                game.Debug("Crashed!");
                game.Debug(e.Message);
                game.Debug(e.StackTrace);
            }
            finally
            {
                WastedTurnsCounter += remaining;

                game.Debug("________");
                game.Debug("turn " + game.GetTurn() + " - moves summary");
                game.Debug("turns used: " + (game.GetActionsPerTurn() - remaining));
                game.Debug("turns wasted: " + remaining);
                game.Debug("________");
                game.Debug("game summary");
                game.Debug("turns used: " + (game.GetTurn() * game.GetActionsPerTurn() - WastedTurnsCounter) + "/" + (game.GetTurn() * game.GetActionsPerTurn()) + ", " + Math.Round((100 - (float)WastedTurnsCounter * 100 / (float)(game.GetTurn() * game.GetActionsPerTurn())), 2) + "% efficiency");
                game.Debug("turns wasted: " + WastedTurnsCounter);
            }
        }