AS 2017 CSharp Skeleton Code

From TRCCompSci - AQA Computer Science
Revision as of 08:45, 1 March 2017 by Admin (talk | contribs) (Created page with "<syntaxhighlight lang=csharp line> // Skeleton Program for the AQA A1 Summer 2017 examination // this code should be used in conjunction with the Preliminary Material // writt...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
  1 // Skeleton Program for the AQA A1 Summer 2017 examination
  2 // this code should be used in conjunction with the Preliminary Material
  3 // written by the AQA AS1 Programmer Team
  4 // developed in Visual Studio 2015
  5 
  6 // Stage : Pre
  7 // Version Number : 1.1
  8 // Author : C Brown
  9 // Edited by : S Langfield
 10 // Edited on : 11 December 2016
 11 
 12 using System;
 13 using System.IO;
 14 
 15 namespace SeedSimulation
 16 {
 17   class Program
 18   {
 19     const char SOIL = '.';
 20     const char SEED = 'S';
 21     const char PLANT = 'P';
 22     const char ROCKS = 'X';
 23     const int FIELDLENGTH = 20;
 24     const int FIELDWIDTH = 35;
 25 
 26     static int GetHowLongToRun()
 27     {
 28       int Years = 0;
 29       Console.WriteLine("Welcome to the Plant Growing Simulation");
 30       Console.WriteLine();
 31       Console.WriteLine("You can step through the simulation a year at a time");
 32       Console.WriteLine("or run the simulation for 0 to 5 years");
 33       Console.WriteLine("How many years do you want the simulation to run?");
 34       Console.Write("Enter a number between 0 and 5, or -1 for stepping mode: ");
 35       Years = Convert.ToInt32(Console.ReadLine());
 36       return Years;
 37     }
 38 
 39     static void CreateNewField(char[,] Field)
 40     {
 41       int Row = 0;
 42       int Column = 0;
 43       for (Row = 0; Row < FIELDLENGTH; Row++)
 44       {
 45         for (Column = 0; Column < FIELDWIDTH; Column++)
 46         {
 47           Field[Row, Column] = SOIL;
 48         }
 49       }
 50       Row = FIELDLENGTH / 2;
 51       Column = FIELDWIDTH / 2;
 52       Field[Row, Column] = SEED;
 53     }
 54 
 55     static void ReadFile(char[,] Field)
 56     {
 57       string FileName = "";
 58       string FieldRow = "";
 59       Console.Write("Enter file name: ");
 60       FileName = Console.ReadLine();
 61       try
 62       {
 63         StreamReader CurrentFile = new StreamReader(FileName);
 64         for (int Row = 0; Row < FIELDLENGTH; Row++)
 65         {
 66           FieldRow = CurrentFile.ReadLine();
 67           for (int Column = 0; Column < FIELDWIDTH; Column++)
 68           {
 69             Field[Row, Column] = FieldRow[Column];
 70           }
 71         }
 72         CurrentFile.Close();
 73       }
 74       catch (Exception)
 75       {
 76         CreateNewField(Field);
 77       }
 78     }
 79 
 80     static void InitialiseField(char[,] Field)
 81     {
 82       string Response = "";
 83       Console.Write("Do you want to load a file with seed positions? (Y/N): ");
 84       Response = Console.ReadLine();
 85       if (Response == "Y")
 86       {
 87         ReadFile(Field);
 88       }
 89       else
 90       {
 91         CreateNewField(Field);
 92       }
 93     }
 94 
 95     static void Display(char[,] Field, string Season, int Year)
 96     {
 97       Console.WriteLine("Season: " + Season + " Year number: " + Year);
 98       for (int Row = 0; Row < FIELDLENGTH; Row++)
 99       {
100         for (int Column = 0; Column < FIELDWIDTH; Column++)
101         {
102           Console.Write(Field[Row, Column]);
103         }
104         Console.WriteLine("| " + String.Format("{0,3}", Row));
105       }
106     }
107 
108     static void CountPlants(char[,] Field)
109     {
110       int NumberOfPlants = 0;
111       for (int Row = 0; Row < FIELDLENGTH; Row++)
112       {
113         for (int Column = 0; Column < FIELDWIDTH; Column++)
114         {
115           if (Field[Row, Column] == PLANT)
116           {
117             NumberOfPlants++;
118           }
119         }
120       }
121       if (NumberOfPlants == 1)
122       {
123         Console.WriteLine("There is 1 plant growing");
124       }
125       else
126       {
127         Console.WriteLine("There are " + NumberOfPlants + " plants growing");
128       }
129     }
130 
131     static void SimulateSpring(char[,] Field)
132     {
133       int PlantCount = 0;
134       bool Frost = false;
135       for (int Row = 0; Row < FIELDLENGTH; Row++)
136       {
137         for (int Column = 0; Column < FIELDWIDTH; Column++)
138         {
139           if (Field[Row, Column] == SEED)
140           {
141             Field[Row, Column] = PLANT;
142           }
143         }
144       }
145       CountPlants(Field);
146       Random RandomInt = new Random();
147       if (RandomInt.Next(0, 2) == 1)
148       {
149         Frost = true;
150       }
151       else
152       {
153         Frost = false;
154       }
155       if (Frost)
156       {
157         PlantCount = 0;
158         for (int Row = 0; Row < FIELDLENGTH; Row++)
159         {
160           for (int Column = 0; Column < FIELDWIDTH; Column++)
161           {
162             if (Field[Row, Column] == PLANT)
163             {
164               PlantCount++;
165               if (PlantCount % 3 == 0)
166               {
167                 Field[Row, Column] = SOIL;
168               }
169             }
170           }
171         }
172         Console.WriteLine("There has been a frost");
173         CountPlants(Field);
174       }
175     }
176 
177     static void SimulateSummer(char[,] Field)
178     {
179       Random RandomInt = new Random();
180       int RainFall = RandomInt.Next(0, 3);
181       int PlantCount = 0;
182       if (RainFall == 0)
183       {
184         PlantCount = 0;
185         for (int Row = 0; Row < FIELDLENGTH; Row++)
186         {
187           for (int Column = 0; Column < FIELDWIDTH; Column++)
188           {
189             if (Field[Row, Column] == PLANT)
190             {
191               PlantCount++;
192               if (PlantCount % 2 == 0)
193               {
194                 Field[Row, Column] = SOIL;
195               }
196             }
197           }
198         }
199         Console.WriteLine("There has been a severe drought");
200         CountPlants(Field);
201       }
202     }
203 
204     static void SeedLands(char[,] Field, int Row, int Column)
205     {
206       if (Row >= 0 && Row < FIELDLENGTH && Column >= 0 && Column < FIELDWIDTH)
207       {
208         if (Field[Row, Column] == SOIL)
209         {
210           Field[Row, Column] = SEED;
211         }
212       }
213     }
214 
215     static void SimulateAutumn(char[,] Field)
216     {
217       for (int Row = 0; Row < FIELDLENGTH; Row++)
218       {
219         for (int Column = 0; Column < FIELDWIDTH; Column++)
220         {
221           if (Field[Row, Column] == PLANT)
222           {
223             SeedLands(Field, Row - 1, Column - 1);
224             SeedLands(Field, Row - 1, Column);
225             SeedLands(Field, Row - 1, Column + 1);
226             SeedLands(Field, Row, Column - 1);
227             SeedLands(Field, Row, Column + 1);
228             SeedLands(Field, Row + 1, Column - 1);
229             SeedLands(Field, Row + 1, Column);
230             SeedLands(Field, Row + 1, Column + 1);
231           }
232         }
233       }
234     }
235 
236     static void SimulateWinter(char[,] Field)
237     {
238       for (int Row = 0; Row < FIELDLENGTH; Row++)
239       {
240         for (int Column = 0; Column < FIELDWIDTH; Column++)
241         {
242           if (Field[Row, Column] == PLANT)
243           {
244             Field[Row, Column] = SOIL;
245           }
246         }
247       }
248     }
249 
250     static void SimulateOneYear(char[,] Field, int Year)
251     {
252       SimulateSpring(Field);
253       Display(Field, "spring", Year);
254       SimulateSummer(Field);
255       Display(Field, "summer", Year);
256       SimulateAutumn(Field);
257       Display(Field, "autumn", Year);
258       SimulateWinter(Field);
259       Display(Field, "winter", Year);
260     }
261 
262     private static void Simulation()
263     {
264       int YearsToRun;
265       char[,] Field = new char[FIELDLENGTH, FIELDWIDTH];
266       bool Continuing;
267       int Year;
268       string Response;
269       YearsToRun = GetHowLongToRun();
270       if (YearsToRun != 0)
271       {
272         InitialiseField(Field);
273         if (YearsToRun >= 1)
274         {
275           for (Year = 1; Year <= YearsToRun; Year++)
276           {
277             SimulateOneYear(Field, Year);
278           }
279         }
280         else
281         {
282           Continuing = true;
283           Year = 0;
284           while (Continuing)
285           {
286             Year++;
287             SimulateOneYear(Field, Year);
288             Console.Write("Press Enter to run simulation for another Year, Input X to stop: ");
289             Response = Console.ReadLine();
290             if (Response == "x" || Response == "X")
291             {
292               Continuing = false;
293             }
294           }
295         }
296         Console.WriteLine("End of Simulation");
297       }
298       Console.ReadLine();
299     }
300 
301     static void Main(string[] args)
302     {
303       Simulation();
304     }
305 
306   }
307 }