Ejemplo n.º 1
0
        }         // calculates the closest treasure to ps[i]

        private void search_n_destroy(IPirateGame game, ref Pirate tar, ref Pirate k)
        {
            int mx = (game.GetRows() + game.GetCols() - game.GetAttackRadius()) / game.GetActionsPerTurn();            // turns it takes to get from a corner to its opposing corner
            int d  = int.MaxValue;

            tar = game.EnemyPiratesWithTreasures()[0];            // TODO: focus on closest to enemy base
            // find closest Pirate
            foreach (PirateContainer p in PirateContainer.free)   // notice all pirates with Treasure already moved, see: ltp
            {
                if (p.AVALIBLE && p.P.ReloadTurns < mx && d > game.Distance(p.P, tar))
                {
                    d = game.Distance(p.P, tar);
                    k = p.P;
                }
            }
            if (k == null)                                                         // no Pirate with ammo, so choose the closest to the InitialLocation then move to there
            {
                foreach (PirateContainer p in PirateContainer.free)                // notice all pirates with Treasure already moved, see: ltp
                {
                    if (p.AVALIBLE && d > game.Distance(p.P, tar.InitialLocation)) // TODO: make the "6" generic to board size
                    {
                        d = game.Distance(p.P, tar.InitialLocation);
                        k = p.P;
                    }
                }
            }
        }         //manages panic mode
Ejemplo n.º 2
0
        //-----------------------------------------------------------------------------------------------------------------------------------------------------

        // calculations
        private void calcKamikazes(PirateContainer[] ps, ref Treasure[] ts, ref int[] ds, IPirateGame game)
        {
            // find the enemies pirate the closest to their base
            List <Pirate> es = new List <Pirate>();

            int[] eds = new int[game.EnemyPiratesWithTreasures().Count];
            foreach (Pirate e in game.EnemyPiratesWithTreasures())
            {
                eds[es.Count] = game.Distance(e, e.InitialLocation) / e.TreasureValue;
                es.Add(e);
            }
            List <int> edss = sortInto(eds);

            // map the kamikazes to the ps[]
            List <PirateContainer> unusedKamikazes = new List <PirateContainer>(PirateContainer.kamikazes);

            for (int i = unusedKamikazes.Count; i > 0;)
            {
                if (unusedKamikazes[--i].P.HasTreasure)
                {
                    unusedKamikazes.RemoveAt(i);                    // if a kamikaze has treasure, deactivate it
                }
            }

            for (int i = 0; i < es.Count && unusedKamikazes.Count > 0; i++)
            {
                // find closest kamikaze to the closest enemy to its initial position
                List <int> kds = new List <int>();
                foreach (PirateContainer p in unusedKamikazes)
                {
                    kds.Add(game.Distance(p.P, es[edss[i]]));
                }

                int min = -1;
                for (int j = 0; j < kds.Count; j++)
                {
                    if (min == -1 || kds[j] <= kds[min])
                    {
                        min = j;
                    }
                }

                // set the target
                int index = unusedKamikazes[min].P.Id;                //kamikazeDefinitions[min];
                ts[index] = new Treasure(100 + i, es[i].InitialLocation, 0);
                ds[index] = (int)Math.Ceiling(game.Distance(unusedKamikazes[min].P, es[i].InitialLocation) / 2f);
                if (ds[index] == 0)
                {
                    ds[index] = int.MaxValue;                    // speed up some things if the Pirate shouldn't move
                }
                unusedKamikazes.RemoveAt(min);
            }
        }
