Britbot
static void Britbot.ConfigHelper.GroupJoining ( List< int >  ultimateConfig) [inline, static, private]

A method that reffers to the ULTIMATE CONFIGURATION and joins the group as needed.

Definition at line 64 of file ConfigHelper.cs.

        {
            Group tempGroup = null;
            int minDistance = Magic.MaxJoinDistance + 1; //maximun joining distance

            //sort configs by size
            Commander.Groups.Sort((a, b) => a.Pirates.Count.CompareTo(b.Pirates.Count));
            ultimateConfig.Sort((a, b) => a.CompareTo(b));

            List<int> joinedGroups = new List<int>();

            //go over the config and correct it
            for (int i = 0; i < Math.Min(ultimateConfig.Count, Commander.Groups.Count); i++)
            {
                if (Commander.Groups[i].Pirates.Count < ultimateConfig[i])
                {
                    //find the best group to join to the current group
                    foreach (Group g in Commander.Groups.Where(g => g.Pirates.Count == 1 && joinedGroups.Contains(g.Id))
                        )
                    {
                        //minimal distance between the two groups
                        int tempDistance = Commander.Groups[i].MinDistance(g);

                        //if the current minimun is better than the last minimun and the group do not cintain the same pirates..
                        if ((tempDistance < minDistance) && (!Commander.Groups[i].Pirates.Intersect(g.Pirates).Any()))
                        {
                            tempGroup = g;
                            minDistance = tempDistance;
                        }
                    }
                    //join the groups if they are close enough
                    if (minDistance <= Magic.MaxJoinDistance)
                    {
                        if (tempGroup != null)
                        {
                            joinedGroups.Add(tempGroup.Id);
                            Commander.Groups[i].Join(tempGroup, false);
                        }
                    }
                }
            }

            //remove all joined group from the commander list
            Commander.Groups.RemoveAll(g => joinedGroups.Contains(g.Id));

            Logger.Write("Commander Groups After Reconfiguration:");
            foreach (Group g in Commander.Groups)
            {
                Logger.Write(g.Pirates.Count.ToString());
            }
        }