Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Game of life
#1
Hello !
I have a problem with my game of life. To move from one step to the next, I execute this code :
import numpy as np

def pulsar():
    X = np.zeros((17, 17))
    X[2, 4:7] = 1
    X[4:7, 7] = 1
    X += X.T
    X += X[:, ::-1]
    X += X[::-1, :]
    return X
p=pulsar()

grid=np.zeros([22,22])
for i in range(np.shape(p)[0]):
    for j in range(np.shape(p)[1]): 
        grid[i+int((np.shape(grid)[0]-np.shape(p)[0])/2)][j+int((np.shape(grid)[1]-np.shape(p)[1])/2)]=p[i][j] 

class game_of_life():
    
    def __init__(self):
        self.automaton()
        
    def automaton(self):
        new=np.zeros_like(grid)
        l=np.shape(new)[0]
        c=np.shape(new)[1]
        for i in range(l):
            for i in range(c):
                if l-1>=0 and c-1>=0 and l+1<=np.shape(new)[0] and c+1<=np.shape(new)[1]:
                    sum_n=grid[l-1][c-1]+grid[l-1][c]+grid[l-1][c+1]+grid[l][c+1]+grid[l+1][c+1]+grid[l+1][c]+grid[l+1][c-1]+grid[l][c-1]
                    if grid[l][c]==0 and sum_n==3:
                        self.new[l][c]=1
                    if grid[l][c]==1:
                        if sum_n>3:
                            self.new[l][c]=0
                        elif 2<=sum_n<=3:
                            self.new[l][c]=1
                        elif sum_n<2:
                            self.new[l][c]=0
        print(new)

game_of_life()
The problem is that my new array is full of zeros !
Could you please help me ? Thanks you very much !
Reply
#2
I trace back the code and found that: your problem begin from line:
sum_n=grid[l-1][c-1]+grid[l-1][c]+grid[l-1][c+1]+grid[l][c+1]+grid[l+1][c+1]+grid[l+1][c]+grid[l+1][c-1]+grid[l][c-1]

The problem is that: The shape is 22 => it run from 0 to 21 => It cannot index the 22 row ( l = c = 22)

So basically sum_n will return nothing
Reply
#3
Thanks !
But I tried a new thing and it still not working...
...
        for i in range(l):
            for i in range(c):
                if i-1>=0 and j-1>=0 and i+1<=np.shape(self.new)[0] and j+1<=np.shape(self.new)[1]:
                    sum_n=grid[i-1][j-1]+grid[i-1][j]+grid[i-1][j+1]+grid[i][j+1]+grid[i+1][j+1]+grid[i+1][j]+grid[i+1][j-1]+grid[i][j-1]
                    if grid[i][j]==0 and sum_n==3:
                        self.new[i][j]=1
                    if grid[i][j]==1:
                        if sum_n>3:
                            self.new[i][j]=0
                        elif 2<=sum_n<=3:
                            self.new[i][j]=1
                        elif sum_n<2:
                            self.new[i][j]=0
        return self.new
...
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Game of Life (neighbors) pawlo392 5 3,961 Jun-20-2019, 03:05 PM
Last Post: Windspar
  [PyGame] Game Logic problem with a "The Game of Life" Replication Coda 2 3,132 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