Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Game of Life (neighbors)
#1
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]
Quote
#2
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
Quote
#3
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)
Quote
#4
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)
Quote
#5
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?
Quote
#6
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
Quote

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)