public bool RoomateMatch(Camper c) { foreach (Room r in Rooms) { if (c.Constraints.isRoomateMatch(r.Camper.CamperName)) { return(true); } } return(false); }
public Camper Rearrange(Camper camper) { Room result = null; int index = 0; bool success = false; Room[] roomlist = new Room[rooms.Count]; rooms.CopyTo(roomlist); // Find a camper that can be replaced // Will get the most recent for (int i = 0; i < rooms.Count; i++) { Room r = roomlist[i]; foreach (Room room in rooms) { if (!room.Camper.Constraints.isRoomateMatch(r.Camper.CamperName)) { result = r; index = i; } } } if (result == null) { return(null); } // Check if the incoming camper is a match foreach (Room room in rooms) { if (room.Camper.Constraints.isRoomateMatch(camper.CamperName)) { success = true; } } // swap campers if (success) { Camper previous = rooms[index].Camper; rooms[index].Camper = camper; return(previous); } else { return(null); } }
private Dictionary <int, List <Lodge> > ProcessUnmatched(List <Camper> unmatched, List <Camper> leftovers, List <Room> rooms, Dictionary <int, List <Lodge> > matches) { Lodge remainders = new Lodge("Unmatched", 100); if (leftovers != null) { while (leftovers.Count != 0) { unmatched.Add(leftovers[0]); leftovers.RemoveAt(0); } } // Resolve roomate preferences foreach (Camper camper in unmatched) { //bool done = false; if (camper.Person) { // find a missing roomate match for the camper foreach (Lodge L in matches[camper.Constraints.GetHashCode()]) { // first, find out if there is someone in the lodge that can be moved Camper returned = L.Rearrange(camper); if (returned != null) { leftovers.Add(returned); } else { leftovers.Add(camper); } } } else { leftovers.Add(camper); } } // find space for remaining foreach (Camper camper in leftovers) { // Find available space with current constriants Room r; if (rooms.Count > 0) { r = rooms[0]; rooms.RemoveAt(0); } else { r = new Room(1); } r.Camper = camper; bool found = false; int top = matches[camper.Constraints.GetHashCode()].Count - 1; if (!matches[camper.Constraints.GetHashCode()][top].isFull()) { matches[camper.Constraints.GetHashCode()][top].AddRoom(r); } else { // Check other Lodges for space foreach (Lodge lodge in matches[camper.Constraints.GetHashCode()]) { if (!lodge.isFull()) { lodge.AddRoom(r); found = true; } } } // find space by removing constraints one by one if (!found) { // Copy camper string pref1 = camper.Constraints.Roomate_Pref1; string pref2 = camper.Constraints.Roomate_Pref2; string pref3 = camper.Constraints.Roomate_Pref3; string pref4 = camper.Constraints.Roomate_Pref4; Constraint c = new Constraint(camper.Constraints.Person, camper.Constraints.Smoking, camper.Constraints.Dog, camper.Constraints.Gender, pref1, pref2, pref3, pref4); Camper nextC = new Camper(camper.Id, camper.CamperName, camper.Person, camper.Smoking, camper.Dog, camper.Gender); nextC.Constraints = c; bool done = false; if (nextC.Constraints.Dog) { nextC.Constraints.Dog = false; if (matches.ContainsKey(nextC.Constraints.GetHashCode())) { foreach (Lodge lodge in matches[nextC.Constraints.GetHashCode()]) { if (!lodge.isFull()) { camper.comment = "Couldn't find space with current preferences. Dog preference removed."; lodge.AddRoom(r); done = true; } } } } if (!done && nextC.Constraints.Smoking) { nextC.Constraints.Smoking = false; if (matches.ContainsKey(nextC.Constraints.GetHashCode())) { foreach (Lodge lodge in matches[nextC.Constraints.GetHashCode()]) { if (!lodge.isFull()) { camper.comment = "Couldn't find space with current preferences. Smoking preference removed."; lodge.AddRoom(r); done = true; } } } } if (!done && nextC.Constraints.Person) { nextC.Constraints.Person = false; if (matches.ContainsKey(nextC.Constraints.GetHashCode())) { foreach (Lodge lodge in matches[nextC.Constraints.GetHashCode()]) { if (!lodge.isFull()) { camper.comment = "Couldn't find space with current preferences. Roomate preference removed."; lodge.AddRoom(r); } } } } else { // Couldn't find a match. Add "Unmatched" Lodge camper.comment = "Couldn't find a match."; remainders.AddRoom(r); } } // If no space, add to unmatched list else { remainders.AddRoom(r); } } matches[100].Add(remainders); return(matches); }