neuropercolation/examples/conways_game_of_life.py

68 lines
2.3 KiB
Python
Raw Normal View History

2019-02-23 15:20:10 +00:00
#!/usr/bin/env python3
2019-02-24 11:37:26 +00:00
"""
Copyright 2019 Richard Feistenauer
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
2019-02-23 15:20:10 +00:00
# pylint: disable=wrong-import-position
# pylint: disable=missing-function-docstring
2019-02-23 15:20:10 +00:00
import random
import sys
import os
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
2019-02-23 15:20:10 +00:00
from cellular_automaton import CellularAutomaton, MooreNeighborhood, CAWindow, EdgeRule
2019-02-23 15:20:10 +00:00
2019-02-23 15:20:48 +00:00
ALIVE = [1.0]
DEAD = [0]
class ConwaysCA(CellularAutomaton):
""" Cellular automaton with the evolution rules of conways game of life """
def __init__(self):
super().__init__(dimension=[100, 100],
neighborhood=MooreNeighborhood(EdgeRule.FIRST_AND_LAST_CELL_OF_DIMENSION_ARE_NEIGHBORS))
2019-02-23 15:20:10 +00:00
def init_cell_state(self, __): # pylint: disable=no-self-use
2019-02-23 15:20:48 +00:00
rand = random.randrange(0, 16, 1)
init = max(.0, float(rand - 14))
return [init]
2019-02-23 15:20:10 +00:00
def evolve_rule(self, last_cell_state, neighbors_last_states):
2019-02-23 15:20:48 +00:00
new_cell_state = last_cell_state
alive_neighbours = self.__count_alive_neighbours(neighbors_last_states)
if last_cell_state == DEAD and alive_neighbours == 3:
new_cell_state = ALIVE
if last_cell_state == ALIVE and alive_neighbours < 2:
new_cell_state = DEAD
if last_cell_state == ALIVE and 1 < alive_neighbours < 4:
new_cell_state = ALIVE
if last_cell_state == ALIVE and alive_neighbours > 3:
new_cell_state = DEAD
return new_cell_state
@staticmethod
def __count_alive_neighbours(neighbours):
alive_neighbors = []
2019-02-23 15:20:48 +00:00
for n in neighbours:
if n == ALIVE:
alive_neighbors.append(1)
return len(alive_neighbors)
2019-02-23 15:20:10 +00:00
if __name__ == "__main__":
CAWindow(cellular_automaton=ConwaysCA()).run(evolutions_per_second=40)