Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/local/bin/python
- # -*- coding:latin-1 -*-
- # Python 2.7
- import subprocess # for 'cls' or 'clear'
- import platform
- import random
- from copy import deepcopy
- clear = ""
- if platform.system() == 'Windows': clear = '@cls'
- elif platform.system() == 'Linux': clear = 'clear' # not tested
- #------------------------------------------------------------------------------
- # settings
- #------------------------------------------------------------------------------
- # wait for key press (Enter) after each generation or run continuously
- WAIT_FOR_INPUT = True
- DEBUG = False
- generations = 1000
- living_cell_glyph = '#' # if USE_ASCII is true, these glyphs will
- dead_cell_glyph = '.' # be used to represent the cells (visual mode)
- GW = 20 # grid width (MUST MATCH THE CELLMAP BELOW)
- GH = 20 # grid height
- cellmap = [
- [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
- [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
- [0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
- [0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
- [0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
- [0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
- [0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
- [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
- [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
- [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
- [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
- [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
- [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
- [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
- [0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
- [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0],
- [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0],
- [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0],
- [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
- [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
- ]
- #------------------------------------------------------------------------------
- # core stuff
- #------------------------------------------------------------------------------
- def main():
- global generations, alive_cells
- cls()
- # preliminary neighbor count, since none of the cells has
- # a neighbor count at the begginning
- count_neighbors()
- # draw the first generation, or else it'll never be drawn
- g = 0
- draw_cells()
- if WAIT_FOR_INPUT: raw_input()
- while g < generations: # loop
- next_generation()
- draw_cells()
- if WAIT_FOR_INPUT: raw_input()
- g += 1
- def cls(): subprocess.call( clear, shell=True )
- def draw_cells():
- cls()
- for j in xrange(0, GH):
- if DEBUG: print ' '.join( [living_cell_glyph if (ch & 0x01) else dead_cell_glyph if (ch >> 1) < 1 else '-' if (ch >> 1) < 2 else '~' if (ch >> 1) < 4 else '+' for ch in cellmap[j][:] ] )
- else: print ' '.join( [living_cell_glyph if (ch & 1) else dead_cell_glyph for ch in cellmap[j][:] ] )
- print # leave a blank line
- def swap_borders():
- cellmap[0], cellmap[GH-1] = cellmap[GH-2], cellmap[1]
- for j in xrange(GH):
- cellmap[j][0], cellmap[j][GW-1] = cellmap[j][GW-2], cellmap[j][1]
- def clamp(val, lower=None, upper=None):
- return max(lower, min(upper, val))
- #------------------------------------------------------------------------------
- # generations stuff
- #------------------------------------------------------------------------------
- def next_generation():
- global old_grid, curr_grid
- swap_borders()
- calc_cells()
- def calc_cells():
- global cellmap, alive_cells
- old_cellmap = deepcopy(cellmap)
- for j in xrange(1, GH-1):
- for i in xrange(1, GW-1):
- # if it's not 0, it's alive or has neighbors
- if old_cellmap[j][i] != 0:
- # neighbor count
- n = old_cellmap[j][i] >> 1
- if old_cellmap[j][i] & 0x01: # if it's alive, kill it unless it has 2 or 3 neighbors
- if (n != 2) and (n != 3):
- cellmap[j][i] &= ~0x01
- # inform neighbors this cell is dead
- cellmap[ j-1 ][ i-1 ] -= 2
- cellmap[ j-1 ][ i ] -= 2
- cellmap[ j-1 ][ i+1 ] -= 2
- cellmap[ j ][ i-1 ] -= 2
- cellmap[ j ][ i+1 ] -= 2
- cellmap[ j+1 ][ i-1 ] -= 2
- cellmap[ j+1 ][ i ] -= 2
- cellmap[ j+1 ][ i+1 ] -= 2
- else: # if it's dead, necro it if == 3 neighbors
- if (n == 3):
- cellmap[j][i] |= 0x01
- # inform neighbors this cell is alive
- cellmap[ j-1 ][ i-1 ] += 2
- cellmap[ j-1 ][ i ] += 2
- cellmap[ j-1 ][ i+1 ] += 2
- cellmap[ j ][ i-1 ] += 2
- cellmap[ j ][ i+1 ] += 2
- cellmap[ j+1 ][ i-1 ] += 2
- cellmap[ j+1 ][ i ] += 2
- cellmap[ j+1 ][ i+1 ] += 2
- def count_neighbors():
- old_cellmap = deepcopy(cellmap)
- for j in xrange(1, GH-1):
- for i in xrange(1, GW-1):
- if old_cellmap[j][i] & 0x01:
- cellmap[ j-1 ][ i-1 ] += 2
- cellmap[ j-1 ][ i ] += 2
- cellmap[ j-1 ][ i+1 ] += 2
- cellmap[ j ][ i-1 ] += 2
- cellmap[ j ][ i+1 ] += 2
- cellmap[ j+1 ][ i-1 ] += 2
- cellmap[ j+1 ][ i ] += 2
- cellmap[ j+1 ][ i+1 ] += 2
- #------------------------------------------------------------------------------
- # run stuff
- #------------------------------------------------------------------------------
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment