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():
    board = [
        [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(board)
    board2 = next_life_generation(board)
    print_board(board2)
    board3 = next_life_generation(board2)
    print_board(board3)


def create_one_row(width):
    """Geeft één rij nullen terug van breedte "width"...
    Deze kun je gebruiken in jouw functie create_board(width, height)"""
    row = []
    for col in range(width):
        row += [0]
    return row


def create_board(width, height):
    """Geeft een 2D-lijst terug met "height" rijen en "width" kolommen."""
    board = []
    for row in range(height):
        board += [
            create_one_row(width)
        ]  # gebruik de bovenstaande functie zodat ... één rij is!!
    return board


def print_board(board):
    """Drukt de 2D-lijst af."""
    for row in board:  # 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):
    """Maakt een leeg bord en past dat zo aan
    dat er een diagonale streep van gevulde cellen in staat.
    Doe dat alleen in het *binnenste* van de 2D-lijst.
    """
    board = create_board(width, height)

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

    return board


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


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


def copy(board):
    """Geeft een DIEPE kopie van de 2D-lijst a terug."""
    height = len(board)
    width = len(board[0])
    new_board = create_board(width, height)

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

    return new_board


def inner_reverse(board):
    height = len(board)
    width = len(board[0])
    new_board = create_board(width, height)

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

    return new_board


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

    return n_neighbours


def next_life_generation(board):
    height = len(board)
    width = len(board[0])
    new_board = copy(board)

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

    return new_board


main()