yiorgos icon

rule110.py

yiorgos | PRO | 02/17/19 01:48:32 PM UTC | 0 ⭐ | 703 👁️ | Never ⏰ | []
Python |

1.15 KB

|

None

|

0 👍

/

0 👎

# Let's have some fun with Rule 110:
# https://en.wikipedia.org/wiki/Rule_110
 
import turtle
import random
 
# enumerate next state for Rule 110
ca_rule = [0, 1, 1, 1, 0, 1, 1, 0]
 
# Note we can use the same encoding trick for all 1D CAs. 
# For example, for Rule 54 we can have:
# ca_rule = [0, 1, 1, 0, 1, 1, 0, 0]
 
def next_state(state):
  n = []
  j = len(state)
  for i in range(j):
    # Let's assume that the two edges of the list are connected
    left = i - 1
    right = i + 1 if i < j - 1 else 0
    n.append(ca_rule[4 * state[left] + 2 * state[i] + state[right]])
 
  return n
 
game_len = 120
x_pos = -100
y_pos = 200
 
window = turtle.Screen()
window.bgcolor('light gray')
 
pen = turtle.Turtle()
pen.speed(20)
pen.color('dark blue')
pen.pensize(2)
pen.shape('classic')
 
# create a random starting state 
curr_state = [ random.choice([1, 0]) for x in range(game_len) ]
 
for g in range(game_len):
  pen.penup()
  pen.setx(x_pos)
  pen.sety(y_pos)
  for c in curr_state:
    if c == 1:
      pen.pendown()
    else:
      pen.penup()
 
    pen.forward(3)
  
  curr_state = next_state(curr_state)
  y_pos -= 3

Comments