How to use the chess.Move.from_uci function in chess

To help you get started, we’ve selected a few chess examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github rpdelaney / python-chess-annotator / tests / test_functions.py View on Github external
def test_long_mating_pv(self):
        """ A long pv that ends the game should not be truncated """

        board = chess.Board('1Q3bk1/5p2/2p3p1/1p1bN2p/4n2P/8/r5PK/8 b - - 1 34')  # noqa E501
        line = [chess.Move.from_uci('g8g7'), chess.Move.from_uci('e5f7'),
                chess.Move.from_uci('d5f7'), chess.Move.from_uci('b8e5'),
                chess.Move.from_uci('e4f6'), chess.Move.from_uci('h2h3'),
                chess.Move.from_uci('b5b4'), chess.Move.from_uci('g2g4'),
                chess.Move.from_uci('f8d6'), chess.Move.from_uci('e5d6'),
                chess.Move.from_uci('h5g4'), chess.Move.from_uci('h3g3'),
                chess.Move.from_uci('f6e4'), chess.Move.from_uci('g3f4'),
                chess.Move.from_uci('e4d6'), chess.Move.from_uci('f4e5'),
                chess.Move.from_uci('b4b3'), chess.Move.from_uci('e5d6'),
                chess.Move.from_uci('b3b2'), chess.Move.from_uci('h4h5'),
                chess.Move.from_uci('g6h5'), chess.Move.from_uci('d6d7'),
                chess.Move.from_uci('b2b1q'), chess.Move.from_uci('d7c7'),
                chess.Move.from_uci('b1b4'), chess.Move.from_uci('c7c6'),
                chess.Move.from_uci('a2c2'), chess.Move.from_uci('c6d7'),
                chess.Move.from_uci('b4b8'), chess.Move.from_uci('d7e7'),
                chess.Move.from_uci('b8c7')]
        result = annotator.truncate_pv(board, line)
        self.assertEqual(result, line)
github rpdelaney / python-chess-annotator / tests / test_functions.py View on Github external
def test_long_non_mating_pv(self):
        """
        A long pv that does not end the game should be truncated to 10 moves
        """

        board = chess.Board('1Q3bk1/5p2/2p3p1/1p1bN2p/4n2P/8/r5PK/8 b - - 1 34')  # noqa E501
        line = [chess.Move.from_uci('g8g7'), chess.Move.from_uci('e5f7'),
                chess.Move.from_uci('d5f7'), chess.Move.from_uci('b8e5'),
                chess.Move.from_uci('e4f6'), chess.Move.from_uci('h2h3'),
                chess.Move.from_uci('b5b4'), chess.Move.from_uci('g2g4'),
                chess.Move.from_uci('f8d6'), chess.Move.from_uci('e5d6'),
                chess.Move.from_uci('h5g4'), chess.Move.from_uci('h3g3'),
                chess.Move.from_uci('f6e4'), chess.Move.from_uci('g3f4'),
                chess.Move.from_uci('e4d6'), chess.Move.from_uci('f4e5'),
                chess.Move.from_uci('b4b3'), chess.Move.from_uci('e5d6'),
                chess.Move.from_uci('b3b2'), chess.Move.from_uci('h4h5'),
                chess.Move.from_uci('g6h5'), chess.Move.from_uci('d6d7'),
                chess.Move.from_uci('b2b1q'), chess.Move.from_uci('d7c7'),
                chess.Move.from_uci('b1b4'), chess.Move.from_uci('c7c6'),
                chess.Move.from_uci('a2c2'), chess.Move.from_uci('c6d7'),
                chess.Move.from_uci('b4b8'), chess.Move.from_uci('d7e7')]
        target = line[:annotator.SHORT_PV_LEN]
        result = annotator.truncate_pv(board, line)
        self.assertEqual(len(result), annotator.SHORT_PV_LEN)
        self.assertEqual(result, target)
github rpdelaney / python-chess-annotator / tests / test_functions.py View on Github external
def test_raises_assertionerror(self):
        """ A line with an illegal move should raise an AssertionError """

        board = chess.Board('1Q3bk1/5p2/2p3p1/1p1bN2p/4n2P/8/r5PK/8 b - - 1 34')  # noqa E501
        line = [chess.Move.from_uci('g8g7'), chess.Move.from_uci('e5f7'),
                chess.Move.from_uci('d5f7'), chess.Move.from_uci('b8e5'),
                chess.Move.from_uci('e4f6'), chess.Move.from_uci('h2h3'),
                chess.Move.from_uci('b5b4'), chess.Move.from_uci('g2g4'),
                chess.Move.from_uci('f8d6'), chess.Move.from_uci('e5d6'),
                chess.Move.from_uci('h5g4'), chess.Move.from_uci('h3g3'),
                chess.Move.from_uci('f6e4'), chess.Move.from_uci('g3f4'),
                chess.Move.from_uci('e4d6'), chess.Move.from_uci('f4e5'),
                chess.Move.from_uci('b4b3'), chess.Move.from_uci('e5d6'),
                chess.Move.from_uci('b3b2'), chess.Move.from_uci('h4h5'),
                chess.Move.from_uci('g6h5'), chess.Move.from_uci('d6c8'),
                chess.Move.from_uci('b2b1q'), chess.Move.from_uci('d7c7'),
                chess.Move.from_uci('b1b4'), chess.Move.from_uci('c7c6'),
                chess.Move.from_uci('a2c2'), chess.Move.from_uci('c6d7'),
                chess.Move.from_uci('b4b8'), chess.Move.from_uci('d7e7'),
                chess.Move.from_uci('b8c7')]
        self.assertRaises(AssertionError, annotator.truncate_pv, board, line)
