Bottom Page

• 0 Vote(s) - 0 Average
• 1
• 2
• 3
• 4
• 5
 Game of Life (neighbors) pawlo392 Programmer named Tim Posts: 16 Threads: 12 Joined: Mar 2019 Reputation: 0 Likes received: 0 #1 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]``` Windspar Minister of Silly Walks Posts: 417 Threads: 13 Joined: Oct 2016 Reputation: 33 Likes received: 72 #2 Jun-08-2019, 09:01 PM 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] ``` 99 percent of computer problems exists between chair and keyboard. hobby-programmer.com ThomasL Spam, spam, eggs, and spam Posts: 242 Threads: 2 Joined: Jun 2019 Reputation: 42 Likes received: 58 #3 Jun-13-2019, 09:37 AM 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)``` ThomasL Spam, spam, eggs, and spam Posts: 242 Threads: 2 Joined: Jun 2019 Reputation: 42 Likes received: 58 #4 Jun-14-2019, 09:06 AM 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) ``` pawlo392 Programmer named Tim Posts: 16 Threads: 12 Joined: Mar 2019 Reputation: 0 Likes received: 0 #5 Jun-20-2019, 02:32 PM 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? Windspar Minister of Silly Walks Posts: 417 Threads: 13 Joined: Oct 2016 Reputation: 33 Likes received: 72 #6 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. 99 percent of computer problems exists between chair and keyboard. hobby-programmer.com « Next Oldest | Next Newest »

Top Page

 Possibly Related Threads... Thread Author Replies Views Last Post [PyGame] Game Logic problem with a "The Game of Life" Replication Coda 2 687 Dec-24-2018, 09:26 AM Last Post: Coda

Forum Jump:

Users browsing this thread: 1 Guest(s)