![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Feb 2006
Posts: 2
Rep Power: 0
![]() |
(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.
|
|
|
|
|
|
#2 |
|
Programming Guru
![]() ![]() ![]() |
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." |
|
|
|
|
|
#3 |
|
Expert Programmer
|
You can find an excellent simulator here. It includes downloadable source code in Java.
|
|
|
|
|
|
#4 |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5
![]() |
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) |
|
|
|
|
|
#5 |
|
Newbie
Join Date: Mar 2006
Posts: 3
Rep Power: 0
![]() |
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:- |
|
|
|
|
|
#6 |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5
![]() |
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 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 |
|
|
|
|
|
#7 |
|
Programming Guru
![]() |
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 ) |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|