Python 3,2D列表 - 调整相邻单元格的值(作业)

2024-05-23 13:43:34 发布

您现在位置:Python中文网/ 问答频道 /正文

注意:我不能导入pygame或numpy之类的内容。

在制作康威的《生活游戏》的一个版本时,我能够得到一个“U”形的移动。在这一点上,我不知道如何调整相邻单元格的值,或者我的代码是否可能。你知道吗

在函数“setInitialPatternList”中,我添加了if语句来打印“PASS”,看看我是否在正确的轨道上。从它打印出来的内容,我可以看出它并不是在看所有的“X”我有个别。我不确定现在该去哪里,或者是否需要将“setInitialPatternList”中的“if”函数移到别处。你知道吗

我需要帮助,想办法调整电路板上“X”附近的单元格。你知道吗

from random import randint
from os import system

MAX_ROW = 30
MAX_COL = 60
currentGen = []
tempGen = []

def displayMenu():
    print("[P]lay – Press 'P' to play.")
    print("[Q]uit – Press 'Q' to exit.\n")

def setZeroList(listName):
    for row in range(MAX_ROW):
        for col in range(MAX_COL):
            listName[row][col] = "."

def setInitialPatternList(listToFormat):
    rowChoice = randint(0,MAX_ROW-7)
    columnChoice = randint(0,MAX_COL-7)
    for foundation in range(1,7):
        listToFormat[rowChoice + foundation][columnChoice] = "X"
        listToFormat[rowChoice + foundation][columnChoice + 6] = "X"
        listToFormat[rowChoice + 6][columnChoice + foundation] = "X"
        counter = 0
    if listToFormat[rowChoice + 1][columnChoice] == "X":
        print("Check1")
    if listToFormat[rowChoice - 1][columnChoice] == "X":
        print("Check2")
    if listToFormat[rowChoice][columnChoice + 1] == "X":
        print("Check3")
    if listToFormat[rowChoice][columnChoice - 1] == "X":
        print("Check4")
    if listToFormat[rowChoice + 1][columnChoice + 1] == "X":
        print("Check5")
    if listToFormat[rowChoice + 1][columnChoice - 1] == "X":
        print("Check6")
    if listToFormat[rowChoice - 1][columnChoice - 1] == "X":
        print("Check7")
    if listToFormat[rowChoice - 1][columnChoice + 1] == "X":
        print("Check8")
    if counter == 0:
        print("Counter Check")

def copyList(tempList, currentList):
    for row in range(MAX_ROW):
        for col in range(MAX_COL):
            currentList[row][col] =  tempList[row][col]

def displayList(currentList):
    MAX_ROW = 30
    MAX_COL = 60
    for row in range(MAX_ROW):
        for col in range(MAX_COL):
            if (col == (MAX_COL - 1)):
                print(currentList[row][col], end = "\n")
            else:
                print(currentList[row][col], end = "")

def displaySubMenu():
    print("[S]top - Press 'S' to stop.")

for row in range(MAX_ROW):
    currentGen.append([0] * MAX_COL)
for row in range(MAX_ROW):
    tempGen.append([0] * MAX_COL)

displayMenu()
setZeroList(tempGen)
setInitialPatternList(tempGen)
copyList(tempGen, currentGen)
displayList(currentGen)

while(True):
    userResponses = ["q", "p"]
    while(True):
        playOrQuit = input(">>").lower()
        if(playOrQuit in userResponses):
            break
        else:
            continue

    if(playOrQuit == "p"):
        system("clear") #Change to cls
        displayMenu()
        setZeroList(tempGen)
        setInitialPatternList(tempGen)
        copyList(tempGen, currentGen)
        displayList(currentGen)
    else:
        system("clear") #Change to cls
        break

Tags: inforifrangecolmaxrowprint