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
|
|
|
|
2020-10-20 10:14:05 +00:00
|
|
|
# pylint: disable=wrong-import-position
|
|
|
|
# pylint: disable=missing-function-docstring
|
|
|
|
# pylint: disable=no-self-use
|
|
|
|
|
2019-02-23 15:20:10 +00:00
|
|
|
import random
|
2020-10-20 10:14:05 +00:00
|
|
|
import contextlib
|
|
|
|
import sys
|
|
|
|
import os
|
|
|
|
from typing import Sequence
|
|
|
|
|
|
|
|
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
|
|
|
|
|
|
|
|
from cellular_automaton import CellularAutomaton, MooreNeighborhood, CAWindow, EdgeRule
|
2019-02-23 15:20:10 +00:00
|
|
|
|
|
|
|
|
2020-10-20 10:14:05 +00:00
|
|
|
class StarFallAutomaton(CellularAutomaton):
|
|
|
|
""" Represents an automaton dropping colorful stars """
|
2019-02-23 15:20:10 +00:00
|
|
|
|
2020-10-20 10:14:05 +00:00
|
|
|
def __init__(self):
|
|
|
|
super().__init__(dimension=[100, 100],
|
|
|
|
neighborhood=MooreNeighborhood(EdgeRule.FIRST_AND_LAST_CELL_OF_DIMENSION_ARE_NEIGHBORS))
|
|
|
|
|
|
|
|
def init_cell_state(self, __) -> Sequence:
|
2019-02-23 15:20:10 +00:00
|
|
|
rand = random.randrange(0, 101, 1)
|
|
|
|
init = max(.0, float(rand - 99))
|
2020-10-20 10:14:05 +00:00
|
|
|
return [init * random.randint(0, 3)]
|
|
|
|
|
|
|
|
def evolve_rule(self, __, neighbors_last_states: Sequence) -> Sequence:
|
|
|
|
return self._neighborhood.get_neighbor_by_relative_coordinate(neighbors_last_states, (-1, -1))
|
2019-02-23 15:20:10 +00:00
|
|
|
|
|
|
|
|
2020-10-20 10:14:05 +00:00
|
|
|
def state_to_color(current_state: Sequence) -> Sequence:
|
|
|
|
return 255 if current_state[0] == 1 else 0, \
|
|
|
|
255 if current_state[0] == 2 else 0, \
|
|
|
|
255 if current_state[0] == 3 else 0
|
2019-02-23 15:20:10 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2020-10-20 10:14:05 +00:00
|
|
|
with contextlib.suppress(KeyboardInterrupt):
|
|
|
|
CAWindow(cellular_automaton=StarFallAutomaton(), state_to_color_cb=state_to_color).run()
|