Advertisement
Guest User

Untitled

a guest
May 3rd, 2016
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.32 KB | None | 0 0
  1. import sys
  2. import random
  3. import time
  4. import os
  5. import fcntl
  6. import random
  7.  
  8. WIDTH = 40 # How wide is the pattern?
  9. w = WIDTH * [0] # create the current generation
  10. nw = WIDTH * [0] # and the next generation
  11. w[WIDTH/2] = 1 # populate with a single one
  12.  
  13. # How wide is the neighborhood of cells that are
  14. # examined? The traditional Wolfram 1D cellular
  15. # automata uses a neighborhood of 3...
  16. NEIGHBORHOOD=3
  17.  
  18. # rtab is an array for the rule table.
  19. rtab = (2**NEIGHBORHOOD) * [0]
  20. rule = 30
  21.  
  22. # This fills-in rtab (rule table)
  23. # by reverse-reading the binary representation of the rule number (dec)
  24. # in case of rule = 110 => 01101110
  25. # rtab will be [0,1,1,1,0,1,1,0] after this loop
  26. for i in range(2**NEIGHBORHOOD):
  27. if ((2**i) & rule) != 0:
  28. rtab[i] = 1
  29.  
  30. # This produces new generations of w
  31. for y in range(40):
  32. # this will dump the 1s in w as Xs into the terminal
  33. for x in w:
  34. if x == 1:
  35. sys.stdout.write('X')
  36. else:
  37. sys.stdout.write(' ')
  38. sys.stdout.write('n')
  39. # this is the core code I don't really understand
  40. for x in range(WIDTH):
  41. sum = 0
  42. for d in range(NEIGHBORHOOD):
  43. sum = sum + (2**d) * w[(x+d+WIDTH - NEIGHBORHOOD/2) % WIDTH]
  44. nw[x] = rtab[sum]
  45. w, nw = nw, w
  46. # time control
  47. time.sleep(0.07)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement