Game of Life (neighbors) - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Game Development (https://python-forum.io/forum-11.html) +--- Thread: Game of Life (neighbors) (/thread-18976.html) |
Game of Life (neighbors) - pawlo392 - Jun-08-2019 How to count the sum of all eight neighbors for this table?: table = np.random.choice(stan, 100*100, p=[0.2, 0.8]).reshape(100, 100)I started to create a Game of Life and I have problem with it. At first I thought about something like that: for i in range(N): for j in range(N): S = table[i-1][j-1]+table[i][j-1]+table[i+1][j-1] +table[i-1][j]\ +table[i+1][j]+table[i-1][j+1]+table[i][j+1]+table[i+1][j+1] RE: Game of Life (neighbors) - Windspar - Jun-08-2019 Numpy arrays can be slice nicer then that. table[0:3, 0:3]Quick way for game of life import numpy size = 10, 10 table = numpy.random.randint(0, 2, size=size).astype(numpy.uint8) print(table) # neighbor count for x in range(size[0]): for y in range(size[1]): min_x = max(0, x - 1) max_x = min(size[0], x + 2) min_y = max(0, y - 1) max_y = min(size[1], y + 2) # Slicing the table slice_ = table[min_x:max_x, min_y:max_y] print(slice_, ' ', x, y, '\n') # Sum all minus self sum_ = numpy.sum(slice_) - table[x, y] RE: Game of Life (neighbors) - ThomasL - Jun-13-2019 Game of Life neighbors calculation can be done like a 2D convolution in machine learning. I assume it to be the fastest way imho. import numpy as np from scipy.signal import convolve2d size = 10, 10 table = np.random.randint(0, 2, size=size).astype(np.uint8) print(table) kernel = np.array([[1,1,1],[1,0,1],[1,1,1]]).astype(np.uint8) print(kernel) neighbors = convolve2d(table, kernel, 'same') print(neighbors) RE: Game of Life (neighbors) - ThomasL - Jun-14-2019 Using above neighbor calculation a simple Game of Life can be done like this: import numpy as np from scipy.signal import convolve2d import os import time SIZE = 30, 30 def show(table): star = table * ord('*') blank = (table != 1) * ord(' ') field = star + blank for row in field: print(''.join(chr(char) for char in row)) kernel = np.array([[1,1,1],[1,0,1],[1,1,1]]).astype(np.uint8) table = np.random.randint(0, 2, size=SIZE).astype(np.uint8) os.system('cls') print(table) for i in range(100): next_table = table.copy() neighbors = convolve2d(table, kernel, 'same') next_table[neighbors < 2] = 0 next_table[neighbors > 3] = 0 next_table[(neighbors == 3) & (table == 0)] = 1 table = next_table os.system('cls') show(table) time.sleep(.050) RE: Game of Life (neighbors) - pawlo392 - Jun-20-2019 I found something like this: for x in range(S): for y in range(S): total = (tablica[x, (y-1)%S] + tablica[x, (y+1)%S] + tablica[(x-1)%S, y] + tablica[(x+1)%S, y] + tablica[(x-1)%S, (y-1)%S] + tablica[(x-1)%S, (y+1)%S] + tablica[(x+1)%S, (y-1)%S] + tablica[(x+1)%S, (y+1)%S])Where S-size. Could You tell my, why we "check" modulo for each coordinate? RE: Game of Life (neighbors) - Windspar - Jun-20-2019 Modulo here is wrapping the world. -1 % 100 = 99 100 % 100 = 0 -1 for a coord would error. |