minte9
LearnRemember



Conway's game

Conway's game of life, with alive and dead squares.
 
"""Conway's game of life, by Al Sweigart
Automate the Boring Stuff with Python, 2nd Edition
More info at: https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life
Version 1:
Displays random alive or dead squares. 
A filled-in square will be "alive" (#)
An empty square will be "dead" (' ')"""

import random, time

X = 6
Y = 6

def get_cols():
    cols = []
    for i in range(X):
        row = []
        for j in range(Y):
            if random.randint(0,1) == 0:
                row.append('#')
            else:
                row.append(' ')
        cols.append(row)
    return cols

def print_cols(cols):
    for row in cols:
        print (*row, sep=' ')

while True:
    print_cols(get_cols())
    print("-------------")
    time.sleep(1)
The neighbours are wraparound with mod-wraparound technique.
 
"""Conway's game of life, by Al Sweigart
Automate the Boring Stuff with Python, 2nd Edition
More info at: https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life
Version 2:
Count cell neighbours (8 maximum).
The neighbours are wraparound, for x=0, the left neighbour has x=59
The mod-wraparound technique works as well for right, above, below"""

import random

X = 6
Y = 6

def get_cells():
    cells = []
    for i in range(X):
        row = []
        for j in range(Y):
            if random.randint(0,1) == 0:
                row.append('#')
            else:
                row.append(' ')
        cells.append(row)
    return cells

def print_cells(cells):
    for row in cells:
        print (*row, sep=' ')

assert (0 - 1) % 6 == 5  # Left neighour (wrap arround)
assert (1 - 1) % 6 == 0
assert (2 - 1) % 6 == 1
assert (3 - 1) % 6 == 2
assert (4 - 1) % 6 == 3
assert (5 - 1) % 6 == 4

assert (0 + 1) % 6 == 1  # Right neighbour
assert (1 + 1) % 6 == 2
assert (2 + 1) % 6 == 3
assert (3 + 1) % 6 == 4
assert (4 + 1) % 6 == 5
assert (5 + 1) % 6 == 0

cells = get_cells()
print_cells(cells)

for x in range(X): # x=0 ... x=5
    for y in range(Y):

        left  = (x - 1) % X  # Look Here
        right = (x + 1) % X
        above = (y - 1) % Y
        below = (y + 1) % Y

        neighbours = 0  # Maxim 8 neighbours

        if cells [left][above] == '#':    # Top left - alive
            neighbours += 1

        if cells [x][above] == '#':       # Top
            neighbours += 1

        if cells [right][above] == '#':   # Top right
            neighbours += 1

        if cells [right][y] == '#':       # Right
            neighbours += 1

        if cells [right][below] == '#':   # Bottom right
            neighbours += 1

        if cells [x][below] == '#':       # Bottom
            neighbours += 1
            
        if cells [left][below] == '#':    # Bottom left
            neighbours += 1

        if cells [left][y] == '#':        # Left
            neighbours += 1

        print(str(x) + ":" + str(y), "neighbours:", neighbours)
Deep copy is relevant only for compound objects (might have references to themselves).
 
"""Conway's game of life, by Al Sweigart
Automate the Boring Stuff with Python, 2nd Edition
More info at: https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life
Version: 3
Main loop to set next alive or dead squares.
Deep copy is relevant only for compound objects
(objects that contain other objects, like lists or class instances)"""

import random, time, copy

X = 6
Y = 6

def get_cells():
    cells = []
    for i in range(X):
        row = []
        for j in range(Y):
            if random.randint(0,1) == 0:
                row.append('#')
            else:
                row.append(' ')
        cells.append(row)
    return cells

def print_cells(cells):
    for row in cells:
        print (*row, sep=' ')

nextC = get_cells()

while True: # main loop
    print("-------------")
    currC = copy.deepcopy(nextC)  # Look Here
    print_cells(currC)

    for x in range(X):
        for y in range(Y):

            L = (x - 1) % X # Left
            R = (x + 1) % X # Right
            A = (y - 1) % Y # Above
            B = (y + 1) % Y # Below

            n = 0

            if currC[L][A] == '#': # Top left
                n += 1
            if currC[x][A] == '#': # Top
                n += 1
            if currC[R][A] == '#': # Top right
                n += 1
            if currC[R][y] == '#': # Right
                n += 1
            if currC[R][B] == '#': # Bottom right
                n += 1
            if currC[x][B] == '#': # Bottom
                n += 1
            if currC[L][B] == '#': # Bottom left 
                n += 1
            if currC[L][y] == '#': # Left
                n += 1

            """Set cells base on Conway's Game of Life rules:
            Living cells with 2 or 3 neighbors stay alive.
            Dead cells with 3 neighbors become alive.
            Everything else dies or stays dead."""

            if currC[x][y] == '#' and n == 2 or n == 3:
                nextC[x][y] = '#'

            elif currC[x][y] == ' ' and n == 3:
                nextC[x][y] = '#'

            else:
                nextC[x][y] = ' '
    
    time.sleep(1)
The glider pattern moves diagonally every four steps.
 
"""Conway's game of life, by Al Sweigart
Automate the Boring Stuff with Python, 2nd Edition
More info at: https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life
Version 4:
Glider pattern (not random).
A pattern that moves diagonally every four steps"""

import time, copy, sys

X = 6
Y = 6

ALIVE = '#'
DEAD = ' '

def get_cells():
    cells = []
    for x in range(X):
        row = []
        for y in range(Y):
            if (x, y) in ((1,0), (2,1), (0,2), (1,2), (2,2)):  # Look Here
                row.append(ALIVE)
            else:
                row.append(DEAD)
        cells.append(row)
    return cells

def print_cells(cells):
    for row in cells:
        print (*row, sep=' ')

nextC = get_cells()

while True:
    print("-------------")
    currC = copy.deepcopy(nextC)
    print_cells(currC)

    for x in range(X):
        for y in range(Y):

            L = (x - 1) % X # Left
            R = (x + 1) % X # Right
            A = (y - 1) % Y # Above
            B = (y + 1) % Y # Below

            n = 0

            if currC[L][A] == ALIVE: # Top left
                n += 1
            if currC[x][A] == ALIVE: # Top
                n += 1
            if currC[R][A] == ALIVE: # Top right
                n += 1
            if currC[R][y] == ALIVE: # Right
                n += 1
            if currC[R][B] == ALIVE: # Bottom right
                n += 1
            if currC[x][B] == ALIVE: # Bottom
                n += 1
            if currC[L][B] == ALIVE: # Bottom left 
                n += 1
            if currC[L][y] == ALIVE: # Left
                n += 1

            # Set cells base on Conway's Game of Life rules:
            if currC[x][y] == ALIVE and n == 2 or n == 3:
                nextC[x][y] = ALIVE

            elif currC[x][y] == DEAD and n == 3:
                nextC[x][y] = ALIVE
                
            else:
                nextC[x][y] = DEAD

    try:         
        time.sleep(1)
    except KeyboardInterrupt:
        sys.exit()  # When Ctrl-C is pressed, end the program.



  Last update: 420 days ago