Python Forum
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]
Reply
#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.
Reply
#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)
Reply
#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)
Reply
#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?
Reply
#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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Game of life Elyur 2 2,014 Mar-30-2020, 10:54 AM
Last Post: Elyur
  [PyGame] Game Logic problem with a "The Game of Life" Replication Coda 2 3,095 Dec-24-2018, 09:26 AM
Last Post: Coda

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020