很抱歉这样直接贴出我的代码,但我已经花了几个小时试图找出我的Python极小极大算法哪里出了问题。任何帮助都将不胜感激!
inf = 1000bo = [["x", "o", "o"], [" ", "o", " "], [" ", "x", "x"]] def bestMove(board): bestScore = -inf bestMove = None for i in range(3): for j in range(3): if(board[i][j]==" "): board[i][j]=getTurn(board) score = minimax(board, searchdepth, True) board[i][j]=" " if score > bestScore: bestScore = score bestMove = [i, j]print("\n\n\n")return bestMovesearchdepth = 10def minimax(node, depth, maxP): resultat = win(node) if resultat=="x": return 1 if resultat=="o": return -1 if resultat=="tie": return 0 if depth == 0: return 0if maxP==True: value = -inf for i in range(3): for j in range(3): if node[i][j] == " ": node[i][j] = getTurn(node) newval = minimax(node, depth - 1, False) node[i][j] = " " value = max(newval, value) return valueif maxP==False: value = inf for i in range(3): for j in range(3): if node[i][j] == " ": node[i][j] = getTurn(node) newval = minimax(node, depth - 1, True) node[i][j] = " " value = min(newval, value) return valueprint(bestMove(bo))
输出: [1, 0]期望输出: [2, 0]
回答:
你总是会在’X’获胜的情况下发送1,这是不正确的。这意味着如果轮到O,它会认为X获胜是一件好事。最简单的方法是根据谁的回合来给予不同的分数,即如果你自己获胜则得分为1,对手获胜则得分为-1,平局则得分为0。