Created
January 30, 2015 21:18
-
-
Save thinkJD/699afe10e50282bb58cc to your computer and use it in GitHub Desktop.
Game Of Life
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
# coding=utf8 | |
""" | |
Description: This is a basic "life" simulation that follows three distinct rules: | |
1.Any live cell with fewer than two live neighbours dies | |
2.Any live cell with two or three live neighbours lives | |
3.Any live cell with more than three live neighbours dies | |
4.Any dead cell with exactly three live neighbours becomes a live cell | |
A neighbor is deemed as any cell directly horizantal/vertical/diagonal | |
meaning there are 9 neighbors to a cell at any time | |
""" | |
import sys | |
import random | |
import copy | |
import time | |
class GOL(object): | |
def __init__(self, row, col, game_field=None): | |
# Setup Game | |
self.row = row | |
self.col = col | |
self.game_field = game_field | |
if game_field is None: | |
self.game_field = [[random.randint(0, 1) for _ in range(col)] for row in range(row)] | |
def next_generation(self): | |
# i make a temp copy of the play field then i modify the original | |
tmp_game_field = copy.deepcopy(self.game_field) | |
for row in range(self.row): | |
for col in range(self.col): | |
is_alive = self.is_cell_living(row, col) | |
neighbours = self.count_neighbours(row, col) | |
if is_alive: | |
if neighbours < 2: | |
tmp_game_field[row][col] = 0 # cell under population | |
if neighbours == 2 or neighbours == 3: | |
tmp_game_field[row][col] = 1 # keeps alive | |
if neighbours > 3: | |
tmp_game_field[row][col] = 0 # cell over population | |
else: | |
if neighbours == 3: | |
tmp_game_field[row][col] = 1 # cell reborn | |
return tmp_game_field | |
def count_neighbours(self, row, col): | |
count = 0 | |
pos_neighbour_row = (row + 1) % self.row | |
pos_neighbour_col = (col + 1) % self.col | |
count += self.is_cell_living(row - 1, col) # top | |
count += self.is_cell_living(row, pos_neighbour_col) # right | |
count += self.is_cell_living(row, col - 1) # left | |
count += self.is_cell_living(pos_neighbour_row, col) # down | |
count += self.is_cell_living(row - 1, pos_neighbour_col) # top right | |
count += self.is_cell_living(row - 1, col - 1) # top left | |
count += self.is_cell_living(pos_neighbour_row, col - 1) # down left | |
count += self.is_cell_living(pos_neighbour_row, pos_neighbour_col) # down right | |
return count | |
@staticmethod | |
def log_debug(message): | |
print "Debug: {0}".format(message) | |
def is_cell_living(self, row, col): | |
return self.game_field[row][col] | |
def render_ascii(self, iterations=10): | |
for iteration in range(iterations): | |
print "Generation {0}\n#################".format(iteration) | |
for row in self.game_field: | |
living_cell = ' ◉ ' | |
dead_cell = ' ◯ ' | |
line = "" | |
for col in row: | |
if col == 0: | |
line += dead_cell | |
if col == 1: | |
line += living_cell | |
sys.stdout.write("\t" + line + "\n") | |
time.sleep(1) | |
self.game_field = self.next_generation() | |
def main(): | |
game_field = [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0], | |
[0, 0, 1, 0, 0, 0, 0, 0, 0, 0], | |
[0, 0, 0, 1, 0, 0, 0, 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, 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]] | |
game_field_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, 1, 0, 0, 0, 0], | |
[0, 0, 0, 1, 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, 1, 0, 1, 0, 0, 0, 0], | |
[0, 0, 0, 1, 0, 1, 0, 0, 0, 0], | |
[0, 0, 0, 1, 1, 1, 0, 0, 0, 0], | |
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]] | |
game = GOL(20, 20) # len(game_field), len(game_field[0]))#, game_field) | |
game.render_ascii(100) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment