Skip to content

Commit 6acae1a

Browse files
Merge pull request #2487 from andoriyaprashant/branch17
Cellular Automation Wars Script Added
2 parents 74ae651 + bd01867 commit 6acae1a

File tree

3 files changed

+119
-0
lines changed

3 files changed

+119
-0
lines changed

Cellular Automaton Wars/README.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Cellular Automaton Wars
2+
3+
Cellular Automaton Wars is a simulation game where players design and deploy cellular automaton-based organisms to compete for resources in a virtual environment. Each organism's behavior is determined by its cellular automaton rules, and the player must strategize to create the most successful organisms and outcompete others.
4+
5+
## Features
6+
7+
- Organisms interact with nearby cells and consume resources.
8+
- Organisms have energy levels and can reproduce when reaching a certain threshold.
9+
- Organisms can move left or right based on certain conditions or rules.
10+
- The player can influence the game by adding/removing organisms and resources at specific locations.
11+
12+
## How to Use
13+
14+
1. Clone the repository or download the script file.
15+
16+
2. Open a terminal or command prompt and navigate to the directory containing the script.
17+
18+
3. Run the script using Python:
19+
20+
```bash
21+
python cellular_automaton_wars.py
22+
```
23+
24+
4. The script will initialize the grid with organisms and resources and simulate their behavior for a certain number of iterations. The output will display the state of the grid at each iteration.
25+
26+
## Customization
27+
28+
You can customize the game by adjusting the following constants in the script:
29+
30+
- `GRID_SIZE`: Change the size of the grid to accommodate more cells.
31+
- `NUM_ORGANISMS`: Set the initial number of organisms in the grid.
32+
- `NUM_RESOURCES`: Set the initial number of resources in the grid.
33+
- `ENERGY_THRESHOLD`: Adjust the energy threshold for organisms to reproduce.
34+
35+
You can also define new rules in the `RULES` dictionary to create different behaviors for organisms based on their energy levels.
36+
37+
## Contributions
38+
39+
Contributions are welcome! If you find any issues or have ideas to improve the game, please feel free to create a pull request or open an issue.
40+
41+
## Acknowledgments
42+
43+
The game is inspired by cellular automaton simulations and evolutionary algorithms.
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import random
2+
3+
GRID_SIZE = 50
4+
NUM_ORGANISMS = 5
5+
NUM_RESOURCES = 20
6+
ENERGY_THRESHOLD = 10
7+
8+
RULES = {
9+
0: lambda neighbors, energy: (sum(neighbors) + energy) % 2,
10+
1: lambda neighbors, energy: (sum(neighbors) - energy) % 2
11+
}
12+
13+
def initialize_grid():
14+
grid = [0] * GRID_SIZE
15+
for _ in range(NUM_ORGANISMS):
16+
organism_position = random.randint(0, GRID_SIZE - 1)
17+
grid[organism_position] = random.randint(1, 5)
18+
for _ in range(NUM_RESOURCES):
19+
resource_position = random.randint(0, GRID_SIZE - 1)
20+
resource_value = random.randint(1, 5)
21+
grid[resource_position] = -resource_value
22+
return grid
23+
24+
def get_neighbors(grid, index):
25+
neighbors = []
26+
if index > 0:
27+
neighbors.append(grid[index - 1])
28+
if index < GRID_SIZE - 1:
29+
neighbors.append(grid[index + 1])
30+
return neighbors
31+
32+
def apply_rule(grid, index):
33+
cell_state = grid[index]
34+
neighbors = get_neighbors(grid, index)
35+
if cell_state > 0: # If the cell represents an organism.
36+
energy = cell_state
37+
new_state = RULES[cell_state % len(RULES)](neighbors, energy)
38+
if energy >= ENERGY_THRESHOLD and grid.count(0) > 1:
39+
empty_spots = [i for i in range(GRID_SIZE) if grid[i] == 0]
40+
new_organism_position = random.choice(empty_spots)
41+
grid[new_organism_position] = energy // 2 # New organism created through reproduction.
42+
grid[index] = new_state
43+
elif cell_state < 0: # If the cell represents a resource.
44+
grid[index] = 0
45+
46+
def run_simulation(grid, num_iterations):
47+
for _ in range(num_iterations):
48+
new_grid = grid.copy()
49+
for i in range(GRID_SIZE):
50+
apply_rule(new_grid, i)
51+
grid = new_grid
52+
return grid
53+
54+
def display_grid(grid):
55+
for cell in grid:
56+
if cell == 0:
57+
print('.', end=' ')
58+
elif cell > 0:
59+
print('O', end=' ')
60+
else:
61+
print(abs(cell), end=' ')
62+
print()
63+
64+
def main():
65+
grid = initialize_grid()
66+
display_grid(grid)
67+
68+
num_iterations = 10
69+
grid = run_simulation(grid, num_iterations)
70+
71+
print("\nSimulation Results:")
72+
display_grid(grid)
73+
74+
if __name__ == "__main__":
75+
main()
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
random

0 commit comments

Comments
 (0)