CHOICE = 0;
def score(State, Depth):
if(conditionX(State)):
return 10 - Depth
elif(conditionO(State)):
return Depth - 10
return 0
def minimax(State, Depth):
if(condition(State)): return score(State, Depth)
scores =[-1000]
moves = [0]
possibleState = ()
for index in range(1,10):
if(not index in State[0] and not index in State[1]):
if(State[2]%2==0):
newState = State[1]
newState.append(index)
possibleState += (State[0],newState,State[2]+1)
scores.append(minimax(possibleState, Depth + 1))
moves.append(index)
elif(State[2]%2!=0):
newState = State[0]
newState.append(index)
possibleState += (newState,State[1],State[2]+1)
scores.append(minimax(possibleState, Depth + 1))
moves.append(index)
maxItem = max(scores)
maxIndex = scores.index(maxItem)
global CHOICE
CHOICE = moves[maxIndex]
return max(scores)
我不知道为什么代码总是播放一次而不是返回所有可能的移动。每次运行时打印的状态显示它只像这样运行一次
([5],[],0)
([5],[1],1)
([5,2],[1],2)
([5,2],[1,3],3)
([5,2,4],[1,3],4)
([5,2,4],[1,3,6],5)
([5,2,4,7],[1,3,6],6)
([5,2,4,7],[1,3,6,8],7)
([5,2,4,7,9],[1,3,6,8],8)
0个 这只是一次,也不是很聪明的举动。功能说明我没有包括:
条件X检查X是否获胜
检查O是否获胜
条件绘制检查是否绘制
只要检查一下以上情况就行了
状态总是一个包含X coors,O coors和turns的元组
目前没有回答
相关问题 更多 >
编程相关推荐