Skip to main content

Python3 library for efficient chess draw-gen functions

Project description

PyPI Release Docker CI

ChessLib Python Extension

About

This project provides an efficient chess draw generation extension for Python3. The main purpose of this project is enabling further TensorFlow AI projects and learning how to write an efficient Python3 extension (using good old C).

Usage

Install the official Python package using pip:

pip install chesslib

For implementing a chess AI, see following example applying randomly selected actions until the chess game is over. You might use this code snippet and make your AI choose more sophisticated actions instead of random ones, that's already it - as simple as that.

import chesslib
import numpy as np


def choose_action(board, drawing_side, last_draw):
    poss_actions = chesslib.GenerateDraws(board, drawing_side, last_draw, True)
    return np.random.choice(poss_actions)


def main():

    # init cache variables
    last_draw = chesslib.ChessDraw_Null
    board = chesslib.ChessBoard_StartFormation()
    drawing_side = chesslib.ChessColor_White

    # init game state
    state = chesslib.GameState_None
    game_over_states = [chesslib.GameState_Checkmate, chesslib.GameState_Tie]

    # log the start formation to console
    print('start of game')
    print(chesslib.VisualizeBoard(board))

    # continue until the game is over
    while state not in game_over_states:

        # choose an action and apply it to the board
        next_action = choose_action(board, drawing_side, last_draw)
        board = chesslib.ApplyDraw(board, next_action)

        # update the cache variables
        last_draw = next_action
        drawing_side = 1 - drawing_side # alternate between white and black side
        state = chesslib.GameState(board, last_draw)

        # log the game progress to console
        print()
        print(chesslib.VisualizeDraw(next_action), f'state={state}')
        print(chesslib.VisualizeBoard(board))

    # log the game's outcome to console
    winning_side = "black" if drawing_side == chesslib.ChessColor_White else "white"
    print('tie' if state == chesslib.GameState_Tie else f'{winning_side} player won')


if __name__ == '__main__':
    main()

Following example outlines some of the chesslib API functionality like applying / reverting draws, creating board hashes or visualizing boards / draws properly as human-readable texts:

import chesslib
import numpy as np


def main():

    # create a new chess board in start formation
    board = chesslib.ChessBoard_StartFormation()
    
    # generate all possible draws
    draws = chesslib.GenerateDraws(board, chesslib.ChessColor_White, chesslib.ChessDraw_Null, True)
    
    # apply one of the possible draws
    draw_to_apply = draws[random.randint(0, len(draws) - 1)]
    new_board = chesslib.ApplyDraw(board, draw_to_apply)
    
    # write the draw's name
    print(chesslib.VisualizeDraw(draw_to_apply))
    
    # visualize the board before / after applying the draw
    print(chesslib.VisualizeBoard(board))
    print(chesslib.VisualizeBoard(new_board))
    
    # revert the draw (just call ApplyDraw again with the new board)
    rev_board = chesslib.ApplyDraw(new_board, draw_to_apply)
    
    # get the board's 40-byte-hash and create a new board instance from the hash
    board_hash = chesslib.Board_ToHash(board)
    board_reloaded = chesslib.Board_FromHash(board_hash)
    
    # see tests/ folder for more examples


if __name__ == '__main__':
    main()

How to Develop

For a quickstart, set up your dev machine as a VM (e.g. Ubuntu 20.04 hosted by VirtualBox). After successfully creating the VM, use following commands to install all essential dev tools (git, docker, good text editor).

# install docker (e.g. Ubuntu 20.04)
sudo apt-get update && sudo apt-get install -y git docker.io
sudo usermod -aG docker $USER && reboot

# download the project's source code (if you haven't done before)
git clone https://github.com/Bonifatius94/ChessLib.Py
cd ChessLib.Py

# install the 'Visual Studio Code' text editor (optional)
sudo snap install code --classic

The commands for dev-testing the chesslib are wrapped within a Docker environment. Therefore build the 'Dockerfile-dev' file which takes your source code and performs all required CI steps (build + install + unit tests). Afterwards you may attach to the Docker image with the bash console interactively and run commands, etc.

# make a dev build using the 'Dockerfile-dev' build environment
# this runs all CI steps (build + install + unit tests)
docker build . --file Dockerfile-dev -t "chesslib-dev"

# attach to the build environment's interactive bash console (leave the session with 'exit')
docker run -it "chesslib-dev" bash

# mount a Python test script into the build environment and run it
docker run -v $PWD:/scripts "chesslib-dev" python3 /scripts/test.py

