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

时间:2018-07-29 21:07:17

标签: python python-3.x conways-game-of-life

注意:我无法导入pygame或numpy之类的东西。

使用Conway的《人生游戏》的一个版本,我能够移动一个“ U”形。至此,我对如何调整相邻单元格的值一无所知,或者我的代码是否有可能。

在函数“ setInitialPatternList”中,我添加了if语句以打印“ PASS”,以查看我是否在正确的轨道上。从打印出来的内容来看,我可以看出它并不是在查看我单独拥有的所有“ X”。我不确定此时该去哪里,或者我的“ if”函数在“ setInitialPatternList”中是否需要移到其他地方。

我需要帮助来弄清楚如何调整板上“ 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

0 个答案:

没有答案