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


Messages In This Thread
Game of Life (neighbors) - by pawlo392 - Jun-08-2019, 07:52 PM
RE: Game of Life (neighbors) - by Windspar - Jun-08-2019, 09:01 PM
RE: Game of Life (neighbors) - by ThomasL - Jun-13-2019, 09:37 AM
RE: Game of Life (neighbors) - by ThomasL - Jun-14-2019, 09:06 AM
RE: Game of Life (neighbors) - by pawlo392 - Jun-20-2019, 02:32 PM
RE: Game of Life (neighbors) - by Windspar - Jun-20-2019, 03:05 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Game of life Elyur 2 2,090 Mar-30-2020, 10:54 AM
Last Post: Elyur
  [PyGame] Game Logic problem with a "The Game of Life" Replication Coda 2 3,146 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