康威的生命游戏Python给出了错误的输出 [英] Conway's game of life Python giving the wrong output

查看:88
本文介绍了康威的生命游戏Python给出了错误的输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用python快速演示Conway的生命游戏。当我运行下面写的代码它似乎有效,但在特定情况下使用时,如振荡器,一切都崩溃了。



(我有示例输出如下)



我尝试过:



I'm trying to make a quick demo of Conway's Game of life using python. When I run the code I've written below It seems to work, but when used in specific cases, like with an oscillator, everything falls apart.

(I have example outputs below)

What I have tried:

import time
import os

def main():
    oldRows=[["-","-","-","-","-","-","-","-","-","-"],
             ["-","-","-","-","-","-","-","-","-","-"],
             ["-","-","-","-","-","-","-","-","-","-"],
             ["-","-","-","-","+","-","-","-","-","-"],
             ["-","-","-","-","+","-","-","-","-","-"],
             ["-","-","-","-","+","-","-","-","-","-"],
             ["-","-","-","-","-","-","-","-","-","-"],
             ["-","-","-","-","-","-","-","-","-","-"],
             ["-","-","-","-","-","-","-","-","-","-"],
             ["-","-","-","-","-","-","-","-","-","-"]]

    newRows=[["-","-","-","-","-","-","-","-","-","-"],
             ["-","-","-","-","-","-","-","-","-","-"],
             ["-","-","-","-","-","-","-","-","-","-"],
             ["-","-","-","-","-","-","-","-","-","-"],
             ["-","-","-","-","-","-","-","-","-","-"],
             ["-","-","-","-","-","-","-","-","-","-"],
             ["-","-","-","-","-","-","-","-","-","-"],
             ["-","-","-","-","-","-","-","-","-","-"],
             ["-","-","-","-","-","-","-","-","-","-"],
             ["-","-","-","-","-","-","-","-","-","-"]]

    printBoard(oldRows)
    for i in range(5):
        cycle(oldRows, newRows)
        oldRows=newRows
        time.sleep(.2)
        os.system('clear')
    cycle(oldRows, newRows)


def printBoard(rows):
    print("______________________________")
    for r in range(len(rows)):
        for i in range(len(rows)):
            print(" " + rows[r][i] + " ", end="")
        print("")
    print("______________________________")


def checkPop(oldRows, r, i):
    popCount = 0

    # check the top row
    if (r != 0):
        if (i != 0):
            if (oldRows[r-1][i-1] == "+"):
                popCount+=1
        if (oldRows[r-1][i] == "+"):
                popCount+=1
        if (i != len(oldRows)-1):
            if (oldRows[r-1][i+1] == "+"):
                popCount+=1

    # check the middle row minus the center
    if (i != 0):
        if (oldRows[r][i-1] == "+"):
            popCount+=1
    if (i != len(oldRows)-1):
        if (oldRows[r][i+1] == "+"):
            popCount+=1

    # check bottom row
    if (r != len(oldRows)-1):
        if (i != 0):
            if (oldRows[r+1][i-1] == "+"):
                popCount+=1
        if (oldRows[r+1][i] == "+"):
            popCount+=1
        if (i != len(oldRows)-1):
            if (oldRows[r+1][i+1] == "+"):
                popCount+=1
    #print("(" + str(r) + "," + str(i) + ") p = " + str(popCount))
    return popCount


def checkBirth(oldRows, r, i):
    if (checkPop(oldRows, r, i) == 3):
        return True
    else:
        return False


def cycle(oldRows, newRows):
    for r in range(len(oldRows)):
        for i in range(len(oldRows)):

            # if the cell starts already dead
            if (oldRows[r][i] == "-"):
                # see if there are 3 live cells around it to bring it to life
                if checkBirth(oldRows, r, i):
                    newRows[r][i] = "+"
                else:
                    newRows[r][i] = "-"
            else:
                # kill if lack of population
                if (checkPop(oldRows, r, i) < 2):
                    newRows[r][i] = "-"

                # kill due to over crowding
                elif (checkPop(oldRows, r, i) > 3):
                    newRows[r][i] = "-"

                # live
                else:
                    newRows[r][i] = "+"
    printBoard(newRows)


if __name__ == "__main__":
    main()





这是我运行时发生的事情( - 是死区,+是活细胞):



**预期输出: **



第1代:



...- - + - - ......

...- - + - - ...

...- - + - - ...



Gen 2:



...- - - - -...

...- + + + - .. 。

...- - - - -...



第3代:


...- - + - - ...

...- - + - - ...

...- - + - - ...



等....





* *实际输出:**



第1代:



...- - + - - 。 ..

...- - + - - ...

...- - + - - ......



第2代:



...- - - - -...

...- + + + -...

...- - - - -...



第3代:



...- - + + - ...

...- + - + - ...

...- - - - - ...



第4代:



...- - + + - ...

...- - + + - ...

......- - - - - ...



Here's what happens when I run it ("-" is a dead cell and "+" is a live cell):

**Expected Output:**

Gen 1:

...- - + - - ...
...- - + - - ...
...- - + - - ...

Gen 2:

...- - - - -...
...- + + + -...
...- - - - -...

Gen 3:

...- - + - - ...
...- - + - - ...
...- - + - - ...

etc....


**Actual Output:**

Gen 1:

...- - + - - ...
...- - + - - ...
...- - + - - ...

Gen 2:

...- - - - -...
...- + + + -...
...- - - - -...

Gen 3:

...- - + + - ...
...- + - + - ...
...- - - - - ...

Gen 4:

...- - + + - ...
...- - + + - ...
...- - - - - ...

推荐答案

我认为问题在于:

I think the problem is here:
oldRows=newRows



您需要更换两块板。



建议:

- 你可以通过使电路板比显示的部分更大(1行和每列)来简化巡视代码,这样你的代码就不必处理寄宿生。

- 如果你想认真对待游戏生活中,您需要以更有效的格式存储初始模式。 RLE是一种常用的格式。

行程编码 - 维基百科 [ ^ ]

示例(源自另一种语言) :HP Prime):


You need to swap both boards.

Advice:
- you can simplify tour code by making the board bigger than the part displayed (1 row and column around) this way you code do not have to handle the boarders.
- If you want be serious about the game of life, you need to store initial patterns on more efficient format. RLE is a commonly used format.
Run-length encoding - Wikipedia[^]
Examples (from another language: HP Prime):

RLEMap (1, 1, "3o!"); // Blinker
RLEMap (13, 5, "bo


3o


obo


这篇关于康威的生命游戏Python给出了错误的输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