.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto-examples/walkthrough.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto-examples_walkthrough.py: Basic Walkthrough ================= First, lets import everything from ``bulletchess`` .. GENERATED FROM PYTHON SOURCE LINES 7-10 .. code-block:: Python from bulletchess import * .. GENERATED FROM PYTHON SOURCE LINES 11-13 The :func:`Board()` constructor returns a :class:`Board` representing the starting position. .. GENERATED FROM PYTHON SOURCE LINES 13-17 .. code-block:: Python board = Board() board .. raw:: html


.. GENERATED FROM PYTHON SOURCE LINES 18-21 The :class:`Board` class defines :func:`Board._repr_html_()`, which allows positions to be rendered like the above in Jupyter note books, or Sphinx documentation like this page. For displaying a :class:`Board` as plain text, we use :func:`Board.__str__()`. .. GENERATED FROM PYTHON SOURCE LINES 21-24 .. code-block:: Python print(str(board)) .. rst-class:: sphx-glr-script-out .. code-block:: none r n b q k b n r p p p p p p p p - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - P P P P P P P P R N B Q K B N R .. GENERATED FROM PYTHON SOURCE LINES 25-26 Other positions can be specified by either using :func:`Board.from_fen()`, .. GENERATED FROM PYTHON SOURCE LINES 26-30 .. code-block:: Python board = Board.from_fen("rnbqkbnr/pp1ppppp/8/2p5/4P3/5N2/PPPP1PPP/RNBQKB1R b KQkq - 1 2") board .. raw:: html


.. GENERATED FROM PYTHON SOURCE LINES 31-32 Or by assigning :class:`Piece` locations manually. We can use :func:`Board.empty()` to start from a clean slate. .. GENERATED FROM PYTHON SOURCE LINES 32-36 .. code-block:: Python board = Board.empty() board .. raw:: html


.. GENERATED FROM PYTHON SOURCE LINES 37-39 And then assign a :class:`Piece` to each :class:`Square`. .. GENERATED FROM PYTHON SOURCE LINES 39-53 .. code-block:: Python board[G2] = Piece(WHITE, KING) board[F2] = Piece(WHITE, PAWN) board[G3] = Piece(WHITE, PAWN) board[H2] = Piece(WHITE, PAWN) board[B3] = Piece(WHITE, ROOK) board[F7] = Piece(BLACK, KING) board[D7] = Piece(BLACK, ROOK) board[F6] = Piece(BLACK, PAWN) board[G7] = Piece(BLACK, PAWN) board .. raw:: html


.. GENERATED FROM PYTHON SOURCE LINES 54-56 We can get the FEN of this position with :func:`Board.fen()`. We might want to set the half-move clock and full-move number for this endgame position. Let's also make it black's turn. .. GENERATED FROM PYTHON SOURCE LINES 56-63 .. code-block:: Python board.halfmove_clock = 3 board.fullmove_number = 43 board.turn = BLACK board.fen() .. rst-class:: sphx-glr-script-out .. code-block:: none '8/3r1kp1/5p2/8/8/1R4P1/5PKP/8 b - - 3 43' .. GENERATED FROM PYTHON SOURCE LINES 64-67 Indexing a :class:`Board` with a :class:`Color`, :class:`PieceType`, or both returns :class:`Bitboard` of squares with the relevant pieces. A :class:`Bitboard` is simply an efficient representation of a set of squares. .. GENERATED FROM PYTHON SOURCE LINES 67-72 .. code-block:: Python print(board[WHITE]) print(board[ROOK]) print(board[BLACK, PAWN]) .. rst-class:: sphx-glr-script-out .. code-block:: none 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 .. GENERATED FROM PYTHON SOURCE LINES 73-74 We can generate legal :class:`Move` objects for this position with :func:`Board.legal_moves()` .. GENERATED FROM PYTHON SOURCE LINES 74-78 .. code-block:: Python moves = board.legal_moves() print(moves) .. rst-class:: sphx-glr-script-out .. code-block:: none [, , , , , , , , , , , , , , , , , , , ] .. GENERATED FROM PYTHON SOURCE LINES 79-81 Let's move our rook. To perform a move, we use :func:`Board.apply()`. Moves can be created manually with the :func:`Move()` constructor. .. GENERATED FROM PYTHON SOURCE LINES 81-87 .. code-block:: Python selected_move = Move(D7, D3) board.apply(selected_move) board .. raw:: html


.. GENERATED FROM PYTHON SOURCE LINES 88-91 Oh, but that was a blunder. Moves can be undone with :func:`Board.undo()`, which returns the last :class:`Move` applied. Getting the ``str`` of a :class:`Move` renders the move in UCI long algebraic notation. .. GENERATED FROM PYTHON SOURCE LINES 91-96 .. code-block:: Python print("Undoing " + str(board.undo())) board .. rst-class:: sphx-glr-script-out .. code-block:: none Undoing d7d3 .. raw:: html


.. GENERATED FROM PYTHON SOURCE LINES 97-98 Let's run the game through for a bit. Moves can also be constructed from UCI or standard algebraic notation. .. GENERATED FROM PYTHON SOURCE LINES 98-107 .. code-block:: Python board.apply(Move.from_san("g5", board)) board.apply(Move.from_uci("h2h4")) board.apply(Move(G5, H4)) board.apply(Move(G3, H4)) board.apply(Move.from_san("Kg7", board)) board.apply(Move.from_san("Rg3", board)) board .. raw:: html


.. GENERATED FROM PYTHON SOURCE LINES 108-109 Black exposed their king, and is now in check. .. GENERATED FROM PYTHON SOURCE LINES 109-112 .. code-block:: Python board in CHECK .. rst-class:: sphx-glr-script-out .. code-block:: none True .. GENERATED FROM PYTHON SOURCE LINES 113-114 But the game is still ongoing... .. GENERATED FROM PYTHON SOURCE LINES 114-117 .. code-block:: Python board in CHECKMATE or board in DRAW .. rst-class:: sphx-glr-script-out .. code-block:: none False .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.005 seconds) .. _sphx_glr_download_auto-examples_walkthrough.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: walkthrough.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: walkthrough.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: walkthrough.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_