Example #1
0
 internal void addAnt(int row, int col, int team)
 {
     AntLoc ant = new AntLoc(this, row, col, team);
     Map[row, col] = new Tile(TileType.Ant, ant);
     if (team == 0)
     {
         MyAnts.Add(ant);
     }
     else
     {
         EnemyAnts.Add(ant);
     }
 }
Example #2
0
File: MyBot.cs Project: ejsmile/ant
        // doTurn is run once per turn
        public override void doTurn(GameState state)
        {
            #if DEBUG
            sw.WriteLine ("!Turn " + number++);
            #endif
            destinations.Clear ();
            currentTurn.Clear ();

            #region Setup Discovery Aims
            List<Location > Discovery = new List<Location> ();
            for (int row = 0; row < state.Height / (state.ViewRadius2 / 2); row++) {
                for (int col = 0; col < state.Width / (state.ViewRadius2 / 2); col++) {
                    Discovery.Add (new Location (row, col));
                }
            }

            foreach (var ant in state.MyAnts) {
                Location tmp = new Location (ant.row / (state.ViewRadius2 / 2), ant.col / (state.ViewRadius2 / 2));
                if (Discovery.Contains (tmp))
                    Discovery.Remove (tmp);
            }
            #endregion

            Location[] hod = new Location[] {new Location (0, -state.ViewRadius2 - 1), new Location (0, state.ViewRadius2 + 1),
                new Location (-state.ViewRadius2 - 1, 0), new Location (state.ViewRadius2 + 1, 0) ,
                new Location (-state.ViewRadius2 / 2 - 1, -state.ViewRadius2 / 2 + 1),
                new Location (state.ViewRadius2 / 2 + 1, state.ViewRadius2 / 2 + 1),
                new Location (state.ViewRadius2 / 2 + 1, -state.ViewRadius2 / 2 - 1),
                new Location (-state.ViewRadius2 / 2 - 1, state.ViewRadius2 / 2 + 1)};
            Location[] smeshnie = new Location[] {new Location (0, -state.ViewRadius2 - rng.Next (state.ViewRadius2)),
                new Location (0, state.ViewRadius2 + rng.Next (state.ViewRadius2)),
                new Location (-state.ViewRadius2 - rng.Next (state.ViewRadius2), 0),
                new Location (state.ViewRadius2 + rng.Next (state.ViewRadius2), 0)};

            #region Find Guard for my Hills
            IDictionary<AntLoc, Location > Guard = new Dictionary<AntLoc, Location> ();
            foreach (var hill in state.MyHills) {
                foreach (var dest in Ants.Aim.Keys) {
                    Location loc = state.destination (hill, dest);
                    if (state.passable (loc)) {
                        Guard.Add (hill, loc);
                        break;
                    }
                }
            }
            #endregion

            #region aim ants to food + Enemy hills

            //Add Enemy Hill to AIM food :)
            foreach (var hill in state.EnemyHills) {
                state.FoodTiles.Add (new Location (hill.row, hill.col));
            }

            foreach (var food in state.FoodTiles) {
                int dist = int.MaxValue;
                AntLoc antFood = null;

                foreach (var ant in state.MyAnts) {
                    if (!currentTurn.ContainsKey (ant))
                    if (state.distance (ant, food) < dist) {
                        dist = state.distance (ant, food);
                        antFood = ant;
                    }

                }
                if (antFood != null) {
                    currentTurn.Add (antFood, food);
                    #if DEBUG
                    sw.WriteLine (" ant " + antFood + " food " + food);
                    #endif

                } else
                    break;
            }
            #endregion

            #region Enemy aim
            foreach (var ant in state.MyAnts) {
                int distEnemy = state.ViewRadius2 * 3;
                Location aimEnemy = null;
                int dist;
                int attakEnemy = 0;

                foreach (var enemy in state.EnemyAnts) {
                    dist = state.distance (ant, enemy);
                    if (dist < distEnemy) {
                        distEnemy = dist;
                        aimEnemy = enemy;
                    }
                    if (dist < state.AttackRadius2 + 1)
                        attakEnemy++;
                }

                if (aimEnemy != null) {

                    //find frinds in  state.AttackRadius2
                    int attakFrinds = 0;
                    int saveDist = state.AttackRadius2 + 1;
                    foreach (var friends in state.MyAnts) {
                        dist = state.distance (aimEnemy, friends);
                        if (dist < saveDist) {
                            attakFrinds++;
                        }
                    }
                    #if DEBUG
                    sw.WriteLine (" ant " + ant + " friends " + attakFrinds + " they " + attakEnemy + " aim " + aimEnemy);
                    #endif

                    //I am alone
                    if ((attakFrinds <= attakEnemy) && (distEnemy < state.AttackRadius2 + 3)) {
                        int runDist = distEnemy;

                        Location runLoc = null;
                        foreach (Location loc in hod) {
                            Location newLoc = state.destination (ant, loc);
                            if (state.unoccupied (newLoc)) {
                                if (runDist < state.distance (newLoc, aimEnemy)) {
                                    runDist = state.distance (newLoc, aimEnemy);
                                    runLoc = newLoc;
                                }
                            }
                        }
                        if (runLoc != null) {
                            aimEnemy = runLoc;
                            distEnemy = runDist;
                        }
                        #if DEBUG
                        sw.WriteLine (" ant " + ant + " run  from enemy to " + aimEnemy);
                        #endif
                    }
                    /**/
                    if (currentTurn.ContainsKey (ant)) {
                        int tmp = state.distance (ant, currentTurn [ant]);
                        if (tmp > distEnemy) {
                            Location food = currentTurn [ant];
                            currentTurn [ant] = aimEnemy;
                            aimEnemy = null;
                            tmp = int.MaxValue;

                            foreach (var ants in state.MyAnts) {
                                if (!currentTurn.ContainsKey (ants) && (state.distance (ant, food) < tmp)) {
                                    tmp = state.distance (ant, food);
                                    aimEnemy = ants;
                                }
                            }
                            if (aimEnemy != null)
                                currentTurn.Add (new AntLoc (aimEnemy, 0), food);
                        }
                    } else
                        currentTurn.Add (ant, aimEnemy);
                }
            }
            #endregion

            #region Move other ants
            foreach (var ant in state.MyAnts) {
                if (!currentTurn.ContainsKey (ant)) {

                    Location aim = smeshnie [rng.Next (4)];
                    if (Discovery.Count > 0) {
                        int dist = int.MaxValue;
                        foreach (var loc in Discovery) {
                            Location aimTmp = new Location (loc.row * (state.ViewRadius2 / 2), loc.col * (state.ViewRadius2 / 2));
                            int tmp = state.distance (aimTmp, ant);
                            if (tmp < dist) {
                                dist = tmp;
                                aim = aimTmp;
                            }
                        }
                    }
                    if (oldTurn.ContainsKey (ant)) {
                        if (state.distance (ant, oldTurn [ant]) > (state.ViewRadius2 / 2)) {
                            aim = oldTurn [ant];
                        }
                    }

                    currentTurn.Add (ant, aim);
                    #if DEBUG
                    sw.WriteLine (" ant " + ant + " move " + aim);
                    #endif
                }
            }
            #endregion

            #region Setup guard
            if (state.MyAnts.Count > 5 * state.MyHills.Count) {
                foreach (var hill in Guard.Keys) {
                    AntLoc tmp = new AntLoc (Guard [hill], 0);
                    if (currentTurn.ContainsKey (tmp)) {
                        currentTurn [tmp] = Guard [hill];
                    } else {

                        int dist = int.MaxValue;
                        AntLoc antGuard = null;
                        foreach (var ant in state.MyAnts) {
                            if (state.distance (hill, ant) < dist) {
                                dist = state.distance (hill, ant);
                                antGuard = ant;
                            }
                        }
                        if (antGuard != null)
                        if (currentTurn.ContainsKey (antGuard))
                            currentTurn [antGuard] = Guard [hill];
                        else
                            currentTurn.Add (antGuard, Guard [hill]);
                    }
                }
            }
            #endregion
            /**/
            #if DEBUG
            sw.WriteLine("runs ANTS");
            #endif

            #region runs Ants
            oldTurn = new Dictionary<AntLoc, Location> ();

            foreach (AntLoc ant in currentTurn.Keys) {
                List<char > directions = state.direction_algor_A (ant, currentTurn [ant]);

                if (directions.Count == 0) {
                    //добавление препядствия
                    state.addAnt (ant.row, ant.col, 0);
                    #if DEBUG
                    sw.WriteLine (" ant " + ant + " stop ");
                    #endif
                } else {
                    AntLoc NewAnt = new AntLoc (state.destination (ant, directions [0]), 0);
                    //странно пытаемся переститься на своего муровья
                    if (!oldTurn.ContainsKey (NewAnt)) {
                        issueOrder (ant, directions [0]);
                        //добавление препядствия
                        state.addAnt (NewAnt.row, NewAnt.col, 0);
                        oldTurn.Add (NewAnt, currentTurn [ant]);
                        #if DEBUG
                        sw.WriteLine (" ant " + ant + " move " + NewAnt);
                        #endif
                    } else {
                        state.addAnt (ant.row, ant.col, 0);
                    }
                }/**/

                if (state.TimeRemaining < 50) {
                    return;
                }
            }
            #endregion
        }

        #endregion Methods
    }
}
Example #3
0
        public void addAnt(int row, int col, int team)
        {
            map [row, col] = Tile.Ant;

            AntLoc ant = new AntLoc (row, col, team);
            if (team == 0) {
                MyAnts.Add (ant);
            } else {
                EnemyAnts.Add (ant);
            }
        }
Example #4
0
        public void addHill(int row, int col, int team)
        {
            map [row, col] = Tile.Hill;

            AntLoc ant = new AntLoc (row, col, team);
            if (team == 0) {
                MyHills.Add (ant);
            } else {
                EnemyHills.Add (ant);
            }
        }
Example #5
0
 private Goal Scout(AntLoc ant, GameState state)
 {
     return SpreadOut(ant, state);
 }
Example #6
0
        private Goal SpreadOut(AntLoc ant, GameState state)
        {
            if (state.TimeRemaining < 10)
                return Scatter(ant, state);
            // try and figure out how close to the edge of vision I am
            var x = ant.Col;
            var y = ant.Row;

            var minX = state.MyAnts.Min(a => a.Col);
            var maxX = state.MyAnts.Max(a => a.Col);
            var minY = state.MyAnts.Min(a => a.Row);
            var maxY = state.MyAnts.Max(a => a.Row);

            var widthModifier = state.Width / 30;
            if (widthModifier <= 1)
                widthModifier = 2;
            var heightModifier = state.Height / 30;
            if (heightModifier <= 1)
                heightModifier = 2;

            Func<GameState, bool> terminationFunc = (s => false);

            Log("spread out: minx:" + minX + " miny:" + minY + " maxx:" + maxX + " maxy:" + maxY + " widthmod:" + widthModifier + " heightmod:" + heightModifier);
            // near the left (ish)
            if (x < ( minX + widthModifier))
            {
                var loc = new Location(state,y,x-widthModifier); //move left
                Log("left, path :" + ant + " " + loc);
                var path = GetPath(ant, loc, state);
                if (path != null)
                {
                    return new Goal(ant, path, terminationFunc, Strategy.SpreadOut);
                }
            }
            else if (x > ( maxX - widthModifier ))
            {
                var loc = new Location(state, y,x + widthModifier); //move right
                Log("right, path :" + ant + " " + loc);
                var path = GetPath(ant, loc, state);
                if (path != null)
                {
                    return new Goal(ant, path, terminationFunc, Strategy.SpreadOut);
                }
            }
            else if (y < ( maxY + heightModifier ))
            {
                var loc = new Location(state,  y - heightModifier,x); //move up
                Log("top, path :" + ant + " " + loc);
                var path = GetPath(ant, loc, state);
                if (path != null)
                {
                    return new Goal(ant, path, terminationFunc, Strategy.SpreadOut);
                }
            }
            else if (y > ( maxY - heightModifier ))
            {
                var loc = new Location(state,  y + heightModifier,x); // move down
                Log("bottom, path :" + ant + " " + loc);
                var path = GetPath(ant, loc, state);
                if (path != null)
                {
                    return new Goal(ant, path, terminationFunc, Strategy.SpreadOut);
                }
            }

            return Scatter(ant,state);
        }
Example #7
0
        private Goal Scatter(AntLoc ant, GameState state)
        {
            // wander aimlessly
            foreach (var d in Ants.Ants.Aim.Values.OrderBy(c => rng.Next()))
            {
                var loc = ant.GetDestination(d);
                if (loc.IsPassable())
                {

                    return new Goal(ant, GetPath(ant, loc, state), (s => state.FoodTiles.Any()), Strategy.Scatter);
                }
            }

            return null;
        }
Example #8
0
 private Goal Retreat(AntLoc ant, GameState state)
 {
     return Condense(ant, state);
 }
Example #9
0
        private Goal GatherFood(AntLoc ant, GameState state)
        {
            if (!state.FoodTiles.Any())
                return Scout(ant, state);

            Log("foodtiles: " + state.FoodTiles.Count);
            // look for some food
            var searchingspots = state.FoodTiles.Where(f => Goals.Count(g => g.EndPoint.Equals(f))==0);
            Log("searchingspots: " + searchingspots.Count());
            //searchingspots = searchingspots.Where(f => f.GetDistance(ant) < GetSearchRadius(state)).OrderBy(f => f.GetDistance(ant)).Take(2);
            var foodpoints = searchingspots.Select(f => new
            {
                Tile = f,
                Point = f.ToPoint(),
                Distance = state.GetDistance(ant,f)//astar.Search(ant.ToPoint(), f.ToPoint(), new Object()) ?? Enumerable.Empty<Tile>()
            });

            var closest = foodpoints.OrderBy(f => f.Distance).FirstOrDefault();

            if (closest != null)
            {
                Log("closest food:" + closest.Tile);

                return new Goal(closest.Tile, GetPath(ant,closest.Tile,state), (g => !g.HasFood(closest.Tile)), Strategy.GatherFood);
            }

            return SpreadOut(ant, state);
        }
