Python 脚本在工作过程中变得无响应,但之后继续工作并正确结束
我正在使用Python 脚本在工作过程中变得无响应,但之后继续工作并正确结束,python,pygame,game-development,chess,minimax,Python,Pygame,Game Development,Chess,Minimax,我正在使用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, i
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
def minimax(self、depth、alpha、beta、is_maxing_white):
#递归树退出条件
#高速缓存中的if板
如果self.board_缓存中的self.hash_board(深度,最大值为白色):
返回self.board\u缓存[self.hash\u board(深度,为最大值\u白色)]
#达到所需深度
如果深度=0:
#返回自我静止(α,β)
return self.evaluation()
#如果是将死还是相持
如果不是[*自板。获得所有的连环动作(自板侧)]:
self.board\u缓存[self.hash\u board](
深度,是否最大化(白色)]=自我评估()
返回self.board\u缓存[self.hash\u board(深度,为最大值\u白色)]
最佳分数=-浮点(“inf”),如果是最大值,则为白色,否则为浮点(“inf”)
对于在self.board.get_中移动的所有ligal_移动(self.side):
自我板推(移动,自我侧)
局部得分=自最小最大值(深度-1,α,β,非最大值为白色)
self.board\u缓存[self.hash\u board](
深度-1,不是最大值(白色)]=本地分数
如果为白色,则为最大值:
最佳分数=最大值(最佳分数、本地分数)
阿尔法=最大值(阿尔法,最佳分数)
其他:
最佳成绩=最低(最佳成绩、本地成绩)
贝塔=最低(贝塔,最佳成绩)
self.board.pop()
如果pygame程序长时间未能调用pygame.event.get()
或pygame.event.pump()
有一些重要的事情必须在事件队列内部处理。主窗口可能需要重新绘制或响应系统。如果长时间未能调用事件队列,系统可能会判定您的程序已锁定
从
如果您确保偶尔在minimax函数中调用pygame.event.pump()
,操作系统就不会认为您的程序崩溃了。因此,您可以单击该窗口,而不会得到“此窗口没有响应”或任何消息
希望这能解决你的问题,而不是别的问题。“[…]在她不回答任何输入的时候,会使它崩溃。”-这很难理解。什么样的输入会使它崩溃?是否显示任何错误消息?程序会对鼠标单击作出反应。现在,在搜索过程中,鼠标输入不会使程序崩溃,但程序窗口在此部分仍处于状态(无响应)。工作正确结束且无错误输出使用循环将干扰主循环,因此我猜导致错误的原因是循环的for
。你可以试着把它放在另一个线程中,看看会发生什么。谢谢,伙计,这正是我一直在寻找的for@Makc22999很高兴听到!既然这解决了问题,如果你能接受这一正确答案,我将不胜感激。