# debug the GitHub release pipeline
docker run -v $PWD/dist:/output -v $PWD:/build -it "chesslib-dev" bash

See the GitHub wiki for more details.

Copyright

This software is available under the MIT licence's terms.

Project details


Download files

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

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

chesslib-1.0.340201150-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.whl (56.8 kB view details)

Uploaded CPython 3.9 manylinux: glibc 2.5+ x86-64

chesslib-1.0.340201150-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (66.5 kB view details)

Uploaded CPython 3.9 manylinux: glibc 2.17+ x86-64 manylinux: glibc 2.5+ x86-64

chesslib-1.0.340201150-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl (66.1 kB view details)

Uploaded CPython 3.9 manylinux: glibc 2.12+ x86-64 manylinux: glibc 2.5+ x86-64

chesslib-1.0.340201150-cp38-cp38-manylinux_2_5_x86_64.manylinux1_x86_64.whl (57.2 kB view details)

Uploaded CPython 3.8 manylinux: glibc 2.5+ x86-64

chesslib-1.0.340201150-cp38-cp38-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (67.0 kB view details)

Uploaded CPython 3.8 manylinux: glibc 2.17+ x86-64 manylinux: glibc 2.5+ x86-64

chesslib-1.0.340201150-cp38-cp38-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl (66.5 kB view details)

Uploaded CPython 3.8 manylinux: glibc 2.12+ x86-64 manylinux: glibc 2.5+ x86-64

chesslib-1.0.340201150-cp37-cp37m-manylinux_2_5_x86_64.manylinux1_x86_64.whl (56.8 kB view details)

Uploaded CPython 3.7m manylinux: glibc 2.5+ x86-64

chesslib-1.0.340201150-cp37-cp37m-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (67.8 kB view details)

Uploaded CPython 3.7m manylinux: glibc 2.17+ x86-64 manylinux: glibc 2.5+ x86-64

chesslib-1.0.340201150-cp37-cp37m-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl (67.3 kB view details)

Uploaded CPython 3.7m manylinux: glibc 2.12+ x86-64 manylinux: glibc 2.5+ x86-64

chesslib-1.0.340201150-cp36-cp36m-manylinux_2_5_x86_64.manylinux1_x86_64.whl (56.8 kB view details)

Uploaded CPython 3.6m manylinux: glibc 2.5+ x86-64

chesslib-1.0.340201150-cp36-cp36m-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (66.8 kB view details)

Uploaded CPython 3.6m manylinux: glibc 2.17+ x86-64 manylinux: glibc 2.5+ x86-64

chesslib-1.0.340201150-cp36-cp36m-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl (66.3 kB view details)

Uploaded CPython 3.6m manylinux: glibc 2.12+ x86-64 manylinux: glibc 2.5+ x86-64

File details

Details for the file chesslib-1.0.340201150-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for chesslib-1.0.340201150-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 aac317d0e62661d010a964eaeb5c2deffa14d275452b111478156c0b5f86e5d0
MD5 74c1977fa529303326b22b47f8b63d6d
BLAKE2b-256 506e0b055d43f0191d0700d9faad6a38b606283288eaecdaa675d581cb3f2d4a

See more details on using hashes here.

File details

Details for the file chesslib-1.0.340201150-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for chesslib-1.0.340201150-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 b3211a37a15a74d43ae5ce7e57a0a98776b7f1f68d6b84095ed7f4a418dfe62e
MD5 39a8849f04cf82a545830e1f715de4cd
BLAKE2b-256 eafdcf884240b22038936e56f2904d3cab4bb41c14df59aa8e94d5a11a34096c

See more details on using hashes here.

File details

Details for the file chesslib-1.0.340201150-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl.

File metadata

File hashes

Hashes for chesslib-1.0.340201150-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl
Algorithm Hash digest
SHA256 534cbace3c118b58227d3c0ac0594caddae193a9439e97273142b38708ff153a
MD5 3998ddddec3d886ab7b9f82500cdbf1d
BLAKE2b-256 e12ba0f2c3eba19041e8c30c866f347ac2173e480d1b025a434a5126a0b4b771

See more details on using hashes here.

File details

Details for the file chesslib-1.0.340201150-cp38-cp38-manylinux_2_5_x86_64.manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for chesslib-1.0.340201150-cp38-cp38-manylinux_2_5_x86_64.manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 59bb4034e1ca7fc63fb47a5c94337d2459e72fd9580cd075b9aaf2501d4d275a
MD5 d2cd746d7905889f0220e04b4f0b8e0f
BLAKE2b-256 be7d32952eb690f9ae0f39193dff01254c28a2f65e91ef8c509b7b9593e7aecd

