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.340226118-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (64.7 kB view details)

Uploaded CPython 3.9 manylinux: glibc 2.17+ x86-64

chesslib-1.0.340226118-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.whl (55.5 kB view details)

Uploaded CPython 3.9 manylinux: glibc 2.5+ x86-64

chesslib-1.0.340226118-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl (64.3 kB view details)

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

chesslib-1.0.340226118-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (65.2 kB view details)

Uploaded CPython 3.8 manylinux: glibc 2.17+ x86-64

chesslib-1.0.340226118-cp38-cp38-manylinux_2_5_x86_64.manylinux1_x86_64.whl (55.9 kB view details)

Uploaded CPython 3.8 manylinux: glibc 2.5+ x86-64

chesslib-1.0.340226118-cp38-cp38-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl (64.7 kB view details)

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

chesslib-1.0.340226118-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (66.0 kB view details)

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

chesslib-1.0.340226118-cp37-cp37m-manylinux_2_5_x86_64.manylinux1_x86_64.whl (55.4 kB view details)

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

chesslib-1.0.340226118-cp37-cp37m-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl (65.5 kB view details)

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

chesslib-1.0.340226118-cp36-cp36m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (65.0 kB view details)

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

chesslib-1.0.340226118-cp36-cp36m-manylinux_2_5_x86_64.manylinux1_x86_64.whl (55.5 kB view details)

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

chesslib-1.0.340226118-cp36-cp36m-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl (64.6 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.340226118-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for chesslib-1.0.340226118-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 bdcce45746daf29d4d6cd7701e6c4fe66f06a018437ff43aad4821be302c25d7
MD5 9bff93170bc78551d2bf2bce367247d7
BLAKE2b-256 6260a3ca051909d9c97c38ddec8b6a32f4499b9c6a2158c6e9e4874c5d3e5e7f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for chesslib-1.0.340226118-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 12e438e8925eb67bf94e07269114ecc7f11de364acd9dd485f5e559913c4729b
MD5 4bc51d844bb50bc23f51384dfd4639d2
BLAKE2b-256 8e249c5c7cbf0c4bd05d29f10342adc955293e4ac89c33b0d3dbf03fd6c30592

See more details on using hashes here.

File details

Details for the file chesslib-1.0.340226118-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.340226118-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl
Algorithm Hash digest
SHA256 740f83d2f06cb6d6dfb2f49b1787df8d29478ade6ea6eaecc9c3964453e7f281
MD5 c13d8dae2a4bbe7a2a1b09d3a4a9ebf8
BLAKE2b-256 c45c0e7d231efe85187ddc330d754ecd00748945ceb8dad7f497ff4886d690e2

See more details on using hashes here.

File details

Details for the file chesslib-1.0.340226118-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for chesslib-1.0.340226118-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 b38d05e25b98d1762a0b6c0a17892ece9b2c7608a1a9d1e61e8d01a9aff1504a
MD5 6d297de167c35c33d5c863de6853b56b
BLAKE2b-256 7c8f1fc76d0a9c6f8867e10586ef788700ce6ab2503563261e2855e2f941fccb

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for chesslib-1.0.340226118-cp38-cp38-manylinux_2_5_x86_64.manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 03bd6239df38d4447f8ec6200b2d566ed75a619583398d81a24ac22daf25a13f
MD5 383660509c068357fbc31870bd35d0bf
BLAKE2b-256 916e4497eae0360c37ddfd20b2eb1513b9043bbfe22b4efab2526b40d8f7e608

See more details on using hashes here.

File details

Details for the file chesslib-1.0.340226118-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.340226118-cp38-cp38-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl
Algorithm Hash digest
SHA256 724461196a9ef1118720df209bc2992effbb21958e28a6c448f8f07af54f94a7
MD5 a076bb83f7644d14761012089fc81b9a
BLAKE2b-256 07cf370af3c23bdf288d3983994fe44b46161ad886e30f603dbfa00c2b6ba32f

See more details on using hashes here.

File details

Details for the file chesslib-1.0.340226118-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for chesslib-1.0.340226118-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 bd39176f13e3cfd8a1f0413c840577c2ef06e90ab2d555774e4b69f827e1bccc
MD5 3e238768f91168f1f724d4d5a3fbd8c9
BLAKE2b-256 e54f1f3e89efcdfc7ea4b693aaec5b26153fc64b1497db74b2ec357229c9d988

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for chesslib-1.0.340226118-cp37-cp37m-manylinux_2_5_x86_64.manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 4899bd7fe1cf6b0bd7b044544548a97cf97e8d81012c478bed9122418093358c
MD5 116ed9c93866029cdb7bdbd66413030e
BLAKE2b-256 9796d60966e078e27767fdf652068face0ae2b6b47689b560f560aa09fb55142

See more details on using hashes here.

File details

Details for the file chesslib-1.0.340226118-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.340226118-cp37-cp37m-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl
Algorithm Hash digest
SHA256 d503de8eeaf8f6b3b9c0d42132d841e5d421bcada6150fb5fc89ce2ce1488cf3
MD5 c361e0f0c4e07f5308f26fa423505ca5
BLAKE2b-256 65a33b2319783c93bcb824586bf55ea4cc4af6597a0c889fede7ffcfb66d3251

See more details on using hashes here.

File details

Details for the file chesslib-1.0.340226118-cp36-cp36m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for chesslib-1.0.340226118-cp36-cp36m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 42e252978ada3741ef3fe4412fe62c1378a1e67508f0614ee7cdf7093c17d5dd
MD5 9e333a12321c60018119a500ecfec049
BLAKE2b-256 d7259db9d9b0529b1562555f06e3b80a9c6cb2335129b64b074ee93aef1b3bf0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for chesslib-1.0.340226118-cp36-cp36m-manylinux_2_5_x86_64.manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 7d6a67718a71eed7d9274704cadaa374e5a8ffce00fff842cbe0f69daa9b096c
MD5 4e1fd6212d6054dbe2c00fa516f96a3b
BLAKE2b-256 bb713c7c821a9a3a571cb4af312353858ebbd6b73bff04ddc2073c7d9b13d28a

See more details on using hashes here.

File details

Details for the file chesslib-1.0.340226118-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.340226118-cp36-cp36m-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl
Algorithm Hash digest
SHA256 f4621406fc6c99ea44aaa503a6710d12fca7a5393b4eba6e133f535cfa8c1350
MD5 17d687c7444297cfd76a6d92e6b80304
BLAKE2b-256 ec93b346ea85255e7d0f0898f166b5675298783ee0f1cf62b6ff54154d805b30

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