Apr 03, 2017 Conways’s Game Of Life is a Cellular Automation Method created by John Conway. This game was created with Biology in mind but has been applied in various fields such as Graphics, terrain generation,etc. This article is contributed by Vaibhav Mehta. If you like GeeksforGeeks and would like to contribute, you can also mail your article to contribute@geeksforgeeks.org. Vie is an implementation of the popular game by John Conway: the Game of Life. Players can create boards and run them through from their favourite device. Features include: - create new random boards - save random boards to replay later - browse and load 1000+ patterns - bookmark favourite pattern.
Conways_Game_Of_Life.py
Conway's Game Of Life Machine
#!/usr/bin/env python |
# one approach to the Conway's Game of Life |
# http://en.wikipedia.org/wiki/Conway's_Game_of_Life |
# Val Neekman val@neekware.com |
# Open a terminal (Mac) 170 x 40, then run this script and see it for yourself |
# It is fun |
importos |
importsys |
importtime |
importrandom |
classConwaysGameOfLife(object): |
HEIGHT=40 |
WIDTH=80 |
DEAD, ALIVE=range(2) |
def__init__(self, height=40, width=80): |
self.HEIGHT=height |
self.WIDTH=width |
defget_grid(self, seed=False): |
grid= {} |
foriinrange(self.WIDTH): |
forninrange(self.HEIGHT): |
grid[(i, n)] =self.DEAD |
ifseed: |
seeded_cell= (random.choice(range(self.WIDTH)), random.choice(range(self.HEIGHT))) |
neighbours=self.get_neighbours(seeded_cell) |
forainrange(3): |
i=random.choice(range(len(neighbours))) |
whilegrid[neighbours[i]] self.ALIVE: |
i=random.choice(range(len(neighbours))) |
grid[neighbours[i]] =self.ALIVE |
returngrid |
defget_neighbours(self, cell): |
x, y=cell |
possible_neighbours= [ |
(x-1, y-1), |
(x, y-1), |
(x+1, y-1), |
(x+1, y), |
(x+1, y+1), |
(x, y+1), |
(x-1, y+1), |
(x-1, y) |
] |
real_neighbours= [] |
forninpossible_neighbours: |
ifn[0] >=0andn[0] <self.WIDTH: |
ifn[1] >=0andn[1] <self.HEIGHT: |
real_neighbours.append(n) |
returnreal_neighbours |
defget_neighbours_total_living(self, grid, cell): |
neighbours=self.get_neighbours(cell) |
total=0 |
forninneighbours: |
ifgrid[n] 1: |
total+=1 |
returntotal |
deftick(self, grid): |
new_grid=self.get_grid() |
foriinrange(self.WIDTH): |
forninrange(self.HEIGHT): |
living_neighbours=self.get_neighbours_total_living(grid, (i, n)) |
ifliving_neighbours2orliving_neighbours3: |
new_grid[(i,n)] =self.ALIVE |
elifgrid[(i,n)] self.DEADandliving_neighbours3: |
new_grid[(i,n)] =self.ALIVE |
returnnew_grid |
defprint_grid(self, grid): |
line=' |
foriinrange(self.HEIGHT): |
line+='|' |
forninrange(self.WIDTH): |
line+='*|'ifgrid[(n, i)] 1else' |' |
line+='n' |
sys.stderr.write (line) |
defrun(self, sleep=1): |
grid=self.get_grid(seed=True) |
self.print_grid(grid) |
whileTrue: |
grid=self.tick(grid) |
self.print_grid(grid) |
time.sleep(sleep) |
if__name__'__main__': |
conway=ConwaysGameOfLife() |
conway.run() |
Conway S Game Of Life Matlab
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment