Posts: 16 Threads: 12 Joined: Mar 2019 Reputation: **0** Likes received: 0 Jun-08-2019, 07:52 PM (This post was last modified: Jun-08-2019, 07:52 PM by pawlo392. Edited 2 times in total.) 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] Posts: 417 Threads: 13 Joined: Oct 2016 Reputation: **33** Likes received: 72 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]
Posts: 242 Threads: 2 Joined: Jun 2019 Reputation: **42** Likes received: 58 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) Posts: 242 Threads: 2 Joined: Jun 2019 Reputation: **42** Likes received: 58 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)
Posts: 16 Threads: 12 Joined: Mar 2019 Reputation: **0** Likes received: 0 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? Posts: 417 Threads: 13 Joined: Oct 2016 Reputation: **33** Likes received: 72 Jun-20-2019, 03:05 PM (This post was last modified: Jun-20-2019, 03:09 PM by Windspar.) Modulo here is wrapping the world. -1 % 100 = 99 100 % 100 = 0
-1 for a coord would error. |