Skip to content

Commit 60f8726

Browse files
committed
Further Enhanced Overseer Class, Added Safe and Load Function
Cells now get 40 food after successfully eating Overseer now contains Random and passes it to the cell Save and Load Function added, so that the Cells can be saved and Loaded back later on
1 parent e2bbcc1 commit 60f8726

4 files changed

Lines changed: 118 additions & 11 deletions

File tree

Cell.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ namespace CellSimulator
88
{
99
public class Cell
1010
{
11-
Random rand = new Random();
12-
1311
public int id;
1412
public int age = 0;
1513

@@ -18,13 +16,15 @@ public class Cell
1816

1917
public int energy = 100;
2018
public int maxEnergy = 100;
19+
20+
public string lastAction = "Born";
2121

2222
public Cell(int cellId)
2323
{
2424
id = cellId;
2525
}
2626

27-
public void Eat()
27+
public bool Eat(Random rand)
2828
{
2929
bool didEat = false;
3030
if(rand.Next(0, 101) > 75)
@@ -33,14 +33,15 @@ public void Eat()
3333
}
3434
if (didEat)
3535
{
36-
food += 10;
36+
food += 40;
3737
}
3838
if(food > maxFood)
3939
{
4040
food = maxFood;
4141
}
42+
return didEat;
4243
}
43-
public bool Split()
44+
public bool Split(Random rand)
4445
{
4546
bool didSplit = false;
4647
if(rand.Next(0, 101) > 75)

CellOverseer.cs

Lines changed: 77 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,41 +3,112 @@
33
using System.Linq;
44
using System.Text;
55
using System.Threading.Tasks;
6+
using System.IO;
67

78
namespace CellSimulator
89
{
910
public class CellOverseer
1011
{
12+
Random rand = new Random();
1113
public List<Cell> cells = new List<Cell>();
1214
public int cellIdCounter = 0;
1315

1416
public CellOverseer(int startingCells)
1517
{
1618
for(int i = 0; i < startingCells; ++i)
1719
{
18-
cells.Add(new Cell(cellIdCounter));
19-
++cellIdCounter;
20+
AddNewCell(cells);
2021
}
2122
}
23+
public void AddNewCell(List<Cell> cellList)
24+
{
25+
cellList.Add(new Cell(cellIdCounter));
26+
++cellIdCounter;
27+
}
2228
public void SimulateNext()
2329
{
2430
List<Cell> stillAliveCells = new List<Cell>();
2531
foreach(Cell cell in cells)
2632
{
27-
if(cell.energy <= 0)
33+
if(cell.energy <= 0 || cell.food <= 0)
2834
{
2935
continue;
3036
}
31-
if(cell.food > 80)
37+
if(cell.food >= 60)
3238
{
33-
39+
if (cell.Split(rand))
40+
{
41+
AddNewCell(stillAliveCells);
42+
cell.food -= 50;
43+
cell.energy -= 20;
44+
cell.lastAction = "SuccessSplit";
45+
}
46+
else
47+
{
48+
cell.lastAction = "FailSplit";
49+
}
50+
cell.energy -= 10;
3451
}
3552
else
3653
{
37-
cell.Eat();
54+
if (cell.Eat(rand))
55+
{
56+
cell.lastAction = "SuccessEat";
57+
}
58+
else
59+
{
60+
cell.lastAction = "FailEat";
61+
}
62+
}
63+
cell.energy -= 5;
64+
cell.food -= 5;
65+
if (cell.energy <= 0 || cell.food <= 0)
66+
{
67+
continue;
3868
}
69+
cell.age += 1;
3970
stillAliveCells.Add(cell);
4071
}
72+
cells = stillAliveCells;
73+
}
74+
public void SaveToFile()
75+
{
76+
List<string> linesToAppend = new List<string>();
77+
foreach(Cell cell in cells)
78+
{
79+
string lineToAppend = cell.id + "|" + cell.age + "|" + cell.food + "|" + cell.maxFood + "|" + cell.energy + "|" + cell.maxEnergy + "|" + cell.lastAction;
80+
linesToAppend.Add(lineToAppend);
81+
}
82+
string nameAppender = DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss");
83+
File.AppendAllLines("CellList-" + nameAppender + ".txt", linesToAppend);
84+
}
85+
public bool LoadFromFile(string fileName)
86+
{
87+
if(!File.Exists(fileName))
88+
{
89+
return false;
90+
}
91+
92+
List<Cell> listOfCells = new List<Cell>();
93+
string[] dataLines = File.ReadAllLines(fileName);
94+
foreach(string dataLine in dataLines)
95+
{
96+
string[] splitDataLine = dataLine.Split('|');
97+
Cell tempCell = new Cell(cellIdCounter)
98+
{
99+
id = Convert.ToInt32(splitDataLine[0]),
100+
age = Convert.ToInt32(splitDataLine[1]),
101+
food = Convert.ToInt32(splitDataLine[2]),
102+
maxFood = Convert.ToInt32(splitDataLine[3]),
103+
energy = Convert.ToInt32(splitDataLine[4]),
104+
maxEnergy = Convert.ToInt32(splitDataLine[5]),
105+
lastAction = splitDataLine[6]
106+
};
107+
listOfCells.Add(tempCell);
108+
}
109+
cells = listOfCells;
110+
111+
return true;
41112
}
42113
}
43114
}

CellSimulator.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@
4242
<Reference Include="System.Xml" />
4343
</ItemGroup>
4444
<ItemGroup>
45+
<Compile Include="Cell.cs" />
46+
<Compile Include="CellOverseer.cs" />
4547
<Compile Include="Program.cs" />
4648
<Compile Include="Properties\AssemblyInfo.cs" />
4749
</ItemGroup>

Program.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,39 @@ class Program
1010
{
1111
static void Main(string[] args)
1212
{
13+
CellOverseer overseer = new CellOverseer(1);
14+
while (true)
15+
{
16+
foreach(Cell cell in overseer.cells)
17+
{
18+
Console.WriteLine("ID:" + cell.id + "|AGE:" + cell.age + "|FOOD:" + cell.food + "|ENERGY:" + cell.energy + "|ACTION:" + cell.lastAction);
19+
}
20+
Console.WriteLine("ALIVE:" + overseer.cells.Count());
21+
string keyPressed = Console.ReadLine();
22+
if(keyPressed != "")
23+
{
24+
if(keyPressed == "save")
25+
{
26+
overseer.SaveToFile();
27+
continue;
28+
}
29+
else if(keyPressed == "load")
30+
{
31+
string fileName = Console.ReadLine();
32+
if(fileName == "")
33+
{
34+
continue;
35+
}
36+
else
37+
{
38+
overseer.LoadFromFile(fileName);
39+
}
40+
}
41+
}
42+
43+
overseer.SimulateNext();
44+
Console.WriteLine();
45+
}
1346
}
1447
}
1548
}

0 commit comments

Comments
 (0)