脚本在工作期间变得无响应,但之后继续工作并正确结束

Script becomes unresponsive during work, but continues to work after that and ends correctly

我正在使用 pygame 在 Python 上实施国际象棋(但不是最佳选择)。为了找到移动,我使用标准对 minimax + alpha 修剪,minimax 是一个递归搜索树,所以程序大部分时间都会做这部分。

def minimax(self, depth, alpha, beta, is_maxing_white):
    # recursive tree exit condition

    # if board in cache
    if self.hash_board(depth, is_maxing_white) in self.board_caches:
        return self.board_caches[self.hash_board(depth, is_maxing_white)]

    # reached the desired depth
    if depth == 0:
        #return self.quiesce(alpha, beta)
        return self.evaluation()

    # if checkmate or stalemate
    if not [*self.board.get_all_ligal_moves(self.side)]:
        self.board_caches[self.hash_board(
            depth, is_maxing_white)] = self.evaluation()
        return self.board_caches[self.hash_board(depth, is_maxing_white)]

    best_score = -float("inf") if is_maxing_white else float("inf")
    for move in self.board.get_all_ligal_moves(self.side):
        self.board.push(move, self.side)

        local_score = self.minimax(depth - 1, alpha, beta, not is_maxing_white)

        self.board_caches[self.hash_board(
            depth - 1, not is_maxing_white)] = local_score

        if is_maxing_white:
            best_score = max(best_score, local_score)
            alpha = max(alpha, best_score)
        else:
            best_score = min(best_score, local_score)
            beta = min(beta, best_score)

        self.board.pop()

        if beta <= alpha:
            print ("pruning")
            break

    return best_score

脚本 returns 正确的评估值并且通常有效,但在它不回答任何输入的时候可能会崩溃。我应该朝哪个方向思考,是否有可能以某种方式禁止不负责任的行为?

Windows10,python3.7,pygame1.9

当pygame程序长时间调用pygame.event.get()pygame.event.pump()失败时,操作系统认为程序崩溃。

There are important things that must be dealt with internally in the event queue. The main window may need to be repainted or respond to the system. If you fail to make a call to the event queue for too long, the system may decide your program has locked up.

来自https://www.pygame.org/docs/ref/event.html#pygame.event.pump

如果您确保偶尔在 minimax 函数中调用 pygame.event.pump(),OS 就不会认为您的程序崩溃了。因此,您可以单击 window 而不会收到“此 window 没有响应”或任何内容。

希望这能解决您的问题,而不是其他问题。