コード例 #1
0
ファイル: GameState.cs プロジェクト: CPutz/KIPracticum1
        public GameState(int width, int height, 
		                  int turntime, int loadtime, 
		                  int viewradius2, int attackradius2, int spawnradius2)
        {
            Width = width;
            Height = height;

            LoadTime = loadtime;
            TurnTime = turntime;
            Turn = 0;

            ViewRadius2 = viewradius2;
            ViewRadius = (int)Math.Floor(Math.Sqrt(this.ViewRadius2));
            AttackRadius2 = attackradius2;
            AttackRadius = (int)Math.Floor(Math.Sqrt(this.AttackRadius2));
            SpawnRadius2 = spawnradius2;

            MyAnts = new List<Ant>();
            MyHills = new List<AntHill>();
            EnemyAnts = new Map<Ant>(Width, Height);
            EnemyHills = new Map<AntHill>(Width, Height);
            MyDeads = new List<Ant>();
            EnemyDeads = new List<Location>();
            FoodTiles = new Map<Location>(Width, Height);

            MyAntsNextTurn = new Ant[Height, Width];
            antNumber = 0;

            map = new Tile[Height, Width];
            for (int row = 0; row < Height; row++) {
                for (int col = 0; col < Width; col++) {
                    map[row, col] = Tile.Land;
                }
            }

            VisibilityMap = new bool[Height, Width];
            EnemyAttackMap = new bool[Height, Width];
        }
コード例 #2
0
ファイル: MyBot.cs プロジェクト: TRdeBie/KI_Project1
        // 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);
                        }
                    }
                }
                //Sort the routes to enemy hill
                if (state.TimeRemaining > 10)
                {
                    hillRoutes.Sort(delegate(Route r1, Route r2)
                    {
                        return r1.CompareTo(r2);
                    });
                    foreach (Route route in hillRoutes)
                    {
                        //Perform the routes from shortest to longest
                        DoMoveLocation(route.GetStart, route.GetEnd, state);
                        // check if we have time left to calculate more orders
                        if (state.TimeRemaining < 10) break;
                    }
                }
            }
            //Explore unseen areas
            if (state.TimeRemaining > 10)
            {
                foreach (Ant ant in state.MyAnts)
                {
                    if (!orders.Contains(ant))
                    {
                        List<Route> unseenRoutes = new List<Route>();
                        //Find the routes to all unseen tiles in the map
                        foreach (Location unseenLoc in unseenTiles)
                        {
                            int distance = state.GetDistance(ant, unseenLoc);
                            Route route = new Route(ant, unseenLoc, distance);
                            unseenRoutes.Add(route);
                        }
                        //Sort the routes from short to long
                        unseenRoutes.Sort(delegate(Route r1, Route r2)
                        {
                            return r1.CompareTo(r2);
                        });
                        //From closest to furthest, check the routes to unseen tiles
                        foreach (Route route in unseenRoutes)
                            if (DoMoveLocation(route.GetStart, route.GetEnd, state)) break;

                        // check if we have time left to calculate more orders
                        if (state.TimeRemaining < 10) break;
                    }
                }
            }
            if (state.TimeRemaining > 10)
            {
                //Create a list of ants in format Location instead of Ant
                List<Location> antlist = new List<Location>();
                foreach (Location ant in state.MyAnts)
                    antlist.Add(ant);
                //Unblock the hills
                foreach (Location myHill in state.MyHills)
                {
                    if (antlist.Contains(myHill) && !orders.Contains(myHill))
                        foreach (Direction direction in Ants.Aim.Keys)
                            if (DoMoveDirection(myHill, direction, state))
                                break;
                }
            }
            orders.Clear();
        }