Guest User

Untitled

a guest
Jan 9th, 2015
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.62 KB | None | 0 0
  1. # Needed for sleep.
  2. import time
  3.  
  4. width  = 30
  5. height = 20
  6.  
  7. on  = 1
  8. off = 0
  9.  
  10. max_steps = 100
  11.  
  12. # Get cell at x y on a w h board.
  13. # Wrap coordinates so that -1 maps to 19 on the other side of 20 board.
  14. def wrap_get_cell(x, y, w, h, b):
  15.    if x<0:
  16.       x = x + w
  17.      
  18.    if y<0:
  19.       y = y + h
  20.      
  21.    x = x % w
  22.    y = y % h
  23.    
  24.    return b[y][x]
  25.  
  26. def calculate_new_cell_at(x, y, w, h, board):
  27.    count_neighbors = wrap_get_cell(x+0, y+1, w, h, board) + \
  28.                      wrap_get_cell(x+1, y+1, w, h, board) + \
  29.                      wrap_get_cell(x+1, y+0, w, h, board) + \
  30.                      wrap_get_cell(x+1, y-1, w, h, board) + \
  31.                      wrap_get_cell(x+0, y-1, w, h, board) + \
  32.                      wrap_get_cell(x-1, y-1, w, h, board) + \
  33.                      wrap_get_cell(x-1, y+0, w, h, board) + \
  34.                      wrap_get_cell(x-1, y+1, w, h, board)
  35.                
  36.    living_currently = board[y][x]
  37.    
  38.    if living_currently:
  39.       if count_neighbors in (2,3):
  40.          return on
  41.       else:
  42.          return off
  43.    else:
  44.       if count_neighbors == 3:
  45.          return on
  46.       else:
  47.          return off    
  48.    
  49.  
  50. def print_board(b):
  51.    for row in b:
  52.       for cell in row:
  53.          print(cell, end="") # end="" suppresses newlines.
  54.       print("\n", end="")
  55.    
  56. # Given a list of strings li, put the character at [y][x]
  57. # into the right place in the given board b.
  58. def setboard(b, li):
  59.    for row_n, row in enumerate(li):
  60.       for x, cell in enumerate(row):
  61.          b[row_n][x] = on if (cell == "*") else off;
  62.    
  63. def run_life():
  64.    # Board is a list of rows, each containing a list of cells.
  65.    # Start off all cell off.
  66.    board    = [[off for x in range(width)] for y in range(height)]
  67.    oldboard = [[off for x in range(width)] for y in range(height)]
  68.    
  69.    # Put a glider in the top left.
  70.    setboard(board, ["  *  ", \
  71.                     "   * ", \
  72.                     " *** "])
  73.  
  74.    for step in range(max_steps):
  75.       # Swap boards.
  76.       # We read from the current board (we just printed) now called 'oldboard'.
  77.       # And we write into a new board now called 'board'.
  78.       board, oldboard = oldboard, board
  79.    
  80.       for y in range(height):
  81.          for x in range(width):
  82.             board[y][x] = calculate_new_cell_at(x, y, width, height, oldboard)
  83.        
  84.       print()
  85.       print()
  86.       print()
  87.       print("LIFE AFTER %d STEPS" % step)      
  88.       print_board(board)
  89.    
  90.       time.sleep(0.2)
  91.    
  92. # If we are -running- this file, not just importing its functions
  93. # this bottom bit is run.
  94. if __name__ == "__main__":  
  95.    run_life()
Advertisement
Add Comment
Please, Sign In to add comment