-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBloodType.cs
147 lines (135 loc) · 3.17 KB
/
BloodType.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
using System;
using System.Diagnostics;
namespace PopulationGenerator
{
public enum BloodGroup
{
O,
A,
B,
AB
}
public enum BloodRHFactor
{
Positive,
Negative
}
public sealed class BloodType
{
public BloodGroup Group { get; private set; }
public BloodRHFactor RHFactor { get; private set; }
public BloodType(BloodGroup group, BloodRHFactor rhFactor)
{
Group = group;
RHFactor = rhFactor;
}
public override string ToString()
{
return Group.ToString() + (RHFactor == BloodRHFactor.Negative ? '-' : '+');
}
public static bool AreGroups(BloodType mother, BloodType father, BloodGroup group1, BloodGroup group2)
{
return (mother.Group == group1 && father.Group == group2) || (mother.Group == group2 && father.Group == group1);
}
public static BloodType Derive(BloodType mother, BloodType father)
{
var rnd = new Random();
BloodRHFactor rhFactor;
if (mother.RHFactor == BloodRHFactor.Negative && father.RHFactor == BloodRHFactor.Negative)
{
rhFactor = BloodRHFactor.Negative;
}
else
{
rhFactor = Utils.Pick<BloodRHFactor>();
}
BloodGroup group;
if (mother.Group == BloodGroup.O && father.Group == BloodGroup.O) // O and O
{
group = BloodGroup.O;
}
else if (AreGroups(mother, father, BloodGroup.O, BloodGroup.A)) // O and A
{
group = Utils.Pick(BloodGroup.O, BloodGroup.A);
}
else if (AreGroups(mother, father, BloodGroup.O, BloodGroup.B)) // O and B
{
group = Utils.Pick(BloodGroup.O, BloodGroup.B);
}
else if (AreGroups(mother, father, BloodGroup.O, BloodGroup.AB)) // O and AB
{
group = Utils.Pick(BloodGroup.A, BloodGroup.B);
}
else if (mother.Group == BloodGroup.A && father.Group == BloodGroup.A) // A and A
{
group = Utils.Pick(BloodGroup.O, BloodGroup.A);
}
else if (AreGroups(mother, father, BloodGroup.A, BloodGroup.B)) // A and B
{
if (rnd.Next(100) <= 50.0)
{
group = Utils.Pick(BloodGroup.O, BloodGroup.A);
}
else
{
group = (BloodGroup)rnd.Next((int)BloodGroup.B, (int)BloodGroup.AB + 1);
}
}
else if (AreGroups(mother, father, BloodGroup.A, BloodGroup.AB)) // A and AB
{
if (rnd.Next(100) <= 33.0)
{
group = BloodGroup.A;
}
else if (rnd.Next(100) <= 33.0)
{
group = BloodGroup.B;
}
else
{
group = BloodGroup.AB;
}
}
else if (mother.Group == BloodGroup.B && father.Group == BloodGroup.B) // B and B
{
group = Utils.Pick(BloodGroup.O, BloodGroup.B);
}
else if (AreGroups(mother, father, BloodGroup.B, BloodGroup.AB)) // B and AB
{
if (rnd.Next(100) <= 33.0)
{
group = BloodGroup.B;
}
else if (rnd.Next(100) <= 33.0)
{
group = BloodGroup.A;
}
else
{
group = BloodGroup.AB;
}
}
else if (mother.Group == BloodGroup.AB && father.Group == BloodGroup.AB) // AB and AB
{
if (rnd.Next(100) <= 33.0)
{
group = BloodGroup.A;
}
else if (rnd.Next(100) <= 33.0)
{
group = BloodGroup.B;
}
else
{
group = BloodGroup.AB;
}
}
else
{
group = BloodGroup.O;
Debug.Assert(false);
}
return new BloodType(group, rhFactor);
}
}
}