簡體   English   中英

康威的人生游戲沒有正確計算鄰居

[英]Conway's Game of Life not counting neighbors correctly

我正在使用Python編寫標准的Conway的“生命游戲”程序。 在遍歷數組時嘗試計算鄰居時遇到問題。 我創建了打印語句,該語句打印連續的if語句以及每個語句的count值。

這是我的代碼:(我在整個代碼中的#內都有問題)

import random

numrows = 10
numcols = 10
def rnd():
    rn = random.randint(0,1)
    return rn

def initial():
    grid = []
    count = 0
    for x in range(numrows):
        grid.append([])
        for y in range(numcols):
            rand=random.randrange(1,3)
            if(rand == 1):
                grid[x].append('O')
            else:
                grid[x].append('-')
    for x in grid:    
        print(*x, sep=' ',end="\n") #this prints the random 2d array 

    print("")
    print("")

    answer = 'y'
    newgrid = []
    count = 0

    while(answer == 'y'): # I believe I am going through, checking neighbors
                          # and moving onto the next index inside these for 
                          #loops below
        for r in range(0,numrows):
            grid.append([])
            for c in range(0,numcols):

                if(r-1 > -1 and c-1 > -1): #I use this to check out of bound
                    if(newgrid[r-1][c-1] == 'O'):#if top left location is O
                        count = count + 1        #should get count += 1
                    else:
                        count = count
                print("top left check complete")
                print(count)
                if(r-1 > -1):
                    if(newgrid[r-1][c] == 'O'):
                        count = count + 1
                    else:
                        count = count
                print("top mid check complete")
                print(count)
                if(r-1 > -1 and c+1 < numcols):
                    if(newgrid[r-1][c+1] == 'O'):
                        count = count + 1
                    else:
                        count = count
                print("top right check complete")
                print(count)
                if(c-1 > -1 and r-1 > -1):
                    if(newgrid[r][c-1] == 'O'):
                        count = count + 1
                    else:
                        count = count
                print("mid left check complete")
                print(count)
                if(r-1 > -1 and c+1 < numcols): 
                    if(newgrid[r][c+1] == 'O'):
                        count = count + 1
                    else:
                        count = count
                print("mid right check complete")
                print(count)
                if(r+1 < numrows and c-1 > -1):
                    if(newgrid[r+1][c-1] == 'O'):
                        count = count + 1
                    else:
                        count = count
                print("bot left check complete")
                print(count)
                if(r+1 < numrows and c-1 > -1):
                    if(newgrid[r+1][c] == 'O'):
                        count = count + 1
                    else:
                        count = count
                print("bot mid check complete")
                print(count)
                if(r+1 < numrows and c+1 < numcols):
                    if(newgrid[r+1][c+1] == 'O'):
                        count = count + 1
                    else:
                        count = count
                print("bot right check complete")
                print(count)

# I am not sure about the formatting of the code below, how do I know that
# the newgrid[r][c] location is changing? should it be according to the for-
# loop above? Or should it get it's own? If so, how could I construct it as
# to not interfere with the other loops and items of them?


            if(newgrid[r][c] == '-' and count == 3):
                newgrid[r][c] ='O'

            elif(newgrid[r][c] == 'O' and count < 2):
                newgrid[r][c] = '-'

            elif(newgrid[r][c] == 'O' and (count == 2 or count == 3)):
                newgrid[r][c] = 'O'

            elif(newgrid[r][c] == 'O' and count > 3):
                newgrid[r][c] = '-'

# I'm also confused how to go about printing out the 'new' grid after each
# element has been evaluated and changed. I do however know that after the
# new grid prints, that I need to assign it to the old grid, so that it can
# be the 'new' default grid. How do I do this? 


        for z in newgrid:
            print(*z, sep=' ',end="\n")


        answer = input("Continue? y or n( lower case only): ")

        newgrid = grid

        if(answer != 'y'):
            print(" Hope you had a great life! Goodbye!")

initial()

這是當前的輸出和錯誤消息:

 >>> initial() - O - - O - - O - - - O - - O - - - OO - O - - O - OO - O O - - O - - OOOO O - OO - - - OO - O - O - O - O - O - O - OOOO - - O - - - - - OOO - - O OO - O - - O - - - - - OOO - O - - - top left check complete 0 top mid check complete 0 top right check complete 0 mid left check complete 0 mid right check complete 0 bot left check complete 0 bot mid check complete 0 Traceback (most recent call last): File "<pyshell#68>", line 1, in <module> initial() File "C:\\Users\\Ted\\Desktop\\combined.py", line 86, in initial if(newgrid[r+1][c+1] == 'O'): IndexError: list index out of range 

當我遍歷隨機數組以查看鄰居是什么時,在檢查bot右鄰居的同時,直到移至[0] [1]為止,它似乎都可以正常工作。

同樣,中右鄰居應該還活着就算+1。 但是,即使繼承了if語句,計數仍為0?

問題1:我怎么可能知道條件是否足以滿足數組所有面的[r] [c]的每個實例?

問題2:我目前的檢查范圍是否最適合我的情況? 有沒有一種方法可以在我檢查值之前進行“全部檢查”?

在這一點上,我已經不知所措。 預先感謝您抽出寶貴時間來回答我的問題

您收到該索引錯誤,因為您的newgrid僅包含一個空行。 然后您在newgrid而不是在grid對鄰居進行測試(如Blckknght在評論中所述)。 我進行了一些修復,但是可以做很多事情來改進此代碼。 看起來它現在正在運行,但是很難確定何時使用隨機Life表單。 :)我建議給您的程序某種方式來使用已知的生活模式,例如眨眼和滑翔傘,以查看它們的行為是否正確。

