当前位置: 首页>>代码示例>>C#>>正文


C# IGameState.GetDistance方法代码示例

本文整理汇总了C#中IGameState.GetDistance方法的典型用法代码示例。如果您正苦于以下问题:C# IGameState.GetDistance方法的具体用法?C# IGameState.GetDistance怎么用?C# IGameState.GetDistance使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在IGameState的用法示例。


在下文中一共展示了IGameState.GetDistance方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: PathfindNode

 public PathfindNode(Location position, PathfindNode parent, Location destination, IGameState state, float heat)
 {
     this.Position = position;
     this.Parent = parent;
     this.E = heat;
     this.H = state.GetDistance(position, destination);
 }
开发者ID:Lapixx,项目名称:CS-UU-KI1,代码行数:7,代码来源:Pathfinding.cs

示例2: DoTurn

        // DoTurn is run once per turn
        public override void DoTurn(IGameState state)
        {
            if(radius == default(int)) // Init
                radius = (int)Math.Sqrt(state.ViewRadius2) / 2;

            foreach (Agent agent in new List<Agent>(agents)) // Die
            {
                if (!state.MyAnts.Contains(new Ant(agent.location.Row, agent.location.Col, state.MyAnts[0].Team)))
                {
                    agent.decisionLog.AddReward(DIE);
                    agents.Remove(agent);
                }
            }

            foreach (Ant a in state.MyAnts) //Spawn new ants
            {
                bool spawn = true;
                foreach (Agent agent in agents)
                {
                    if (agent.location.Equals(a))
                    {
                        spawn = false;
                        break;
                    }
                }

                if (spawn)
                {
                    Agent ag = new Agent(a);
                    decisionLogs.Add(ag.decisionLog);
                    agents.Add(ag);

                    foreach (Agent age in agents) // Take food
                    {
                        if(age.path.Count > 0)
                            if (age.currentAction == Action.TakeFood && state.GetDistance(age.location, age.path[age.path.Count - 1]) <= 1)
                                age.decisionLog.AddReward(EAT);
                    }
                }
            }

            foreach(Agent agent in agents){

                if (agent.path.Count == 0)
                {
                    State s = CalculateState(agent.location, state);
                    double[] desirabilities = rewardLog.GetDesirabilities(s);
                    int i = -1;
                    HashSet<Action> aas = s.GetActions();
                    double x = random.NextDouble();
                    if (x <= exploration) // Explore
                    {
                        do
                        {
                            i = random.Next(Enum.GetValues(typeof(Action)).Length);
                        }
                        while (!aas.Contains((Action)i));
                    }
                    else // Exploit
                    {
                        x = random.NextDouble();
                        for (i = 0; i < Enum.GetValues(typeof(Action)).Length; i++)
                        {
                            if (x <= desirabilities[i])
                                break;
                            else
                                x -= desirabilities[i];
                        }
                    }

                    if (i == Enum.GetValues(typeof(Action)).Length)
                    {
                        do
                        {
                            i = random.Next(Enum.GetValues(typeof(Action)).Length);
                        }
                        while (!aas.Contains((Action)i));
                    }

                    PerformAction(agent, s, (Action)i, state);
                }

                if (agent.path.Count == 0)
                    continue;

                if (!state.GetIsPassable(agent.path[0]))
                    agent.path = Pathfinding.FindPath(agent.location, agent.path[agent.path.Count - 1], state);
                Location next = agent.path[0];
                agent.path.RemoveAt(0);

                if (state.EnemyHills.Count > 0)
                    if (state.EnemyHills.Contains(new AntHill(next.Row, next.Col, state.EnemyHills[0].Team)))
                        agent.decisionLog.AddReward(WIN);

                IssueOrder(agent.location, ((List<Direction>)state.GetDirections(agent.location, next))[0]);
                agent.location = next;
            }
        }
开发者ID:Lapixx,项目名称:CS-UU-KI2,代码行数:99,代码来源:MyBot.cs

