我正在使用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
当pygame程序长时间无法调用
pygame.event.get()
或pygame.event.pump()
时,操作系统会认为程序崩溃从https://www.pygame.org/docs/ref/event.html#pygame.event.pump
如果您确保偶尔在minimax函数中调用
pygame.event.pump()
,操作系统就不会认为您的程序崩溃了。因此,您可以单击该窗口,而不会得到“此窗口没有响应”或任何消息希望这能解决你的问题,而不是别的问题
相关问题 更多 >
编程相关推荐