• API
• FAQ
• Tools
• Archive
SHARE
TWEET

# rule110.py yiorgos   Feb 17th, 2019 131 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. # Let's have some fun with Rule 110:
2. # https://en.wikipedia.org/wiki/Rule_110
3.
4. import turtle
5. import random
6.
7. # enumerate next state for Rule 110
8. ca_rule = [0, 1, 1, 1, 0, 1, 1, 0]
9.
10. # Note we can use the same encoding trick for all 1D CAs.
11. # For example, for Rule 54 we can have:
12. # ca_rule = [0, 1, 1, 0, 1, 1, 0, 0]
13.
14. def next_state(state):
15.   n = []
16.   j = len(state)
17.   for i in range(j):
18.     # Let's assume that the two edges of the list are connected
19.     left = i - 1
20.     right = i + 1 if i < j - 1 else 0
21.     n.append(ca_rule[4 * state[left] + 2 * state[i] + state[right]])
22.
23.   return n
24.
25. game_len = 120
26. x_pos = -100
27. y_pos = 200
28.
29. window = turtle.Screen()
30. window.bgcolor('light gray')
31.
32. pen = turtle.Turtle()
33. pen.speed(20)
34. pen.color('dark blue')
35. pen.pensize(2)
36. pen.shape('classic')
37.
38. # create a random starting state
39. curr_state = [ random.choice([1, 0]) for x in range(game_len) ]
40.
41. for g in range(game_len):
42.   pen.penup()
43.   pen.setx(x_pos)
44.   pen.sety(y_pos)
45.   for c in curr_state:
46.     if c == 1:
47.       pen.pendown()
48.     else:
49.       pen.penup()
50.
51.     pen.forward(3)
52.
53.   curr_state = next_state(curr_state)
54.   y_pos -= 3
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.

Top