Simple Python Games: John Conway's Game of Life

Source code of life.py

# John Conway's Game of Life

from time import sleep

# initial population (three gliders and one blinker, char other than X has no effect)
desk = [
'................................................................................',
'.X..............................................................................',
'..X.............................................................................',
'XXX.............................................................................',
'................................................................................',
'.......................................................................X........',
'......................................................................X.........',
'......................................................................XXX.......',
'................................................................................',
'................................................................................',
'................................................................................',
'................................................................................',
'................................................................................',
'................................................................................',
'................................................................................',
'................................................................................',
'......................................................................X.........',
'....XXX...............................................................X.........',
'......X...............................................................X.........',
'....X...........................................................................',
'................................................................................',
'................................................................................',
]
# generation counter
generation = 1

# prints the desk
def print_desk(desk, generation):
    print()
    print('Generation:', generation, '   (press Ctrl+C to stop)')
    for ln in desk:
        print(ln)

# counts live cells around coordinates x, y
def count_cells(desk, x, y):
    count = 0
    for y2 in range(y-1, y+2):
        y2 = y2 % len(desk)
        for x2 in range(x-1, x+2):
            x2 = x2 % len(desk[y2])
            if desk[y2][x2] == 'X':
                count += 1
    return count

# main cycle
while True:
    print_desk(desk, generation)
    # evolve the desk - new state stored into the 'desk2'
    desk2 = []
    for y, ln in enumerate(desk):
        ln2 = ''
        for x, cell in enumerate(ln):
            alive = cell == 'X'
            count = count_cells(desk, x, y)
            # the three rules od Life:
            if not alive and count == 3:
                ln2 += 'X'
            elif alive and count < 3 or count > 4:
                ln2 += ' '
            else:
                ln2 += cell
        desk2.append(ln2)
    desk = desk2
    generation += 1
    sleep(0.1)


Download

Source code of John Conway's Game of Life (life.py).