Task: Conway's Game of Life







Python: cell_size = 10
sample = 10 
play = False   
last_cell = 0

def setup():
    global grid, next_grid, rows, cols
    size(800, 500)

    rows = height / cell_size
    cols = width / cell_size
    grid = empty_grid()
    next_grid = empty_grid()
    randomize_grid()

    println("Press 'space' to start/stop")
    println("'e' to clear all cells")
    println("'b' demonstrate 'blinker'")
    println("'g' demonstrate glider")
    println("'r' to randomize grid")
    println("'+' and '-' to change speed")

def draw():
    background(0)
    for i in range(cols):
        x = i * cell_size
        for j in range(rows):
            y = j * cell_size
            current_state = grid[i][j]
            fill(255)
            noStroke()
            if current_state:
                rect(x, y, cell_size, cell_size)
            if play:
                ngbs_alive = calc_ngbs_alive(i, j)
                result = rule(current_state, ngbs_alive)
                next_grid[i][j] = result
                
    if play and frameCount % sample == 0 and not mousePressed:
        step()

def rule(current, ngbs):
    
    if ngbs < 2 or ngbs > 3:
        return 0  
    elif ngbs == 3:
        return 1  
    else:
        return current  

def calc_ngbs_alive(i, j):
    NEIGHBOURS = ((-1, 00), (01, 00),  
                  (-1, -1), (00, -1),
                  (01, -1), (-1, 01),
                  (00, 01), (01, 01))
    alive = 0
    for iv, jv in NEIGHBOURS:
        alive += grid[(i + iv) % cols][(j + jv) % rows]
    return alive

def empty_grid():
    grid = []
    for _ in range(cols):
        grid.append([0] * rows)
    return grid

def randomize_grid():
    from random import choice
    for i in range(cols):
        for j in range(rows):
            grid[i][j] = choice((0, 1))

def step():
    global grid, next_grid
    grid = next_grid
    next_grid = empty_grid()

def keyReleased():
    global grid, play, sample
    if key == "e":
        grid = empty_grid()
    if key == "r":
        randomize_grid()
    if key == "g":
         grid[10][10:13] = [0, 1, 0]       
         grid[11][10:13] = [0, 0, 1]       
         grid[12][10:13] = [1, 1, 1]       
    if key == "b":
         grid[10][10:13] = [0, 1, 0]       
         grid[11][10:13] = [0, 1, 0]       
         grid[12][10:13] = [0, 1, 0]               
    if key == " ":
        play = not play 
    if  str(key) in '+=':
        sample = max(sample - 1, 1);
    if key == '-':
        sample += 1

def mousePressed():
    paint()
    
def mouseDragged():
    paint()

def paint():
    global last_cell
    i, j = mouseX // cell_size, mouseY // cell_size
    p = j * cols + i
    if p != last_cell:
        last_cell = p
        grid[i][j] = (1, 0)[grid[i][j]]



COBOL: identification division.
program-id. game-of-life-program.
data division.
working-storage section.
01  grid.
    05 cell-table.
        10 row occurs 5 times.
            15 cell pic x value space occurs 5 times.
    05 next-gen-cell-table.
        10 next-gen-row occurs 5 times.
            15 next-gen-cell pic x occurs 5 times.
01  counters.
    05 generation pic 9.
    05 current-row pic 9.
    05 current-cell pic 9.
    05 living-neighbours pic 9.
    05 neighbour-row pic 9.
    05 neighbour-cell pic 9.
    05 check-row pic s9.
    05 check-cell pic s9.
procedure division.
control-paragraph.
    perform blinker-paragraph varying current-cell from 2 by 1
    until current-cell is greater than 4.
    perform show-grid-paragraph through life-paragraph
    varying generation from 0 by 1
    until generation is greater than 2.
    stop run.
blinker-paragraph.
    move '#' to cell(3,current-cell).
show-grid-paragraph.
    display 'GENERATION ' generation ':'.
    display '   +---+'.
    perform show-row-paragraph varying current-row from 2 by 1
    until current-row is greater than 4.
    display '   +---+'.
    display ''.
life-paragraph.
    perform update-row-paragraph varying current-row from 2 by 1
    until current-row is greater than 4.
    move next-gen-cell-table to cell-table.
show-row-paragraph.
    display '   |' with no advancing.
    perform show-cell-paragraph varying current-cell from 2 by 1
    until current-cell is greater than 4.
    display '|'.
show-cell-paragraph.
    display cell(current-row,current-cell) with no advancing.
update-row-paragraph.
    perform update-cell-paragraph varying current-cell from 2 by 1
    until current-cell is greater than 4.
update-cell-paragraph.
    move 0 to living-neighbours.
    perform check-row-paragraph varying check-row from -1 by 1
    until check-row is greater than 1.
    evaluate living-neighbours,
        when 2 move cell(current-row,current-cell) to next-gen-cell(current-row,current-cell),
        when 3 move '#' to next-gen-cell(current-row,current-cell),
        when other move space to next-gen-cell(current-row,current-cell),
    end-evaluate.
check-row-paragraph.
    add check-row to current-row giving neighbour-row.
    perform check-cell-paragraph varying check-cell from -1 by 1
    until check-cell is greater than 1.
check-cell-paragraph.
    add check-cell to current-cell giving neighbour-cell.
    if cell(neighbour-row,neighbour-cell) is equal to '#',
    and check-cell is not equal to zero or check-row is not equal to zero,
    then add 1 to living-neighbours.