看似不可能写入变量
Seemingly impossible writing to a variable
所以,我正在做一些 minMax AI,不知何故,在递归例程中,一个未写入的变量正在发生变化。我附上了整个代码,但问题似乎包含在 populateChildren() 子例程中。每次,'sectors' 数组都以某种方式被写入,我唯一的想法是,也许 'sectors' 以某种方式成为其他东西的别名,或者作为参数导入某些东西。我看不出问题所在,但如果有人能告诉我我做错了什么,请提前致谢。
class Node(object):
def populateChildren(self,player,board):
newBoard=board
start=randomStart(board, player)
if self.depth>-1:
for i in board[start][4]:
print("1: ",sectors[0][3]) ##OUTPUTS "1: 0"
newBoard[0][3]=1000000 ##This is not the actually intented process, but shows the issue
print("2: ",sectors[0][3]) ##OUTPUTS "2: 1000000"
self.children.append(Node((self.depth)-1,-self.player,newBoard,self.treeVal(newBoard)))
else:
print("RECURSION END")
def treeVal(self,board):
if checkWin(board)==True:
return maxsize*self.player
elif checkWin:
return maxsize*-self.player
return 0
def __init__(self,depth,player,newBoard,value=0):
self.depth=depth
self.player=player
self.value=value
self.children=[]
#self.board=board
#print("pop conditions: ",player,newBoard)
self.populateChildren(player,newBoard)
#print("post-pop conditions: ",player,newBoard)
def minMax(node,depth,player):
print("RUN Run Run run run \n\n\n\n\n\n\n\ run ")
if depth==0 or abs(node.value)==maxsize:
print("DONE\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\nDONE")
return node.value
bestValue=maxsize*-playerNum
for i in range(0,5):
child=node.children[i]
value=MinMax(child,depth-1,-playerNum)
if abs(maxsize*playerNum-value)<abs(maxsize*playerNum-bestValue):
bestValue=value
print(str(depth*playerNum)+")"+" "*depth+str(bestvalue))
return bestValue
def AIMove(Node,sectors):
temp=sectors
newBoard=temp
##newBoard=sectors
currentPlayer=-1
depth=5
print(depth)
looks=0
while looks<6:
Node.player=-1
node=Node(depth,currentPlayer,newBoard,value=0)
bestChoice=-100
bestValue=currentPlayer*maxsize
for i in range(len(node.children)):
child=node.children[i]
value=minMax(child,depth,currentPlayer)
if (abs(currentPlayer*maxsize-value)<=abs(currentPlayer*maxsize-bestValue)):
bestValue=value
bestChoice=i
bestChoice+=1
print("node: ",node.children[0].value)
print("choice: ",bestChoice)
looks+=1
据我所知,在 AIMove
中,您将 temp
设置为 sectors
,然后将 newBoard
设置为 temp
,并将其传递给 Node
进一步向下作为 newBoard
参数。您将此传递给子项作为 board
,您将其分配给您更改的 newBoard
,因此 sectors
更改。
呸!您总是只是链接到内存中的相同位置,因此新对象名称并不意味着新对象。
所以,我正在做一些 minMax AI,不知何故,在递归例程中,一个未写入的变量正在发生变化。我附上了整个代码,但问题似乎包含在 populateChildren() 子例程中。每次,'sectors' 数组都以某种方式被写入,我唯一的想法是,也许 'sectors' 以某种方式成为其他东西的别名,或者作为参数导入某些东西。我看不出问题所在,但如果有人能告诉我我做错了什么,请提前致谢。
class Node(object):
def populateChildren(self,player,board):
newBoard=board
start=randomStart(board, player)
if self.depth>-1:
for i in board[start][4]:
print("1: ",sectors[0][3]) ##OUTPUTS "1: 0"
newBoard[0][3]=1000000 ##This is not the actually intented process, but shows the issue
print("2: ",sectors[0][3]) ##OUTPUTS "2: 1000000"
self.children.append(Node((self.depth)-1,-self.player,newBoard,self.treeVal(newBoard)))
else:
print("RECURSION END")
def treeVal(self,board):
if checkWin(board)==True:
return maxsize*self.player
elif checkWin:
return maxsize*-self.player
return 0
def __init__(self,depth,player,newBoard,value=0):
self.depth=depth
self.player=player
self.value=value
self.children=[]
#self.board=board
#print("pop conditions: ",player,newBoard)
self.populateChildren(player,newBoard)
#print("post-pop conditions: ",player,newBoard)
def minMax(node,depth,player):
print("RUN Run Run run run \n\n\n\n\n\n\n\ run ")
if depth==0 or abs(node.value)==maxsize:
print("DONE\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\nDONE")
return node.value
bestValue=maxsize*-playerNum
for i in range(0,5):
child=node.children[i]
value=MinMax(child,depth-1,-playerNum)
if abs(maxsize*playerNum-value)<abs(maxsize*playerNum-bestValue):
bestValue=value
print(str(depth*playerNum)+")"+" "*depth+str(bestvalue))
return bestValue
def AIMove(Node,sectors):
temp=sectors
newBoard=temp
##newBoard=sectors
currentPlayer=-1
depth=5
print(depth)
looks=0
while looks<6:
Node.player=-1
node=Node(depth,currentPlayer,newBoard,value=0)
bestChoice=-100
bestValue=currentPlayer*maxsize
for i in range(len(node.children)):
child=node.children[i]
value=minMax(child,depth,currentPlayer)
if (abs(currentPlayer*maxsize-value)<=abs(currentPlayer*maxsize-bestValue)):
bestValue=value
bestChoice=i
bestChoice+=1
print("node: ",node.children[0].value)
print("choice: ",bestChoice)
looks+=1
据我所知,在 AIMove
中,您将 temp
设置为 sectors
,然后将 newBoard
设置为 temp
,并将其传递给 Node
进一步向下作为 newBoard
参数。您将此传递给子项作为 board
,您将其分配给您更改的 newBoard
,因此 sectors
更改。
呸!您总是只是链接到内存中的相同位置,因此新对象名称并不意味着新对象。