See more details on using hashes here.

File details

Details for the file chesslib-1.0.340201150-cp38-cp38-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for chesslib-1.0.340201150-cp38-cp38-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 e5b0a46621b28c0c2769672c75671fe63e7ee45f9b3533fb2000a95836d34be9
MD5 c4c0704bd0c45edfbb6c960da9f95d8f
BLAKE2b-256 0efc053b8db74e78838f7a4598f83e1f39eb04713cfc7c292dc31895d05f075a

See more details on using hashes here.

File details

Details for the file chesslib-1.0.340201150-cp38-cp38-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl.

File metadata

File hashes

Hashes for chesslib-1.0.340201150-cp38-cp38-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl
Algorithm Hash digest
SHA256 7d51a378f817b0f805278e3bb2a35fc4a90ef879ac5059900b988ee7230c639b
MD5 64d00195e25e70b2f9384d0b12f717ad
BLAKE2b-256 4ef614b588a64e5e84695d2c8fd64479e0ccb87a421367cc889485cf4a2113e0

See more details on using hashes here.

File details

Details for the file chesslib-1.0.340201150-cp37-cp37m-manylinux_2_5_x86_64.manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for chesslib-1.0.340201150-cp37-cp37m-manylinux_2_5_x86_64.manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 11654dbc5adf1e1cdd58e18f5045602936dc4fd67020361a12a5e6e5b2f3b330
MD5 3f56d14fded2ddbc97343a6fa8c8d5f7
BLAKE2b-256 dc8b17c94ee3d4c01475677e782367135b3e0737693b77d7403e67867f2ad988

See more details on using hashes here.

File details

Details for the file chesslib-1.0.340201150-cp37-cp37m-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for chesslib-1.0.340201150-cp37-cp37m-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 7edf33f22121322cb02347090b32fb188acabf7289e2f0f34264572df07f30f1
MD5 8f3e381b74d4e8b3fa79b3fb5cf80c2f
BLAKE2b-256 a901db607a92eb4168acb80b21acefca9e57b7d6b70c349274aefb09ea00e8ab

See more details on using hashes here.

File details

Details for the file chesslib-1.0.340201150-cp37-cp37m-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl.

File metadata

File hashes

Hashes for chesslib-1.0.340201150-cp37-cp37m-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl
Algorithm Hash digest
SHA256 ef7f070dd4bad55ecfc7f15e4ac7e83c371eaea909fb62433d862221c87ebb6f
MD5 44592409fed27d5537e3b7f28dbc2b26
BLAKE2b-256 1e0d7fdd0e52014c3c1760d1a7ec6cc049db46091db672fb87889d20e07bc3b8

See more details on using hashes here.

File details

Details for the file chesslib-1.0.340201150-cp36-cp36m-manylinux_2_5_x86_64.manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for chesslib-1.0.340201150-cp36-cp36m-manylinux_2_5_x86_64.manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 d20020a6097c08042ecb3b1d56755b139855d7aa9b04af6f5ecf3aafca0884c8
MD5 b5a273e435a6be08d04b3b83b9296ee8
BLAKE2b-256 a8eb4e2a7617690c5ff176cca2898f8b3ed5a5452f318f7f40b13840649def64

See more details on using hashes here.

File details

Details for the file chesslib-1.0.340201150-cp36-cp36m-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for chesslib-1.0.340201150-cp36-cp36m-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 671ede9ca03c553c5c9760fba93f4b65061536a98df16cad90b078f3513d3873
MD5 6587508e48a19e2d91bc44be886210f9
BLAKE2b-256 874b9532a8b782b54e551f45236554dcec98bf56683f9f125dcb6385bfb81518

See more details on using hashes here.

File details

Details for the file chesslib-1.0.340201150-cp36-cp36m-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl.

File metadata

File hashes

Hashes for chesslib-1.0.340201150-cp36-cp36m-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl
Algorithm Hash digest
SHA256 1023044e7d49585957ba88f4eba7ea6e49020c52a31bc72fef8b3740eaf2fd44
MD5 e412ae420b7aac1ef8ec1290af58b88b
BLAKE2b-256 7899ea9542099ca4fa0cf4655243caffac2103dd618bf9a2cbb425b724b38897

See more details on using hashes here.

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