Skip to main content

A chess library.

Project description

Introduction

This is the scholars mate in python-chess:

pos = chess.Position()
pos.make_move_from_san("e4")
pos.make_move_from_san("e5")
pos.make_move_from_san("Qh5")
pos.make_move_from_san("Nc6")
pos.make_move_from_san("Bc4")
pos.make_move_from_san("Nf6")
pos.make_move_from_san("Qxf7")
assert pos.is_checkmate()

Features

  • Legal move generator and move validation. This includes all castling rules and en-passant captures.

    assert not chess.Move.from_uci("a8a1") in pos.get_legal_moves()
  • Detects checkmates, stalemates and draws by insufficient material. Has a half-move clock.

    assert not pos.is_stalemate()
    assert not pos.is_insufficient_material()
    assert pos.is_game_over()
  • Detects checks and can enumerate attackers and defenders of a square.

    assert pos.is_check()
    assert chess.Square("f7") in pos.get_attackers("w", chess.Square("e8"))
  • Parses and creates SAN representation of moves.

    pos = chess.Position()
    assert "e4" == pos.make_move(chess.Move("e2e4")).san
  • Parses and creates FENs.

    assert pos.fen == "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"
    pos = chess.Position("8/8/8/2k5/4K3/8/8/8 w - - 4 45")
    assert pos["c5"] == chess.Piece("k")
  • Read Polyglot opening books.

    book = chess.PolyglotOpeningBook("data/opening-books/performance.bin")
    pos = chess.Position()
    # for entry in book.get_entries_for_position(pos):
    #    assert chess.Move.from_uci("e2e4") == entry["move"]
    #    break

Peformance

python-chess is not intended to be used by chess engines where performance is critical. The goal is rather to create a simple and highlevel library.

However parts like move generation are in C++ (see the libchess directory) to improve the performance over pure Python code.

Building

libboost-regex-dev and libboost-python-dev are required.

python setup.py build
sudo python.setup.py install

License

python-chess is licensed under the GPL3. See the LICENSE file for the full copyright and license information.

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

python-chess-0.0.2.tar.gz (45.8 kB view hashes)

Uploaded Source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page