Last active
February 28, 2019 13:19
-
-
Save pncnmnp/ad35f32d3d88109417be473bfdd6b8ad to your computer and use it in GitHub Desktop.
conway's game of life ( simple implementation )
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from termcolor import colored | |
import matplotlib.pyplot as plt | |
import matplotlib.animation as animation | |
class Conway: | |
def __init__(self): | |
self.N = 60 | |
self.generations = 300 | |
self.curr_board = [[0 for i in range(self.N)] for j in range(self.N)] | |
self.prev_board = [[0 for i in range(self.N)] for j in range(self.N)] | |
# just add here a 10*10 matrix with your game of life arrangment | |
self.inf = [[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,1,0,1,0,0,0], | |
[0,0,1,0,0,0,0,0,0,0], | |
[0,0,0,0,0,1,1,0,0,0], | |
[0,0,0,1,1,0,1,0,0,0], | |
[0,0,1,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]] | |
self.pentadecathlon = [[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,0,0,0,0,0,0,0], | |
[0,1,1,1,1,1,1,1,1,0], | |
[0,1,0,1,1,1,1,0,1,0], | |
[0,1,1,1,1,1,1,1,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]] | |
self.diehard = [[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,0,0,0,0,1,0,0], | |
[1,1,0,0,0,0,0,0,0,0], | |
[0,1,0,0,0,0,1,1,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,0,0,0,0,0,0,0,0]] | |
self.switchengine = [[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,1,0,1,1,0], | |
[0,0,0,0,0,1,0,1,0,0], | |
[0,0,0,0,0,1,0,0,0,0], | |
[0,0,0,1,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,0,0,0], | |
[0,0,0,0,0,0,0,0,0,0]] | |
def next_gen(self): | |
""" | |
returns the next generation by observing the 8 cells: | |
(i, j+1) | |
(i, j-1) | |
(i+1, j) | |
(i-1, j) | |
(i+1, j+1) | |
(i+1, j-1 | |
(i-1, j+1) | |
(i-1, j-1) | |
""" | |
self.curr_board = [[0 for i in range(self.N)] for j in range(self.N)] | |
for i in range(0, self.N-1): | |
for j in range(0, self.N-1): | |
cells = [(i, j+1), (i, j-1), (i+1, j), (i-1, j), (i+1, j+1), (i+1, j-1), (i-1, j+1), (i-1, j-1)] | |
cells = [cell for cell in cells if(cell[0] >= 0 and cell[1] >= 0)] | |
live, dead = 0, 0 | |
for cell in cells: | |
if self.prev_board[cell[0]][cell[1]] == 1: | |
live += 1 | |
else: | |
dead += 1 | |
if self.prev_board[i][j] == 1: | |
if live < 2 or live > 3: | |
self.curr_board[i][j] = 0 | |
if live == 2 or live == 3: | |
self.curr_board[i][j] = 1 | |
elif self.prev_board[i][j] == 0: | |
if live == 3: | |
self.curr_board[i][j] = 1 | |
self.prev_board = self.curr_board | |
return self.curr_board | |
def print_board(self): | |
""" | |
[ for command line debuging ] | |
""" | |
for i in range(0, self.N): | |
for j in range(0, self.N): | |
if self.curr_board[i][j] == 1: | |
print(colored(self.curr_board[i][j], "red"), end=' ') | |
else: | |
print(colored(self.curr_board[i][j], "blue"), end=' ') | |
print() | |
def progress(self): | |
""" | |
displays the animation | |
""" | |
# to insert the demo in prev_array | |
row, col = 0, 0 | |
for i in range(self.N//2 - 5, self.N//2 + 5): | |
row = 0 | |
for j in range(self.N//2 - 5, self.N//2 + 5): | |
self.prev_board[i][j] = self.pentadecathlon[row][col] | |
row += 1 | |
col += 1 | |
fig = plt.figure() | |
imgs = [] | |
for gen in range(self.generations): | |
self.next_gen() | |
im = plt.imshow(self.curr_board) | |
imgs.append([im]) | |
ani = animation.ArtistAnimation(fig, imgs, interval=500) | |
plt.axis("off") | |
plt.show() | |
if __name__ == '__main__': | |
obj = Conway() | |
obj.progress() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment