public int LoadSpeelveld(string ecFilename, ref List <Mens> mensen, ref List <Carnivoor> carnivoren, ref List <Herbivoor> herbivoren, ref List <Plant> planten) { int lnAmAdded = 0; Console.Clear(); using (StreamReader loBestand = new StreamReader(ecFilename)) { mensen.Clear(); carnivoren.Clear(); herbivoren.Clear(); planten.Clear(); for (int x = 0; x < this.GrootteX; x++) { for (int y = 0; y < this.GrootteY; y++) { this.Terrarium[x, y] = null; } } String lcLine = loBestand.ReadLine(); while (!String.IsNullOrEmpty(lcLine)) { String[] laCell = lcLine.Split(new[] { ',' }); string lcType = ""; int lnPosX = 0, lnPosY = 0, lnLevenskracht = 0; if (laCell.Length > 0) { lcType = laCell[0]; } else { lcType = ""; } if (laCell.Length > 1 && Int32.TryParse(laCell[1], out lnPosX)) { } else { lnPosX = 0; } if (laCell.Length > 2 && Int32.TryParse(laCell[2], out lnPosY)) { } else { lnPosY = 0; } if (laCell.Length > 3 && Int32.TryParse(laCell[3], out lnLevenskracht)) { } else { lnLevenskracht = 0; } switch (lcType.ToUpper()) { case "M": Mens loMens = new Mens(); loMens.PosX = lnPosX; loMens.PosY = lnPosY; loMens.Levenskracht = lnLevenskracht; mensen.Add(loMens); lnAmAdded++; break; case "C": Carnivoor loCarnivoor = new Carnivoor(); loCarnivoor.PosX = lnPosX; loCarnivoor.PosY = lnPosY; loCarnivoor.Levenskracht = lnLevenskracht; carnivoren.Add(loCarnivoor); lnAmAdded++; break; case "H": Herbivoor loHerbivoor = new Herbivoor(); loHerbivoor.PosX = lnPosX; loHerbivoor.PosY = lnPosY; loHerbivoor.Levenskracht = lnLevenskracht; herbivoren.Add(loHerbivoor); lnAmAdded++; break; case "P": Plant loPlant = new Plant(); loPlant.PosX = lnPosX; loPlant.PosY = lnPosY; loPlant.Levenskracht = lnLevenskracht; planten.Add(loPlant); lnAmAdded++; break; case "SIZE": this.GrootteX = lnPosX; this.GrootteY = lnPosY; this.Terrarium = new IOrganismen[GrootteX, GrootteY]; break; } lcLine = loBestand.ReadLine(); } this.AddPlantenToSpeelveld(planten, true); this.AddCarnivorenToSpeelveld(carnivoren, true); this.AddHerbivorenToSpeelveld(herbivoren, true); this.AddMensenToSpeelveld(mensen, true); } return(lnAmAdded); }
public int DoActionsOf1Day(List <Mens> mensen, List <Carnivoor> carnivoren, List <Herbivoor> herbivoren, List <Plant> planten, int enOffsetX = 0) { vandaagVal++; foreach (Mens m in mensen) { m.Levenskracht--; if (m.Levenskracht <= 0) { m.Delete(); this.Terrarium[m.PosX, m.PosY] = null; } } foreach (Herbivoor h in herbivoren) { h.Levenskracht--; if (h.Levenskracht <= 0) { h.Delete(); this.Terrarium[h.PosX, h.PosY] = null; } } foreach (Carnivoor c in carnivoren) { c.Levenskracht--; if (c.Levenskracht <= 0) { c.Delete(); this.Terrarium[c.PosX, c.PosY] = null; } } foreach (Plant p in planten) { p.Levenskracht--; if (p.Levenskracht <= 0) { p.Delete(); this.Terrarium[p.PosX, p.PosY] = null; } } int lnOffset = 0; for (int i = 0; i <= this.CountAmounOfCarnivorenInSpeelveld(); i++) { } this.ToonSpeelveld(enOffsetX * lnOffset, 0, "Start"); lnOffset++; List <Herbivoor> nieuweherbivoren = new List <Herbivoor>(); ///////////////////////////////////////////////// // carnivoren business ///////////////////////////////////////////////// foreach (Carnivoor C in carnivoren) { if (!C.IsDeleted) { Dier dier = null; if ((C.PosY + 1 < this.GrootteY) && (this.Terrarium[C.PosX, C.PosY + 1] != null) && (this.Terrarium[C.PosX, C.PosY + 1].GetType() == typeof(Dier))) { dier = (Dier)this.Terrarium[C.PosX, C.PosY + 1]; } C.Eet(this, dier); C.Vecht(this, dier); C.Beweeg(this); this.ToonSpeelveld(enOffsetX * lnOffset, 0, "Carni"); lnOffset++; } } ///////////////////////////////////////////////// // herbivoren business ///////////////////////////////////////////////// foreach (Herbivoor H in herbivoren) { if (!H.IsDeleted) { Dier dier = null; if ((H.PosY + 1 < this.GrootteY) && (this.Terrarium[H.PosX, H.PosY + 1] != null) && (this.Terrarium[H.PosX, H.PosY + 1].GetType() == typeof(Dier))) { dier = (Dier)this.Terrarium[H.PosX, H.PosY + 1]; } H.Eet(this, dier); H.Beweeg(this); Herbivoor nieuweherbivoor = H.PlantVoort(this, herbivoren); if (nieuweherbivoor != null) { nieuweherbivoren.Add(nieuweherbivoor); } this.ToonSpeelveld(enOffsetX * lnOffset, 0, "Herbi"); lnOffset++; } } this.AddHerbivorenToSpeelveld(nieuweherbivoren); if (nieuweherbivoren != null) { foreach (Herbivoor Dier in nieuweherbivoren) { herbivoren.Add(Dier); } } this.ToonSpeelveld(enOffsetX * lnOffset, 0, "Paren"); lnOffset++; ///////////////////////////////////////////////// // mensen business ///////////////////////////////////////////////// foreach (Mens M in mensen) { if (!M.IsDeleted) { Dier dier = null; if ((M.PosY + 1 < this.GrootteY) && (this.Terrarium[M.PosX, M.PosY + 1] != null) && (this.Terrarium[M.PosX, M.PosY + 1].GetType() == typeof(Dier))) { dier = (Dier)this.Terrarium[M.PosX, M.PosY + 1]; } M.Vecht(this, dier); M.Eet(this, dier); M.Beweeg(this); this.ToonSpeelveld(enOffsetX * lnOffset, 0, "Mens"); lnOffset++; } } ///////////////////////////////////////////////// // planten business ///////////////////////////////////////////////// List <Plant> nieuweplanten = Plant.CreatePlanten((this.GrootteX * this.GrootteY) / (this.GrootteX + this.GrootteY)); this.AddPlantenToSpeelveld(nieuweplanten); foreach (var plant in nieuweplanten) { planten.Add(plant); } this.ToonSpeelveld(enOffsetX * lnOffset, 0, "Plant"); lnOffset++; //Console.ReadLine(); //Thread.Sleep(500); //Console.ReadLine(); ///////////////////////////////////////////////// // input business ///////////////////////////////////////////////// System.IO.Directory.CreateDirectory("c:\\Temp\\"); string lcFilename = "c:\\Temp\\TerrariumSettings.txt"; int lnReturn = 0; Console.WriteLine("[Enter] Volgende dag, (B)ewaar, (L)ees, (V)ulkaan, (A)ardbeving, (S)top"); string lcInput = Console.ReadLine().ToLower(); switch (lcInput) { case "b": this.SaveSpeelveld(lcFilename, mensen, carnivoren, herbivoren, planten); break; case "l": this.LoadSpeelveld(lcFilename, ref mensen, ref carnivoren, ref herbivoren, ref planten); break; case "v": this.Volcano(ref mensen, ref carnivoren, ref herbivoren, ref planten, this); this.ToonSpeelveld(enOffsetX * lnOffset, 0, "Vulcano"); break; case "a": this.Earthquake(ref mensen, ref carnivoren, ref herbivoren, ref planten, this); this.ToonSpeelveld(enOffsetX * lnOffset, 0, "Earthquake"); break; case "s": lnReturn = -1; break; } return(lnReturn); }