项目作者: CarlosLunaMota

项目描述 :
A borderless implementation of Rule 110
高级语言: Python
项目地址: git://github.com/CarlosLunaMota/Rule110.git
创建时间: 2020-04-15T14:45:51Z
项目社区:https://github.com/CarlosLunaMota/Rule110

开源协议:The Unlicense

下载


Rule 110

A borderless implementation of the Rule 110 cellular automaton.

Code:

  1. def Rule110(universe):
  2. """Performs a single iteration of the Rule 110 in a borderless universe."""
  3. new = set()
  4. for x in universe:
  5. if x+1 not in universe: new.add(x)
  6. if x-1 not in universe: new.add(x); new.add(x-1)
  7. return new
  8. def show(universe, window, alive='■', dead=' ', space=' '):
  9. """Prints a segment of the universe on the screen."""
  10. print(space.join(alive if x in universe else dead for x in range(*window)))

Example:

  1. universe = {-1} # A set containing the indices of living cells
  2. window = (-40, 0) # The segment of the universe that will be shown
  3. iterations = 40 # Number of iterations that will be shown
  4. for i in range(iterations):
  5. show(universe, window)
  6. universe = Rule110(universe)

Output: