我正在尝试让 AI 为回合制战略游戏(如 RISK)工作。我对 AI 编程有点陌生,但我认为我的问题可能只是传递了错误的参数或其他东西,无论哪种方式,如果有人能告诉我出了什么问题,或者其他任何会导致使用 minMax 解决这个问题的问题问题; 答案将不胜感激。我认为,问题在于沿线的某个地方(可能在传递“newBoard”时)“newBoard”被覆盖为一个大整数值
##sectors array takes this form: [['Canada',[300,200],'',0,[1,2,12]],['Greenland',[600,120],'',0,[0,7]],['USA',[360,300],'',0,[0,3]],['Central America',[360,410],'',0,[2,4]],['South America',[480,510],'',0,[3,5]],['Brazil',[560,580],'',0,[4,6,15]],['Argentina',[490,700],'',0,[5]],['West Europe',[760,260],'',0,[1,8,9,15]],['East Europe',[875,260],'',0,[7,9,10,11,13]],['Scandanavia',[870,170],'',0,[7,8,10]],['West Russia',[970,220],'',0,[8,9,11,13]],['Central Russia',[1160,210],'',0,[10,12,13,19]],['Far East',[1350,220],'',0,[0,11,19]],['Central Asia',[1030,320],'',0,[8,10,11,14,18]],['Middle East',[970,380],'',0,[13,16]],['North Africa',[760,400],'',0,[5,7,16]],['Central Africa',[880,470],'',0,[15,14,17]],['South Africa',[880,610],'',0,[16,18]],['India',[1125,390],'',0,[13,17,19]],['East Asia',[1225,360],'',0,[11,12,13,18,20]],['Indonesia',[1290,510],'',0,[19,21]],['Australia',[1360,640],'',0,[20]]]
每个子列表中的数据是:名称、屏幕上的位置(此处不重要)、控制它的玩家(随机)、人口、它连接到的其他部门
class Node(object):
def populateChildren(self,player,board):
print("start conditions: ",player,board)
newBoard=board
start=randomStart(board, player)
if self.depth>-1:
##[base case]
for i in board[start][4]:
newBoard[i][3]=(board[start][3]-board[i][3])
newBoard[start][3]=1
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.populateChildren(player,newBoard)
def minMax(node,depth,player):
if depth==0 or abs(node.value)==maxsize:
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
return bestValue
def AIMove(Node,sectors):
newBoard=sectors
currentPlayer=-1
depth=10
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
looks+=1
如果我遗漏了任何内容,我非常乐意用任何所需的信息更新线程,谢谢。