Ejemplo n.º 3
0
        public void DoTurn(IPirateGame game)
        {
            List <Pirate>   full_ships        = game.MyPiratesWithTreasures();
            List <Pirate>   empty_ships       = game.MyPiratesWithoutTreasures();
            List <Treasure> current_treasures = game.Treasures();
            int             min   = game.Distance(empty_ships[0], current_treasures[0]);
            Pirate          S     = empty_ships[0];
            Treasure        T     = current_treasures[0];
            int             steps = 6;
            bool            valid = false;

            if (full_ships != null)
            {
                foreach (Pirate s in full_ships)
                {
                    game.SetSail(s, game.GetSailOptions(s, s.InitialLocation, 1)[0]);
                    steps--;
                }
            }

            if (current_treasures != null && empty_ships != null)
            {
                while (steps != 0)
                {
                    foreach (Pirate s in empty_ships)
                    {
                        if (s.ReloadTurns != 0)
                        {
                            empty_ships.Remove(S);
                            continue;
                        }
                        valid = true;
                        foreach (Treasure t in current_treasures)
                        {
                            if (game.Distance(s, t) < min && game.Distance(s, t) != 0)
                            {
                                min = game.Distance(s, t);
                                S   = s;
                                T   = t;
                            }
                        }
                    }
                    if (valid)
                    {
                        game.SetSail(S, game.GetSailOptions(S, T, (steps < min) ? steps : min)[0]);
                        steps -= min;
                        empty_ships.Remove(S);
                    }
                }
            }
        }
Ejemplo n.º 4
0
		public int DoTurn(IPirateGame game)
		{
			int movesConsumed = 0;

			var pirates = game.MyPirates().Where(pirate => pirate.Id % 2 == 0).ToArray();

			var firingPirates = new HashSet<Pirate>();
			var firedUponTargets = new HashSet<Pirate>();
			var shootingTuples =
				from pirate in pirates
				from enemy in game.EnemyPiratesWithTreasures()
				where pirate.ReloadTurns == 0
				where pirate.TurnsToSober == 0
				where enemy.TurnsToSober == 0
				where game.Distance(pirate, enemy) <= 4
				select new { Pirate = pirate, Target = enemy };
			if (shootingTuples.Any())
			{
				foreach (var shootingTuple in shootingTuples)
				{
					if (!firingPirates.Contains(shootingTuple.Pirate) && !firedUponTargets.Contains(shootingTuple.Target))
					{
						firingPirates.Add(shootingTuple.Pirate);
						firedUponTargets.Add(shootingTuple.Target);
						game.Attack(shootingTuple.Pirate, shootingTuple.Target);
						movesConsumed++;
					}
				}
			}

			var piratesAndEnemies = pirates.Except(firingPirates).Select(pirate => Tuple.Create(pirate, game.GetEnemyPirate(pirate.Id))).ToList();

			if (piratesAndEnemies.Any())
			{
				// if any pirate needs to move, move only the first pirate
				var movingTuple = piratesAndEnemies.Where(pair => game.Distance(pair.Item1, pair.Item2.InitialLocation) > 2).FirstOrDefault();

				if (movingTuple != null)
				{
					var movingPirate = movingTuple.Item1;
					var enemyHomeBase = movingTuple.Item2.InitialLocation;
					int distance = Math.Min(4, game.Distance(movingPirate, enemyHomeBase) - 2);
					var sailOptions = game.GetSailOptions(movingPirate, enemyHomeBase, distance);
					game.SetSail(movingPirate, sailOptions[random.Next(sailOptions.Count)]);
					movesConsumed += distance;
				}
			}

			return movesConsumed;
		}
Ejemplo n.º 5
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);
        }
Ejemplo n.º 6
0
        }         // calculates the closest powerups to ps[i]

        // can we move the given Pirate to the given Location according to the number of moves?
        // if so --> move it!
        private static int move(PirateContainer p, Location t, int moves, IPirateGame game, bool dontHitEnemies = false)
        {
            if (moves == 0 || (!p.AVALIBLE && p.S != PirateContainer.State.moved) || p.P.Location.Equals(t))
            {
                return(0);
            }

            var X = from l in game.GetSailOptions(p.P, t, moves)
                    where (!QueuedMotion.isOccupied(l, game, dontHitEnemies) && p.move1(l, game, moves))
                    select l;

            if (X.Count() > 0)
            {
                PirateContainer.free.Remove(p);

                Location loc = X.ElementAt(rand.Next(X.Count()));

                game.SetSail(p.P, loc);
                new QueuedMotion(p.P, loc);
                return(game.Distance(p.P, loc));
            }

            game.Debug("Failed to find a move for " + p.P.Id + " to " + t);
            return(0);
        }
Ejemplo n.º 7
0
 private void locatePowerups(IPirateGame game, int ships, ref PirateContainer[] ps, ref int[] ds, ref Powerup[] pu)
 {
     for (int i = 0; i < ships; i++)
     {
         //if (game.GetMyPirate(i).HasTreasure)
         //break;
         ps[i] = new PirateContainer(game.GetMyPirate(i), (i % 2) == 1);
         ds[i] = int.MaxValue;
         foreach (Powerup p in pu)
         {
             if (p == null)
             {
                 break;
             }
             //game.Debug("here is the prub: " + i + " - " + p);
             int d = game.Distance(ps[i].P.Location, p.Location);
             if (d < ds[i])
             {
                 ds[i] = d;
                 pu[i] = p;
                 game.Debug("Powerup found in distance " + d + " for ship " + i);
             }
         }
     }
 }         // calculates the closest powerups to ps[i]
Ejemplo n.º 8
0
        public void DoTurn(IPirateGame game)
        {
            int reamaining = 6;

            Pirate[] ps = new Pirate[4];
            for (int i = 0; i < 4; i++)
            {
                ps[i] = game.GetMyPirate(i);
            }

            List <Pirate> ltp = game.MyPiratesWithTreasures();

            reamaining -= ltp.Count;
            foreach (Pirate p in ltp)
            {
                game.SetSail(p, game.GetSailOptions(p, p.InitialLocation, 1)[0]);
            }

            for (int i = 0; i < 4; i++)
            {
                if (!ps[i].HasTreasure)
                {
                    Treasure cull = null;
                    int      minD = int.MaxValue;

                    foreach (Treasure t in game.Treasures())
                    {
                        if (game.Distance(ps[i], t) < minD)
                        {
                            minD = game.Distance(ps[i], t);
                            cull = t;
                        }
                    }

                    game.SetSail(ps[i], game.GetSailOptions(ps[i], cull, reamaining)[0]);
                    reamaining = 0;
                    break;
                }
            }
        }
Ejemplo n.º 9
0
        public bool move1(Location l, IPirateGame game, int remaining)
        {
            if (s != State.none && s != State.treasure && s != State.moved)
            {
                game.Debug("State on Pirate " + P.Id + " cannot shift from " + s.ToString() + " to moved!");
                return(false);
            }
            int d = game.Distance(P, l);

            if (d > remaining || (P.HasTreasure && d > P.CarryTreasureSpeed))
            {
                game.Debug("Pirate " + P.Id + " cannot move, not enough moves!");
                return(false);
            }

            s = State.moved;
            return(true);
        }
Ejemplo n.º 10
0
        // can we move the given Pirate to the given Location according to the number of moves?
        // if so --> move it!
        private static int move(PirateContainer p, Location t, int moves, IPirateGame game, bool dontHitEnemies = false)
        {
            if (moves == 0 || !p.AVALIBLE || p.P.Location.Equals(t))
            {
                return(0);
            }

            // calculate the best route
            foreach (Location l in game.GetSailOptions(p.P, t, moves))
            {
                if (!QueuedMotion.isOccupied(l, game, dontHitEnemies) && p.move(l, game))
                {
                    return(game.Distance(p.P, l));
                }
            }

            game.Debug("Failed to find a move for " + p.P.Id + " to " + t);
            return(0);
        }
Ejemplo n.º 11
0
        private Island GetClosestIsland(IPirateGame state, Pirate myPirate, List <Island> islands)
        {
            // Move from the island
            // Move to closest attacked island
            //
            int    closetDIstance = int.MaxValue;
            Island closestIsland  = null;

            islands.ForEach(island =>
            {
                int distnace = state.Distance(myPirate, island);
                if (distnace < closetDIstance)
                {
                    closestIsland  = island;
                    closetDIstance = distnace;
                }
            });

            return(closestIsland);
        }
Ejemplo n.º 12
0
        // can we move the given Pirate to the given Location according to the number of moves?
        // if so --> move it!
        private static int move(PirateContainer p, Location t, int moves, IPirateGame game, bool dontHitEnemies = false)
        {
            if (moves == 0 || !p.AVALIBLE || p.P.Location.Equals(t))
            {
                return(0);
            }

            // calculate the best route

            /*foreach (Location l in game.GetSailOptions(p.P, t, moves))
             * {
             *      if (!QueuedMotion.isOccupied(l, game, dontHitEnemies) && p.move(l, game))
             *              return game.Distance(p.P, l);
             * }*/
            var X = from l in game.GetSailOptions(p.P, t, moves)
                    where (!QueuedMotion.isOccupied(l, game, dontHitEnemies) && p.move1(l, game))
                    select l;

            if (X.Count() > 0)
            {
                PirateContainer.free.Remove(p);

                Location loc = X.ElementAt(rand.Next(X.Count()));

                game.SetSail(p.P, loc);
                new QueuedMotion(p.P, loc);
                return(game.Distance(p.P, loc));
            }

            else if (X.Count() == 0)
            {
                if (PirateContainer.GetRemainingMoves() > 2)
                {
                    Location loc = p.P.Location;
                    p.move(loc, game);
                }
            }

            game.Debug("Failed to find a move for " + p.P.Id + " to " + t);
            return(0);
        }
Ejemplo n.º 13
0
        public bool move(Location l, IPirateGame game)
        {
            if (s != State.none && s != State.treasure)
            {
                game.Debug("State on Pirate " + P.Id + " cannot shift from " + s.ToString() + " to moved!");
                return(false);
            }
            int d = game.Distance(P, l);

            if (d > remainingMoves || (P.HasTreasure && d > 1))
            {
                game.Debug("Pirate " + P.Id + " cannot move, not enough moves!");
                return(false);
            }

            free.Remove(this);
            s = State.moved;
            game.SetSail(P, l);
            new QueuedMotion(P, l);
            return(true);
        }
Ejemplo n.º 14
0
        }         // checks wether to activate nowhere mode

        private void calcBestTreasure(IPirateGame game, int ships, ref PirateContainer[] ps, ref int[] ds, ref Treasure[] ts)
        {
            for (int i = 0; i < ships; i++)
            {
                ps[i] = new PirateContainer(game.GetMyPirate(i), (i % 2) == 1);
                ds[i] = int.MaxValue;
                foreach (Treasure t in game.Treasures())
                {
                    int d = game.Distance(ps[i].P, t) / t.Value;
                    if (powerup(ps[i].P.Location, t.Location, game) != null)
                    {
                        d -= 3;
                    }
                    if (d < ds[i])
                    {
                        ds[i] = d;
                        ts[i] = t;
                    }
                }
            }
        }         // calculates the closest treasure to ps[i]
Ejemplo n.º 15
0
        private Pirate GetClosestPirate(IPirateGame state, Island island, List <Pirate> pirates)
        {
            // Move from the island
            // Move to closest attacked island
            //
            int    closetDIstance = int.MaxValue;
            Pirate closestPirate  = null;

            pirates.ForEach(pirate =>
            {
                if (!pirate.IsLost)
                {
                    int distnace = state.Distance(pirate, island);
                    if (distnace < closetDIstance)
                    {
                        closestPirate  = pirate;
                        closetDIstance = distnace;
                    }
                }
            });

            return(closestPirate);
        }
Ejemplo n.º 16
0
 private PirateAndCandidate GetCandidate(IPirateGame game, Pirate pirate)
 {
     var treasures = game.Treasures();
     var selectedTreasure = treasures.OrderBy(t => game.Distance(pirate, t)).FirstOrDefault();
     return new PirateAndCandidate
     {
         Pirate = pirate,
         Treasure = selectedTreasure,
         Distance = game.Distance(pirate, selectedTreasure)
     };
 }
Ejemplo n.º 17
0
        public void DoTurn(IPirateGame game)
        {
            int remaining = 6;

            Pirate[]   ps  = new Pirate[4];
            Location[] l   = new Location[4];
            int[]      ds  = new int[4];
            List <int> dss = new List <int>();          // should always be size 4

            for (int i = 0; i < 4; i++)
            {
                ps[i] = game.GetMyPirate(i);
                ds[i] = int.MaxValue;
                foreach (Treasure t in game.Treasures())
                {
                    if (game.Distance(ps[i], t) < ds[i])
                    {
                        ds[i] = game.Distance(ps[i], t);
                        l[i]  = t.Location;
                    }
                }
            }

            // sort the ds into the dss
            {
                bool add;
                do
                {
                    add = false;
                    int min = -1;
                    for (int i = 0; i < ds.Length; i++)
                    {
                        if (!dss.Contains(i))
                        {
                            if (min == -1 || ds[i] <= ds[min])
                            {
                                min = i;
                                add = true;
                            }
                        }
                    }
                    if (add)
                    {
                        dss.Add(min);
                    }
                } while (add);
            }

            List <Pirate> ltp = game.MyPiratesWithTreasures();

            remaining -= ltp.Count;
            foreach (Pirate p in ltp)
            {
                game.SetSail(p, game.GetSailOptions(p, p.InitialLocation, 1)[0]);
            }

            if (game.Treasures().Count == 0)
            {
                return;
            }
            for (int j = 0; j < 4; j++)
            {
                int i = dss[j];
                if (!ps[i].HasTreasure)
                {
                    bool attacked = false;
                    if (ps[i].ReloadTurns == 0)
                    {
                        foreach (Pirate e in game.EnemySoberPirates())
                        {
                            if (game.InRange(ps[i], e))
                            {
                                game.Attack(ps[i], e);
                                attacked = true;
                                break;
                            }
                        }
                    }

                    if (!attacked && ps[i].TurnsToSober == 0 && ps[i].TurnsToRevive == 0)
                    {
                        game.SetSail(ps[i], game.GetSailOptions(ps[i], /*cull*/ l[i], remaining)[0]);
                        remaining = 0;
                    }
                }
            }
        }
