Ejemplo n.º 1
0
        public override void doTurn(GameState state)
        {
            try
            {
                if (state.TimeRemaining < 5)
                    return;

                if (turn == 0)
                {
                    Log("my team: player " + (state.MyAnts.First().Team + 1));
                    Log("map " + state.Width+"x"+state.Height);
                    Log("turn time " + state.TurnTime);
                }
                Log("");
                Log("Turn " + (turn++));
                Log("My Ants: " + state.MyAnts.Count);

                SetTurnStrategy(state);

                string stratstring = "";

                foreach (var v in Enum.GetValues(typeof(Strategy)))
                {
                    stratstring += Enum.GetName(typeof(Strategy), v) + ":" + strategies[(int)v] + ";";
                }

                Log("Strategy: " + stratstring);

                this.Destinations.Clear();

                astar = new SpatialAStar<Tile, object>(state.Map);

                foreach (var goal in Goals)
                    goal.AntExists = false; //need to check these each turn and clean up if ant gone

                foreach (AntLoc ant in state.MyAnts)
                {
                    Log("ant: " + ant);
                    Goal goal = this.Goals.FirstOrDefault(g => g.CurrentPoint.Equals(ant));

                    if (goal != null && (goal.CurrentPath.Count==0 || goal.IsTerminated(state)))
                    {
                        if (goal.CurrentPath.Count == 0)
                            Log("ant goal complete");
                        else
                            Log("ant goal terminated");
                        Goals.Remove(goal);
                        goal = null;
                    }
                    if (goal != null)
                    {
                        goal.AntExists = true;
                        Log("ant existing goal: " + String.Join(";", goal.CurrentPath.Select(p => p.Location.ToString()).ToArray()) + " " + Enum.GetName(typeof(Strategy), goal.CurrentStrategy));
                    }
                    else
                    {
                        if (state.TimeRemaining < 50) // choose a fast strategy
                        {
                            Log("short on time ("+state.TimeRemaining+") - choose scatter");
                            goal = Scatter(ant, state);
                        }
                        else
                        {

                            goal = ChooseStrategy(ant, state);
                            if (!goal.CurrentPath.Any())
                            {
                                Log("bad goal/path - scatter instead");
                                goal = Scatter(ant, state);
                            }
                        }
                        Goals.Add(goal);
                        Log("new ant goal: " + String.Join(";", goal.CurrentPath.Select(p => p.Location.ToString()).ToArray()) + " " + Enum.GetName(typeof(Strategy), goal.CurrentStrategy));

                    }

                    if (goal != null)
                    {
                        var loc = goal.CurrentPath.Dequeue();
                        var directions = ant.GetDirections(loc.Location);

                        foreach (var d in directions)
                        {
                            var newLoc = ant.GetDestination(d);
                            if (state.IsUnoccupied(newLoc) && state.IsPassable(newLoc) && !Destinations.Contains(newLoc))
                            {
                                ant.Move(d);
                                goal.CurrentPoint = newLoc;
                                goal.CurrentStep++;
                                Destinations.Add(newLoc);
                                break;
                            }
                        }
                    }
                }
                int removed = Goals.RemoveAll(g => !g.AntExists);//clean up goals for missing ants

                Log("ant goals(" + Goals.Count + ", "+removed+" removed): " + String.Join(";", Goals.Select(g => "["+Enum.GetName(typeof(Strategy),g.CurrentStrategy)+"]"+g.CurrentPoint.ToString()+"->"+g.EndPoint.ToString()).ToArray()));
                if (removed > 0) // losing fights - condense
                {
                    AlterStrategy(Strategy.Condense, Preference.StronglyEncourage);
                    AlterStrategy(Strategy.Condense, Preference.StronglyEncourage);
                    AlterStrategy(Strategy.Condense, Preference.StronglyEncourage);
                }
            }
            catch (Exception exc)
            {
                Log(exc.Message);
                Log(exc.StackTrace);
            }
        }