Example #10
0
        private Goal Fight(AntLoc ant, GameState state)
        {
            if (!state.EnemyAnts.Any())
                return Scout(ant, state);

            // look for some food
            var searchingspots = state.EnemyAnts.Where(f => Goals.Count(g => g.EndPoint.Equals(f)) <= 1);
            //searchingspots = searchingspots.Where(f => f.GetDistance(ant) < GetSearchRadius(state)).OrderBy(f => f.GetDistance(ant)).Take(2);
            var antpoint = ant.ToPoint();
            var enemypoints = searchingspots.Select(f => new
            {
                Tile = f,
                Point = f.ToPoint(),
                Distance = state.GetDistance(ant,f)//astar.Search(ant.ToPoint(), f.ToPoint(), new Object()) ?? Enumerable.Empty<Tile>()
            });

            var closest = enemypoints.OrderBy(f => f.Distance).FirstOrDefault();

            if (closest != null)
            {
                var goal = new Goal(closest.Tile, GetPath(ant, closest.Tile, state), (g => g.IsUnoccupied(closest.Tile)), Strategy.Fight);
                return goal;
            }

            return SpreadOut(ant, state);
        }
Example #11
0
        private Goal ChooseStrategy(AntLoc ant, GameState state)
        {
            Strategy? strat = null;
            float rnd = rng.Next(0, (int)(strategies.Sum()*100)) * .01f;

            for (int i = 1; i < STRAT_COUNT; i++)
            {
                if (rnd < strategies.Take(i).Sum())
                {
                    strat =  (Strategy)(i - 1);
                    break;
                }
            }

            if(strat==null)
                strat = (Strategy)(STRAT_COUNT - 1);

            Goal goal = null;

            if (strat == Strategy.GatherFood)
                goal = GatherFood(ant, state);
            else if (strat == Strategy.Fight)
                goal = Fight(ant, state);
            else if (strat == Strategy.SpreadOut)
                goal = SpreadOut(ant, state);
            else if (strat == Strategy.Scatter)
                goal = Scatter(ant, state);
            else if (strat == Strategy.Scout)
                goal = Scout(ant, state);
            else if (strat == Strategy.Condense)
                goal = Condense(ant, state);
            else if (strat == Strategy.Retreat)
                goal = Retreat(ant, state);

            if (goal == null)
            {
                goal = GatherFood(ant, state);
            }
            if (goal == null)
            {
                goal = Scout(ant, state);
            }
            if (goal == null)
            {
                goal = Fight(ant, state);
            }
            if (goal == null)
            {
                goal = SpreadOut(ant, state);
            }
            if (goal == null)
            {
                goal = Scatter(ant, state);
            }
            if (goal == null)
            {
                goal = Condense(ant, state);
            }
            if (goal == null)
            {
                goal = Retreat(ant, state);
            }

            return goal;
        }