10
10
1 : lambda neighbors , energy : (sum (neighbors ) - energy ) % 2
11
11
}
12
12
13
+
13
14
def initialize_grid ():
14
15
grid = [0 ] * GRID_SIZE
15
16
for _ in range (NUM_ORGANISMS ):
16
17
organism_position = random .randint (0 , GRID_SIZE - 1 )
17
- grid [organism_position ] = random .randint (1 , 5 )
18
+ grid [organism_position ] = random .randint (1 , 5 )
18
19
for _ in range (NUM_RESOURCES ):
19
20
resource_position = random .randint (0 , GRID_SIZE - 1 )
20
21
resource_value = random .randint (1 , 5 )
21
- grid [resource_position ] = - resource_value
22
+ grid [resource_position ] = - resource_value
22
23
return grid
23
24
25
+
24
26
def get_neighbors (grid , index ):
25
27
neighbors = []
26
28
if index > 0 :
@@ -29,6 +31,7 @@ def get_neighbors(grid, index):
29
31
neighbors .append (grid [index + 1 ])
30
32
return neighbors
31
33
34
+
32
35
def apply_rule (grid , index ):
33
36
cell_state = grid [index ]
34
37
neighbors = get_neighbors (grid , index )
@@ -38,11 +41,13 @@ def apply_rule(grid, index):
38
41
if energy >= ENERGY_THRESHOLD and grid .count (0 ) > 1 :
39
42
empty_spots = [i for i in range (GRID_SIZE ) if grid [i ] == 0 ]
40
43
new_organism_position = random .choice (empty_spots )
41
- grid [new_organism_position ] = energy // 2 # New organism created through reproduction.
44
+ # New organism created through reproduction.
45
+ grid [new_organism_position ] = energy // 2
42
46
grid [index ] = new_state
43
47
elif cell_state < 0 : # If the cell represents a resource.
44
48
grid [index ] = 0
45
49
50
+
46
51
def run_simulation (grid , num_iterations ):
47
52
for _ in range (num_iterations ):
48
53
new_grid = grid .copy ()
@@ -51,6 +56,7 @@ def run_simulation(grid, num_iterations):
51
56
grid = new_grid
52
57
return grid
53
58
59
+
54
60
def display_grid (grid ):
55
61
for cell in grid :
56
62
if cell == 0 :
@@ -61,6 +67,7 @@ def display_grid(grid):
61
67
print (abs (cell ), end = ' ' )
62
68
print ()
63
69
70
+
64
71
def main ():
65
72
grid = initialize_grid ()
66
73
display_grid (grid )
@@ -71,5 +78,6 @@ def main():
71
78
print ("\n Simulation Results:" )
72
79
display_grid (grid )
73
80
81
+
74
82
if __name__ == "__main__" :
75
83
main ()
0 commit comments