0
我試圖使用洪水填充,但只有一定距離(包括對角線)。洪水工作正常(可能不完美)。但是在每次移動之後,我都會補充/向下/向左/向右重新繪製棋盤。但相反,它正在重繪上一屆董事會,並且正在改變應該永不改變的全球董事會。全局數組被覆蓋而沒有重新分配它
我嘗試了一些分配新鮮板不同的方式調用汛前,但似乎沒有什麼改變結果。我的猜測是它與
board = gameBoard[:]
在gameFlow()函數中。但我不知道那是爲什麼。 我在閱讀了其他stack exchange post之後使用了[:],但似乎沒有解決問題。從我留在打印報表
輸出實例:
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教我洪災的基本填補
謝謝你的建議有一次我回家,我會嘗試和並編輯這條評論 – canpan14
完美工作我沒看過其他崗位正常和思想。![:]將使副本所有元素也。 – canpan14