Python 似乎不可能写入变量

Python 似乎不可能写入变量,python,recursion,minmax,Python,Recursion,Minmax,所以,我在做一些minMax人工智能,不知怎么的,在一个递归例程中,一个没有写入的变量正在改变。我已经附加了整个代码,但问题似乎包含在populateChildren()子例程中。每次都会以某种方式写入“扇区”数组,我唯一的想法是,“扇区”可能会以某种方式成为其他对象的别名,或者作为参数导入到其他对象。我看不出这个问题,但如果有人能告诉我我做错了什么,请提前感谢 class Node(object): def populateChildren(self,player,board):

所以,我在做一些minMax人工智能,不知怎么的,在一个递归例程中,一个没有写入的变量正在改变。我已经附加了整个代码,但问题似乎包含在populateChildren()子例程中。每次都会以某种方式写入“扇区”数组,我唯一的想法是,“扇区”可能会以某种方式成为其他对象的别名,或者作为参数导入到其他对象。我看不出这个问题,但如果有人能告诉我我做错了什么,请提前感谢

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
类节点(对象):
def PopulatechChildren(自身、玩家、棋盘):
新板
开始=随机开始(棋盘、玩家)
如果自深度>-1:
[start][4]中的i:
打印(“1:,扇区[0][3])35;#输出“1:0”
newBoard[0][3]=1000000##这不是实际计划的过程,但显示了问题
打印(“2:,扇区[0][3])##输出“2:1000000”
self.children.append(节点((self.depth)-1,-self.player,newBoard,self.treeVal(newBoard)))
其他:
打印(“递归结束”)
def treeVal(自身、板):
如果checkWin(board)=True:
返回maxsize*self.player
埃利夫·切克温:
返回maxsize*-self.player
返回0
定义初始值(自我、深度、玩家、新板,值=0):
self.depth=深度
self.player=player
自我价值=价值
self.children=[]
#self.board=board
#打印(“弹出条件:”,玩家,新手板)
大众儿童(玩家、新手)
#打印(“后弹出条件:”,玩家,新手)
def minMax(节点、深度、播放器):
打印(“运行\n\\n\n\n\n\n\n\n\n\n运行”)
如果深度==0或abs(节点值)==maxsize:
打印(“完成\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n完成”)
返回node.value
bestValue=maxsize*-playerNum
对于范围(0,5)内的i:
child=node.children[i]
值=最小最大值(子级,深度-1,-playerNum)

如果abs(maxsize*playerNum值)如我所见,在
AIMove
中将
temp
设置为
sectors
,然后
newBoard
设置为
temp
并将其作为
newBoard
参数传递到
节点。您将其传递给子级以填充为
board
,您将其分配给
newBoard
,然后更改
扇区。

呸!你总是链接到内存中的同一个位置,所以新的对象名并不意味着新的对象。

如果你已经将它保存在
self
中,为什么要将
player
传递给
populatechildens
??哦,我明白了,太棒了,谢谢你了:)你建议我如何复制它成为一个新变量(即:创建一个新的baord,我可以在不更改原始区域secotrs阵列的情况下进行工作)?因此,您需要制作一个深度副本,看看这些问题:并且。它们应该有帮助:-)非常感谢!多年来,我一直试图解决这个问题,我选择使用copy.deepcopy而不是numpy,但它似乎起到了作用。干杯