Advertisement
Shlocko

GoL(ish)

Jan 16th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.64 KB | None | 0 0
  1. import time
  2.  
  3. board_size = 6
  4. game_board = []
  5. carryOn = True
  6.  
  7.  
  8. # create board for given board size
  9. for x in range(board_size):
  10.     game_board.append(['-'] * board_size)
  11.  
  12. #sub_game_board = game_board  # Creating substitute board, for mainly debugging
  13.  
  14. # setting board state
  15. game_board[2][2] = 'O'
  16. game_board[2][3] = 'O'
  17. game_board[2][1] = 'O'
  18.  
  19.  
  20. def print_board(board):  # prints board
  21.     for crow in board:
  22.         print(" ".join(crow))
  23.  
  24.  
  25. def check_tile(tile_row, tile_col, neighbors):  # Checks the tiles surrounding the given tile
  26.    
  27.    
  28.     if tile_row > 0 and game_board[tile_row - 1][tile_col] == 'O':
  29.         neighbors += 1
  30.     if tile_row > 0 and tile_col > 0 and game_board[tile_row - 1][tile_col - 1] == 'O':
  31.         neighbors += 1
  32.     if tile_row > 0 and tile_col < board_size - 1 and game_board[tile_row - 1][tile_col + 1] == 'O':
  33.         neighbors += 1
  34.     if tile_col > 0 and game_board[tile_row][tile_col - 1] == 'O':
  35.         neighbors += 1
  36.     if tile_col < board_size-1 and game_board[tile_row][tile_col + 1] == 'O':
  37.         neighbors += 1
  38.     if tile_row < board_size - 1 and game_board[tile_row + 1][tile_col] == 'O':
  39.         neighbors += 1
  40.     if tile_row < board_size - 1 and tile_col > 0 and game_board[tile_row + 1][tile_col - 1] == 'O':
  41.         neighbors += 1
  42.     if tile_row < board_size - 1 and tile_col < board_size - 1 and game_board[tile_row + 1][tile_col + 1] == 'O':
  43.         neighbors += 1
  44.     return neighbors
  45.  
  46.  
  47. def update_tile(tile_row, tile_col):  # updates the list of tiles to be flipped
  48.     neighbors = check_tile(tile_row, tile_col, 0)
  49.    
  50.     if game_board[tile_row][tile_col] == 'O':
  51.         if neighbors < 2 or neighbors > 3:
  52.             #game_board[tile_row][tile_col] = '-'
  53.             to_be_update.append([tile_row, tile_col])
  54.     else:
  55.         if neighbors == 3:
  56.             to_be_update.append([tile_row, tile_col])
  57.    
  58.  
  59. def flip_tile(flip_tile):  # Flips the tiles listed as needing to be flipped
  60.     for x in flip_tile:
  61.         if game_board[x[0]][x[1]] == '-':
  62.             game_board[x[0]][x[1]] = 'O'
  63.         else:
  64.             game_board[x[0]][x[1]] = '-'
  65.    
  66.  
  67.  
  68. print_board(game_board)
  69.  
  70. while carryOn:  # main game loop
  71.     #game_board = sub_game_board  # Setting main board equal to substitute board
  72.     to_be_update = []
  73.     tile_row = 0
  74.     for row in game_board:
  75.         tile_col = 0
  76.        
  77.         for col in row:
  78.             update_tile(tile_row, tile_col)
  79.             tile_col += 1
  80.         tile_row += 1
  81.     print("")  # Spacing
  82.     print("")  # Spacing
  83.     flip_tile(to_be_update)
  84.     print_board(game_board)
  85.     time.sleep(1)  # Delay for debugging purposes
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement