我正在制作一个递归的井字游戏搜索播放器,并且有一个名为 board 的对象,每次我模拟一个新的棋盘时都会创建它。但是,原始板正在被未来的板覆盖。我不确定这是怎么发生的。
我有下面重要的代码。发生的事情是这样的: 我启动 ai 播放器并调用 getMove(),它应该返回一个数字。 在 getMove 中,我启动了一个递归 ai,它在启动时开始构建每个棋盘状态的数组。该列表是全局的,因此 build frontier 不会返回任何内容。它只说“如果这是一个结束状态,x/o/tie,然后将它添加到列表中。否则,制作一个新的 ai 播放器,这个位置已填充并检查结束状态,等等。没有在哪里它引用了 aiPlayer.board,但不知何故它被覆盖了。
当 ai.getMove 返回一个随机移动时,(找到棋盘上的所有点并从这些点中随机返回一个)线
print("the game board this sees")
self.board.printBoard()
打印我第一次调用 ai 播放器时输入的棋盘。如果我标记一个点,然后调用 ai 来决定下一个点,这个打印函数会在我标记一个点之后但在它被填充之前打印板。这很好。
然而,当使用实际算法时,同一行打印一个填充板。我从不触及 ai 的 self.board 对象来改变它,我只复制 c_board 对象并从中构建迭代。
我的问题是:虽然我从来没有碰过self.board,为什么它被覆盖了?我稍后制作的 c_boards 是指向同一个原始板的指针吗?这是怎么回事?
global overallFrontier
overallFrontier = []
class aiPlayer:
def __init__(self, field, currentTurn,firstPlayer,width,path,aiMode):
self.board = c_board(False,field,width,path,currentTurn) # <-- this board is getting overwritten
self.mode = aiMode
self.firstPlayer = firstPlayer
overallFrontier = []
def getMove(self):
self.bestTileNum = self.smartSearchGetMove()
print("getMove: ", self.bestTileNum)
print("the game board this sees")
self.board.printBoard() # <-- When I print this, I don't see the original board.
#instead, I see a hypothetical board created later
#return
return self.bestTileNum #int
def smartSearchGetMove(self):
recurse_buildFrontier(self.board.field,self.board.width,self.board.path,self.board.currentTurn,0)
return bestNumber #any number 1-9, the implementation isn't where my issue is
class recurse_buildFrontier:
def __init__(self,field,width,path,currentTurn,iteration):
print("RecurseAI: ", iteration)
self.board = c_board(False,field,width,path,currentTurn)
self.iteration = iteration
self.buildFrontier()
def buildFrontier(self):
print("================ BuildFrontierCall =====================")
print("iteration: ", self.iteration + 1)
print("frontier: ", overallFrontier)
for row in self.board.field:
for spot in row:
if(spot in range(1,11) and spot not in self.board.path):#the not path bit is implied, just for clarity its written
spotFrontierBoard = c_board(False,self.board.field,self.board.width,self.board.path,not self.board.currentTurn)
spotFrontierBoard.fillBoard(spot)
print("-- spot: ", spot)
print("! spotFrontierBoard print !")
spotFrontierBoard.printBoard()
print("! self board print !")
self.board.printBoard()
spotFrontierBoard.evaluatePosition()
evaluate = spotFrontierBoard.winState
evaluatePrinter = "X" if evaluate == c_board.winType["x"] else "O" if evaluate == c_board.winType["o"] else "TIE" if evaluate == c_board.winType["tie"] else "none" if evaluate == c_board.winType["none"] else "evaluate error"
print("evaluate: ",evaluatePrinter )
if (evaluate in [c_board.winType["x"],c_board.winType["o"],c_board.winType["tie"]]):
overallFrontier.append(spotFrontierBoard)
else:
recurse_buildFrontier(spotFrontierBoard.field,self.board.width,spotFrontierBoard.path,not self.board.currentTurn,self.iteration+1).buildFrontier()
print("++++++++++ END BuildFrontierCall +++++++++++")