private bool TryAttack(IPirateGame game, Pirate pirate) { foreach (Pirate enemy in game.EnemyPirates()) { if (game.InRange(pirate, enemy)) { game.Attack(pirate, enemy); return(true); } } return(false); }
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; }
public bool attack(Pirate p, IPirateGame game) { if (s != State.none) { game.Debug("State on Pirate " + P.Id + " cannot shift from " + s.ToString() + " to attacked!"); return(false); } if (P.ReloadTurns > 0) { game.Debug("Pirate " + P.Id + " cannot attack, no ammo!"); return(false); } free.Remove(this); s = State.attacked; game.Attack(P, p); return(true); }
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; } } } }
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); } }