Warren

From TRCCompSci - AQA Computer Science
Jump to: navigation, search

The Code

  1   class Warren
  2   {
  3     private const int MaxRabbitsInWarren = 99;
  4     private Rabbit[] Rabbits;
  5     private int RabbitCount = 0;
  6     private int PeriodsRun = 0;
  7     private bool AlreadySpread = false;
  8     private int Variability;
  9     private static Random Rnd = new Random();
 10 
 11     public Warren(int Variability)
 12     {
 13       this.Variability = Variability;
 14       Rabbits = new Rabbit[MaxRabbitsInWarren];
 15       RabbitCount = (int)(CalculateRandomValue((int)(MaxRabbitsInWarren / 4), this.Variability));
 16       for (int r = 0; r < RabbitCount; r++)
 17       {
 18         Rabbits[r] = new Rabbit(Variability);
 19       }
 20     }
 21 
 22     public Warren(int Variability, int rabbitCount)
 23     {
 24       this.Variability = Variability;
 25       this.RabbitCount = rabbitCount;
 26       Rabbits = new Rabbit[MaxRabbitsInWarren];
 27       for (int r = 0; r < RabbitCount; r++)
 28       {
 29         Rabbits[r] = new Rabbit(Variability);
 30       }
 31     }
 32 
 33     private double CalculateRandomValue(int BaseValue, int Variability)
 34     {
 35       return BaseValue - (BaseValue * Variability / 100) + (BaseValue * Rnd.Next(0, (Variability * 2) + 1) / 100);
 36     }
 37 
 38     public int GetRabbitCount()
 39     {
 40       return RabbitCount;
 41     }
 42 
 43     public bool NeedToCreateNewWarren()
 44     {
 45       if ((RabbitCount == MaxRabbitsInWarren) && (!AlreadySpread))
 46       {
 47         AlreadySpread = true;
 48         return true;
 49       }
 50       else
 51       {
 52         return false;
 53       }
 54     }
 55 
 56     public bool WarrenHasDiedOut()
 57     {
 58       if (RabbitCount == 0)
 59       {
 60         return true;
 61       }
 62       else
 63       {
 64         return false;
 65       }
 66     }
 67 
 68     public void AdvanceGeneration(bool ShowDetail)
 69     {
 70       PeriodsRun++;
 71       if (RabbitCount > 0)
 72       {
 73         KillByOtherFactors(ShowDetail);
 74       }
 75       if (RabbitCount > 0)
 76       {
 77         AgeRabbits(ShowDetail);
 78       }
 79       if ((RabbitCount > 0) && (RabbitCount <= MaxRabbitsInWarren))
 80       {
 81         if (ContainsMales())
 82         {
 83           MateRabbits(ShowDetail);
 84         }
 85       }
 86       if ((RabbitCount == 0) && (ShowDetail))
 87       {
 88         Console.WriteLine("  All rabbits in warren are dead");
 89       }
 90     }
 91 
 92     public int EatRabbits(int RabbitsToEat)
 93     {
 94       int DeathCount = 0;
 95       int RabbitNumber;
 96       if (RabbitsToEat > RabbitCount)
 97       {
 98         RabbitsToEat = RabbitCount;
 99       }
100       while (DeathCount < RabbitsToEat)
101       {
102         RabbitNumber = Rnd.Next(0, RabbitCount);
103         if (Rabbits[RabbitNumber] != null)
104         {
105           Rabbits[RabbitNumber] = null;
106           DeathCount++;
107         }
108       }
109       CompressRabbitList(DeathCount);
110       return RabbitsToEat;
111     }
112 
113     private void KillByOtherFactors(bool ShowDetail)
114     {
115       int DeathCount = 0;
116       for (int r = 0; r < RabbitCount; r++)
117       {
118         if (Rabbits[r].CheckIfKilledByOtherFactor())
119         {
120           Rabbits[r] = null;
121           DeathCount++;
122         }
123       }
124       CompressRabbitList(DeathCount);
125       if (ShowDetail)
126       {
127         Console.WriteLine("  " + DeathCount + " rabbits killed by other factors.");
128       }
129     }
130 
131     private void AgeRabbits(bool ShowDetail)
132     {
133       int DeathCount = 0;
134       for (int r = 0; r < RabbitCount; r++)
135       {
136         Rabbits[r].CalculateNewAge();
137         if (Rabbits[r].CheckIfDead())
138         {
139           Rabbits[r] = null;
140           DeathCount++;
141         }
142       }
143       CompressRabbitList(DeathCount);
144       if (ShowDetail)
145       {
146         Console.WriteLine("  " + DeathCount + " rabbits die of old age.");
147       }
148     }
149 
150     private void MateRabbits(bool ShowDetail)
151     {
152       int Mate = 0;
153       int Babies = 0;
154       double CombinedReproductionRate;
155       for (int r = 0; r < RabbitCount; r++)
156       {
157         if ((Rabbits[r].IsFemale()) && (RabbitCount + Babies < MaxRabbitsInWarren))
158         {
159           do
160           {
161             Mate = Rnd.Next(0, RabbitCount);
162           } while ((Mate == r) || (Rabbits[Mate].IsFemale()));
163           CombinedReproductionRate = (Rabbits[r].GetReproductionRate() + Rabbits[Mate].GetReproductionRate()) / 2;
164           if (CombinedReproductionRate >= 1)
165           {
166             Rabbits[RabbitCount + Babies] = new Rabbit(Variability, CombinedReproductionRate);
167             Babies++;
168           }
169         }
170       }
171       RabbitCount = RabbitCount + Babies;
172       if (ShowDetail)
173       {
174         Console.WriteLine("  " + Babies + " baby rabbits born.");
175       }
176     }
177 
178     private void CompressRabbitList(int DeathCount)
179     {
180       if (DeathCount > 0)
181       {
182         int ShiftTo = 0;
183         int ShiftFrom = 0;
184         while (ShiftTo < RabbitCount - DeathCount)
185         {
186           while (Rabbits[ShiftFrom] == null)
187           {
188             ShiftFrom++;
189           }
190           if (ShiftTo != ShiftFrom)
191           {
192             Rabbits[ShiftTo] = Rabbits[ShiftFrom];
193           }
194           ShiftTo++;
195           ShiftFrom++;
196         }
197         RabbitCount = RabbitCount - DeathCount;
198       }
199     }
200 
201     private bool ContainsMales()
202     {
203       bool Males = false;
204       for (int r = 0; r < RabbitCount; r++)
205       {
206         if (!Rabbits[r].IsFemale())
207         {
208           Males = true;
209         }
210       }
211       return Males;
212     }
213 
214     public void Inspect()
215     {
216       Console.WriteLine("Periods Run " + PeriodsRun + " Size " + RabbitCount);
217     }
218 
219     public void ListRabbits()
220     {
221       if (RabbitCount > 0)
222       {
223         for (int r = 0; r < RabbitCount; r++)
224         {
225           Rabbits[r].Inspect();
226         }
227       }
228     }
229   }

Explanation