確保newgrid有效的最簡單方法是從grid復制它。 如果我們只是做newgrid = grid ,那只會使newgrid成為grid對象的另一個名稱。 為了正確復制列表列表,我們需要復制每個內部列表。 我的新代碼使用了copy_grid函數。

我已經修復了計算鄰居的本節中的if測試中的一些小錯誤,並且簡化了從鄰居計數更新單元格的邏輯。 我還壓縮了構成隨機網格的代碼,並添加了一個簡單的函數,該函數可以從字符串中讀取Life模式並從中構建網格。 這使我們可以使用Glider測試代碼。 我還添加了一個使空白網格的函數。 盡管我在測試中使用過該程序,但該程序目前未使用該功能,我想這是一個有用的示例。 :)

import random

# Set a seed so that we get the same random numbers each time we run the program
# This makes it easier to test the program during development
random.seed(42)

numrows = 10
numcols = 10

glider = '''\
----------
--O-------
---O------
-OOO------
----------
----------
----------
----------
----------
----------
'''

# Make an empty grid
def empty_grid():
    return [['-' for y in range(numcols)]
        for x in range(numrows)]

# Make a random grid
def random_grid():
    return [[random.choice('O-') for y in range(numcols)]
        for x in range(numrows)]

# Make a grid from a pattern string
def pattern_grid(pattern):
    return [list(row) for row in pattern.splitlines()]

# Copy a grid, properly!
def copy_grid(grid):
    return [row[:] for row in grid]

# Print a grid
def show_grid(grid):
    for row in grid:
        print(*row)
    print()

def run(grid):
    show_grid(grid)

    # Copy the grid to newgrid.
    newgrid = copy_grid(grid)
    while True:
        for r in range(numrows):
            for c in range(numcols):
                # Count the neighbours, making sure that they are in bounds
                count = 0
                # Above this row
                if(r-1 > -1 and c-1 > -1): 
                    if(grid[r-1][c-1] == 'O'):
                        count += 1
                if(r-1 > -1):
                    if(grid[r-1][c] == 'O'):
                        count += 1
                if(r-1 > -1 and c+1 < numcols):
                    if(grid[r-1][c+1] == 'O'):
                        count += 1

                # On this row
                if(c-1 > -1):
                    if(grid[r][c-1] == 'O'):
                        count += 1
                if(c+1 < numcols): 
                    if(grid[r][c+1] == 'O'):
                        count += 1

                # Below this row
                if(r+1 < numrows and c-1 > -1):
                    if(grid[r+1][c-1] == 'O'):
                        count += 1
                if(r+1 < numrows):
                    if(grid[r+1][c] == 'O'):
                        count += 1
                if(r+1 < numrows and c+1 < numcols):
                    if(grid[r+1][c+1] == 'O'):
                        count += 1

                # Update the cell in the new grid
                if grid[r][c] == '-':
                    if count == 3:
                        newgrid[r][c] ='O'
                else:
                    if count < 2 or count> 3:
                        newgrid[r][c] = '-'

        # Copy the newgrid to grid
        grid = copy_grid(newgrid)
        show_grid(grid)

        answer = input("Continue? [Y/n]: ")
        if not answer in 'yY':
            print(" Hope you had a great life! Goodbye!")
            break

#grid = random_grid()
grid = pattern_grid(glider)
run(grid)

該代碼可以正常工作,但是仍有很大的改進空間。 例如,這是run()的改進版本,它通過使用幾個循環來壓縮鄰居計數部分。

def run(grid):
    show_grid(grid)

    # Copy the grid to newgrid.
    newgrid = copy_grid(grid)
    while True:
        for r in range(numrows):
            for c in range(numcols):
                # Count the neighbours, making sure that they are in bounds
                # This includes the cell itself in the count
                count = 0
                for y in range(max(0, r - 1), min(r + 2, numrows)):
                    for x in range(max(0, c - 1), min(c + 2, numcols)):
                        count += grid[y][x] == 'O'

                # Update the cell in the new grid
                if grid[r][c] == '-':
                    if count == 3:
                        newgrid[r][c] ='O'
                else:
                    # Remember, this count includes the cell itself
                    if count < 3 or count > 4:
                        newgrid[r][c] = '-'

        # Copy the newgrid to grid
        grid = copy_grid(newgrid)
        show_grid(grid)

        answer = input("Continue? [Y/n]: ")
        if not answer in 'yY':
            print(" Hope you had a great life! Goodbye!")
            break

要計算鄰居數,只需遵循以下簡單規則即可。 您將擁有當前單元格的行和列。 使用該表格可以形成兩個數組,一個數組用於行,另一個數組用於列。 以下是您的邏輯。

您可以在此處找到帶有演示視頻的詳細實現。

this.neibhours = function() {
  var rows = [row-1, row, row+1];
  var cols = [col-1, col, col+1];
  neibhourCells = [];

  for(var i=0; i < rows.length; i++) {
    for(var j=0; j < cols.length; j++) {
      if(!(col === cols[j] && row === rows[i])) {
        var cell = new Cell(rows[i], cols[j])
        if(cell.isOnGrid()) {
          neibhourCells.push(cell);
        }
      }
    }
  }

  return neibhourCells;
}

暫無
暫無

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

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