Advertisement
makispaiktis

Conway's Game Of Life

Jun 7th, 2021 (edited)
328
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 11.43 KB | None | 0 0
  1. from random import randrange
  2. from copy import deepcopy
  3.  
  4. # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  5. # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  6. # Function 1
  7. def createBoard(N):
  8.     return [[0 for _ in range(N)] for __ in range(N)]
  9. # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  10. # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  11.  
  12.  
  13. # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  14. # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  15. # Function 2 - Next State: Alive or dead?
  16. def decide(state, statesNear):
  17.     SUM = sum(statesNear)
  18.     nextState = -1000
  19.     if state == 0:
  20.         # It's a dead cell and the only choice for being again alive is 3 live neighbours
  21.         if SUM == 3:
  22.             nextState = 1
  23.         else:
  24.             nextState = 0
  25.     else:
  26.         # It's a live cell. To continue its life, its live neighbours must be 2 or 3
  27.         if SUM == 2 or SUM == 3:
  28.             nextState = 1
  29.         else:
  30.             nextState = 0
  31.     return nextState
  32. # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  33. # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  34.  
  35.  
  36.  
  37. # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  38. # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  39. # Function 3 - Check the neighbours to produce the next generation
  40. def nextGeneration(BOARD):
  41.  
  42.     N = len(BOARD)
  43.     board = createBoard(N)
  44.  
  45.     for x in range(N):
  46.         for y in range(N):
  47.             state = BOARD[x][y]
  48.             # state = 0 ----> dead cell      state = 1 ----> live cell
  49.             # 1. First, I have to check if x or y are in the edges of my matrix
  50.             if x == 0 or x == N - 1 or y == 0 or y == N - 1:
  51.  
  52.                 # Case 1.a - UP
  53.                 if x == 0 and y == 0:
  54.                     # Neighbours are (0,1), (1,0) and (1,1)
  55.                     state1 = BOARD[0][1]
  56.                     state2 = BOARD[1][0]
  57.                     state3 = BOARD[1][1]
  58.                     statesNear = [state1, state2, state3]
  59.                     nextState = decide(state, statesNear)
  60.                     board[x][y] = nextState
  61.  
  62.                 # Case 1.b - UP
  63.                 elif x == 0 and y == N-1:
  64.                     state1 = BOARD[0][N-2]
  65.                     state2 = BOARD[1][N-1]
  66.                     state3 = BOARD[1][N-2]
  67.                     statesNear = [state1, state2, state3]
  68.                     nextState = decide(state, statesNear)
  69.                     board[x][y] = nextState
  70.  
  71.                 # Case 1.c - UP
  72.                 elif x == 0 and (y != 0 or y != N-1):
  73.                     state1 = BOARD[0][y-1]
  74.                     state2 = BOARD[0][y+1]
  75.                     state3 = BOARD[1][y-1]
  76.                     state4 = BOARD[1][y]
  77.                     state5 = BOARD[1][y+1]
  78.                     statesNear = [state1, state2, state3, state4, state5]
  79.                     nextState = decide(state, statesNear)
  80.                     board[x][y] = nextState
  81.  
  82.                 # Case 2.a - DOWN
  83.                 elif x == N-1 and y == 0:
  84.                     # Neighbours are (N-1,1), (N-2,0) and (N-2,1)
  85.                     state1 = BOARD[N-1][1]
  86.                     state2 = BOARD[N-2][0]
  87.                     state3 = BOARD[N-2][1]
  88.                     statesNear = [state1, state2, state3]
  89.                     nextState = decide(state, statesNear)
  90.                     board[x][y] = nextState
  91.  
  92.                 # Case 2.b - DOWN
  93.                 elif x == N-1 and y == N-1:
  94.                     state1 = BOARD[N-1][N-2]
  95.                     state2 = BOARD[N-2][N-1]
  96.                     state3 = BOARD[N-2][N-2]
  97.                     statesNear = [state1, state2, state3]
  98.                     nextState = decide(state, statesNear)
  99.                     board[x][y] = nextState
  100.  
  101.                 # Case 2.c - DOWN
  102.                 elif x == N-1 and (y != 0 or y != N-1):
  103.                     state1 = BOARD[N-1][y-1]
  104.                     state2 = BOARD[N-1][y+1]
  105.                     state3 = BOARD[N-2][y-1]
  106.                     state4 = BOARD[N-2][y]
  107.                     state5 = BOARD[N-2][y+1]
  108.                     statesNear = [state1, state2, state3, state4, state5]
  109.                     nextState = decide(state, statesNear)
  110.                     board[x][y] = nextState
  111.  
  112.                 # Case 3 - LEFT
  113.                 elif y == 0 and (x != 0 and x != N-1):
  114.                     # Number of neighbours is 5
  115.                     state1 = BOARD[x-1][0]
  116.                     state2 = BOARD[x+1][0]
  117.                     state3 = BOARD[x-1][1]
  118.                     state4 = BOARD[x][1]
  119.                     state5 = BOARD[x+1][1]
  120.                     statesNear = [state1, state2, state3, state4, state5]
  121.                     nextState = decide(state, statesNear)
  122.                     board[x][y] = nextState
  123.  
  124.                 # Case 4 - RIGHT
  125.                 elif y == N-1 and (x != 0 and x != N-1):
  126.                     # Number of neighbours is 5
  127.                     state1 = BOARD[x-1][N-1]
  128.                     state2 = BOARD[x+1][N-1]
  129.                     state3 = BOARD[x-1][N-2]
  130.                     state4 = BOARD[x][N-2]
  131.                     state5 = BOARD[x+1][N-2]
  132.                     statesNear = [state1, state2, state3, state4, state5]
  133.                     nextState = decide(state, statesNear)
  134.                     board[x][y] = nextState
  135.  
  136.  
  137.             # 2. Now, it's time to see what happens in the middle where we have 8 neighbours
  138.             else:
  139.                 state1 = BOARD[x-1][y-1]
  140.                 state2 = BOARD[x-1][y]
  141.                 state3 = BOARD[x-1][y+1]
  142.                 state4 = BOARD[x][y-1]
  143.                 state5 = BOARD[x][y+1]
  144.                 state6 = BOARD[x+1][y-1]
  145.                 state7 = BOARD[x+1][y]
  146.                 state8 = BOARD[x+1][y+1]
  147.                 statesNear = [state1, state2, state3, state4, state5, state6, state7, state8]
  148.                 nextState = decide(state, statesNear)
  149.                 board[x][y] = nextState
  150.  
  151.     return board
  152. # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  153. # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  154.  
  155.  
  156.  
  157. # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  158. # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  159. # Function 4 - Find sum of all cells
  160. def findSum(BOARD):
  161.     N = len(BOARD)
  162.     SUM = 0
  163.     for i in range(N):
  164.         SUM1 = 0
  165.         for j in range(N):
  166.             SUM1 += BOARD[i][j]
  167.         SUM += SUM1
  168.     return SUM
  169. # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  170. # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  171.  
  172.  
  173.  
  174. # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  175. # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  176. # Function 5 - Print the generation's image
  177. def generationPrint(BOARD):
  178.     N = len(BOARD)
  179.     for i in range(N):
  180.         result = ""
  181.         for j in range(N):
  182.             result += str(BOARD[i][j]) + "  "
  183.         print(result)
  184.     print()
  185. # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  186. # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  187.  
  188.  
  189.  
  190.  
  191.  
  192. # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  193. # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  194. # Function 6 - Simulate the Conway's Game Of Life for an input BOARD = initial state
  195. def simulate(BOARD):
  196.     counter = 1
  197.     N = 7
  198.     # BOARD = Generation 1
  199.     print("************************************************")
  200.     print("Generation " + str(counter))
  201.     generationPrint(BOARD)
  202.     nextBoard = nextGeneration(BOARD)           # Generation 2 now
  203.  
  204.  
  205.     while (nextBoard != BOARD) and (findSum(nextBoard) != 0):
  206.         counter += 1
  207.         print("************************************************")
  208.         print("Generation " + str(counter))
  209.         generationPrint(nextBoard)
  210.         TEMP = deepcopy(nextBoard)
  211.         BOARD = deepcopy(nextBoard)
  212.         nextBoard = nextGeneration(TEMP)
  213.  
  214.     # If the while-loop ends, that means that one of the above conditions is False
  215.     if nextBoard == BOARD:
  216.         counter += 1
  217.         print("************************************************")
  218.         print("Generation " + str(counter))
  219.         generationPrint(nextBoard)
  220.         print("END! BOARD and nextBoard are equal ----> STABLE STATE")
  221.     elif findSum(nextBoard) == 0:
  222.         counter += 1
  223.         print("************************************************")
  224.         print("Generation " + str(counter))
  225.         generationPrint(nextBoard)
  226.         print("END! This board consists of only dead cells")
  227. # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  228. # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  229.  
  230. # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  231. # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  232. # Function 7 - It will be used for random BOARDS
  233. def randomBoardGenerator(N):
  234.     board = createBoard(N)
  235.     for i in range(N):
  236.         for j in range(N):
  237.             if randrange(2) == 1:
  238.                 board[i][j] = 1     # Otherwise, the element remains 0
  239.     return board
  240. # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  241. # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  242.  
  243.  
  244.  
  245. # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  246. # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  247. # MAIN FUNCTION
  248. # Create BOARD_1 = Standard for every programme run
  249. N = 7
  250. BOARD = createBoard(N)                      # Generation 1 now
  251. BOARD[4][5] = 1
  252. BOARD[5][4] = 1
  253. BOARD[4][4] = 1
  254. BOARD[5][5] = 1
  255. BOARD[N-1][N-1] = 1
  256. simulate(BOARD)
  257. print()
  258.  
  259. # Create BOARD_2 using the random board generator function
  260. print()
  261. N2 = randrange(10, 20)
  262. BOARD2 = randomBoardGenerator(N2)
  263. simulate(BOARD2)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement