void LoadTeamsExecute() { OpenFileDialog loadFileDialog = new OpenFileDialog(); loadFileDialog.InitialDirectory = AppDomain.CurrentDomain.BaseDirectory; loadFileDialog.Filter += "Name Files (*.rndt)|*.rndt|All Files (*.*)|*.*"; loadFileDialog.DefaultExt = "rnd"; loadFileDialog.AddExtension = true; List <List <string> > teamList = new List <List <string> >(); if (loadFileDialog.ShowDialog() == true) { using (StreamReader sr = new StreamReader(loadFileDialog.FileName)) { XmlSerializer xs = new XmlSerializer(typeof(List <List <string> >)); teamList = (List <List <string> >)xs.Deserialize(sr); } } List <string> names = new List <string>(); _teams.Clear(); foreach (List <string> team in teamList) { MemberGroup newTeam = new MemberGroup(); _teams.Add(newTeam); foreach (string member in team) { newTeam.Users.Add(new Member(member)); names.Add(member); } } // also load names string current = _currentName; AllMembers.Clear(); foreach (string name in names) { _currentName = name; AddMemberExecute(); } _currentName = current; }
// recursive find teams public static void MakeTeam(List <Member> users) { // return - all users are mapped if (users.Count == 0) { return; } int teamCounter = 1; Random random = new Random(); // get first user of the new team // first look for members with illegal group Member m1 = GetMemberWithIllegal(users); // take random if no illegal groups left if (m1 == null) { m1 = users[random.Next(0, users.Count - 1)]; } // remove this user from the pool of all users users.Remove(m1); // get all valid users left in the pool var tUsers = users.Except(m1.IllegalMembers); List <Member> validUsers = new List <Member>(); foreach (Member item in tUsers) { validUsers.Add(item); } // create new team MemberGroup team = new MemberGroup(); team.Number = teamCounter++; // add firts user team.Users.Add(m1); // !!! not enough users left !!! if (users.Count <= _teamSize - 1) { foreach (Member item in users) { team.Users.Add(item); } _teams.Add(team); return; // } // take x (according to team size) random users from remaining valid users for (int i = 0; i < _teamSize - 1; i++) { int rndIndex = 0; if (validUsers.Count > 1) { rndIndex = random.Next(0, validUsers.Count - 1); } if (validUsers.Count > rndIndex) { team.Users.Add(validUsers[rndIndex]); users.Remove(validUsers[rndIndex]); validUsers.Remove(validUsers[rndIndex]); } } // finally add the new team _teams.Add(team); MakeTeam(users); }