/
Simulation.cs
108 lines (87 loc) · 3.09 KB
/
Simulation.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MorgSimulator
{
/******************************************
* Class: Simulation
* Date: 10/10/2015
* Overview: implements the simulation engine for the morgs to live in.
*
* Parameters: Requires a random number engine for certain operations within the engine
* Requires an integer to set the size of the Petri Dish
*
*
******************************************/
public class Simulation
{
public List<Morg> Morgs;
public bool[,] PetriDish;
//will be the seed that will be used for the whole simulation
public Random Rand;
/*
* Simulation Constructor that creates an instance of the object
* Requires: A random engine and an int for the PetriDishSize
*
**/
public Simulation(Random r, int PetriDishSize)
{
PetriDish = new bool[(PetriDishSize), (PetriDishSize)];
Rand = r;
Morgs = new List<Morg>();
}
/*
* AddaMorg adds a morg to the simulation's petriDish list
*
**/
public void AddAMorg(Morg newMorg)
{
Console.WriteLine("Adding a new Morg to the Simulation of type: " + newMorg);
Morgs.Add(newMorg);
foreach (Morg o in Morgs)
{
o.updateList(Morgs);
}
}
/*
* RemoveAMorg removes a morg to the simulation's petriDish list
*
**/
public void RemoveAMorg(Morg deadMorg)
{
Morgs.Remove(deadMorg);
foreach (Morg o in Morgs)
{
o.updateList(Morgs);
}
}
/*
* RunSimulationOneCycle calls the behaviors for each morg in its simulation
*
**/
public void RunSimulationOneCycle()
{
for (int i = Morgs.Count - 1; i >= 0; i--)
{
Console.WriteLine("-----------------Beginning of Morg movement-------");
//set the old position of the morg to false
PetriDish[Morgs.ElementAt(i).Position.Xpos, Morgs.ElementAt(i).Position.Ypos] = false;
//call movebehavior function to do all the work of the cycle for the morg
bool preyHunted = Morgs.ElementAt(i).moveBehavior();
if (preyHunted == true)
{
//calls simulation function that removes morg from list and tells all morgs of removal
PetriDish[Morgs.ElementAt(i-1).Position.Xpos, Morgs.ElementAt(i-1).Position.Ypos] = true;
RemoveAMorg(Morgs.ElementAt(i-1).Prey);
i--;
}
else
PetriDish[Morgs.ElementAt(i).Position.Xpos, Morgs.ElementAt(i).Position.Ypos] = true;
}
Console.WriteLine("-----------------------------------End of Morg Cycle------------------------- \n");
}
}
}
//END OF SIMULATION CLASS