Extra#

John Conway’s Game of Life#

Game of Life is een cellulaire automaat uitgevonden door de in 2020 overleden wiskundige John Conway. Game of Life is niet zozeer een “spel” in de traditionele betekenis, maar een proces dat over tijd veranderd aan de hand van een paar simpele regels. Het proces wordt opgezet als een raster cellen, die ieder “levend” of “dood” kunnen zijn op een gegeven tijdstip. Op elk tijdstip overleven of sterven cellen aan de hand van de volgende regels:

  1. Een cel met minder dan twee levende buren sterft (vanwege isolatie)

  2. Een cel met meer dan 3 levende buren sterft (vanwege overbevolking)

  3. Een dode cel met precies 3 levende buren komt weer tot leven

  4. Alle andere cellen blijven in dezelfde toestand

Alhoewel deze regels simpel lijken leiden ze tot complexe en interessante patronen. Meer informatie en een aantal interessante patronen kan je vinden op Conway’s Game of Life.

import random


def main():
    a = [[0, 0, 0, 0, 0], 
        [0, 0, 1, 0, 0], 
        [0, 0, 1, 0, 0], 
        [0, 0, 1, 0, 0], 
        [0, 0, 0, 0, 0]
    ]  # vertical bar

    print_board(a)
    a2 = next_life_generation(a)
    print_board(a2)
    a3 = next_life_generation(a2)
    print_board(a3)


def create_one_row(width):
    """ returns one row of zeros of width "width"...  
         You might use this in your create_board(width, height) function """
    row = []
    for col in range(width):
        row += [0]
    return row

def create_board(width, height):
    """Returns a 2D array with "height" rows and "width" columns."""
    a = []
    for row in range(height):
        a += [create_one_row(width)]        # gebruik de bovenstaande functie zodat ... Ă©Ă©n rij is!!
    return a

def print_board(a):
    """This function prints the 2D list-of-lists a."""
    for row in a:               # row is de hele rij
        for col in row:         # col is het individuele element
            print(col, end='')  # druk dat element af
        print()

def diagonalize(width, height):
    """Creates an empty board and then modifies it
       so that it has a diagonal strip of "on" cells.
       But do that only in the *interior* of the 2D array.
    """
    a = create_board(width, height)

    for row in range(1, height - 1):
        for col in range(1, width - 1):
            if row == col:
                a[row][col] = 1
            else:
                a[row][col] = 0

    return a

def inner_cells(w, h):
    board = create_board(w,h)
    for row in range (1, h -1):
        for col in range(1, w-1):
            board[row][col] = 1
    return board

def random_cells(w, h):
    board = create_board(w,h)
    for row in range (1, h -1):
        for col in range(1, w-1):
            board[row][col] = random.choice([0,1])
    return board

def copy(a):
    """Returns a DEEP copy of the 2D array a."""
    height = len(a)
    width = len(a[0])
    new_a = create_board(width, height)

    for row in range(1, height - 1):
        for col in range(1, width - 1):
            new_a[row][col] = a[row][col]
            ...

    return new_a

def inner_reverse(a):

    height = len(a)
    width = len(a[0])
    new_a = create_board(width, height)

    for row in range(1, height - 1):
        for col in range(1, width - 1):
            new_a[row][col] = abs(a[row][col] - 1)

    return new_a

def count_neighbours(row, col, a):
    buren = 0
    if a[row-1][col-1] == 1:
        buren += 1
    if a[row-1][col] == 1:
        buren += 1
    if a[row-1][col+1] == 1:
        buren += 1
    if a[row][col-1] == 1:
        buren += 1
    if a[row][col+1] == 1:
        buren += 1
    if a[row+1][col-1] == 1:
        buren += 1
    if a[row+1][col] == 1:
        buren += 1
    if a[row+1][col+1] == 1:
        buren += 1

    return buren


def next_life_generation(a):

    height = len(a)
    width = len(a[0])
    new_a = copy(a)

    for row in range(1, height - 1):
        for col in range(1, width - 1):
            buren = count_neighbours(row, col, a)
            if a[row][col] == 1 and buren < 2:
                new_a[row][col] = 0
            elif a[row][col] == 1 and buren >3:
                new_a[row][col] = 0
            elif a[row][col] == 0 and buren == 3:
                new_a[row][col] = 1
            

    return new_a


main()