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

2024-05-16 14:18:32 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用pygame在Python上实现chess(但不是最佳选择)。为了找到移动,我使用标准的pair 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

脚本返回正确的求值值,通常可以工作,但在不回答任何输入的情况下,脚本可能会崩溃。我应该朝哪个方向思考,是否有可能以某种方式禁用不负责任的行为

Windows 10、python 3.7、pygame 1.9


Tags: selfalphaboardreturnifishashbeta
1条回答
网友
1楼 · 发布于 2024-05-16 14:18:32

当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(),操作系统就不会认为您的程序崩溃了。因此,您可以单击该窗口,而不会得到“此窗口没有响应”或任何消息

希望这能解决你的问题,而不是别的问题

相关问题 更多 >