Skaruts

gol_bits.py

Jun 24th, 2018
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.37 KB | None | 0 0
  1. #!/usr/local/bin/python
  2. # -*- coding:latin-1 -*-
  3. # Python 2.7
  4.  
  5. import subprocess       # for 'cls' or 'clear'
  6. import platform
  7. import random
  8. from copy import deepcopy
  9.  
  10. clear = ""
  11. if platform.system() == 'Windows': clear = '@cls'
  12. elif platform.system() == 'Linux': clear = 'clear'  # not tested
  13.  
  14. #------------------------------------------------------------------------------
  15. #       settings
  16. #------------------------------------------------------------------------------
  17. # wait for key press (Enter) after each generation or run continuously
  18. WAIT_FOR_INPUT = True
  19. DEBUG = False
  20.  
  21. generations = 1000
  22. living_cell_glyph = '#'     # if USE_ASCII is true, these glyphs will
  23. dead_cell_glyph   = '.'     # be used to represent the cells (visual mode)
  24.  
  25. GW = 20 # grid width    (MUST MATCH THE CELLMAP BELOW)
  26. GH = 20 # grid height
  27.  
  28. cellmap = [
  29.         [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  30.         [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  31.         [0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  32.         [0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  33.         [0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  34.         [0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  35.         [0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  36.         [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  37.         [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  38.         [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  39.         [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  40.         [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  41.         [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  42.         [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
  43.         [0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
  44.         [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0],
  45.         [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0],
  46.         [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0],
  47.         [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  48.         [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
  49.     ]
  50.  
  51.  
  52. #------------------------------------------------------------------------------
  53. #       core stuff
  54. #------------------------------------------------------------------------------
  55. def main():
  56.     global generations, alive_cells
  57.     cls()
  58.  
  59.     # preliminary neighbor count, since none of the cells has
  60.     # a neighbor count at the begginning
  61.     count_neighbors()
  62.  
  63.     # draw the first generation, or else it'll never be drawn
  64.     g = 0
  65.     draw_cells()
  66.     if WAIT_FOR_INPUT: raw_input()
  67.  
  68.     while g < generations:  # loop
  69.         next_generation()
  70.         draw_cells()
  71.         if WAIT_FOR_INPUT: raw_input()
  72.         g += 1
  73.  
  74. def cls()subprocess.call( clear, shell=True )
  75.  
  76. def draw_cells():
  77.     cls()
  78.     for j in xrange(0, GH):
  79.         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][:] ] )
  80.         else:       print ' '.join( [living_cell_glyph if (ch & 1) else dead_cell_glyph for ch in cellmap[j][:] ] )
  81.     print # leave a blank line
  82.  
  83. def swap_borders():
  84.     cellmap[0], cellmap[GH-1] = cellmap[GH-2], cellmap[1]
  85.     for j in xrange(GH):
  86.         cellmap[j][0], cellmap[j][GW-1] = cellmap[j][GW-2], cellmap[j][1]
  87.  
  88. def clamp(val, lower=None, upper=None):
  89.     return max(lower, min(upper, val))
  90.  
  91. #------------------------------------------------------------------------------
  92. #       generations stuff
  93. #------------------------------------------------------------------------------
  94. def next_generation():
  95.     global old_grid, curr_grid
  96.  
  97.     swap_borders()
  98.     calc_cells()
  99.  
  100. def calc_cells():
  101.     global cellmap, alive_cells
  102.     old_cellmap = deepcopy(cellmap)
  103.  
  104.     for j in xrange(1, GH-1):
  105.         for i in xrange(1, GW-1):
  106.             # if it's not 0, it's alive or has neighbors
  107.             if old_cellmap[j][i] != 0:
  108.                 # neighbor count
  109.                 n = old_cellmap[j][i] >> 1
  110.  
  111.                 if old_cellmap[j][i] & 0x01:    # if it's alive, kill it unless it has 2 or 3 neighbors
  112.                     if (n != 2) and (n != 3):
  113.                         cellmap[j][i] &= ~0x01
  114.                         # inform neighbors this cell is dead
  115.                         cellmap[ j-1 ][ i-1 ] -= 2
  116.                         cellmap[ j-1 ][ i   ] -= 2
  117.                         cellmap[ j-1 ][ i+1 ] -= 2
  118.                         cellmap[ j   ][ i-1 ] -= 2
  119.                         cellmap[ j   ][ i+1 ] -= 2
  120.                         cellmap[ j+1 ][ i-1 ] -= 2
  121.                         cellmap[ j+1 ][ i   ] -= 2
  122.                         cellmap[ j+1 ][ i+1 ] -= 2
  123.                 else:                           # if it's dead, necro it if == 3 neighbors
  124.                     if (n == 3):
  125.                         cellmap[j][i] |= 0x01
  126.                         # inform neighbors this cell is alive
  127.                         cellmap[ j-1 ][ i-1 ] += 2
  128.                         cellmap[ j-1 ][ i   ] += 2
  129.                         cellmap[ j-1 ][ i+1 ] += 2
  130.                         cellmap[ j   ][ i-1 ] += 2
  131.                         cellmap[ j   ][ i+1 ] += 2
  132.                         cellmap[ j+1 ][ i-1 ] += 2
  133.                         cellmap[ j+1 ][ i   ] += 2
  134.                         cellmap[ j+1 ][ i+1 ] += 2
  135.  
  136. def count_neighbors():
  137.     old_cellmap = deepcopy(cellmap)
  138.     for j in xrange(1, GH-1):
  139.         for i in xrange(1, GW-1):
  140.             if old_cellmap[j][i] & 0x01:
  141.                 cellmap[ j-1 ][ i-1 ] += 2
  142.                 cellmap[ j-1 ][ i   ] += 2
  143.                 cellmap[ j-1 ][ i+1 ] += 2
  144.                 cellmap[ j   ][ i-1 ] += 2
  145.                 cellmap[ j   ][ i+1 ] += 2
  146.                 cellmap[ j+1 ][ i-1 ] += 2
  147.                 cellmap[ j+1 ][ i   ] += 2
  148.                 cellmap[ j+1 ][ i+1 ] += 2
  149.  
  150.  
  151. #------------------------------------------------------------------------------
  152. #       run stuff
  153. #------------------------------------------------------------------------------
  154. if __name__ == '__main__':
  155.     main()
Advertisement
Add Comment
Please, Sign In to add comment