Ejemplo n.º 18
0
        // this is the actual turn
        public void DoTurn(IPirateGame game)
        {
            if (game.GetTurn() == 1)
            {
                rand = new Random(42934837);                 //,,79409223
                if (game.AllEnemyPirates().Count > game.AllMyPirates().Count)
                {
                    rand = new Random(31400000);
                }
                Location l = new Location(1, 1);
                if (game.Treasures().Count == 1 && game.AllMyPirates().Count == game.AllEnemyPirates().Count)
                {
                    deadMap = true;
                }

                game.Debug((deadMap ? "DEADMAP!!!" : "NIE!"));
                if (deadMap)
                {
                    return;                    // this stops some fighting over treasures
                }
            }

            nowhere = (game.GetEnemyScore() == game.GetMyScore() && game.GetTurn() == (game.GetMaxTurns() / 3));
            if (nowhere)
            {
                game.Debug("ACTIVATING NOWHERE MODE!!!");
            }

            panic = (((game.Treasures().Count == 0 || game.GetEnemyScore() >= (game.GetMaxPoints() - 2))) && game.EnemyPiratesWithTreasures().Count > 0);
            if (panic)
            {
                game.Debug("ACTIVATING PANIC MODE!!!");
            }

            PirateContainer.init(game);
            QueuedAttack.init();
            QueuedMotion.init();
            int remaining = game.GetActionsPerTurn();
            int ships     = game.AllMyPirates().Count;

            try
            {
                // calculate the closest treasure to ps[i]
                PirateContainer[] ps = new PirateContainer[ships];
                int[]             ds = new int[ships];
                Treasure[]        ts = new Treasure[ships];
                for (int i = 0; i < ships; i++)
                {
                    ps[i] = new PirateContainer(game.GetMyPirate(i), (i % 2) == 1);
                    ds[i] = int.MaxValue;
                    foreach (Treasure t in game.Treasures())
                    {
                        if (game.Distance(ps[i].P, t) < ds[i])
                        {
                            ds[i] = game.Distance(ps[i].P, t);
                            ts[i] = t;
                        }
                    }
                }

                // control the kamikazes
                calcKamikazes(ps, ref ts, ref ds, game);

                // move Pirates that have treasures towards the base
                {
                    List <PirateContainer> ltp = PirateContainer.withTreasure;
                    foreach (PirateContainer p in ltp)
                    {
                        List <Pirate> es = findEnemiesFor(p.P, game);
                        if (es.Count > 0 && p.P.ReloadTurns == 0)
                        {
                            p.defend(game);
                        }
                        else
                        {
                            remaining -= move(p, p.P.InitialLocation, 1, game, true);
                        }
                    }
                }

                // search and destroy, TODO: prioritise this!!!
                Pirate k = null, tar = null;
                if (panic)
                {
                    int mx = (game.GetRows() + game.GetCols() - game.GetAttackRadius()) / game.GetActionsPerTurn(); // turns it takes to get from a corner to its opposing corner
                    int d  = int.MaxValue;
                    tar = game.EnemyPiratesWithTreasures()[0];                                                      // TODO: focus on closest to enemy base
                    // find closest Pirate
                    foreach (PirateContainer p in PirateContainer.free)                                             // notice all pirates with Treasure already moved, see: ltp
                    {
                        game.Debug("panic->k testing for " + p.P.Id + " | " + d);
                        game.Debug(p.AVALIBLE + " && " + (p.P.ReloadTurns < mx) + " && " + (d > game.Distance(p.P, tar)));
                        if (p.AVALIBLE && p.P.ReloadTurns < mx && d > game.Distance(p.P, tar))
                        {
                            d = game.Distance(p.P, tar);
                            k = p.P;
                            game.Debug("panic->k = " + p.P.Id + " | " + d);
                        }
                    }
                    if (k == null)                                                         // no Pirate with ammo, so choose the closest to the InitialLocation then move to there
                    {
                        foreach (PirateContainer p in PirateContainer.free)                // notice all pirates with Treasure already moved, see: ltp
                        {
                            if (p.AVALIBLE && d > game.Distance(p.P, tar.InitialLocation)) // TODO: make the "6" generic to board size
                            {
                                d = game.Distance(p.P, tar.InitialLocation);
                                k = p.P;
                            }
                        }
                    }
                }

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

                // AAAAATTTTTTTTTTTTAAAAAAAAAAACCCCCCCCKKKKKKKKKKKKKKK!!!!!!!!!!!!!!!! (or defend...)
                for (int i = PirateContainer.free.Count; i > 0;)
                {
                    PirateContainer p = PirateContainer.free[--i];
                    if (p.P.ReloadTurns == 0 && !p.P.HasTreasure && p.AVALIBLE)
                    {
                        List <Pirate> es = findTargetsFor(p.P, game);
                        if (es.Count > 0)
                        {
                            new QueuedAttack(p, es);
                        }
                    }
                }
                QueuedAttack.doAttacks(game, deadMap);

                // 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
                        {
                            int 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);
                        }
                    }
                }
            }
            catch (Exception e)
            {
                game.Debug("Crashed!");
                game.Debug(e.Message);
                game.Debug(e.StackTrace);
            }
            game.Debug("turn " + game.GetTurn() + ": ran " + (game.GetActionsPerTurn() - remaining) + " motions");
        }
Ejemplo n.º 19
0
        public void DoTurn(IPirateGame game)
        {
            try
            {
                int remaining = 6;

                Pirate[]   ps  = new Pirate[4];
                int[]      ds  = new int[4];
                List <int> dss = new List <int>();// should always be size 4
                for (int i = 0; i < 4; i++)
                {
                    ps[i] = game.GetMyPirate(i);
                    ds[i] = int.MaxValue;
                    if (game.Treasures().Contains(ts[i]))
                    {
                        continue;
                    }
                    foreach (Treasure t in game.Treasures())
                    {
                        if (game.Distance(ps[i], t) < ds[i])
                        {
                            ds[i] = game.Distance(ps[i], t);
                            //ts[i] = t;
                        }
                    }
                }


                // sort the ds into the dss
                {
                    bool add;
                    do
                    {
                        add = false;
                        int min = -1;
                        for (int i = 0; i < ds.Length; i++)
                        {
                            if (!dss.Contains(i))
                            {
                                if (min == -1 || ds[i] <= ds[min])
                                {
                                    min = i;
                                    add = true;
                                }
                            }
                        }
                        if (add)
                        {
                            dss.Add(min);
                        }
                    } while (add);
                }


                if (kamikaze)
                {
                    if (ps[0].InitialLocation.Equals(new Location(23, 1)))
                    {
                        ts[3] = new Treasure(19, new Location(17, 19));
                        ts[1] = new Treasure(20, new Location(24, 30));
                        ts[2] = new Treasure(20, new Location(25, 29));
                        ts[0] = new Treasure(20, new Location(26, 28));
                    }
                    else
                    {
                        ts[3] = new Treasure(19, new Location(17, 13));
                        ts[1] = new Treasure(20, new Location(24, 2));
                        ts[2] = new Treasure(20, new Location(25, 3));
                        ts[0] = new Treasure(20, new Location(26, 4));
                    }
                    ds[0] = 0;
                    ds[1] = 0;
                    ds[2] = 0;
                    ds[3] = 0;
                }


                List <Pirate> ltp = game.MyPiratesWithTreasures();
                remaining -= ltp.Count;
                foreach (Pirate p in ltp)
                {
                    move(p, p.InitialLocation, 1, game);
                }


                Pirate k = null, tar = null;
                if (game.Treasures().Count == 0 && game.EnemyPiratesWithTreasures().Count > 0)
                {
                    int d = int.MaxValue;
                    tar = game.EnemyPiratesWithTreasures()[0];
                    foreach (Pirate p in game.MyPiratesWithoutTreasures())
                    {
                        if (p.TurnsToSober == 0 && p.ReloadTurns < 6 && d > game.Distance(p, tar))
                        {
                            d = game.Distance(p, tar);
                            k = p;
                        }
                    }
                }


                for (int j = 0; j < 4; j++)
                {
                    int i = dss[j];
                    if (!ps[i].HasTreasure)
                    {
                        bool attacked = false;
                        if (ps[i].ReloadTurns == 0)
                        {
                            Pirate t = null;
                            foreach (Pirate e in game.EnemySoberPirates())
                            {
                                if (game.InRange(ps[i], e))
                                {
                                    if (e.ReloadTurns == 0)
                                    {
                                        t = e;
                                        break;
                                    }
                                    else if (e.HasTreasure)
                                    {
                                        if (t == null || t.HasTreasure || t.ReloadTurns > 0)
                                        {
                                            t = e;
                                        }
                                    }
                                    else if (e.ReloadTurns > 0 && !e.HasTreasure)
                                    {
                                        continue;
                                    }
                                }
                            }
                            if (t != null)
                            {
                                game.Attack(ps[i], t);
                                attacked = true;
                            }
                        }
                        if (!attacked && ps[i].TurnsToSober == 0 && ps[i].TurnsToRevive == 0)
                        {
                            if ((game.Treasures().Count > 0 && move(ps[i], ts[i].Location, remaining, game) || (game.EnemyPiratesWithTreasures().Count > 0 && ps[i] == k && move(ps[i], tar.Location, remaining, game))))
                            {
                                remaining = 0;
                            }
                        }
                    }
                }
            }
            catch (Exception e)
            {
                game.Debug("Crashed!");
                game.Debug(e.Message);
                game.Debug(e.StackTrace);
            }
        }