Skip to content

Commit 4f68225

Browse files
committed
Added UnitTests for Cell and CellOverseer and implemented Methods, Added Actions to ActionEnum
1 parent 3e4ec4b commit 4f68225

5 files changed

Lines changed: 81 additions & 4 deletions

File tree

Enums/ActionEnum.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,9 @@ namespace CellSimulator.Enums
44
public enum ActionEnum
55
{
66
Born,
7+
SuccessSplit,
8+
FailSplit,
9+
SuccessEat,
10+
FailEat,
711
}
812
}

Interfaces/ICell.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@ public interface ICell
1111
int Energy { get; set; }
1212
int MaxEnergy { get; set; }
1313
ActionEnum LastAction { get; set; }
14+
bool IsAlive { get; set; }
1415

1516
bool TrySplit();
1617
bool TryEat();
18+
void PerformAction();
1719
}
1820
}

Interfaces/ICellOverseer.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@ namespace CellSimulator.Interfaces
88
{
99
public interface ICellOverseer
1010
{
11-
List<ICell> CellList { get; set; }
11+
List<ICell> CellList { get; }
1212

1313
void AddCell();
14+
void AddCell(ICell cell);
1415
void SimulateNext();
1516
}
1617
}

Models/Cell.cs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,20 @@ public class Cell : ICell
1717
public int Energy { get; set; }
1818
public int MaxEnergy { get; set; }
1919
public ActionEnum LastAction { get; set; }
20+
public bool IsAlive { get; set; } = true;
2021

2122
private const double PROPABILITY_EAT_FOOD = 0.25;
2223
private const int INCREMENTOR_EAT_FOOD = 40;
2324

2425
private const double PROPABILITY_SPLIT = 0.25;
2526

27+
private const int FOOD_REQUIRED_FOR_SUCCESSFUL_SPLIT = 50;
28+
private const int ENERGY_REQUIRED_FOR_SPLIT = 20;
29+
private const int FOOD_REQUIRED_FOR_TRY_SPLIT = 10;
30+
31+
private const int FOOD_CONSUMED_PER_ACTION = 5;
32+
private const int ENERGY_CONSUMED_PER_ACTION = 5;
33+
2634
private Random rand;
2735

2836
public Cell(int id = 0, Random rand = null)
@@ -53,5 +61,46 @@ public bool TrySplit()
5361
{
5462
return rand.NextDouble() <= PROPABILITY_SPLIT;
5563
}
64+
65+
public void PerformAction()
66+
{
67+
if(Food >= FOOD_REQUIRED_FOR_SUCCESSFUL_SPLIT + FOOD_REQUIRED_FOR_TRY_SPLIT)
68+
{
69+
if (TrySplit())
70+
{
71+
LastAction = ActionEnum.SuccessSplit;
72+
Food -= FOOD_REQUIRED_FOR_SUCCESSFUL_SPLIT;
73+
Energy -= ENERGY_REQUIRED_FOR_SPLIT;
74+
}
75+
else
76+
{
77+
LastAction = ActionEnum.FailSplit;
78+
}
79+
Energy -= FOOD_REQUIRED_FOR_TRY_SPLIT;
80+
}
81+
else
82+
{
83+
if (TryEat())
84+
{
85+
LastAction = ActionEnum.SuccessEat;
86+
}
87+
else
88+
{
89+
LastAction = ActionEnum.FailEat;
90+
}
91+
}
92+
Energy -= FOOD_CONSUMED_PER_ACTION;
93+
Food -= ENERGY_CONSUMED_PER_ACTION;
94+
CheckIfDead();
95+
++Age;
96+
}
97+
98+
private void CheckIfDead()
99+
{
100+
if(Energy <= 0 || Food <= 0)
101+
{
102+
IsAlive = false;
103+
}
104+
}
56105
}
57106
}

Models/CellOverseer.cs

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,37 @@ namespace CellSimulator.Models
99
{
1010
public class CellOverseer : ICellOverseer
1111
{
12-
public List<ICell> CellList { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
12+
public List<ICell> CellList { get; private set; } = new List<ICell>();
13+
private int internalIdCounter = 0;
14+
15+
private Random rand = new Random();
1316

1417
public void AddCell()
1518
{
16-
throw new NotImplementedException();
19+
AddCell(new Cell(internalIdCounter, rand));
20+
++internalIdCounter;
21+
}
22+
23+
public void AddCell(ICell cell)
24+
{
25+
CellList.Add(cell);
1726
}
1827

1928
public void SimulateNext()
2029
{
21-
throw new NotImplementedException();
30+
for (int i = 0; i < CellList.Count; i++)
31+
{
32+
ICell cell = CellList[i];
33+
cell.PerformAction();
34+
if (!cell.IsAlive)
35+
{
36+
CellList.Remove(cell);
37+
}
38+
else if(cell.LastAction == Enums.ActionEnum.SuccessSplit)
39+
{
40+
AddCell();
41+
}
42+
}
2243
}
2344
}
2445
}

0 commit comments

Comments
 (0)