import random class Sidewinder(object): def on(grid): for row in grid.each_row(): run = [] for cell in row: run.append(cell) at_eastern_boundary = (cell.east == None) at_northern_boundary = (cell.north == None) should_close_out = at_eastern_boundary or \ (not at_northern_boundary and random.randint(0, 1) == 0) if should_close_out: member = random.choice(run) if member.north: member.link(member.north) del run[:] else: cell.link(cell.east) #print(grid) return grid