Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from random import getrandbits
- WIDTH = 80
- HEIGHT = 23
- def evolve (c):
- def new_cell(bit, n):
- if bit > 0:
- if n == 2 or n == 3:
- return 1
- else:
- return 0
- else:
- if n == 3:
- return 1
- else:
- return 0
- acc_y = []
- for y in xrange(HEIGHT):
- acc_x = []
- for x in xrange(WIDTH):
- if y == 0 and x == 0:
- neighbors = (c[y][x+1] + c[y-1][x] + c[y-1][x+1])
- elif y == HEIGHT-1 and x == WIDTH-1:
- neighbors = (c[y][x-1] + c[y-1][x-1] + c[y-1][x])
- elif y == 0 and x == WIDTH-1:
- neighbors = (c[y][x-1] + c[y+1][x-1] + c[y+1][x])
- elif y == HEIGHT-1 and x == 0:
- neighbors = (c[y-1][x] + c[y-1][x+1] + c[y][x+1])
- elif y == 0:
- neighbors = (c[y][x-1] + c[y][x+1] +
- c[y+1][x-1] + c[y+1][x] + c[y+1][x+1])
- elif x == 0:
- neighbors = (c[y+1][x] + c[y+1][x+1] +
- c[y][x+1] +
- c[y-1][x] + c[y-1][x+1])
- elif y == HEIGHT-1:
- neighbors = (c[y][x-1] + c[y][x+1] +
- c[y-1][x-1] + c[y-1][x] + c[y-1][x+1])
- elif x == WIDTH-1:
- neighbors = (c[y-1][x] + c[y-1][x-1] +
- c[y][x-1] +
- c[y+1][x] + c[y+1][x-1])
- else:
- neighbors = (c[y-1][x-1] + c[y-1][x] + c[y-1][x+1] +
- c[y][x-1] + c[y][x+1] +
- c[y+1][x-1] + c[y+1][x] + c[y+1][x+1])
- acc_x.append(new_cell(c[y][x],neighbors))
- acc_y.append(acc_x)
- return acc_y
- def print_culture (c):
- for row in c:
- print "".join(['#' if bit > 0 else '_' for bit in row])
- culture = [[int(getrandbits(1)) for x in xrange(WIDTH)]
- for y in xrange(HEIGHT)]
- generation = 0
- while True:
- print_culture(culture)
- usertext = raw_input("Generation " + str(generation) +
- ": Input x to exit, or press Enter to continue. ")
- if usertext == "x":
- break
- else:
- generation +=1
- culture = evolve(culture)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement