Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support uci go perft #951

Open
thomasahle opened this issue Jan 8, 2023 · 3 comments
Open

Support uci go perft #951

thomasahle opened this issue Jan 8, 2023 · 3 comments
Labels
engine Chess engine integration

Comments

@thomasahle
Copy link

thomasahle commented Jan 8, 2023

Many engines, such as Stockfish, support a non-standard UCI command go perft <depth>. This would be very useful to have in python-chess as a way to test engines.

This could be implemented using a Limit(). Alternatively, is there a good way to send custom commands and read the output through python-chess?

@thomasahle
Copy link
Author

thomasahle commented Jan 8, 2023

I ended up writing this code:

async def uci_perft(engine, depth):
    class UciPerftCommand(BaseCommand[UciProtocol, None]):
        def __init__(self, engine: UciProtocol):
            super().__init__(engine)
            self.moves = []

        def start(self, engine: UciProtocol) -> None:
            engine.send_line(f"go perft {depth}")

        def line_received(self, engine: UciProtocol, line: str) -> None:
            match = re.match("(\w+): (\d+)", line)
            if match:
                move = chess.Move.from_uci(match.group(1))
                cnt = int(match.group(2))
                self.moves.append((move, cnt))

            match = re.match("Nodes searched: (\d+)", line)
            if match:
                self.result.set_result(self.moves)
                self.set_finished()

    return await engine.communicate(UciPerftCommand)

@niklasf niklasf added the engine Chess engine integration label Jan 8, 2023
@niklasf
Copy link
Owner

niklasf commented Jan 12, 2023

This is mostly Stockfish and derivates, right? Maybe not standardized enough to add to the API, but your snippet makes for a good example of custom engine communication. Then again, adding it to the documentation means that engine.communicate() would become a stable/public interface. I am on the fence ...

@thomasahle
Copy link
Author

I noticed it in a few other engines:

But I guess it's bad to support non-standard syntax.

A supported way to do non-standard communication would be nice though. It allows writing general tools for testing chess engines. The engines only have to support a few "debug" commands (like perft) and the testing tool can run a suite of tests.

Btw, did you see this effort to formalize uci? https://talkchess.com/forum3/viewtopic.php?f=7&t=81233

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
engine Chess engine integration
Projects
None yet
Development

No branches or pull requests

2 participants