github olinrobotics / hiro / hiro_archive / Fall_2018 / chess / chesstest2.py View on Github external
print(loc)

for i in range(8):
    for j in range(8):
        if previous_board[i][j] <> current_board[i][j] and current_board[i][j] <> 0:
            loc = loc + str(i) + str(j)


print(loc)

change_to_cord = {'0':'a', '1':'b', '2':'c', '3':'d', '4':'e', '5':'f', '6':'g', '7':'h'}

loc = change_to_cord[loc[0]] + str(int(loc[1])+1) + change_to_cord[loc[2]] + str(int(loc[3])+1)

move = chess.Move.from_uci(loc)

print(move)

print(board.is_capture(move))

board.push(move)

print(board)
github niklasf / python-chess / chess / uci.py View on Github external
def handle_move_token(token, fn):
            try:
                move = chess.Move.from_uci(token)
            except ValueError:
                LOGGER.exception("exception parsing move token from info: %r", arg)
                return

            for info_handler in self.info_handlers:
                fn(info_handler, move)
github niklasf / python-chess / chess / engine.py View on Github external
try:
                kind = tokens.pop(0)
                value = tokens.pop(0)
                if tokens and tokens[0] in ["lowerbound", "upperbound"]:
                    info[tokens.pop(0)] = True
                if kind == "cp":
                    info["score"] = PovScore(Cp(int(value)), root_board.turn)
                elif kind == "mate":
                    info["score"] = PovScore(Mate(int(value)), root_board.turn)
                else:
                    LOGGER.error("unknown score kind %r in info (expected cp or mate): %r", kind, arg)
            except (ValueError, IndexError):
                LOGGER.error("exception parsing score from info: %r", arg)
        elif parameter == "currmove":
            try:
                info["currmove"] = chess.Move.from_uci(tokens.pop(0))
            except (ValueError, IndexError):
                LOGGER.error("exception parsing currmove from info: %r", arg)
        elif parameter == "currline" and selector & INFO_CURRLINE:
            try:
                if "currline" not in info:
                    info["currline"] = {}

                cpunr = int(tokens.pop(0))
                info["currline"][cpunr] = currline = []

                board = root_board.copy(stack=False)
                while tokens and UCI_REGEX.match(tokens[0]):
                    currline.append(board.push_uci(tokens.pop(0)))
            except (ValueError, IndexError):
                LOGGER.error("exception parsing currline from info: %r, position at root: %s", arg, root_board.fen())
        elif parameter == "refutation" and selector & INFO_REFUTATION:
github securesystemslab / zippy / zippy / benchmarks / src / benchmarks / python-chess-bench.py View on Github external
def move_from_uci():
    board = chess.Bitboard()
    assert not chess.Move.from_uci("a8a1") in board.legal_moves
github johncheetham / jcchess / jcchess / jcchess.py View on Github external
def redo_move(self):
        move = None
        try:
            move = self.redolist.pop()

            # get side to move before appending to movelist
            self.stm = self.get_side_to_move()
            self.movelist.append(move)
            
            # do the move in jcchess engine
            #engine.setplayer(self.stm)
            #engine.hmove(move)
            cmove = chess.Move.from_uci(move)
            gv.board.add_move(cmove)
        except IndexError:
            pass
github FTdiscovery / 64CrazyhouseDeepLearning / NNHumanCompetition.py View on Github external
move = input("Enter move: ")
                if len(move) == 4 or len(move) == 5:
                    if chess.Move.from_uci(move) in sim.board.legal_moves:
                        legal = True
                    else:
                        print("Illegal move! Try again:")
                else:
                    print("Illegal move! Try again:")
            print(move)
            sim.makeMove(move)
            sim.gameResult()

        if sim.plies == 1:
            node = PGN.add_variation(chess.Move.from_uci(move))
        else:
            node = node.add_variation(chess.Move.from_uci(move))

        print(sim.board)
        print("WHITE POCKET")
        print(sim.whiteCaptivePieces)
        print("BLACK POCKET")
        print(sim.blackCaptivePieces)
        

    if sim.result == 1:
        PGN.headers["Result"] = "1-0"
    if sim.result == 0:
        PGN.headers["Result"] = "1/2-1/2"
    if sim.result == -1:
        PGN.headers["Result"] = "0-1"

    print(PGN)

chess

A chess library with move generation and validation, Polyglot opening book probing, PGN reading and writing, Gaviota tablebase probing, Syzygy tablebase probing, and XBoard/UCI engine communication.

GPL-3.0
Latest version published 1 month ago

Package Health Score

82 / 100
Full package analysis