全局数组被覆盖而不重新分配它

时间:2016-03-30 12:08:23

标签: python arrays python-3.x global-variables

我试图使用洪水填充但仅限于一定距离(包括对角线)。洪水工作正常(可能并不完美)。但是每次运动后,我都会上/下/左/右,它应该重新绘制板。但相反,它正在重新绘制前一个董事会,同时也改变了应该永远不会改变的全球董事会。

在调用洪水之前,我尝试了许多不同的方法来分配新的电路板,但似乎没有任何改变结果。我的猜测是它与

有关
board = gameBoard[:]

在gameFlow()函数中。但我不知道为什么会这样。 我在阅读了其他{​​{3}}之后使用了[:],但它似乎没有解决问题。

我留下的印刷语句的输出示例:

GameBoard
#########
#..     #
#..     #
#       #
#        
#       #
#       #
#       #
#########
LightBoard
#########
#..     #
#..     #
#       #
#        
#       #
#       #
#       #
#########

当游戏板保持不变时,只有灯板才会发生变化。

代码:

import random

#Sample Game Board
gameBoard = [
            ["#","#","#","#","#","#","#", "#", "#"],
            ["#"," "," "," "," "," "," ", " ", "#"],
            ["#"," "," "," "," "," "," ", " ", "#"],
            ["#"," "," "," "," "," "," ", " ", "#"],
            ["#"," "," "," "," "," "," ", " ", " "],
            ["#"," "," "," "," "," "," ", " ", "#"],
            ["#"," "," "," "," "," "," ", " ", "#"],
            ["#"," "," "," "," "," "," ", " ", "#"],
            ["#","#","#","#","#","#","#", "#", "#"]
            ]

#Defining globals
currentPlayerX = -1
currentPlayerY = -1
lightRadiusMax = -1

#function flood
#Params:
#   lightBoard: array, starts off empty
#   x: int, current x position to check, starts off as players starting x
#   y: int, current y position to check, starts off as players starting y
#   oldChard: char, character to replace
#   fillChar: char, character to replace with
#Return:
#   returns the completed lightBoard
def flood(lightBoard, x, y, oldChar, fillChar):
    global currentPlayerX
    global currentPlayerY
    global lightRadiusMax
    global gameBoard
    width = len(gameBoard[0])
    height = len(gameBoard)

    if gameBoard[y][x] == " ":
            oldChar = lightBoard[y][x]
    if gameBoard[y][x] == oldChar:
        if (lightRadiusMax >= abs(currentPlayerX-x)) and (lightRadiusMax >= abs(currentPlayerY-y)):
            lightBoard[y][x] = fillChar
            if (lightRadiusMax == abs(currentPlayerX-x)) and (lightRadiusMax == abs(currentPlayerY-y)):
                flood(lightBoard, x+1, y+1, oldChar, fillChar)
            if x > 0:
                flood(lightBoard, x-1, y, oldChar, fillChar)
            if y > 0:
                flood(lightBoard, x, y-1, oldChar, fillChar)
            if x < width-1:
                flood(lightBoard, x+1, y, oldChar, fillChar)
            if y < height-1:
                flood(lightBoard, x, y+1, oldChar, fillChar)
    print("GameBoard")
    for row in gameBoard:
                for item in row:
                    print(item,end="")
                print("")
    print("LightBoard")
    for row in lightBoard:
                for item in row:
                    print(item,end="")
                print("")
    return lightBoard


def gameFlow():
    global currentPlayerX
    global currentPlayerY
    global gameBoard
    while(1):
        board = gameBoard[:]
        board = flood(board, currentPlayerX, currentPlayerY, None, ".")
        board[currentPlayerY][currentPlayerX] = "@"
        for row in board:
                for item in row:
                    print(item,end="")
                print("")
        moveDirection = input("What would you like to do?\nMove (N,S,E,W) or (Q)uit: ")
        moveDirection = moveDirection.lower()
        if moveDirection == "n":
            currentPlayerY -= 1
        if moveDirection == "s":
            currentPlayerY += 1
        if moveDirection == "e":
            currentPlayerX += 1
        if moveDirection == "w":
            currentPlayerX -= 1
        if moveDirection == "q":
            print("Thanks for playing!")
            exit(0)


def startGame():
    global currentPlayerX
    global currentPlayerY
    global lightRadiusMax
    #randXStart = random.randint(1, len(gameBoard[0])-2)
    #randYStart = random.randint(1, len(gameBoard)-2)
    currentPlayerX = 1
    currentPlayerY = 1
    print(currentPlayerX)
    print(currentPlayerY)
    lightRadiusMax = int(input("Enter your light radius: "))
    gameFlow()

startGame()

stack exchange post向我传授洪水填充基础知识

1 个答案:

答案 0 :(得分:0)

我认为你正在寻找这个,它会复制所有元素,而不仅仅是参考文献的副本。 在你的代码上试了一下(用“board = copy.deepcopy(gameBoard)”代替第70行,现在gameBoard在第一次迭代时保持空白。

import copy
board = copy.deepcopy(gameBoard)

在你提到的答案中实际上都提到了这两个。