我的国际象棋引擎搜索没有返回任何内容

问题描述 投票:0回答:1

当我离开我的国际象棋引擎中的书本时,它不会返回任何内容。

import random

import chess
import chess.polyglot
import time
from evaluate import *

max_depth = 18
move_points = []
def nextMove(board, color)->(chess.Move):
    try:
        best_move = chess.polyglot.MemoryMappedReader("books/human.bin").weighted_choice(board).move
        return best_move
    except IndexError:
        best_move = iterativeDeepening(board, color,5)
    return best_move


def iterativeDeepening(board, color, max_time):
    start = time.time()
    for move in board.legal_moves:
        move_points.append((0,move))
    best_move = None
    for i in range(1, max_depth + 1):
        points,best_move = minimax(i, board, float('inf'), -float('inf'), ai_color==board.turn,None, color, )
        if time.time() - start >= max_time:
            break
    move_points.clear()
    return best_move



def minimax(depth, board, alpha, beta, maxPlayer, curmove, ai_color):
    if depth == 0:
        for move in board.legal_moves:
            if move == curmove:
                points = quiescence(1, board, alpha, beta, maxPlayer)
        quiescence(1, board, alpha, beta, maxPlayer)

        if eval(board) == (float('inf') or -float('inf')) and board.turn == ai_color:
            print("mate you lose")
            exit(0)
        if eval(board) == (float('inf') or -float('inf')) and board.turn != ai_color:
            print("mate you win")
            exit(0)
        if eval(board) == "draw":
            print("draw")
            exit(0)
        return -eval(board), curmove
    if maxPlayer:
        maxEval = -float('inf')
        for move in board.legal_moves:
            board.push(move)
            evalu, _ = minimax(depth - 1, board, alpha, beta, False, move, ai_color)
            board.pop()
            maxEval = max(maxEval, evalu)
            alpha = max(alpha, evalu)
            if beta <= alpha:
                break
        return maxEval, curmove
    else:
        minEval = float('inf')
        for move in board.legal_moves:
            board.push(move)
            evalu,_ = minimax(depth - 1, board, alpha, beta, True, move, ai_color)
            board.pop()
            minEval = min(minEval, evalu)
            beta = min(beta, evalu)
            if beta <= alpha:
                break
        return minEval, curmove


def quiescence(depth, board, alpha, beta, maximizingPlayer):
    if depth == 0:
        return -eval(board)
    if maximizingPlayer:
        maxEval = -float('inf')
        for move in board.legal_moves:
            if board.is_capture(move):
                board.push(move)
                evalu = quiescence(depth - 1, board, alpha, beta, False)
                board.pop()
                maxEval = max(maxEval, evalu)
                alpha = max(alpha, evalu)
                if beta <= alpha:
                    break
        if(maxEval==None):
            return eval(board)
        return maxEval
    else:
        minEval = float('inf')
        for move in board.legal_moves:
            if board.is_capture(move):
                board.push(move)
                evalu = quiescence(depth - 1, board, alpha, beta, True)
                board.pop()
                minEval = min(minEval, evalu)
                beta = min(beta, evalu)
                if beta <= alpha:
                    break
        return minEval

我尝试了所有方法,但不知道出了什么问题。

有人能指出问题吗?

这是它返回的错误:

Traceback (most recent call last):
  File "/Users/user/PycharmProjects/chess bot/main.py", line 21, in <module>
    board.push(result)
  File "/Users/user/PycharmProjects/chess bot/venv/lib/python3.9/site-packages/chess/__init__.py", line 2224, in push
    move = self._to_chess960(move)
  File "/Users/user/PycharmProjects/chess bot/venv/lib/python3.9/site-packages/chess/__init__.py", line 3664, in _to_chess960
    if move.from_square == E1 and self.kings & BB_E1:
AttributeError: 'NoneType' object has no attribute 'from_square'
AI move: None
python chess minimax python-chess
1个回答
0
投票
if best_move is None:
print("No move found in the book or iterative deepening. Choosing a random move.")
return random.choice(list(board.legal_moves))

其他: 返回最佳移动


这样,如果迭代加深后 best_move 仍然为 None,您将从当前棋盘的合法走法中随机选择一个走法。根据您的要求调整此逻辑。

© www.soinside.com 2019 - 2024. All rights reserved.