简体   繁体   中英

I'm trying to make my own Game of Life (conway) but it doesn't work, the patterns turn out wrong... can someone help me?

I have following 2 files;

GOL_CORE

from random import choice
import pygame

RECT_COLOR = [200, 200, 0]

class GOL_CLASS:
    def __init__(self, SQUARED_LENGTH):
        self.SYSTEM = {(x, y):choice([False, False, False, False, True]) for x in range(SQUARED_LENGTH) for y in range(SQUARED_LENGTH)}

    def activate(self, TEMPORARY_DICTIONARY, X, Y):
        RETURN_VALUE = TEMPORARY_DICTIONARY
        RETURN_VALUE[(X, Y)] = True
        return RETURN_VALUE
    
    def deactivate(self, TEMPORARY_DICTIONARY, X, Y):
        RETURN_VALUE = TEMPORARY_DICTIONARY
        RETURN_VALUE[(X, Y)] = False
        return RETURN_VALUE
    
    def check(self, X, Y):
        Counter = 0
        a = [(X+1,Y+1),(X,Y+1),(X-1,Y+1),(X+1,Y),(X-1,Y),(X+1,Y-1),(X,Y-1),(X-1,Y-1)]
        for i in range(8):
            if a[i] in self.SYSTEM:
                if self.SYSTEM[a[i]] == True:
                    Counter += 1
        return Counter

    def step(self, SCREEN):
        TEMPORARY_DICTIONARY = self.SYSTEM
        for x, y in self.SYSTEM:
            TEMP = self.check(x, y)
            if self.SYSTEM[(x, y)] == False:
                if TEMP == 3:                       #Rule 4
                    TEMPORARY_DICTIONARY = self.activate(TEMPORARY_DICTIONARY, x, y)
            else:
                if TEMP < 2:                        #Rule 1
                    TEMPORARY_DICTIONARY = self.deactivate(TEMPORARY_DICTIONARY, x, y)
                elif (TEMP == 2) or (TEMP == 3):    #Rule 2
                    pass
                elif TEMP > 3:                      #Rule 3
                    TEMPORARY_DICTIONARY = self.deactivate(TEMPORARY_DICTIONARY, x, y)
                else:
                    raise Exception("You found a bug, congratulation!")
        self.apply(SCREEN, TEMPORARY_DICTIONARY)    
    
    def apply(self, SCREEN, DICT):
        for x, y in  DICT:
            if self.SYSTEM[(x, y)] == True:
                pygame.draw.rect(SCREEN, RECT_COLOR, [(x*10), (y*10), 9, 9], 0)

main

import GOL_CORE as CORE
import pygame, time, random

MAIN_SYSTEM = CORE.GOL_CLASS(100)

pygame.init()

WIDTH, HEIGHT = 1000, 1000
screen = pygame.display.set_mode((WIDTH, HEIGHT))

RUNNING = True
while RUNNING:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            RUNNING = False

    MAIN_SYSTEM.step(screen)

    pygame.display.update()
    screen.fill([0, 0, 0])
    time.sleep(2)
pygame.quit()

I found out about this bug when I tried to make a blinker. In case you don't know what that is, it's an 1 by 3 that rotates. Sadly, it always ended up as a cube. But why? I changed a lot of things, rewrote all of my code, but it didn't work.

TEMPORARY_DICTIONARY = self.SYSTEM does not create a copy of the system. It just creates a reference to the same object. You have to create a copy of the data:

TEMPORARY_DICTIONARY = self.SYSTEM.copy()

After executing the algorithm, the copy is the new system ( self.SYSTEM = TEMPORARY_DICTIONARY ):

class GOL_CLASS:
    # [...]

    def step(self, SCREEN):
        TEMPORARY_DICTIONARY = self.SYSTEM.copy()
        for x, y in self.SYSTEM:
            TEMP = self.check(x, y)
            if self.SYSTEM[(x, y)] == False:
                if TEMP == 3:                       #Rule 4
                    TEMPORARY_DICTIONARY = self.activate(TEMPORARY_DICTIONARY, x, y)
            else:
                if TEMP < 2:                        #Rule 1
                    TEMPORARY_DICTIONARY = self.deactivate(TEMPORARY_DICTIONARY, x, y)
                elif (TEMP == 2) or (TEMP == 3):    #Rule 2
                    pass
                elif TEMP > 3:                      #Rule 3
                    TEMPORARY_DICTIONARY = self.deactivate(TEMPORARY_DICTIONARY, x, y)
                else:
                    raise Exception("You found a bug, congratulation!")
        self.SYSTEM = TEMPORARY_DICTIONARY
        self.apply(SCREEN)    
    
    def apply(self, SCREEN):
        for x, y in  self.SYSTEM:
            if self.SYSTEM[(x, y)] == True:
                pygame.draw.rect(SCREEN, RECT_COLOR, [(x*10), (y*10), 9, 9], 0)

See also Pygame is running game of life very slowly and Game of life

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM