コード例 #1
0
        public void Matching()
        {
            while (Relationships.Count < N)
            {
                //Console.WriteLine("---------\n");
                for (ushort m = 0; m < Men.Count; m++)
                {
                    Person man = Men[m];

                    if (man.IsSingle)
                    {
                        for (ushort p = 0; p < man.Preferences.Count; p++)
                        {
                            Person manPreference = Women.Find(w => w.Name == man.Preferences[p].Name);

                            if (!man.RefusedBy.Contains(manPreference))
                            {
                                //Console.WriteLine($"{man} z {manPreference}: ");

                                if (manPreference.IsSingle)
                                {
                                    man.CreateRelationShip(ref manPreference);
                                    Relationships.Add(man);

                                    //Console.WriteLine("Związali się");

                                    break;
                                }
                                else if (man.IsBetterThanRival(manPreference.RelationshipWith, manPreference))
                                {
                                    Person rival = Men.Find(r => r.Name == manPreference.RelationshipWith.Name);
                                    rival.Deserted(manPreference);
                                    Relationships.Remove(rival);

                                    man.CreateRelationShip(ref manPreference);
                                    Relationships.Add(man);

                                    //Console.WriteLine($"Związali się i zniszczyli życie {rival}");

                                    break;
                                }
                                else
                                {
                                    man.AddRefusedProposal(manPreference);
                                    //Console.WriteLine("Nie zostaną nigdy małżeństwem");
                                }
                            }
                        }
                    }
                }
            }
        }