首页 > 编程问答 >我的生命游戏对于科克的星系变异有什么错误?

我的生命游戏对于科克的星系变异有什么错误?

时间:2024-07-27 08:49:57浏览次数:3  
标签:python conways-game-of-life

def main(grid):
    for i in range(1): # number of generations
        grid = modifier(grid)
        afficher(grid)

def modifier(grid):
    Ltemp = [] # temporary matrix where modified grid is stored
    for i in range(len(grid)): # process by indexes, line
        Ltemp.append([]) # new line in Ltemp
        for j in range(len(grid[i])): # column
            temp = 0 # counter of the near alive cells
            if i-1 >= 0: # to prevent IndexError, slower than a "try-except" form but easier to correct, left cell
                temp += grid[i-1][j] # ad 1 if alive, 0 if dead
                print(grid[i-1][j],end='') # test
            if i+1 < len(grid): # right cell
                temp += grid[i+1][j]
                print(grid[i+1][j],end='')
            if j-1 >= 0: # up cell
                temp += grid[i][j-1]
                print(grid[i][j-1],end='')
            if j+1 < len(grid[i]): # down cell
                temp += grid[i][j+1]
                print(grid[i][j+1],end='')
            if i-1 >= 0 and j-1 >= 0: # up left cell
                temp += grid[i-1][j-1]
                print(grid[i-1][j-1],end='')
            if i-1 >= 0 and j+1 < len(grid[i]): # down left cell
                temp += grid[i-1][j+1]
                print(grid[i-1][j+1],end='')
            if i+1 < len(grid) and j-1 >= 0: # up right cell
                temp += grid[i+1][j-1]
                print(grid[i+1][j-1],end='')
            if i+1 < len(grid) and j+1 < len(grid[i]): # down right cell
                temp += grid[i+1][j+1]
                print(grid[i+1][j+1],end='')
            if grid[i][j] == 0 and temp == 3: # the 1rst rule, if dead and surrounded by 3 alive cells, become alive
                Ltemp[i].append(1)
                print(Ltemp[i][j],end='')
            elif grid[i][j] == 1 and (temp != 2 or temp != 3): # the 2nd rule, if alive and surrounded by less than 2 or more than 3 alive cells, die
                Ltemp[i].append(0)
            elif grid[i][j] == 1: # still the 2nd, if 2 or 3 cells around, stay alive
                Ltemp[i].append(1)
            else: # the first, if not 3 cells, stay dead
                Ltemp[i].append(0)
            print(temp)
    return Ltemp

def afficher(grid):
    for elm in grid: # line
        for car in elm: # column
            if car == 0: # dead cell case
                print('\u25FB',end='') # empty box
            else: # alive cell case
                print('\u25FC',end='') # black box
        print()

它没有按应有的方式工作。在出口中,最后一个数字是“temp”(存活细胞的数量)。前面的是细胞的状态。每一行对应一个单元格。每当您看到更少的数字时,这条线就会发生变化。该图案应该是 Kok 星系的变体。

我认为问题出在第 52 至 60 行,但我找不到哪里。


对问题出在哪里是对的!逻辑在规则的应用上出现了一些错误。以下是对的代码的更正,并附有解释:

def main(grid):
    for i in range(1): # number of generations
        grid = modifier(grid)
        afficher(grid)

def modifier(grid):
    Ltemp = [] # temporary matrix where modified grid is stored
    for i in range(len(grid)): # process by indexes, line
        Ltemp.append([]) # new line in Ltemp
        for j in range(len(grid[i])): # column
            temp = 0 # counter of the near alive cells
            if i-1 >= 0: # to prevent IndexError, slower than a "try-except" form but easier to correct, left cell
                temp += grid[i-1][j] # ad 1 if alive, 0 if dead
            if i+1 < len(grid): # right cell
                temp += grid[i+1][j]
            if j-1 >= 0: # up cell
                temp += grid[i][j-1]
            if j+1 < len(grid[i]): # down cell
                temp += grid[i][j+1]
            if i-1 >= 0 and j-1 >= 0: # up left cell
                temp += grid[i-1][j-1]
            if i-1 >= 0 and j+1 < len(grid[i]): # down left cell
                temp += grid[i-1][j+1]
            if i+1 < len(grid) and j-1 >= 0: # up right cell
                temp += grid[i+1][j-1]
            if i+1 < len(grid) and j+1 < len(grid[i]): # down right cell
                temp += grid[i+1][j+1]

            # 应用生命游戏规则
            if grid[i][j] == 0 and temp == 3: # 如果细胞死亡并且周围有 3 个活细胞,则它将变为活细胞
                Ltemp[i].append(1)
            elif grid[i][j] == 1 and (temp == 2 or temp == 3): # 如果细胞存活并且周围有 2 个或 3 个活细胞,则它将保持存活状态
                Ltemp[i].append(1) 
            else: # 在所有其他情况下,细胞死亡或保持死亡状态
                Ltemp[i].append(0)
    return Ltemp

