Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Feb 15th, 2006, 7:00 PM   #1
jade_casey
Newbie
 
Join Date: Feb 2006
Posts: 2
Rep Power: 0 jade_casey is on a distinguished road
(Conway's Game of Life)

Can someone please show me the python code for (Conway's) Game of Life so I can use it as a template to create a similar game.
jade_casey is offline   Reply With Quote
Old Feb 15th, 2006, 8:19 PM   #2
Infinite Recursion
Programming Guru
 
Infinite Recursion's Avatar
 
Join Date: Jul 2004
Location: United States
Posts: 3,471
Rep Power: 8 Infinite Recursion is on a distinguished road
Send a message via MSN to Infinite Recursion Send a message via Yahoo to Infinite Recursion
I wrote an implementation of the Game of Life for a college assignment back in 2000... but it is in C++. I'll post it if you are interested.
__________________
http://jasonpowers.net

"There are a thousand hacking at the branches of evil to one who is striking at the root."
Infinite Recursion is online now   Reply With Quote
Old Feb 17th, 2006, 2:13 AM   #3
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 928
Rep Power: 4 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
You can find an excellent simulator here. It includes downloadable source code in Java.
titaniumdecoy is offline   Reply With Quote
Old Feb 17th, 2006, 7:42 AM   #4
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
This is just what I knocked up in a text editor, so I haven't tested or run it yet, but it seems like it would work. I'll test it when I get home.
class Grid:
    def __init__(self, (width, height)):
        self.size = width, height
        self.grid = [[None for i in range(width)] for j in range(height)]

    def get(self, (x, y)):
        if not in_bounds((x, y)):
            raise ValueError, "coordinates are out of bounds"
        return self.grid[y][x]

    def put(self, (x, y), object):
        if not in_bounds((x, y)):
            raise ValueError, "coordinates are out of bounds"
        self.grid[y][x] = object

    def in_bounds(self, (x, y)):
        width, height = self.size
        return (x >= 0 and x < width) and (y >= 0 and y < height)

    def neighbours(self, (x, y)):
        neighbours = []
        for i in [x - 1, x, x + 1]:
            for j in [y - 1, y, y + 1]:
                if x == i and y == j:
                    continue
                try:
                    neighbours.append(self.get(i, j))
                except ValueError:
                    continue
        return neighbours

    def __iter__(self):
        for x, row in enumerate(self.grid):
            for y, square in enumerate(row):
                yield (x, y), square

class Cell:
    pass

cell = Cell()

class GameOfLife:
    def __init__(self, grid):
        self.grid = grid

    def turn():
        for position, square in self.grid:
            if square == None:
                if self.grid.neighbours(position).count(cell) == 3:
                    self.add_cell(position)
            else:
                if self.grid.neighbours(position).count(cell) <= 1:
                    self.remove_cell(position)
                if self.grid.neighbours(position).count(cell) >= 4:
                    self.remove_cell(position)

    def add_cell(self, position):
        self.grid.put(position, cell)

    def remove_cell(self, position):
        self.grid.put(position, None)
Arevos is offline   Reply With Quote
Old Mar 18th, 2006, 11:47 PM   #5
ale
Newbie
 
Join Date: Mar 2006
Posts: 3
Rep Power: 0 ale is on a distinguished road
I never understood that game

How are you supposed to play the game of life huh? I tried it many times but I just get pixels moving around my screen and killing each other or something, could somebody explain me how it works? Cause I don't get it! lol
-:A:l:e:-
ale is offline   Reply With Quote
Old Mar 19th, 2006, 5:26 AM   #6
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
It's not a "game" per se. It's a set of simple rules designed to result in more complex behaviour, much as the rules governing organisms in life result in larger biodiversity.

The game takes place on a two dimensional board. Each square on the board can either be empty, or contain a "cell". Each square (X) on the board has eight neighbours bordering it, both adjacently and diagonally:
1 2 3
4 X 5
6 7 8
The game has three rules:
1. If an empty square has three neighbouring cells, a new cell is created in that square
2. If a cell has less than two neighbouring cells, it dies of loneliness
3. If a cell has more than three neighbouring cells, it dies from overcrowding
Arevos is offline   Reply With Quote
Old Mar 19th, 2006, 10:43 PM   #7
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 2,069
Rep Power: 6 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 offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 9:09 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC