summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--1d_automata.py132
-rw-r--r--2d_automata.py119
2 files changed, 251 insertions, 0 deletions
diff --git a/1d_automata.py b/1d_automata.py
new file mode 100644
index 0000000..699faa9
--- /dev/null
+++ b/1d_automata.py
@@ -0,0 +1,132 @@
+#!/usr/bin/env python3
+
+import numpy as np
+import matplotlib.pyplot as plt
+import random
+
+print("hello")
+
+# field = np.array([[0,1,1,0], [1,0,0,1], [0,0,0,0], [0,0,0,0]])
+
+
+def timestep(rule, field):
+ # if rule == "conway":
+ # for i in range(4):
+ # for j in range(4):
+ pass
+
+
+# print(field)
+# field = timestep("conway", field)
+#
+def rule30(field):
+ result = field.copy()
+ for i in range(len(field)-2):
+ x = [field[i], field[i+1], field[i+2]]
+
+ if x == [1,1,1]:
+ result[i+1] = 0
+ if x == [1,1,0]:
+ result[i+1] = 0
+ if x == [1,0,1]:
+ result[i+1] = 0
+ if x == [1,0,0]:
+ result[i+1] = 1
+ if x == [0,1,1]:
+ result[i+1] = 1
+ if x == [0,1,0]:
+ result[i+1] = 1
+ if x == [0,0,1]:
+ result[i+1] = 1
+ if x == [0,0,0]:
+ result[i+1] = 0
+ return result
+
+def rule184(field):
+ result = field.copy()
+ for i in range(len(field)-2):
+ x = [field[i], field[i+1], field[i+2]]
+
+ if x == [1,1,1]:
+ result[i+1] = 1
+ if x == [1,1,0]:
+ result[i+1] = 0
+ if x == [1,0,1]:
+ result[i+1] = 1
+ if x == [1,0,0]:
+ result[i+1] = 1
+ if x == [0,1,1]:
+ result[i+1] = 1
+ if x == [0,1,0]:
+ result[i+1] = 0
+ if x == [0,0,1]:
+ result[i+1] = 0
+ if x == [0,0,0]:
+ result[i+1] = 0
+ return result
+
+def rule110(field):
+ result = field.copy()
+ for i in range(len(field)-2):
+ x = [field[i], field[i+1], field[i+2]]
+
+ if x == [1,1,1]:
+ result[i+1] = 0
+ if x == [1,1,0]:
+ result[i+1] = 1
+ if x == [1,0,1]:
+ result[i+1] = 1
+ if x == [1,0,0]:
+ result[i+1] = 0
+ if x == [0,1,1]:
+ result[i+1] = 1
+ if x == [0,1,0]:
+ result[i+1] = 1
+ if x == [0,0,1]:
+ result[i+1] = 1
+ if x == [0,0,0]:
+ result[i+1] = 0
+ return result
+
+def ruletest(field):
+ result = field.copy()
+ for i in range(len(field)-2):
+ x = [field[i], field[i+1], field[i+2]]
+
+ if x == [1,1,1]:
+ result[i+1] = 0
+ if x == [1,1,0]:
+ result[i+1] = 0
+ if x == [1,0,1]:
+ result[i+1] = 0
+ if x == [1,0,0]:
+ result[i+1] = 1
+ if x == [0,1,1]:
+ result[i+1] = 0
+ if x == [0,1,0]:
+ result[i+1] = 0
+ if x == [0,0,1]:
+ result[i+1] = 1
+ if x == [0,0,0]:
+ result[i+1] = 0
+ return result
+
+field = [0] * 1000
+# field[500] = 1
+for i, _ in enumerate(field):
+ field[i] = random.choice([0, 1])
+# print(field)
+# exit(0)
+# print(field)
+# print(rule30(field))
+total_time = 1000
+history = []
+for t in range(total_time):
+ if t > 0:
+ field = ruletest(field)
+ # print(field)
+ history.append(field)
+
+plt.figure(figsize=(10,6))
+plt.imshow(np.array(history), cmap='hot')
+plt.show()
diff --git a/2d_automata.py b/2d_automata.py
new file mode 100644
index 0000000..d27b536
--- /dev/null
+++ b/2d_automata.py
@@ -0,0 +1,119 @@
+#!/usr/bin/env python3
+
+import pyray as pr
+import random
+import numpy as np
+import time
+
+screen_width = 1000
+screen_height = 1000
+
+pr.init_window(screen_width, screen_height, "Hello")
+
+cols = 20
+rows = 20
+grid = np.random.randint(2, size=(rows, cols))
+# grid = np.zeros((100, 100))
+# grid[0][1] = 1
+# grid[1][2] = 1
+# grid[2][0] = 1
+# grid[2][1] = 1
+# grid[2][2] = 1
+# grid = np.array([[1, 0, 0, 1], [1, 0, 1, 1], [1, 0, 0, 1], [0, 0, 1, 1]])
+
+
+col_space = int(screen_width / cols)
+row_space = int(screen_height / rows)
+
+def draw_game(grid):
+ y = 0
+ for i in grid:
+ x = 0
+ for j in i:
+ if j == 1:
+ pr.draw_rectangle(x, y, col_space, row_space, pr.BLACK)
+ else:
+ pr.draw_rectangle(x, y, col_space, row_space, pr.WHITE)
+ x += col_space
+ y += row_space
+
+""" Rules of Conway's game of life:
+1. Any live cell with fewer than two live neighbours dies, as if by
+underpopulation.
+2. Any live cell with two or three live neighbours lives on to the next
+generation.
+3. Any live cell with more than three live neighbours dies, as if by
+overpopulation.
+4. Any dead cell with exactly three live neighbours becomes a live cell,
+as if by reproduction.
+"""
+
+
+def apply_conway(grid):
+ new_grid = np.zeros((rows, cols))
+ for j, row in enumerate(grid):
+ for i, cell in enumerate(row):
+ neighbours = 0
+ # right neighbour
+ # print(
+ # f"I'm {i+1} and {"alive" if cell == 1 else "dead"}, my right neighbour is {((i+1)%cols) +1} and {"alive" if grid[j][(i+1)%cols] == 1 else "dead"}"
+ # )
+ if grid[j][(i+1)%cols] == 1:
+ neighbours += 1
+ # left neighbour
+ if grid[j][(i-1)%cols] == 1:
+ neighbours += 1
+ # bottom neighbour
+ if grid[(j+1) % rows][i] == 1:
+ neighbours += 1
+ # top neighbour
+ if grid[(j-1) % rows][i] == 1:
+ neighbours += 1
+ # print(f"Cell in row {j}, col {i} has {neighbours} neighbours")
+ # top right neighbour
+ if grid[(j-1) % rows][(i+1)%cols] == 1:
+ neighbours += 1
+ # top left neighbour
+ if grid[(j-1) % rows][(i-1)%cols] == 1:
+ neighbours += 1
+ # bottom right neighbour
+ if grid[(j+1) % rows][(i+1)%cols] == 1:
+ neighbours += 1
+ # bottom left neighbour
+ if grid[(j+1) % rows][(i-1)%cols] == 1:
+ neighbours += 1
+
+ # Rule 1
+ if cell == 1 and neighbours < 2:
+ new_grid[j, i] = 0
+ # Rule 2
+ if cell == 1 and neighbours == 2:
+ new_grid[j, i] = 1
+ if cell == 1 and neighbours == 3:
+ new_grid[j, i] = 1
+ # Rule 3
+ if cell == 1 and neighbours > 3:
+ new_grid[j, i] = 0
+ # Rule 4
+ if cell == 0 and neighbours == 3:
+ new_grid[j, i] = 1
+
+ # TODO: consider alternative compute to avoid all the ifs: add all
+ # nine fields, if 3 life, if 4 stay current state, else dead
+
+ return new_grid
+
+first_iter = True
+while not pr.window_should_close():
+ if not first_iter:
+ grid = apply_conway(grid)
+
+ pr.begin_drawing()
+ pr.clear_background(pr.WHITE)
+ # pr.draw_text("Hello World", 190, 200, 20, pr.VIOLET)
+
+ draw_game(grid)
+ pr.end_drawing()
+ time.sleep(.5)
+ first_iter = False
+pr.close_window()