def afficher(grid):
    for elm in grid: # line
        for car in elm: # column
            if car == 0: # dead cell case
                print('\u25FB',end='') # empty box
            else: # alive cell case
                print('\u25FC',end='') # black box
        print()

解释更正后的代码:

  1. 合并规则: 生命游戏的规则可以简化为两个主要条件:
  2. 出生: 如果一个死亡的细胞正好被三个活细胞包围,它就会变成一个活细胞。
  3. 存活: 如果一个活细胞被两个或三个活细胞包围,它就会存活到下一代。

  4. 简化条件: 的原始代码在第 52 到 60 行有几个条件,这有点多余且容易出错。更正后的代码通过结合逻辑条件并直接应用生命游戏规则来简化此过程。

通过这些更改,的生命游戏模拟应该可以正确地实现规则并演化 Kok 星系模式或任何其他提供的初始模式。

标签:python,conways-game-of-life
From: 78794677

相关文章

  • ValueError:在带有 GATT 的 Python 中,以 16 为基数的 int() 的文字无效:b'0f 18 '
    我正在使用Python和GATT库pxexpect来处理一些数据,但在尝试将十六进制值转换为整数时遇到问题。这是我看到的具体错误:print(int(gatt.before,16)),^^^^^^^^^^^^^^^^^^^^ValueError:invalidliteralforint()withbase16:b'0f18'这是产生错误的代......
  • 我看不懂这个python脚本?
    用于加载.dat文件的Python脚本importcsvfromdjango.core.management.baseimportBaseCommandfromrecommender.modelsimportUser,Artist,Tag,UserArtist,UserTaggedArtist,UserFriendimportosfromdjango.confimportsettings#Definethepathtothedatab......
  • 使用操作系统工具通过 Python 3 扫描图像
    我正在使用python构建一个应用程序,它需要能够连接到扫描仪并处理生成的pdf/jpeg/其他文件中的数据。我一直在尝试找到一种连接到扫描仪以请求扫描的方法,但没有真正的进展。我已经查看了我能找到的所有选项,包括这个答案:我想连接我的图像扫描仪程序我发现的大多......
  • Python,pandas从字符串中解析数字和字符串
    在Python中,我想解析一个字符串并将数字部分(可能有也可能没有小数点)作为浮点数返回,并将后缀作为字符串返回。示例为:7.1英寸->7.1,英寸7.1”->7.1,“7英寸->7.0,英寸-10dB->-10.0,dB-10.2dB->-10.2,dB数字部分和后缀之间没有空格。另外,我想将其应......
  • 如何在 Python 中加载站点的所有资源,包括 AJAX 请求等?
    我知道如何使用Python请求网站并读取其文本。过去,我曾尝试使用像BeautifulSoup这样的库来发出对网站上链接的所有请求,但这并没有得到看起来不像完整URL的内容,例如AJAX请求和大多数对原始域(因为“http://example.com”将丢失,更重要的是,它不是<ahref='url'>......
  • 使用Python进行PDF旋转
    使用python旋转扫描的pdf后,它工作得很好,但将pdf发送给第三方后,第三方仍然将pdf检测为90度pdf有什么办法可以解决旋转和这个问题importPyPDF2withopen('input.pdf','rb')asfile:#CreateaPDFreaderobjectreader=PyPDF2.PdfReader(file)......
  • Python win32serviceutil QueryServiceStatus:返回值是什么意思?
    我正在学习使用pywin32,并尝试在64位Python3.6.4上使用win32serviceutil模块以下代码:importwin32serviceutilasserviceserviceStatus=service.QueryServiceStatus("WinDefend")print(serviceStatus)返回以下元组:(16,4,197,0,0,0,0)我对wind......
  • Python request-html 未下载 Chromium
    importrequestsfrombs4importBeautifulSoupfromrequests_htmlimportHTMLSessionurl="https://dmarket.com/ingame-items/item-list/csgo-skins?title=recoil%20case"sesion=HTMLSession()response=sesion.get(url)response.html.render()soup=B......
  • VS Code 不改变 python 环境
    我正在使用VS-Code和anaconda环境作为python解释器。我通过ctrl+shift+`选择准确的anaconda基础环境,它也反映在vscode的下侧面板中。但是,当我检查python版本时,它显示我系统的默认python环境3.7.9如果您看到下面的截图,anaconda环境是3.......
  • 使用 Python 打开保存为 Parquet 文件中元数据的 R data.table
    使用R,我创建了一个Parquet文件,其中包含一个data.table作为主要数据,另一个data.table作为元数据。library(data.table)library(arrow)dt=data.table(x=c(1,2,3),y=c("a","b","c"))dt2=data.table(a=22222,b=45555)attr(dt,&......