无法弄清两个数组为何互相复制

时间:2019-10-26 00:16:43

标签: arrays python-3.x

因此,我正在学习python并尝试编写Conway的生活游戏: (link

我创建了一个网格,可以在其上单击以选择图块,以及一个二维数组,如果图块处于活动状态,则该数组包含值1,否则为0。

然后,我需要计算每个图块的邻居数。为此,我创建了一个函数(voisins_calc)和另一个2D数组,其中包含每个图块的邻居数。

问题是:二维数组“状态”和“ voisins”是彼此的副本,我真的不知道为什么!我认为国家正在复制voisins的价值,但我看不到哪里。

这是代码(尽管有规则,我还是放了整个代码,因为我不知道它出了什么问题,对此表示抱歉)

import pygame
pygame.init()

screen_height = screen_width = 700
win = pygame.display.set_mode((screen_height, screen_width))
run = True

cols = rows = 2
state = []
voisins = []

def state_init():
    line = []
    line_transi = []
    for col in range (cols):
        for row in range (rows):
            line.append(0)
        line_transi = line.copy()
        state.append(line_transi)
        voisins.append(line_transi)
        line.clear()
    return (state)

def voisins_calc (x, y):
    voisin = 0
    for i in range (-1, 2, 1):
        for j in range (-1, 2, 1):
            if state[(y + rows + i) % rows][(x + cols + j) % cols] == 1:
                voisin += 1
    if state[x][y] == 1 :
        voisin -= 1
    return (voisin)


state_init()   

while run : 
    print(state, voisins)
    pygame.time.delay(100)

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    if pygame.mouse.get_pressed()[0]:
        state[round(pygame.mouse.get_pos()[1] // (screen_height / rows))] 
   [round(pygame.mouse.get_pos()[0] // (screen_width / cols))] = 1
    if pygame.mouse.get_pressed()[1]:
        state[round(pygame.mouse.get_pos()[1] // (screen_height / rows))] 
   [round(pygame.mouse.get_pos()[0] // (screen_width / cols))] = 0

    keys = pygame.key.get_pressed()

    if keys[pygame.K_SPACE]:
        for col in range (cols):
            for row in range (rows):
                voisins[row][col] = voisins_calc(row, col)

    win.fill((249, 203, 156))

    for col in range (cols):
        for row in range (rows):
            if state[col][row] == 1 :
                pygame.draw.rect(win, (75, 75, 75), (row * (screen_width / 
rows), col * (screen_height / cols), (screen_width / cols), (screen_height 
/ rows)), 0)

    for row in range (rows) : 
        pygame.draw.line(win, (0, 0, 0),(row * screen_height / rows, 0), 
(row * screen_height / rows, screen_width), 1)
    for col in range (cols):
        pygame.draw.line(win, (0, 0, 0),(0, col * screen_width / cols), 
(screen_width, col * screen_width / cols), 1)
    print(state, voisins)
    pygame.display.update()

pygame.quit()

2 个答案:

答案 0 :(得分:2)

state_init中,您将相同的副本添加到statevoisins中:

state.append(line_transi)  # These are the same lists
voisins.append(line_transi)

如果您希望将它们分开,则需要将副本添加到voisins

不过,我会指出,您正在使它变得比所需的复杂得多。只需创建一个助手即可创建网格,然后调用它两次:

def create_grid(width, height):
    # 2D list comprehension to create nested list
    return [[0 for _ in range(width)] for _ in range(height)]

state = create_grid(cols, rows)
voisins = create_grid(cols, rows)

答案 1 :(得分:1)

line_transi = line.copy()
state.append(line_transi)
voisins.append(line_transi)

在这里,您appendline_transistate都使用相同的数组voisins。替代方法

state.append(line.copy())
voisins.append(line.copy())