示例3: DoTurn

        // DoTurn is run once per turn
        public override void DoTurn(IGameState state)
        {
            //For assigning jobs to and keeping track of ants
            List<Route> foodRoutes = new List<Route>();
            List<Location> foodLocations = state.FoodTiles;
            //Keep track of food and which ants are delegated to foodfinding
            List<Location> foodTargets = new List<Location>();
            List<Location> foodAnts = new List<Location>();

            //If map has not been set yet, add a new map
            if (map == null)
            {
                //Create a new map
                map = new Map(state.Width, state.Height);
            }

            //Add all locations to unseen tiles set, run once
            if (unseenTiles == null)
            {
                unseenTiles = new List<Location>();
                for (int row = 0; row < state.Width; row++)
                {
                    for (int col = 0; col < state.Height; col++)
                    { unseenTiles.Add(new Location(row, col)); }
                }
            }
            //Remove any tiles that can be seen, run each turn
            int count = unseenTiles.Count;
            for (int i = 0; i < count; i++)
            {
                //Check if each tile in unseentiles is visible or not
                if (state.GetIsVisible(unseenTiles[i]))
                {
                    //Add the tile info to the map
                    map.AddLocation(unseenTiles[i], state);
                    //If visible, remove. Then put i back 1 and lower count by 1
                    unseenTiles.RemoveAt(i);
                    i--;
                    count--;
                }
            }

            //To prevent stepping on your own hills
            foreach (AntHill myHill in state.MyHills)
            {
                orders.Add(myHill); //Now the hills always count as occupied
            }
            if (state.TimeRemaining > 10)
            {
                //Create a route from each ant to each food
                foreach (Location food in foodLocations)
                {
                    foreach (Ant ant in state.MyAnts)
                    {
                        int distance = state.GetDistance(ant, food);
                        Route route = new Route(ant, food, distance);
                        foodRoutes.Add(route);
                    }
                }

                //Sort routes on distance (Note: This took waaay too long to figure out)
                foodRoutes.Sort(delegate(Route r1, Route r2)
                {
                    return r1.CompareTo(r2);
                });
                foreach (Route route in foodRoutes)
                {
                    //Check if food has ant sent to it already
                    //Check if ant has been sent already
                    //If not, sent ant to food if move is possible
                    if (!foodTargets.Contains(route.GetEnd)
                        && !foodAnts.Contains(route.GetStart)
                        && DoMoveLocation(route.GetStart, route.GetEnd, state))
                    {
                        foodTargets.Add(route.GetEnd);
                        foodAnts.Add(route.GetStart);
                    }
                    // check if we have time left to calculate more orders
                    if (state.TimeRemaining < 10) break;
                }
            }

            //Add new hills to the list of enemy hills
            foreach (Location enemyHill in state.EnemyHills)
                if (!enemyHills.Contains(enemyHill)) enemyHills.Add(enemyHill);
            //Attack enemy hills
            if (state.TimeRemaining > 10)
            {
                List<Route> hillRoutes = new List<Route>();
                foreach (Location hillLoc in enemyHills)
                {
                    foreach (Location antLoc in state.MyAnts)
                    {
                        //Check for all the ants whether
                        if (!orders.Contains(antLoc))
                        {
                            int distance = state.GetDistance(antLoc, hillLoc);
                            Route route = new Route(antLoc, hillLoc, distance);
                            hillRoutes.Add(route);
//.........这里部分代码省略.........
开发者ID:TRdeBie,项目名称:KI_Project1,代码行数:101,代码来源:MyBot.cs

示例4: GetNearestFriend

 public Location GetNearestFriend(Location location, IGameState state)
 {
     int d = int.MaxValue;
     int x;
     Location l = null;
     foreach (Ant a in state.MyAnts)
     {
         if (a.Equals(location))
             continue;
         x = state.GetDistance(location, a);
         if (x < d)
         {
             d = x;
             l = a;
         }
     }
     return l;
 }
开发者ID:Lapixx,项目名称:CS-UU-KI2,代码行数:18,代码来源:MyBot.cs

示例5: GetNearestHill

 public Location GetNearestHill(Location location, IGameState state)
 {
     int d = int.MaxValue;
     int x;
     Location l = null;
     foreach (AntHill a in state.MyHills)
     {
         x = state.GetDistance(location, a);
         if (x < d)
         {
             d = x;
             l = a;
         }
     }
     return l;
 }
开发者ID:Lapixx,项目名称:CS-UU-KI2,代码行数:16,代码来源:MyBot.cs

示例6: GetNearestFood

 public Location GetNearestFood(Location location, IGameState state)
 {
     int d = int.MaxValue;
     int x;
     Location l = null;
     foreach (Location a in state.FoodTiles)
     {
         x = state.GetDistance(location, a);
         if (x < d)
         {
             d = x;
             l = a;
         }
     }
     return l;
 }
开发者ID:Lapixx,项目名称:CS-UU-KI2,代码行数:16,代码来源:MyBot.cs

示例7: DoTurn

        // DoTurn is run once per turn
        public override void DoTurn(IGameState state)
        {
            this.decision.Update(state);

            //searches a path that cannot be attacked in one turn, and that does look whether another ant is in the way.
            Search search1 = new Search(state, state.GetDistance,
                (Location location) => { return state.GetIsUnoccupied(location) && !state.GetIsAttackable(location); });

            //searches a path that cannot be attacked in one turn, but that does not look whether another ant is in the way
            //so the path is planned like all friendly ants do not exist.
            Search search2 = new Search(state, state.GetDistance,
                (Location location) => { return state.GetIsPassable(location) && !state.GetIsAttackable(location); });

            //search for a path that takes in account where other friendly ants are, but does not look at enemy ants.
            Search search3 = new Search(state, state.GetDistance, state.GetIsPassable);

            foreach (Ant ant in state.MyAnts) {

                //check whether a defend ant is blocking hill because it cannot go to its defend position.
                if (ant.Mode == AntMode.Defend && ant.WaitTime > 5) {
                    foreach (AntHill hill in state.MyHills) {
                        if (ant.Equals(hill)) {
                            decision.ReportInvalidDefendPosition(ant.Target);

                            //reset ant
                            ant.Target = null;
                            ant.Route = null;
                            ant.Mode = AntMode.None;
                        }
                    }
                }

                ant.WaitTime++;

                if (ant.Mode == AntMode.None) {
                    this.decision.SetAntMode(ant);
                }

                //updates the secondary target of the ant, so gets hill or food target when needed
                decision.UpdateTarget2(ant, state);

                if (ant.Target2 != null) {

                    if (ant.Route2 == null) {
                        //only get food if youre not gonna die for it, and if no other ant is in the way (use search1).
                        //and there exists a path to the food/hill which distance is less than 1.5 times
                        //the distance between the ant and the food/hill.
                        ant.Route2 = search1.AStar(ant, ant.Target2, (int)(state.GetDistance(ant, ant.Target2) * 1.5));

                        //remove the last node if its food because we don't need to stand on it to get it.
                        if (ant.Route2 != null && state.FoodTiles.Contains(ant.Target2)) {
                            ant.Route2.RemoveAt(ant.Route2.Count - 1);
                        }
                    }

                    //if the route2 is null after recalculation, drop the target
                    if (ant.Route2 == null) {
                        ant.Target2 = null;
                    } else {
                        //only get food if no other ant is blocking it
                        if (ant.Route2.Count > 1 && state.GetIsUnoccupied(ant.Route2[1])) {
                            IssueOrder(state, ant, GetDirectionFromPath(ant.Route2, state));
                            ant.Route2.RemoveAt(0);

                                //original route has become invalid
                                ant.Route = null;
                        } else {
                            ant.Target2 = null;
                            ant.Route2 = null;
                        }
                    }
                }

                //if there exists no secundairy target, then get the primary target.
                //it is possible that there exists no target, and then no target is found.
                //therefore it we can't use an else construction but should again check if (ant.Target2 == null.)
                if (ant.Target2 == null) {

                    //if we reached our target, drop it.
                    if (ant.Equals(ant.Target)) {
                        ant.Target = null;
                        ant.Route = null;
                    }

                    //if an ant in not in attack mode, avoid getting killed by not taking this route.
                    //we need to check this each turn because our routes are calculated only one time
                    //and enemy ants may be moved.
                    if (ant.Route != null && ant.Route.Count > 1) {
                        if (state.GetIsAttackable(ant.Route[1]) && ant.Mode != AntMode.Attack) {
                            ant.Route = null;
                        }
                    }

                    //if an ant has no target or waited for too long, get a new target
                    if (ant.Target == null || ant.WaitTime > 2) {
                        decision.SetTarget(ant, state);
                        ant.Route = null;
                    }

//.........这里部分代码省略.........
开发者ID:CPutz,项目名称:KIPracticum1,代码行数:101,代码来源:MyBot.cs

示例8: DoTurn

        public override void DoTurn(IGameState state)
        {
            var usedMoves = new HashSet<Location>();

            foreach (Ant ant in state.MyAnts)
            {
                int min = int.MaxValue;
                Location minLoc = ant;

                foreach (var point in state.FoodTiles)
                {
                    int ras = state.GetDistance(point, ant);
                    if (min > ras)
                    {
                        min = ras;
                        minLoc = point;
                    }
                }
                int x1 = ant.Col;
                int y1 = ant.Row;
                int x2 = minLoc.Col;
                int y2 = minLoc.Row;
                int dx = Math.Abs(x1-x2);
                int dy = Math.Abs(y1-y2);
                var direction = Direction.East;
                if (dx > 0)
                {
                    if (dx < state.Width - dx)
                        direction = GetDirectionX(x2 - x1);
                    else
                        direction = GetDirectionX(x1 - x2);
                }
                else if(dy > 0)
                {
                    if (dy < state.Height - dy)
                        direction = GetDirectionY(y2 - y1);
                    else
                        direction = GetDirectionY(y1 - y2);
                }

                Location newLoc = state.GetDestination(ant, direction);
                if (state.GetIsPassable(newLoc) && !usedMoves.Contains(newLoc))
                {
                    usedMoves.Add(newLoc);
                    IssueOrder(ant, direction);
                }
                /*for (int i = 0; i < 4; i++ )
                {
                    var direction = _directions[
                        _random.Next(4)];

                    Location newLoc = state.GetDestination(ant, direction);

                    if (state.GetIsPassable(newLoc) && !usedMoves.Contains(newLoc))
                    {
                        usedMoves.Add(newLoc);
                        IssueOrder(ant, direction);
                        break;
                    }

                }*/

                if (state.TimeRemaining < 10) break;
            }
        }
开发者ID:MichaelEk,项目名称:AIChallengeTraining,代码行数:65,代码来源:MyBot.cs

示例9: GetReward12

 /// <summary>
 /// Gets the reward by going from <paramref name="oldLocation"/> to <paramref name="newLocation"/>
 /// by doing some action <paramref name="a"/> on a 12x12 map.
 /// </summary>
 /// <param name="gameState">The GameState.</param>
 /// <param name="oldLocation">The location before taking the action.</param>
 /// <param name="a">The action that was taken.</param>
 /// <param name="newLocation">The location after taking the action.</param>
 /// <returns>The reward by doing action <paramref name="a"/> from Location 
 /// <paramref name="oldLocation"/> to get to Location <paramref name="newLocation"/> for a 12x12 map.</returns>
 private float GetReward12(IGameState gameState, Location oldLocation, Action a, Location newLocation)
 {
     Location ownHill = new Location(9, 3);
     int d1 = gameState.GetDistance(ownHill, oldLocation);
     int d2 = gameState.GetDistance(ownHill, newLocation);
     return 1.0f * (d2 - d1);
 }
开发者ID:CPutz,项目名称:KIPracticum2,代码行数:17,代码来源:MyBot.cs

示例10: WalkThatWay

        // Local planning (for individual ant)
        public void WalkThatWay(Ant a, IGameState state)
        {
            if (heatMap.GetCell(a) > 0.1f)
                heatMap.UpdateCell(a, -0.1f);

            string key = LocationToKey(a);
            if (!currentTasks.ContainsKey(key))
            {
                if (state.MyAnts.Count / 8 > martinSheen.Count && timRobbins.Count > 0)
                {
                    Location martin = timRobbins[timRobbins.Count - 1];
                    timRobbins.RemoveAt(timRobbins.Count - 1);
                    martinSheen.Add(martin);
                    currentTasks.Add(key, new CurrentTask(Task.Guaaard, martin));
                }
                else if (state.MyAnts.Count >= armySize)
                {
                    return;
                }
                else
                {
                    LogShit("Shitjeweet.txt", currentStep + " added or something youknowyourself");
                    currentTasks.Add(key, new CurrentTask(Task.Roaming, a));
                }
            }

            CurrentTask task = currentTasks[key];
            task.from = a;
            task.resolved = false;

            if (task.task != Task.Terminating && task.task != Task.Guaaard)
            {
                foreach (Location e in theirHills)
                {
                    if (state.GetDistance(a, e) <= raidRadius)
                    {
                        task.hill = e;
                        task.task = Task.Terminating;
                        break;
                    }
                }
            }

            if (task.task == Task.Terminating)
            {
                if (task.hill.Equals(a))
                {
                    theirHills.Remove(task.hill);
                    heatMap.ResetCell(task.hill);
                }
                if (!theirHills.Contains(task.hill))
                {
                    task.task = Task.Roaming;
                    task.roam = a;
                }
            }

            if (task.task == Task.Dinner)
            {
                if (task.food.Equals(a) || (state[task.food.Row, task.food.Col] != Tile.Food && state.GetIsVisible(task.food)))
                {
                    task.task = Task.Roaming;
                    yummies.Remove(task.food);
                }
            }

            if (task.task == Task.Roaming)
            {

                Location f = SearchFood(a, state);
                if (f != null)
                {
                    task.food = f;
                    task.task = Task.Dinner;
                    yummies.Add(f);
                }

                if (task.roam.Equals(a) || !state.GetIsPassable(task.roam) || state.GetDistance(a, task.roam) <= arrivalRadius)
                {
                    heatMap.UpdateCell(task.roam, -5f);
                    task.roam = GetNewDestination(a, state);
                    if (task.roam == null)
                    {
                        task.to = task.from;
                        return;
                    }
                }
            }

            List<Location> avoid = new List<Location>();
            avoid.AddRange(state.MyHills);
            avoid.AddRange(martinSheen);
            if (task.task == Task.Guaaard)
                avoid.Remove(task.roam);

            Location tgt = null;
            switch (task.task)
            {
                case Task.Roaming: tgt = task.roam; break;
//.........这里部分代码省略.........
开发者ID:Lapixx,项目名称:CS-UU-KI1,代码行数:101,代码来源:MyBot.cs


注:本文中的IGameState.GetDistance方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。