@@ -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}
0 commit comments