Minimax 不起作用并且没有给出最佳着法(python)

Minimax not working and not giving out a best move(python)

我一直在尝试对 connect 4 游戏使用 minimax 算法,然后稍后实施 alpha beta p运行ing 以使其更强大。但是我 运行 正在研究计算机不是 return 最佳着法的问题它只会 运行 通过 minimax 5 次然后放弃而不是 return 最佳着法.我不太确定如何处理这个问题。这是相关代码: 注意:运行 故意在低深度,因为我的电脑无法处理大量计算,仅用于测试目的 atm

import pygame as p
import sys 
import math
from time import sleep

board = [
    [0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0],
]

def gravity(col):
    for r in range(5, -1, -1):
        if board[r][col] == 0:
            return r

def place_counter(playe, mousex, mousey):
    global colour
    if playe == 1:
        colour = (255, 255, 0)
    if playe == 2:
        colour = (255, 0, 0)
    xbox = math.floor(mousex / 100)
    ybox = math.floor(mousey / 100)
    row = gravity(xbox)
    board[row][xbox] = playe
    print_board(board)
    p.draw.circle(screen, colour, (50 + 100 * xbox, 50 + 100 * row), 40)

def c_move(b_move):
    place_counter(2, b_move, b_move)

def comp_move():
    best_score = - 10
    best_move = 0
    for key in range(len(board)):
        print(key)
        if board[key][gravity(key)] == 0:
            board[key][gravity(key)] = bot
            score = minimax(0, False)
            board[key][gravity(key)] = 0
            if score > best_score:
                best_score = score
                best_move = key
    if best_move != 0:
        c_move(best_move)
        print_board(board)

def minimax(depth, isMaximizing):
    if isMaximizing:
        best_score = -800
        for key in range(len(board)):
            if board[key][gravity(key)] == 0:
                board[key][gravity(key)] = bot
                score = minimax(depth + 1, False)
                board[key][gravity(key)] = 0
                if score > best_score:
                    best_score = score
                if depth == 5:
                    best_score = score
            return best_score
    else:
        best_score = 800
        for key in range(len(board)):
            if board[key][gravity(key)] == 0:
                board[key][gravity(key)] = player
                score = minimax(depth + 1, True)
                board[key][gravity(key)] = 0
                if score < best_score:
                    best_score = score
                if depth == 5:
                    best_score = score
        return best_score

while game_over is False:
    for event in p.event.get():
        if event.type == p.QUIT:
            p.quit()
            sys.exit()
        if event.type == SCREEN_UPDATE:
            p.display.update()
        if event.type == p.MOUSEBUTTONDOWN:
            if turn % 2 != 0:
                player1_move()
                winning_move(1)
                turn += 1
            else:
                comp_move()
                winning_move(2)
                turn += 1

非常欢迎任何建议

您必须提供一个评估函数来估计搜索树叶子的分数(当深度==5 时)。 就像现在一样,所有可能的移动顺序都会导致 800 或 -800,因此实际上每次都会选择第一步。