private static IHasName DateCouple(Human firstHuman, Human secondHuman) { if (firstHuman == null || secondHuman == null) { throw new ArgumentNullException(); } if (firstHuman.Sex == secondHuman.Sex) { throw new WrongCoupleException(Resources.AdvancedGod_WouldNotWorkOut); } var firstAttribute = GetAttribute(firstHuman, secondHuman); var secondAttribute = GetAttribute(secondHuman, firstHuman); if (!Randomizer.CheckIfLikes(firstAttribute.Probability) || !Randomizer.CheckIfLikes(secondAttribute.Probability)) { return null; } return MakeChild( firstAttribute.ChildType == secondAttribute.ChildType ? firstAttribute.ChildType : null, firstHuman.Sex == Sex.Female ? firstHuman : secondHuman, firstHuman.Sex == Sex.Female ? secondHuman : firstHuman); }
private static IHasName MakeChild(string childType, Human mother, Human father) { if (childType == null) throw new ArgumentNullException(nameof(childType)); try { var type = GetTypeOfHuman(childType); var name = GetChildsName(mother); var constructor = type.GetConstructors().FirstOrDefault(); if (constructor != null && constructor.GetParameters().Length > 1) { return Activator.CreateInstance(type, name, Names.PatronymicFromParentName(Sex.Female, father.Name), NewBornAge) as IHasName; } return Activator.CreateInstance(type, name) as IHasName; } catch (Exception e) { throw new Exception("Child of given type can not be constructed", e); } }
private static string GetChildsName(Human human) { var namingMethod = human.GetType().GetMethods() .First(x => x.Name == Human.ChildNamingMethod && x.ReturnType == typeof(string)); try { return namingMethod.Invoke(human, null) as string; } catch (Exception e) { throw new Exception(Resources.AdvancedGod_ChildNamingNotSupported, e); } }
private static CoupleAttribute GetAttribute(Human firstHuman, Human secondHuman) { var enumerator = new CoupleAttributeEnumerator(firstHuman.GetType()); while (enumerator.MoveNext()) { if (enumerator.Current.Pair.Equals(secondHuman.GetType().Name)) { return enumerator.Current; } } throw new WrongCoupleException(Resources.AdvancedGod_IncompatibleTypes); }