康威的生命游戏 - 规则

Conway's Game of Life - rules

下面是我的康威人生游戏代码。我目前正在为不正确的功能而苦苦挣扎——细胞仍在繁殖,而不是灭绝或汇合点。我认为规则功能出了点问题(我认为特定规则没问题?),但我无法弄清楚。如果您知道出了什么问题,我将不胜感激您的帮助。谢谢

import pygame
import sys
import random
from pygame.locals import *

FPS = 10
fpsClock = pygame.time.Clock()

WINDOWSIZE = 500
CELLSIZE = 5
assert WINDOWSIZE % CELLSIZE == 0, "win size must be a multiple of cell"


class Board():

    def __init__(self):

        pygame.init()
        pygame.display.set_caption('Game of Life')
        self.DISPLAYSURF = pygame.display.set_mode((WINDOWSIZE, WINDOWSIZE))
        self.grid = [[0] * (WINDOWSIZE // CELLSIZE) for i in range(WINDOWSIZE // CELLSIZE)]

    def draw(self):
        i = 0
        j = 0

        for x in range(0, WINDOWSIZE, CELLSIZE):
            for y in range(0, WINDOWSIZE, CELLSIZE):

                if self.grid[i][j] == 0:
                    pygame.draw.rect(
                    self.DISPLAYSURF, (20, 120, 20), Rect((x, y), (CELLSIZE, CELLSIZE)))

                else:
                    pygame.draw.rect(
                    self.DISPLAYSURF, (255, 255, 255), Rect((x, y), (CELLSIZE, CELLSIZE)))

                if j == (WINDOWSIZE // CELLSIZE) - 1:
                    j = 0

                else:
                    j = j + 1

            i = i + 1

    def randomize(self):
        for i in range(CELLSIZE, (WINDOWSIZE // CELLSIZE) - CELLSIZE):
            for j in range(CELLSIZE, (WINDOWSIZE // CELLSIZE) - CELLSIZE):

                if random.randint(0, 100) < 15:
                    self.grid[i][j] = 1


    def rules(self):
        for i in range(CELLSIZE, (WINDOWSIZE // CELLSIZE) - CELLSIZE):
            for j in range(CELLSIZE, (WINDOWSIZE // CELLSIZE) - CELLSIZE):

                neighbors = 0

                if self.grid[i][j] == 0:
                    neighbors = self.grid[i + 1][j] + self.grid[i - 1][j] + self.grid[i][j + 1] + self.grid[i][j - 1] + self.grid[i - 1][j - 1] + self.grid[i + 1][j + 1] + self.grid[i + 1][j - 1] + self.grid[i - 1][j + 1]

                    if neighbors == 3:
                        self.grid[i][j] = 1
                        continue
                    else:
                        self.grid[i][j] = 0

                if self.grid[i][j] == 1:
                    neighbors = self.grid[i + 1][j] + self.grid[i - 1][j] + self.grid[i][j + 1] + self.grid[i][j - 1] + self.grid[i - 1][j - 1] + self.grid[i + 1][j + 1] + self.grid[i + 1][j - 1] + self.grid[i - 1][j + 1]

                    if neighbors < 2:
                        self.grid[i][j] = 0

                    elif neighbors > 3:
                        self.grid[i][j] = 0

                    else:
                        self.grid[i][j] = 1


board = Board()
board.randomize()

while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()

    board.rules()
    board.draw()
    pygame.display.update()
    fpsClock.tick(FPS)

您的错误 - 当您仍然需要原始值来计算其他单元格时,您更改了 grid 中单元格中的值。您无法更改原始 grid 中的值。您必须将新值放入新 new_grid 并在末尾替换网格。

import pygame
import sys
import random

# --- constanst ---

FPS = 10
WINDOWSIZE = 500
CELLSIZE = 5

assert WINDOWSIZE % CELLSIZE == 0, "win size must be a multiple of cell"

# --- classes ---

class Board():

    def __init__(self):
        pygame.init()
        self.screen = pygame.display.set_mode((WINDOWSIZE, WINDOWSIZE))
        pygame.display.set_caption('Game of Life')

        self.grid = [[0] * (WINDOWSIZE // CELLSIZE) for i in range(WINDOWSIZE // CELLSIZE)]


    def draw(self):

        for i, x in enumerate(range(0, WINDOWSIZE, CELLSIZE)):
            for j, y in enumerate(range(0, WINDOWSIZE, CELLSIZE)):

                if self.grid[i][j] == 0:
                    color = (20, 120, 20)
                else:
                    color = (255, 255, 255)

                pygame.draw.rect(self.screen, color, pygame.Rect((x, y), (CELLSIZE, CELLSIZE)))


    def randomize(self):

        for i in range(CELLSIZE, (WINDOWSIZE // CELLSIZE) - CELLSIZE):
            for j in range(CELLSIZE, (WINDOWSIZE // CELLSIZE) - CELLSIZE):
                if random.randint(0, 100) < 15:
                    self.grid[i][j] = 1


    def rules(self):
        # create new grid
        new_grid = [[0] * (WINDOWSIZE // CELLSIZE) for i in range(WINDOWSIZE // CELLSIZE)]

        # put results in new grid
        for i in range(CELLSIZE, (WINDOWSIZE // CELLSIZE) - CELLSIZE):
            for j in range(CELLSIZE, (WINDOWSIZE // CELLSIZE) - CELLSIZE):

                neighbors = self.grid[i + 1][j] + self.grid[i - 1][j] + self.grid[i][j + 1] + self.grid[i][j - 1] + self.grid[i - 1][j - 1] + self.grid[i + 1][j + 1] + self.grid[i + 1][j - 1] + self.grid[i - 1][j + 1]

                if self.grid[i][j] == 0:
                    if neighbors == 3:
                        new_grid[i][j] = 1
                    else:
                        new_grid[i][j] = 0
                elif self.grid[i][j] == 1:
                    if neighbors < 2:
                        new_grid[i][j] = 0
                    elif neighbors > 3:
                        new_grid[i][j] = 0
                    else:
                        new_grid[i][j] = 1

        # replace grid
        self.grid = new_grid


    def mainloop(self):
        fps_clock = pygame.time.Clock()

        while True:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    pygame.quit()
                    sys.exit()
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_ESCAPE:
                        pygame.quit()
                        sys.exit()

            self.rules()
            self.draw()
            pygame.display.update()

            fps_clock.tick(FPS)        

# --- main ---

board = Board()
board.randomize()
board.mainloop()

在移植到下一代之前不要修改细胞!以下是您需要遵循的简单算法。您可以查看详细的实现和视频演示 here

  1. 创建一个代表您的单元格的网格。
  2. 细胞可以是死的也可以是活的。所以有两个css classes,一个活的,一个死的。
  3. 找出单元格的邻居。
  4. 应用以上规则并计算出细胞在下一代中是活的还是死的。
  5. 存储需要移动到下一代的细胞。
  6. 将无法存活或将在下一代死亡的细胞储存起来。
  7. 遍历移动到下一代的细胞列表,并通过向它们添加 alive css class 将它们标记为 alive
  8. 迭代将在下一代死亡的细胞列表,并通过向它们添加死亡 css class 将它们标记为死亡。
  9. 运行 3-8 的步骤在一个小的时间间隔后重新开始。