Python chess.uci() Examples

The following are 4 code examples of chess.uci(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module chess , or try the search function .
Example #1
Source File: engine_wrapper.py    From lichess-bot with GNU Affero General Public License v3.0 6 votes vote down vote up
def __init__(self, board, commands, options, silence_stderr=False):
        commands = commands[0] if len(commands) == 1 else commands
        self.go_commands = options.get("go_commands", {})

        self.engine = chess.uci.popen_engine(commands, stderr = subprocess.DEVNULL if silence_stderr else None)
        self.engine.uci()

        if options:
            self.engine.setoption(options)

        self.engine.setoption({
            "UCI_Variant": type(board).uci_variant,
            "UCI_Chess960": board.chess960
        })
        self.engine.position(board)

        info_handler = chess.uci.InfoHandler()
        self.engine.info_handlers.append(info_handler) 
Example #2
Source File: picochess.py    From picochess with GNU General Public License v3.0 6 votes vote down vote up
def book(self, bookreader, game_copy: chess.Board):
        """Get a BookMove or None from game position."""
        try:
            choice = bookreader.weighted_choice(game_copy, self.excludemoves)
        except IndexError:
            return None

        book_move = choice.move()
        self.add(book_move)
        game_copy.push(book_move)
        try:
            choice = bookreader.weighted_choice(game_copy)
            book_ponder = choice.move()
        except IndexError:
            book_ponder = None
        return chess.uci.BestMove(book_move, book_ponder) 
Example #3
Source File: make_data.py    From fastchess with GNU General Public License v3.0 5 votes vote down vote up
def run_thread(thread_id, module, example_queue):
    selfplay_model = args.model and module.Model(args.model)

    engine = chess.uci.popen_engine(STOCKFISH_PATH)
    info_handler = chess.uci.InfoHandler()
    engine.info_handlers.append(info_handler)
    engine.uci()
    engine.isready()
    start, last = time.time(), 0
    for i in range(N_GAMES // THREADS):
        # Predicting progress and ETA
        if thread_id == 0 and time.time() - last > .5:
            pg = i * THREADS / N_GAMES
            if i == 0:
                pg += 1 / 1000000
            etr = (time.time() - start) * (1 / pg - 1)
            print('Progress: {:.1f}%. Remaining: {}'
                  .format(pg * 100, str(timedelta(seconds=int(etr)))),
                  file=sys.stderr, end='\r')
            last = time.time()

        # Play a game
        for line in play_game(engine, info_handler, module, selfplay_model):
            example_queue.put(line)
    example_queue.put(None)

    if thread_id == 0:
        print()
        print('Finishing remaining threads...', file=sys.stderr) 
Example #4
Source File: chessbot.py    From python-zulip-api with Apache License 2.0 4 votes vote down vote up
def initialize(self, bot_handler: Any) -> None:
        self.config_info = bot_handler.get_config_info('chess')

        try:
            self.engine = chess.uci.popen_engine(
                self.config_info['stockfish_location']
            )
            self.engine.uci()
        except FileNotFoundError:
            # It is helpful to allow for fake Stockfish locations if the bot
            # runner is testing or knows they won't be using an engine.
            print('That Stockfish doesn\'t exist. Continuing.')