我正在尝试在 Python 中使用 minimax for Connect 4 作为个人项目来实现 AI。目前我有这个。
def alphaBeta(myBoard, column, depth, alpha, beta, player):
parent = board()
for r in range(ROWS):
for c in range(COLUMNS):
parent.board[r][c] = myBoard.board[r][c]
parent.move(column, player)
if parent.isFull() or parent.isWon()[0] or depth <= 0:
if parent.isFull(): return 0
if parent.isWon()[1] == player: return float('inf')
elif parent.isWon()[0]: return -1*float('inf')
else: return statScore(parent, player)
if player == "O":
for child in range(COLUMNS):
alpha = max(alpha, alphaBeta(parent, child, depth-1, alpha, beta, "X"))
if beta <= alpha:
break
return alpha
else:
for child in range(COLUMNS):
beta = min(beta, alphaBeta(parent, child, depth-1, alpha, beta, "O"))
if beta <= alpha:
break
return beta
def ai(myBoard, depth):
output = []
bestScore = float('inf')
for column in range(COLUMNS):
if myBoard.isValid(column):
score = alphaBeta(myBoard, column, depth, -1*float('inf'), float('inf'), "O")
if score < bestScore:
output = [column]
bestScore = score
elif score == bestScore:
output.append(column)
move = random.choice(output)
return move
但是,由于某种原因,计算机不会做出获胜的举动。例如,给定这个棋盘,计算机应该在第 4 列进行游戏,从而获胜。但是,它实际上在第 3 列中播放。
0 1 2 3 4 5 6
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | |O| | |
| | |X|O|O| | |
|X|X|O|X|O|X|X|
---------------
0 1 2 3 4 5 6