我的minimax算法的代码tic tac toe AI似乎不起作用,我无法弄清楚原因。如果一个举动导致亏损,那么在recusrion方面似乎有问题并且返回负值;它没有区分防守动作和进攻动作。
不是选择将X放在第6位以阻止对手连续达到3,而是将其放在另一个牌上
board = [
"X", "X", "O",
"O", "O", "X",
"-", "-", "-",
]
opp = "O"
plyr = "X"
def getOpenPos(board):
openPos = []
for index, state in enumerate(board):
if state == "-":
openPos.append(index)
return openPos
def winning(board, plyr):
if ((board[0] == plyr and board[1] == plyr and board[2] == plyr) or
(board[3] == plyr and board[4] == plyr and board[5] == plyr) or
(board[6] == plyr and board[7] == plyr and board[8] == plyr) or
(board[0] == plyr and board[4] == plyr and board[8] == plyr) or
(board[1] == plyr and board[4] == plyr and board[7] == plyr) or
(board[2] == plyr and board[4] == plyr and board[6] == plyr) or
(board[0] == plyr and board[3] == plyr and board[6] == plyr) or
(board[2] == plyr and board[5] == plyr and board[8] == plyr)):
return True
else:
return False
def minimax(board, turn, FIRST):
possibleMoves = getOpenPos(board)
#check if won
if (winning(board, opp)):
return -10
elif (winning(board, plyr)):
return 10
scores = []
#new board created for recursion, and whoevers turn it is
for move in possibleMoves:
newBoard = board
newBoard[move] = turn
if (turn == plyr):
scores.append( [move,minimax(newBoard, opp, False)] )
elif (turn == opp):
scores.append( [move, minimax(newBoard, plyr, False)] )
#collapse recursion by merging all scores to find optimal position
#see if there is a negative value (loss) and if there is its a -10
if not FIRST:
bestScore = 0
for possibleScore in scores:
move = possibleScore[0]
score = possibleScore[1]
if score == -10:
return -10
else:
if score > bestScore:
bestScore = score
return bestScore
else:
bestMove, bestScore = 0, 0
for possibleScore in scores:
move = possibleScore[0]
score = possibleScore[1]
if score > bestScore:
bestMove = move
bestScore = score
#returns best position
return bestMove
print(minimax(board, plyr, True))
答案 0 :(得分:1)
我发现您的代码存在两个问题。如果你修复它们,那么在这种情况下你至少会获得6
。
第一个问题是,行newBoard = board
实际上并没有制作列表的副本,它只是制作了引用的副本。可以通过将其更改为newBoard = board[:]
来解决此问题。
第二个问题是bestScore
的起始值实际上并未超出预期范围,因此您每次都无法获得bestIndex
的值。我将bestMove, bestScore = 0, 0
更改为bestMove, bestScore = 0, -11
,这似乎对我有用。