public void checkInSight(Map map)
        {
            inSight = false;

            foreach (Region reg in SubRegions)
            {
                if (!map.GetRegion(reg.Id).OwnedByPlayer("unknown")) inSight = true;
            }
        }
 public bool HasPlayer(Map map, string playerName)
 {
     foreach (Region region in subRegions)
     {
         Region mapr = map.GetRegion(region.Id);
         if (playerName == mapr.PlayerName)
             return true;
     }
     return false;
 }
 /**
  * @return A string with the name of the player that fully owns this SuperRegion
  */
 public string OwnedByPlayer(Map map)
 {
     String playerName = map.GetRegion(subRegions.First().Id).PlayerName;
     foreach(Region region in subRegions)
     {
         Region mapr = map.GetRegion(region.Id);
         if (playerName != mapr.PlayerName)
             return null;
     }
     return playerName;
 }
 public bool MightBeOwnedBy(Map map, String enemyName)
 {
     bool owned = true;
     foreach (Region region in subRegions)
     {
         Region mapr = map.GetRegion(region.Id);
         String playerName = mapr.PlayerName;
         if ((playerName != enemyName) && (playerName != "unknown")) owned = false;
     }
     return owned;
 }
 /**
  * @return : a new Map object exactly the same as this one
  */
 public Map GetMapCopy()
 {
     Map newMap = new Map();
     foreach (SuperRegion sr in superRegions) //copy superRegions
     {
         SuperRegion newSuperRegion = new SuperRegion(sr.Id, sr.ArmiesReward);
         newMap.Add(newSuperRegion);
     }
     foreach (Region r in regions) //copy regions
     {
         try
         {
             Region newRegion = new Region(r.Id, newMap.GetSuperRegion(r.SuperRegion.Id), r.PlayerName, r.Armies);
             newMap.Add(newRegion);
         }
         catch (Exception exc) {
             Console.Error.WriteLine(exc.Message);
             Console.Error.WriteLine("couldn't copy region");
             Console.Error.WriteLine("id: " + r.Id);
             Console.Error.WriteLine("parent: " + r.SuperRegion.Id);
             Console.Error.WriteLine("playername: " + r.PlayerName);
             Console.Error.WriteLine("armies: " + r.Armies);
         }
     }
     foreach (Region r in regions) //Add neighbors to copied regions
     {
         Region newRegion = newMap.GetRegion(r.Id);
         foreach (Region neighbor in r.Neighbors)
             newRegion.AddNeighbor(newMap.GetRegion(neighbor.Id));
     }
     return newMap;
 }
        // Visible regions are given to the bot with player and armies info
        public void UpdateMap(String[] mapInput)
        {
            foreach (Region r in fullMap.regions)
            {
                r.PlayerName = "unknown";
            }

            visibleMap = fullMap.GetMapCopy();
            for (int i = 1; i < mapInput.Length; i++)
            {
                try
                {
                    Region region = visibleMap.GetRegion(int.Parse(mapInput[i]));
                    String playerName = mapInput[i + 1];
                    int armies = int.Parse(mapInput[i + 2]);

                    region.PlayerName = playerName;
                    region.PreviousTurnArmies = region.Armies;
                    region.Armies = armies;
                    //region.DeployedOrTransferedLastTurn = region.DeployedOrTransferedThisTurn;
                    region.DeployedOrTransferedThisTurn = false;
                    // clean up temporary variables
                    region.ReservedArmies = 0;
                    region.PledgedArmies = 0;

                    // update fullmap aswell
                    Region region2 = fullMap.GetRegion(region.Id);
                    region2.PlayerName = playerName;
                    region2.PreviousTurnArmies = region2.Armies;
                    region2.Armies = armies;
                    //region2.DeployedOrTransferedLastTurn = region2.DeployedOrTransferedThisTurn;
                    region2.DeployedOrTransferedThisTurn = false;
                    region2.PledgedArmies = 0;
                    region2.ReservedArmies = 0;

                    i += 2;
                }
                catch (Exception e)
                {
                    Console.Error.WriteLine("Unable to parse Map Update " + e);
                }
            }

            enemySighted = false;
            enemyBorders.Clear();

            foreach (SuperRegion r in fullMap.superRegions)
            {
                r.checkInSight(fullMap);
            }

            List<Region> unknownRegions = new List<Region>();

            foreach (var region in visibleMap.Regions)
            {
                // Remove regions from visible map which are unknown
                if (region.PlayerName == "unknown")
                    unknownRegions.Add(region);

                // Determing if there are any enemy sightings
                if (region.OwnedByPlayer(OpponentPlayerName))
                {
                    enemySighted = true;
                    enemyBorders.Add(region);
                }
            }

            // remove unknownRegions from visible map
            foreach (Region unknownRegion in unknownRegions)
                visibleMap.Regions.Remove(unknownRegion);

            scheduledAttack.Clear();

            ownedSR.Clear();
            foreach (SuperRegion sr in fullMap.superRegions) {
                sr.numberOfRegionsOwnedByUs = 0;
                sr.numberOfRegionsOwnedByOpponent = 0;
                sr.ownedByUs = false;
            }

            // check what superregions we own
            foreach (Region reg in visibleMap.regions)
            {
                SuperRegion par = fullMap.GetSuperRegion(reg.SuperRegion.Id);
                if (reg.OwnedByPlayer(myName)) par.numberOfRegionsOwnedByUs++;
                if (reg.OwnedByPlayer(opponentName)) par.numberOfRegionsOwnedByOpponent++;
            }
            foreach (SuperRegion sr in fullMap.superRegions) {
                if (sr.numberOfRegionsOwnedByUs == sr.SubRegions.Count) sr.ownedByUs = true;
            }

            // update list of opponent start regions
            UpdateOpponentStartRegions();

            naBased = false;
            saBased = false;
            africaBased = false;
            ozBased = false;

            // figure out best expansion target
            switch (RoundNumber) // start of round 1
            {
                case 1:
                    {
                        expansionTargetSuperRegions = FullMap.GetMapCopy().SuperRegions;

                        // sort super region by quality of expansibility
                        foreach (SuperRegion a in expansionTargetSuperRegions)
                        {
                            // superregion is considered safe if we have all the strategic starting picks (in, or neighbouring) the superregion
                            // this function quantifies how safe it really is

                            int count = 0;

                            bool redflag = false; //redflag when there is enemy or unknown on a starting pick in or neighbouring
                            foreach (Region reg in pickableStartingRegions)
                            {
                                //bool found = false;

                                // check if we have any neighbour bordering this superregion (this selection includes picks in or countering)
                                foreach (Region neighbour in reg.Neighbors)
                                {

                                    if (neighbour.SuperRegion.Id == a.Id)
                                    {
                                        //if it is neighboring an area of this superregion, check who the pick was assigned to
                                        switch (reg.PlayerName)
                                        {
                                            case "player1":
                                                if (myName == "player1")
                                                {
                                                    count += 2;
                                                }
                                                else
                                                {
                                                    redflag = true;
                                                }
                                                break;
                                            case "player2":
                                                if (myName == "player2")
                                                {
                                                    count += 2;
                                                }
                                                else
                                                {
                                                    redflag = true;
                                                }
                                                break;
                                            case "neutral":
                                                count++;
                                                break;
                                            case "unknown":
                                                //todo: check if the pick was picked by me in lower position then my last gotten pick
                                                //count--;
                                                //todo: instead of giving it lower priority, it could also be worth increasing the aggressivity
                                                count--;
                                                break;
                                        }

                                        // we only need to check one of the found neighbours to know this is a relevant pick, skip the rest
                                        //found = true;
                                        break;
                                    }
                                }

                                //if (found) break;

                            }

                            if (a.SubRegions.Count <= 4) count += 2;
                            else if (a.SubRegions.Count <= 5) count++;

                            if (redflag) count = -1;

                            a.tempSortValue = count;
                        }

                        expansionTargetSuperRegions = expansionTargetSuperRegions.OrderByDescending(p => p.tempSortValue).ToList();

                    }
                    break;
                default: // start of other rounds
                    {

                        // check if we are based somewhere
                        int naCount = 0;
                        int saCount = 0;
                        africaCount = 0;
                        int ozCount = 0;
                        foreach (Region reg in FullMap.Regions)
                        {
                            //todo: refactor this to use the new SuperRegion.ownedByUs bool

                            // check if we are NABased
                            // all 7 areas of north america are ours
                            if (((reg.Id == 1) && (reg.OwnedByPlayer(MyPlayerName))) ||
                                    ((reg.Id == 2) && (reg.OwnedByPlayer(MyPlayerName))) ||
                                    ((reg.Id == 3) && (reg.OwnedByPlayer(MyPlayerName))) ||
                                    ((reg.Id == 4) && (reg.OwnedByPlayer(MyPlayerName))) ||
                                    ((reg.Id == 5) && (reg.OwnedByPlayer(MyPlayerName))) ||
                                    ((reg.Id == 6) && (reg.OwnedByPlayer(MyPlayerName))) ||
                                    ((reg.Id == 7) && (reg.OwnedByPlayer(MyPlayerName))) ||
                                    ((reg.Id == 8) && (reg.OwnedByPlayer(MyPlayerName)))
                                )
                            {
                                naCount++;
                            }

                            // check if we are saBased
                            // all 4 areas of south america are ours
                            if (((reg.Id == 10) && (reg.OwnedByPlayer(MyPlayerName))) ||
                                    ((reg.Id == 11) && (reg.OwnedByPlayer(MyPlayerName))) ||
                                    ((reg.Id == 12) && (reg.OwnedByPlayer(MyPlayerName))) ||
                                    ((reg.Id == 13) && (reg.OwnedByPlayer(MyPlayerName)))
                                )
                            {
                                saCount++;
                            }

                            // check if we are africaBased
                            // all 6 areas of africa are ours
                            if (((reg.Id == 21) && (reg.OwnedByPlayer(MyPlayerName))) ||
                                    ((reg.Id == 22) && (reg.OwnedByPlayer(MyPlayerName))) ||
                                    ((reg.Id == 23) && (reg.OwnedByPlayer(MyPlayerName))) ||
                                    ((reg.Id == 24) && (reg.OwnedByPlayer(MyPlayerName))) ||
                                    ((reg.Id == 25) && (reg.OwnedByPlayer(MyPlayerName))) ||
                                    ((reg.Id == 26) && (reg.OwnedByPlayer(MyPlayerName)))
                                )
                            {
                                africaCount++;
                            }

                            // check if we are ozBased
                            // all 4 areas of oz are ours & siam is not enemy & brazil is not ours
                            // brazil is main target, if we have a foot there it doesnt matter if we are oz based or not
                            if (((reg.Id == 39) && (reg.OwnedByPlayer(MyPlayerName))) ||
                                    ((reg.Id == 40) && (reg.OwnedByPlayer(MyPlayerName))) ||
                                    ((reg.Id == 41) && (reg.OwnedByPlayer(MyPlayerName))) ||
                                    ((reg.Id == 42) && (reg.OwnedByPlayer(MyPlayerName))) ||
                                    ((reg.Id == 12) && (!reg.OwnedByPlayer(MyPlayerName))) ||
                                    ((reg.Id == 38) && (!reg.OwnedByPlayer(OpponentPlayerName)))
                                )
                            {
                                ozCount++;
                            }
                        }
                        if (ozCount == 6) ozBased = true;
                        if (saCount == 4) saBased = true;
                        if (africaCount == 6) africaBased = true;
                        if (naCount == 7) naBased = true;

                        bool enemyOnAfrica = false;
                        for (int j = 21; j <= 26; j++)
                        {
                            Region reg = fullMap.GetRegion(j);
                            if (reg.OwnedByPlayer(opponentName)) enemyOnAfrica = true;
                        }

                        // update our expansion target
                        if (expansionTargetSuperRegions.Count > 0)
                        {
                            // make sure we are not still trying to expand on a superregion we already own
                            if (FullMap.GetSuperRegion(expansionTargetSuperRegions[0].Id).ownedByUs) expansionTargetSuperRegions.RemoveAt(0);

                            // figure out the next best thing
                            foreach (SuperRegion sr in expansionTargetSuperRegions)
                            {
                                int count = 0;

                                bool redflag = false; //redflag when there is enemy or too many unknowns
                                int unknowns = 0;
                                int mine = 0;
                                int neutrals = 0;

                                foreach (Region region in sr.SubRegions)
                                {
                                    Region reg = FullMap.GetRegion(region.Id);

                                    if (reg.OwnedByPlayer(OpponentPlayerName))
                                    {
                                        redflag = true;
                                        continue;
                                    }

                                    if (reg.OwnedByPlayer(MyPlayerName)) mine++;
                                    if (reg.OwnedByPlayer("unknown")) unknowns++;
                                    if (reg.OwnedByPlayer("neutral")) neutrals++;

                                    // check neighbours
                                    foreach (Region rn in region.Neighbors)
                                    {
                                        Region regn = FullMap.GetRegion(rn.Id);

                                        // more borders belonging to us the better
                                        if (regn.OwnedByPlayer(MyPlayerName)) count += 2;

                                        // if there is a border beloging to the enemy, it's not a very good area for expansion
                                        if (regn.OwnedByPlayer(OpponentPlayerName)) count -= 6;
                                    }

                                }

                                count += (sr.SubRegions.Count - unknowns); // the less unknowns ratio the better
                                count += mine * 3;
                                count += (12 - sr.SubRegions.Count) * 2; // less territories the better, easier to defend
                                count += sr.ArmiesReward; // more army rewards the better

                                //todo: later: fine tune this heuristic math

                                // priority exceptions:

                                // if we are ozbased and africa has enemies, main expansion target should be asia
                                //if (sr.Id == 5) // asia
                                //{
                                //    if ((OZBased) && (enemyOnAfrica)) count += 10;
                                //}

                                // if we are ozbased and enemy is stuck in south america, finish up africa
                                if (sr.Id == 4) // africa
                                {
                                    if ((OZBased) && (!enemyOnAfrica)) count += 2;
                                }

                                // if we are sabased main expansion target should always be north america
                                // africa is too vulnerable to stack from oz!
                                if (sr.Id == 1) // north america
                                {
                                    if (SABased && !OZBased) count += 35;
                                }

                                if (redflag) count = -1;

                                sr.tempSortValue = count;
                            }

                            expansionTargetSuperRegions = expansionTargetSuperRegions.OrderByDescending(p => p.tempSortValue).ToList();

                        }
                        else
                        {
                            //todo: we have run out of expansion targets, very rare situation but i guess we should go into another mode of some sort
                        }

                    } break;
            }

            // reset hotstackzone
            hotStackZone = -1;
        }