From 0d707d4ffb2727584791260cf0ad0cdc36b3ee03 Mon Sep 17 00:00:00 2001 From: Niclas Dobbertin Date: Tue, 14 Jul 2026 23:18:59 +0200 Subject: some automata --- 2d_automata.py | 119 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 119 insertions(+) create mode 100644 2d_automata.py (limited to '2d_automata.py') 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() -- cgit v1.2.3