View Single Post
Old Mar 19th, 2006, 9:43 PM   #7
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 1,890
Rep Power: 5 Sane will become famous soon enough
Send a message via MSN to Sane
Here's my version, heavily commented and elaborated because I don't know how experienced in Python you are to understand it.

from os import system  # for clear screen function
from time import sleep # for delay between each tick


def get_crowd(l, x, y, sx, sy): # note: sy not required
    
        """return the number of items surrounding
        item 'x', 'y' on grid 'l' ('sx' X 'sy')"""
    
        # add padding to the grid to make 'nl'
        nl = [[0]*(sx+2)]
        for ll in l:
                nl += [[0]+ll+[0]]
        nl += [[0]*(sx+2)]
        
        # we've offseted 'x', 'y' by 1, 1
        ox, oy = 1, 1

        i = 0
        # count neighbours from eight possibilities
        for ny, nx in ((y+oy, x+1+ox),(y+oy, x-1+ox),      # right, left
                       (y-1+oy, x+ox),(y+1+oy, x+ox),      # top, bottom
                       (y+1+oy, x+1+ox),(y-1+oy, x-1+ox),  # bottom-right, top-left
                       (y-1+oy, x+1+ox),(y+1+oy, x-1+ox)): # top-right, bottom-left
                if nl[ny][nx]: i += 1
        return i


def GOL(grid, dfunc):
    
        """          Conway's Game Of Life
        http://en.wikipedia.org/wiki/Conways_Game_of_Life"""

        sizex = len(grid[0])
        sizey = len(grid)

        # start board
        dfunc(grid)

        # infinite loop (same as while 1 == 1)
        while 1:

                # calculate neighbours
                igrid = []
                for y in range(sizey):
                        igrid += [[]]
                        for x in range(sizex):
                                igrid[-1] += [get_crowd(grid, x, y, sizex, sizey)]

                # apply calculations
                for y in range(sizey):
                        for x in range(sizex):
                                if igrid[y][x] < 2 or igrid[y][x] > 3:
                                    # if, less then two neighbours
                                    # or, more then three neighbours: dead
                                    grid[y][x] = 0
                                elif igrid[y][x] == 3 and not grid[y][x]:
                                    # if, three neighbours
                                    # and, is dead: reborn
                                    grid[y][x] = 1

                # show results
                dfunc(grid)

                                
def my_output(grid):
    
        """this function is called every
        time the grid is changed"""

        system('cls')  # note: os-dependant
        p = ''
        for y in grid:
            for x in y:
                p += [' ', 'X'][x]
            p += '\n'
        print p

        sleep(0.02) # delay
    

if __name__ == '__main__':

        # example grids
        default = [[1, 1, 1],
                   [1, 0, 1],
                   [1, 1, 1]]

        glider  = [[1, 1, 1],
                   [1, 0, 0],
                   [0, 1, 0]]

        boat    = [[1, 1, 0],
                   [1, 0, 1],
                   [0, 1, 0]]

        blinker = [[1, 1, 1]]

        toad    = [[0, 1, 1, 1],
                   [1, 1, 1, 0]]

        gosper  = [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                   [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                   [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1],
                   [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1],
                   [1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                   [1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                   [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                   [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                   [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]


        # program is initialised, start GOL
        GOL( grid   = gosper,
              dfunc = my_output )
Sane is online now   Reply With Quote