SilphCoding
SilphCoding

Reputation: 11

I dont know why my Conway's game of life isnt working

import pygame, random, time, copy

w, h = 500, 500
cellsize = 5
cells = []
pygame.init()
clock = pygame.time.Clock()
width, height = (w // cellsize), (h // cellsize)

width = int(width)
height = int(height)

dis = pygame.display.set_mode((w, h))
dis.fill((0, 0, 0))

randomBool = []

cellPc = 0.1
for y in range(height + 1):
    randomBool.append([])
    for x in range(width + 1):
        if random.random() < cellPc:
            randomBool[y].append(True)
        else:
            randomBool[y].append(False)


#sojipo ajs;koojihhasuiio h;asjioasddfoiaidhoiiosaiof
def die():
    global randomBool
    global height
    global width
    temp = copy.deepcopy(randomBool)
    for y in range(height):
        for x in range(width):
            count = 0
            turn=0
            if temp[y - 1][x - 1] == True:
                count += 1
              
            if temp[y - 1][x] == True:
                count += 1
     
            if temp[y - 1][x + 1] == True:
                count += 1

            if temp[y + 1][x - 1] == True:
                count += 1
            
            if temp[y + 1][x] == True:
                count += 1
             
            if temp[y + 1][x + 1] == True:
                count += 1
              
            if temp[y][x - 1] == True:
                count += 1
               
            if temp[y][x + 1] == True:
                count += 1
            turn+=1
          
            if (not temp[y][x]) and count == 3:
                temp[y][x] = True
            if temp and (count > 3 and count < 2):
                temp[y][x] = False
              
            
    randomBool = temp


running = True



while running:
    dis.fill((0, 0, 0))
    clock.tick(30)
    die()
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            quit()

    for y in range(height):
        for x in range(width):
            if randomBool[y][x] == True:
                pygame.draw.rect(dis, (random.randint(0,255), 0, 0),(((x * cellsize),(y * cellsize), cellsize, cellsize)))
    for x in range(0, w, cellsize):
        pygame.draw.line(dis, (0, 0, 0), (x, 0), (x, h))
    for y in range(0, h, cellsize):
        pygame.draw.line(dis, (0, 0, 0), (0, y), (w, y))
    pygame.display.update()

Here is my code. I thought this would delete the cells automatically (as done in the game of life) after 2 cell life cycles but the cells never seem to delete. It just keeps multiplying, I'm not trying to make a cancer cell simulator, I'm trying to make Conway's game of life using pygame. I'm completely lost on what to do, any help would be appreciated.

Upvotes: 0

Views: 55

Answers (1)

jsbueno
jsbueno

Reputation: 110311

You are correctly making a copy of your cell grid in the die function - so that you can count cells on the existing grid, and update the grid for the next step.

The problem is that you are not doing that: as your checks are on the temp grid, they are "seeing" the cells for the next-step: so cells past the first one will "see" the future grid, and this totally messes the rules.

Just change all the checks in die (the "if" expressions) to look at the "is" grid in the randomBool variable, not in the toBe grid in the temp variable.

Actually, no need to copy.deepcopy the grid, you just need a grid of the same size - it will be filled up by the checking of each cell.

    ...
    def die():
        ...
        for y in range(height):
            for x in range(width):
                count = 0
                ...
                if randomBool[y - 1][x - 1]:  # <- here: "look at" the _current_ state, not the one in  "temp"
                    count += 1
                # change all other if statements for counting
                ... 
                # at the end of the count, check the new value, but again
                # looking at the current value in the current grid
                # to know if a cell was already living:
                if randomBool[y][x] and count == 2:
                    temp[y][x] = True
                elif count == 3:
                    temp[y][x] = True
                else:
                    temp[y][x] = False
                ...
         

That said, your program (here, and programming style in general) can improve a lot if you make more use of "functions": this basic programming primitive really, really helps with code readability, writting, maintainability - and not only in places where you know beforehand you will have to repeat code: encapsulating code in functions helps the overall project.

Upvotes: 1

Related Questions