Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from random import randrange
- from copy import deepcopy
- # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- # Function 1
- def createBoard(N):
- return [[0 for _ in range(N)] for __ in range(N)]
- # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- # Function 2 - Next State: Alive or dead?
- def decide(state, statesNear):
- SUM = sum(statesNear)
- nextState = -1000
- if state == 0:
- # It's a dead cell and the only choice for being again alive is 3 live neighbours
- if SUM == 3:
- nextState = 1
- else:
- nextState = 0
- else:
- # It's a live cell. To continue its life, its live neighbours must be 2 or 3
- if SUM == 2 or SUM == 3:
- nextState = 1
- else:
- nextState = 0
- return nextState
- # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- # Function 3 - Check the neighbours to produce the next generation
- def nextGeneration(BOARD):
- N = len(BOARD)
- board = createBoard(N)
- for x in range(N):
- for y in range(N):
- state = BOARD[x][y]
- # state = 0 ----> dead cell state = 1 ----> live cell
- # 1. First, I have to check if x or y are in the edges of my matrix
- if x == 0 or x == N - 1 or y == 0 or y == N - 1:
- # Case 1.a - UP
- if x == 0 and y == 0:
- # Neighbours are (0,1), (1,0) and (1,1)
- state1 = BOARD[0][1]
- state2 = BOARD[1][0]
- state3 = BOARD[1][1]
- statesNear = [state1, state2, state3]
- nextState = decide(state, statesNear)
- board[x][y] = nextState
- # Case 1.b - UP
- elif x == 0 and y == N-1:
- state1 = BOARD[0][N-2]
- state2 = BOARD[1][N-1]
- state3 = BOARD[1][N-2]
- statesNear = [state1, state2, state3]
- nextState = decide(state, statesNear)
- board[x][y] = nextState
- # Case 1.c - UP
- elif x == 0 and (y != 0 or y != N-1):
- state1 = BOARD[0][y-1]
- state2 = BOARD[0][y+1]
- state3 = BOARD[1][y-1]
- state4 = BOARD[1][y]
- state5 = BOARD[1][y+1]
- statesNear = [state1, state2, state3, state4, state5]
- nextState = decide(state, statesNear)
- board[x][y] = nextState
- # Case 2.a - DOWN
- elif x == N-1 and y == 0:
- # Neighbours are (N-1,1), (N-2,0) and (N-2,1)
- state1 = BOARD[N-1][1]
- state2 = BOARD[N-2][0]
- state3 = BOARD[N-2][1]
- statesNear = [state1, state2, state3]
- nextState = decide(state, statesNear)
- board[x][y] = nextState
- # Case 2.b - DOWN
- elif x == N-1 and y == N-1:
- state1 = BOARD[N-1][N-2]
- state2 = BOARD[N-2][N-1]
- state3 = BOARD[N-2][N-2]
- statesNear = [state1, state2, state3]
- nextState = decide(state, statesNear)
- board[x][y] = nextState
- # Case 2.c - DOWN
- elif x == N-1 and (y != 0 or y != N-1):
- state1 = BOARD[N-1][y-1]
- state2 = BOARD[N-1][y+1]
- state3 = BOARD[N-2][y-1]
- state4 = BOARD[N-2][y]
- state5 = BOARD[N-2][y+1]
- statesNear = [state1, state2, state3, state4, state5]
- nextState = decide(state, statesNear)
- board[x][y] = nextState
- # Case 3 - LEFT
- elif y == 0 and (x != 0 and x != N-1):
- # Number of neighbours is 5
- state1 = BOARD[x-1][0]
- state2 = BOARD[x+1][0]
- state3 = BOARD[x-1][1]
- state4 = BOARD[x][1]
- state5 = BOARD[x+1][1]
- statesNear = [state1, state2, state3, state4, state5]
- nextState = decide(state, statesNear)
- board[x][y] = nextState
- # Case 4 - RIGHT
- elif y == N-1 and (x != 0 and x != N-1):
- # Number of neighbours is 5
- state1 = BOARD[x-1][N-1]
- state2 = BOARD[x+1][N-1]
- state3 = BOARD[x-1][N-2]
- state4 = BOARD[x][N-2]
- state5 = BOARD[x+1][N-2]
- statesNear = [state1, state2, state3, state4, state5]
- nextState = decide(state, statesNear)
- board[x][y] = nextState
- # 2. Now, it's time to see what happens in the middle where we have 8 neighbours
- else:
- state1 = BOARD[x-1][y-1]
- state2 = BOARD[x-1][y]
- state3 = BOARD[x-1][y+1]
- state4 = BOARD[x][y-1]
- state5 = BOARD[x][y+1]
- state6 = BOARD[x+1][y-1]
- state7 = BOARD[x+1][y]
- state8 = BOARD[x+1][y+1]
- statesNear = [state1, state2, state3, state4, state5, state6, state7, state8]
- nextState = decide(state, statesNear)
- board[x][y] = nextState
- return board
- # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- # Function 4 - Find sum of all cells
- def findSum(BOARD):
- N = len(BOARD)
- SUM = 0
- for i in range(N):
- SUM1 = 0
- for j in range(N):
- SUM1 += BOARD[i][j]
- SUM += SUM1
- return SUM
- # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- # Function 5 - Print the generation's image
- def generationPrint(BOARD):
- N = len(BOARD)
- for i in range(N):
- result = ""
- for j in range(N):
- result += str(BOARD[i][j]) + " "
- print(result)
- print()
- # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- # Function 6 - Simulate the Conway's Game Of Life for an input BOARD = initial state
- def simulate(BOARD):
- counter = 1
- N = 7
- # BOARD = Generation 1
- print("************************************************")
- print("Generation " + str(counter))
- generationPrint(BOARD)
- nextBoard = nextGeneration(BOARD) # Generation 2 now
- while (nextBoard != BOARD) and (findSum(nextBoard) != 0):
- counter += 1
- print("************************************************")
- print("Generation " + str(counter))
- generationPrint(nextBoard)
- TEMP = deepcopy(nextBoard)
- BOARD = deepcopy(nextBoard)
- nextBoard = nextGeneration(TEMP)
- # If the while-loop ends, that means that one of the above conditions is False
- if nextBoard == BOARD:
- counter += 1
- print("************************************************")
- print("Generation " + str(counter))
- generationPrint(nextBoard)
- print("END! BOARD and nextBoard are equal ----> STABLE STATE")
- elif findSum(nextBoard) == 0:
- counter += 1
- print("************************************************")
- print("Generation " + str(counter))
- generationPrint(nextBoard)
- print("END! This board consists of only dead cells")
- # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- # Function 7 - It will be used for random BOARDS
- def randomBoardGenerator(N):
- board = createBoard(N)
- for i in range(N):
- for j in range(N):
- if randrange(2) == 1:
- board[i][j] = 1 # Otherwise, the element remains 0
- return board
- # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- # MAIN FUNCTION
- # Create BOARD_1 = Standard for every programme run
- N = 7
- BOARD = createBoard(N) # Generation 1 now
- BOARD[4][5] = 1
- BOARD[5][4] = 1
- BOARD[4][4] = 1
- BOARD[5][5] = 1
- BOARD[N-1][N-1] = 1
- simulate(BOARD)
- print()
- # Create BOARD_2 using the random board generator function
- print()
- N2 = randrange(10, 20)
- BOARD2 = randomBoardGenerator(N2)
- simulate(BOARD2)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement