簡體   English   中英

使用 Python Conway 的生命游戲卡住 ATBS

[英]Stuck with ATBS with python Conway's Game of Life

在 ATBS with python 一書的第 4 章列表中有一個簡短的程序:康威的生命游戲。 據我所見,我完全復制了代碼。 當我運行代碼時,它沒有給出錯誤消息。 但我只看到出現空格。 我正在使用書中指出的 Mu 編輯器軟件。

有人可以復制並粘貼我的代碼,看看他們是否比空格更重要?

下面是代碼:

# Conway's Game of Life
import random, time, copy
WIDTH = 60
HEIGHT = 20

# Create a list of list for the cells:
nextCells = []
for x in range(WIDTH):
    column = []  # Create a new column
    for y in range(HEIGHT):
        if random.randint(0, 1) == 0:
            column.append('#')  # add a living cell
        else:
            column.append(' ')  # add a dead cell
    nextCells.append(column)  # NextCells is a list of a column lists.
    
while True:  # Main program loop
    print('\n\n\n\n\n')  # Seperate each step with newlines
    currentCells = copy.deepcopy(nextCells)
    
# Print currentCells on the screen:
for y in range(HEIGHT):
    for x in range(WIDTH):
        print(currentCells[x][y], end='')  # Print the # or space
    print()  # Print a newline at the end of the row

# Calculate the next step's cells based on current steps cells:
for x in range(WIDTH):
    for y in range(HEIGHT):
        # Get neighboring coordinates:
        # '% WIDTH' ensures leftCoord is always between 0 and WIDTH -1
        leftCoord = (x - 1) % WIDTH
        rightCoord = (x + 1) % WIDTH
        aboveCoord = (y - 1) % HEIGHT
        belowCoord = (y + 1) % HEIGHT
        
        # Count number of living neighbors
        numNeighbors = 0
        if currentCells[leftCoord][aboveCoord] == '#':
            numNeighbors += 1  # Top-left neighbor is alive
        if currentCells[x][aboveCoord] == '#':
            numNeighbors += 1  # Top neighbor is alive
        if currentCells[rightCoord][aboveCoord] == '#':
            numNeighbors += 1  # Top-right neighbor is alive
        if currentCells[leftCoord][y] == '#':
            numNeighbors += 1  # Left neighbor is alive
        if currentCells[rightCoord][y] == '#':
            numNeighbors += 1  # Right neighbor is alive
        if currentCells[leftCoord][belowCoord] == '#':
            numNeighbors += 1  # below-left is alive
        if currentCells[x][belowCoord] == '#':
            numNeighbors += 1  # below is alive
        if currentCells[rightCoord][belowCoord] == '#':
            numNeighbors += 1  # below-right is alive
        
        # Set cell based on Conway's Game of Life rules:
        if currentCells[x][y] == '#' and (numNeighbors == 2 or numNeighbors == 3):
            # Living cells with 2 or 3 neighbors stay alive:
            nextCells[x][y] = '#'
        elif currentCells[x][y] == ' ' and numNeighbors == 3:
            # Dead cells with 3 neighbors become alive:
            nextCells[x][y] = '#'
        else:
            # Everything else dies or stays dead
            nextCells[x][y] = ' '

time.sleep(1)  # add a 1 second pause to reduce flickering

我的代碼有什么問題?

您的問題是第一個while True:語句之后的所有內容都應該在循環內,但是,代碼中的縮進(在 Python 中定義范圍)導致循環范圍僅跨越 2 行,而不是整個其余部分文件。

解決方案是在每行代碼的開頭添加一個[TAB]

while True:  # Main program loop
    print('\n\n\n\n\n')  # Seperate each step with newlines
    currentCells = copy.deepcopy(nextCells)

以便

for x in range(WIDTH):
    for y in range(HEIGHT):

等等轉向

    for x in range(WIDTH):
        for y in range(HEIGHT):

等等。 如果您使用適當的代碼編輯器或 IDE,您應該能夠選擇所有這些行並按鍵盤上的[TAB]來解決問題。

我也可以看到你提到的那本書的源代碼完好無損,所以作者肯定已經解決了這個問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM