簡體   English   中英

腳本在工作期間變得無響應,但之后繼續工作並正確結束

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

我正在使用pygame在 Python (但不是最佳選擇)上實施國際象棋。 為了找到移動,我使用標准對 minimax + alpha pruning,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

當pygame程序長時間調用pygame.event.get()pygame.event.pump()失敗時,操作系統認為程序崩潰。

有一些重要的事情必須在事件隊列內部處理。 主window可能需要重繪或響應系統。 如果您長時間未能調用事件隊列,系統可能會判定您的程序已鎖定。

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

如果你確保偶爾在極小 function 中調用pygame.event.pump() ,操作系統不會認為你的程序已經崩潰。 因此,您將能夠單擊 window 而不會出現“此 window 沒有響應”或任何內容。

希望這會解決您的問題,而不是其他問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM