我正在尝试做一个人工智能,它玩的游戏看起来很像跳棋,逻辑基本上是一样的。但是我没有用蒙特卡罗的方法来实现树的结构。如果我没有错,我的树的根应该是初始状态或板,节点应该是所有可能的播放。我知道我必须创建一个函数来计算每个节点的权重并选择可能的最佳播放。我的问题是,如前所述,我不知道如何在python中实现所述树。在
到目前为止,我有我的董事会和两个功能,返回您可以采取的合法行动的列表。电路板是用一个10×10多维数组创建的,为了找到可能的移动,我有两个函数来接收要移动的工件的X和Y坐标,并检查所有可用的选项。我之所以有两个移动功能,是因为其中一个功能用于基本移动,即当您旁边的空间相邻时,另一个功能检查“跳跃”,即当您旁边的空间被占用,但旁边的空间是自由的。在
我将在这里添加我的代码,以防你们更容易理解我在做什么。在import numpy as np
matrix = [[1,1,1,1,1,0,0,0,0,0], [1,1,1,1,0,0,0,0,0,0], [1,1,1,0,0,0,0,0,0,0], [1,1,0,0,0,0,0,0,0,0], [1,0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0,2], [0,0,0,0,0,0,0,0,2,2], [0,0,0,0,0,0,0,2,2,2], [0,0,0,0,0,0,2,2,2,2], [0,0,0,0,0,2,2,2,2,2]]
#new_matrix = np.fliplr(np.flipud(matrix))
#new_matrix = new_matrix.tolist()
print “\n”.join(” “.join(str(el) for el in row) for row in matrix)
#print “\n”
#print “\n”.join(” “.join(str(el) for el in row) for row in new_matrix)
def basicMove(x,y):
listMoves = []
if x > 0 and matrix[x-1][y] == 0: #left
listMoves.append([x-1,y])
if x < 9 and matrix[x+1][y] == 0: #right
listMoves.append([x+1,y])
if y < 9: #up
if matrix[x][y+1] == 0:
listMoves.append([x,y+1])
if x>0 and matrix[x-1][y+1] == 0: #up left
listMoves.append([x-1,y+1])
if x < 9 and matrix[x+1][y+1] == 0: #up right
listMoves.append([x+1,y+1])
if y > 0: #down
if matrix[x][y-1] == 0:
listMoves.append([x,y-1])
if x > 0 and matrix[x-1][y-1] == 0: #down left
listMoves.append([x-1,y-1])
if x<9 and matrix[x+1][y-1] == 0: #down right
listMoves.append([x+1,y-1])
return listMoves
def hopper(x,y):
listHops = []
listHops.append(basicMove(x,y)) #Call the basic move function inside the hop function
if x > 1 and matrix[x-1][y] != 0 and matrix[x-2][y] == 0: #left
listHops.append([x-2,y])
if x < 8 and matrix[x+1][y] != 0 and matrix[x+2][y] == 0: #right
listHops.append([x+2,y])
if y > 1:
if matrix[x][y-1] != 0 and matrix[x][y-2] == 0: #down
listHops.append([x,y-2])
if x>1 and matrix[x-1][y-1] != 0 and matrix[x-2][y-2] == 0: #down left
listHops.append([x-2,y-2])
if x < 8 and matrix[x+1][y+1] != 0 and matrix[x+2][y-2] == 0: #down right
listHops.append([x+2,y-2])
if y < 8:
if matrix[x][y+1] != 0 and matrix[x][y+2] == 0: #up
listHops.append([x,y+2])
if x > 1 and matrix[x-1][y+1] != 0 and matrix[x-2][y+2] == 0: #up left
listHops.append([x-2,y+2])
if x < 8 and matrix[x+1][y+1] != 0 and matrix[x+2][y+2] == 0: #up right
listHops.append([x+2,y+2])
return listHops
hopper(2,1) #Testing the function
最后一个问题是,对我来说,使用面向对象编程会使事情变得更容易/更高效吗?我已经检查了一些在Python上为游戏实现mct的例子,比如Tic-tac-toe和Reversi,他们似乎都在使用OOP。谢谢你的帮助。在