Difference between revisions of "Simulation"

From TRCCompSci - AQA Computer Science
Jump to: navigation, search
(Created page with "==The Code== <syntaxhighlight lang="csharp" line> class Simulation { private Location[,] Landscape; private int TimePeriod = 0; private int WarrenCount = 0;...")
(No difference)

Revision as of 20:58, 29 November 2016

The Code

  1   class Simulation
  2   {
  3     private Location[,] Landscape;
  4     private int TimePeriod = 0;
  5     private int WarrenCount = 0;
  6     private int FoxCount = 0;
  7     private bool ShowDetail = false;
  8     private int LandscapeSize;
  9     private int Variability;
 10     private static Random Rnd = new Random();
 11 
 12     public Simulation(int LandscapeSize, int InitialWarrenCount, int InitialFoxCount, int Variability, bool FixedInitialLocations)
 13     {
 14       int menuOption;
 15       int x;
 16       int y;
 17       string viewRabbits;
 18       this.LandscapeSize = LandscapeSize;
 19       this.Variability = Variability;
 20       Landscape = new Location[LandscapeSize, LandscapeSize];
 21       CreateLandscapeAndAnimals(InitialWarrenCount, InitialFoxCount, FixedInitialLocations);
 22       DrawLandscape();
 23       do
 24       {
 25         Console.WriteLine();
 26         Console.WriteLine("1. Advance to next time period showing detail");
 27         Console.WriteLine("2. Advance to next time period hiding detail");
 28         Console.WriteLine("3. Inspect fox");
 29         Console.WriteLine("4. Inspect warren");
 30         Console.WriteLine("5. Exit");
 31         Console.WriteLine();
 32         Console.Write("Select option: ");
 33         menuOption = Convert.ToInt32(Console.ReadLine());
 34         if (menuOption == 1)
 35         {
 36           TimePeriod++;
 37           ShowDetail = true;
 38           AdvanceTimePeriod();
 39         }
 40         if (menuOption == 2)
 41         {
 42           TimePeriod++;
 43           ShowDetail = false;
 44           AdvanceTimePeriod();
 45         }
 46         if (menuOption == 3)
 47         {
 48           x = InputCoordinate('x');
 49           y = InputCoordinate('y');
 50           if (Landscape[x, y].Fox != null)
 51           {
 52             Landscape[x, y].Fox.Inspect();
 53           }
 54         }
 55         if (menuOption == 4)
 56         {
 57           x = InputCoordinate('x');
 58           y = InputCoordinate('y');
 59           if (Landscape[x, y].Warren != null)
 60           {
 61             Landscape[x, y].Warren.Inspect();
 62             Console.Write("View individual rabbits (y/n)?");
 63             viewRabbits = Console.ReadLine();
 64             if (viewRabbits == "y") {
 65               Landscape[x, y].Warren.ListRabbits();
 66             }
 67           }
 68         }
 69       } while (((WarrenCount > 0) || (FoxCount > 0)) && (menuOption != 5));
 70       Console.ReadKey();
 71     }
 72 
 73     private int InputCoordinate(char Coordinatename)
 74     {
 75       int Coordinate;
 76       Console.Write("  Input " + Coordinatename + " coordinate: ");
 77       Coordinate = Convert.ToInt32(Console.ReadLine());
 78       return Coordinate;
 79     }
 80 
 81     private void AdvanceTimePeriod()
 82     {
 83       int NewFoxCount = 0;
 84       if (ShowDetail)
 85       {
 86         Console.WriteLine();
 87       }
 88       for (int x = 0; x < LandscapeSize; x++)
 89       {
 90         for (int y = 0; y < LandscapeSize; y++)
 91         {
 92           if (Landscape[x, y].Warren != null)
 93           {
 94             if (ShowDetail)
 95             {
 96               Console.WriteLine("Warren at (" + x + "," + y + "):");
 97               Console.Write("  Period Start: ");
 98               Landscape[x, y].Warren.Inspect();
 99             }
100             if (FoxCount > 0)
101             {
102               FoxesEatRabbitsInWarren(x, y);
103             }
104             if (Landscape[x, y].Warren.NeedToCreateNewWarren())
105             {
106               CreateNewWarren();
107             }
108             Landscape[x, y].Warren.AdvanceGeneration(ShowDetail);
109             if (ShowDetail)
110             {
111               Console.Write("  Period End: ");
112               Landscape[x, y].Warren.Inspect();
113               Console.ReadKey();
114             }
115             if (Landscape[x, y].Warren.WarrenHasDiedOut())
116             {
117               Landscape[x, y].Warren = null;
118               WarrenCount--;
119             }
120           }
121         }
122       }
123       for (int x = 0; x < LandscapeSize; x++)
124       {
125         for (int y = 0; y < LandscapeSize; y++)
126         {
127           if (Landscape[x, y].Fox != null)
128           {
129             if (ShowDetail)
130             {
131               Console.WriteLine("Fox at (" + x + "," + y + "): ");
132             }
133             Landscape[x, y].Fox.AdvanceGeneration(ShowDetail);
134             if (Landscape[x, y].Fox.CheckIfDead())
135             {
136               Landscape[x, y].Fox = null;
137               FoxCount--;
138             }
139             else
140             {
141               if (Landscape[x, y].Fox.ReproduceThisPeriod())
142               {
143                 if (ShowDetail) {
144                   Console.WriteLine("  Fox has reproduced. ");
145                 }
146                 NewFoxCount++;
147               }
148               if (ShowDetail) {
149                 Landscape[x, y].Fox.Inspect();
150               }
151               Landscape[x, y].Fox.ResetFoodConsumed();
152             }
153           }
154         }
155       }
156       if (NewFoxCount > 0)
157       {
158         if (ShowDetail)
159         { 
160           Console.WriteLine("New foxes born: ");
161         }
162         for (int f = 0; f < NewFoxCount; f++) {
163           CreateNewFox();
164         }
165       }
166       if (ShowDetail) {
167         Console.ReadKey();
168       }
169       DrawLandscape();
170       Console.WriteLine();
171     }
172 
173     private void CreateLandscapeAndAnimals(int InitialWarrenCount, int InitialFoxCount, bool FixedInitialLocations)
174     {
175       for (int x = 0; x < LandscapeSize; x++)
176       {
177         for (int y = 0; y < LandscapeSize; y++)
178         {
179           Landscape[x, y] = new Location();
180         }
181       }
182       if (FixedInitialLocations)
183       { 
184         Landscape[1, 1].Warren = new Warren(Variability, 38);
185         Landscape[2, 8].Warren = new Warren(Variability, 80);
186         Landscape[9, 7].Warren = new Warren(Variability, 20);
187         Landscape[10, 3].Warren = new Warren(Variability, 52);
188         Landscape[13, 4].Warren = new Warren(Variability, 67);
189         WarrenCount = 5;
190         Landscape[2, 10].Fox = new Fox(Variability);
191         Landscape[6, 1].Fox = new Fox(Variability);
192         Landscape[8, 6].Fox = new Fox(Variability);
193         Landscape[11, 13].Fox = new Fox(Variability);
194         Landscape[12, 4].Fox = new Fox(Variability);
195         FoxCount = 5;
196       }
197       else
198       {
199         for (int w = 0; w < InitialWarrenCount; w++)
200         {
201           CreateNewWarren();
202         }
203         for (int f = 0; f < InitialFoxCount; f++)
204         {
205           CreateNewFox();
206         }
207       }
208     }
209 
210     private void CreateNewWarren()
211     {
212       int x, y;
213       do
214       {
215         x = Rnd.Next(0, LandscapeSize);
216         y = Rnd.Next(0, LandscapeSize);
217       } while (Landscape[x, y].Warren != null);
218       if (ShowDetail)
219       {
220         Console.WriteLine("New Warren at (" + x + "," + y + ")");
221       }
222       Landscape[x, y].Warren = new Warren(Variability);
223       WarrenCount++;
224     }
225 
226     private void CreateNewFox()
227     {
228       int x, y;
229       do
230       {
231         x = Rnd.Next(0, LandscapeSize);
232         y = Rnd.Next(0, LandscapeSize);
233       } while (Landscape[x, y].Fox != null);
234       if (ShowDetail) {
235         Console.WriteLine("  New Fox at (" + x + "," + y + ")");
236       }
237       Landscape[x, y].Fox = new Fox(Variability);
238       FoxCount++;
239     }
240     
241     private void FoxesEatRabbitsInWarren(int WarrenX, int WarrenY)
242     {
243       int FoodConsumed;
244       int PercentToEat;
245       double Dist;
246       int RabbitsToEat;
247       int RabbitCountAtStartOfPeriod = Landscape[WarrenX, WarrenY].Warren.GetRabbitCount();
248       for (int FoxX = 0; FoxX < LandscapeSize; FoxX++)
249       {
250         for (int FoxY = 0; FoxY < LandscapeSize; FoxY++)
251         {
252           if (Landscape[FoxX, FoxY].Fox != null)
253           {
254             Dist = DistanceBetween(FoxX, FoxY, WarrenX, WarrenY);
255             if (Dist <= 3.5)
256             {
257               PercentToEat = 20;
258             }
259             else if (Dist <= 7)
260             {
261               PercentToEat = 10;
262             }
263             else
264             {
265               PercentToEat = 0;
266             }
267             RabbitsToEat = (int)Math.Round((double)(PercentToEat * RabbitCountAtStartOfPeriod / 100.0));
268             FoodConsumed = Landscape[WarrenX, WarrenY].Warren.EatRabbits(RabbitsToEat);
269             Landscape[FoxX, FoxY].Fox.GiveFood(FoodConsumed);
270             if (ShowDetail)
271             {
272               Console.WriteLine("  " + FoodConsumed + " rabbits eaten by fox at (" + FoxX + "," + FoxY + ").");
273             }
274           }
275         }
276       }
277     }
278 
279     private double DistanceBetween(int x1, int y1, int x2, int y2)
280     {
281       return Math.Sqrt(Math.Pow(x1 - x2, 2) + Math.Pow(y1 - y2, 2));
282     }
283 
284     private void DrawLandscape()
285     {
286       Console.WriteLine();
287       Console.WriteLine("TIME PERIOD: " + TimePeriod);
288       Console.WriteLine();
289       Console.Write("    ");
290       for (int x = 0; x < LandscapeSize; x++)
291       {
292         if (x < 10)
293         {
294           Console.Write(" ");
295         }
296         Console.Write(x + " |");
297       }
298       Console.WriteLine();
299       for (int x = 0; x <= LandscapeSize * 4 + 3; x++)
300       {
301         Console.Write("-");
302       }
303       Console.WriteLine();
304       for (int y = 0; y < LandscapeSize; y++)
305       {
306         if (y < 10) {
307           Console.Write(" ");
308         }
309         Console.Write(" " + y + "|");
310         for (int x = 0; x < LandscapeSize; x++)
311         {
312           if (Landscape[x, y].Warren != null)
313           {
314             if (Landscape[x, y].Warren.GetRabbitCount() < 10)
315             {
316               Console.Write(" ");
317             }
318             Console.Write(Landscape[x, y].Warren.GetRabbitCount());
319           }
320           else
321           {
322             Console.Write("  ");
323           }
324           if (Landscape[x, y].Fox != null)
325           {
326             Console.Write("F");
327           }
328           else
329           {
330             Console.Write(" ");
331           }
332           Console.Write("|");
333         }
334         Console.WriteLine();
335       }
336     }
337   }

Explanation