Advertisement
Guest User

Untitled

a guest
Feb 18th, 2017
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.33 KB | None | 0 0
  1. from random import getrandbits
  2. WIDTH = 80
  3. HEIGHT = 23
  4.  
  5. def evolve (c):
  6.     def new_cell(bit, n):
  7.         if bit > 0:
  8.             if n == 2 or n == 3:
  9.                 return 1
  10.             else:
  11.                 return 0
  12.         else:
  13.             if n == 3:
  14.                 return 1
  15.             else:
  16.                 return 0
  17.     acc_y = []        
  18.     for y in xrange(HEIGHT):
  19.         acc_x = []
  20.         for x in xrange(WIDTH):
  21.             if y == 0 and x == 0:
  22.                 neighbors = (c[y][x+1] + c[y-1][x] + c[y-1][x+1])
  23.             elif y == HEIGHT-1 and x == WIDTH-1:
  24.                 neighbors = (c[y][x-1] + c[y-1][x-1] + c[y-1][x])
  25.             elif y == 0 and x == WIDTH-1:
  26.                 neighbors = (c[y][x-1] + c[y+1][x-1] + c[y+1][x])
  27.             elif y == HEIGHT-1 and x == 0:
  28.                 neighbors = (c[y-1][x] + c[y-1][x+1] + c[y][x+1])
  29.             elif y == 0:
  30.                 neighbors = (c[y][x-1] + c[y][x+1] +
  31.                              c[y+1][x-1] + c[y+1][x] + c[y+1][x+1])
  32.             elif x == 0:
  33.                 neighbors = (c[y+1][x] + c[y+1][x+1] +
  34.                              c[y][x+1] +
  35.                              c[y-1][x] + c[y-1][x+1])
  36.             elif y == HEIGHT-1:
  37.                 neighbors = (c[y][x-1] + c[y][x+1] +
  38.                              c[y-1][x-1] + c[y-1][x] + c[y-1][x+1])
  39.             elif x == WIDTH-1:
  40.                 neighbors = (c[y-1][x] + c[y-1][x-1] +
  41.                              c[y][x-1] +
  42.                              c[y+1][x] + c[y+1][x-1])
  43.             else:
  44.                 neighbors = (c[y-1][x-1] + c[y-1][x] + c[y-1][x+1] +
  45.                              c[y][x-1] + c[y][x+1] +
  46.                              c[y+1][x-1] + c[y+1][x] + c[y+1][x+1])
  47.             acc_x.append(new_cell(c[y][x],neighbors))
  48.         acc_y.append(acc_x)
  49.     return acc_y
  50.  
  51. def print_culture (c):
  52.     for row in c:
  53.         print "".join(['#' if bit > 0 else '_' for bit in row])
  54.  
  55. culture = [[int(getrandbits(1)) for x in xrange(WIDTH)]
  56.            for y in xrange(HEIGHT)]
  57.  
  58. generation = 0
  59.  
  60. while True:
  61.     print_culture(culture)
  62.     usertext = raw_input("Generation " + str(generation) +
  63.                          ": Input x to exit, or press Enter to continue. ")
  64.     if usertext == "x":
  65.         break
  66.     else:
  67.         generation +=1
  68.         culture = evolve(culture)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement