summaryrefslogtreecommitdiffstats
path: root/sidewinder.py
blob: c047eed05fe856e3bf1fb3d701b8bdc4292e7576 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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