我试图使用洪水填充,但只有一定的距离(包括对角线)。洪水没问题(可能不完美)。但每次我做上/下/左/右的动作后,它都应该重新画板。但事实上,它正在对上一届董事会进行重新划分,同时也在改变本应永不改变的全球董事会
在打电话给flood之前,我尝试了许多不同的方法来分配一个新的董事会,但似乎没有什么改变结果。我猜这和
board = gameBoard[:]
在gameFlow()函数中。但我不知道为什么会这样。 在阅读了另一个stack exchange post之后,我使用了[:],但是它似乎没有解决这个问题
我留下的print语句的输出示例:
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()
感谢inventwithpython.com教我洪水泛滥的基础知识
我想你在找这个,它会复制所有元素,而不仅仅是引用的副本。 在您的代码中尝试过它(将第70行替换为“board=copy.deepcopy(gameBoard)”,现在gameBoard在第一次迭代中保持空白
你提到的答案中实际上都提到了这两个问题
相关问题 更多 >
编程相关推荐