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

Use the chesslib package like in the following example:

import chesslib
import numpy as np
import random


def test():

    # 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

For playing a chess game with randomly selected action, see this example:

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()

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

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.340199386-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.whl (56.7 kB view details)

Uploaded CPython 3.9 manylinux: glibc 2.5+ x86-64

chesslib-1.0.340199386-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (66.4 kB view details)

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

chesslib-1.0.340199386-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl (65.9 kB view details)

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

chesslib-1.0.340199386-cp38-cp38-manylinux_2_5_x86_64.manylinux1_x86_64.whl (57.1 kB view details)

Uploaded CPython 3.8 manylinux: glibc 2.5+ x86-64

chesslib-1.0.340199386-cp38-cp38-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.8 manylinux: glibc 2.17+ x86-64 manylinux: glibc 2.5+ x86-64

chesslib-1.0.340199386-cp38-cp38-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.8 manylinux: glibc 2.12+ x86-64 manylinux: glibc 2.5+ x86-64

chesslib-1.0.340199386-cp37-cp37m-manylinux_2_5_x86_64.manylinux1_x86_64.whl (56.7 kB view details)

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

chesslib-1.0.340199386-cp37-cp37m-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (67.6 kB view details)

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

chesslib-1.0.340199386-cp37-cp37m-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl (67.1 kB view details)

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

chesslib-1.0.340199386-cp36-cp36m-manylinux_2_5_x86_64.manylinux1_x86_64.whl (56.7 kB view details)

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

chesslib-1.0.340199386-cp36-cp36m-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (66.6 kB view details)

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

chesslib-1.0.340199386-cp36-cp36m-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl (66.2 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.340199386-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for chesslib-1.0.340199386-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 7fe7aaefb31221f01af1042ce915909549a50dcb0f02eebd05f7377bb119ba07
MD5 2ca71d96eb6c8b8058b18bbfc809463f
BLAKE2b-256 da7ce00859443e2b7d44c97b246d95d30956cef5c676624fcd25b8492b4ffc94

See more details on using hashes here.

File details

Details for the file chesslib-1.0.340199386-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.340199386-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 2637724462097adb45154c41d426c354ca4fa90e03a03234c07a2f22a72e708e
MD5 b3082fc83b8e5dfcdc4cf5646985d5cc
BLAKE2b-256 7be1a5da056fa8ed53cf73922cf72485b7639e6500c9da92f88c6d3588dfdad9

See more details on using hashes here.

File details

Details for the file chesslib-1.0.340199386-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.340199386-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl
Algorithm Hash digest
SHA256 b1a584b443ccd5ee00883d9205788928309faa8542d6275023626100797677d9
MD5 14c64dc32d9cbaa9e36a9cf1c0193565
BLAKE2b-256 995d9e66e9e4332ba63990c7b5b2dbadc888f21435032f9e2e14e924302e7db3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for chesslib-1.0.340199386-cp38-cp38-manylinux_2_5_x86_64.manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 67616e964eca7863e6879674abe283d8422602d3c1a34406b797c5171214595f
MD5 9711a46eca9d80d0ced2878b9b9ab0a5
BLAKE2b-256 242b437008a2c5cca91334ec223f9d532329d918d5c41cd7fdbae1389088d196

See more details on using hashes here.

File details

Details for the file chesslib-1.0.340199386-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.340199386-cp38-cp38-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 7b332e68e25d113d443ba9dfd16ea10a890ef5f8f257bc90b48281d72baf3cc8
MD5 9b1d817fc227c25bfd7b86ebb1f0a35b
BLAKE2b-256 efe9d706fcb2eecd001419825713dae13dad0f3a4b9b9a54eebd2124ee30ae4c

See more details on using hashes here.

File details

Details for the file chesslib-1.0.340199386-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.340199386-cp38-cp38-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl
Algorithm Hash digest
SHA256 12f1a56141793f3aff432bb7b6ea848218bea80100437937a381a8225ec79cfc
MD5 b8b1b8c2e5f7139bd796371eecb73b9a
BLAKE2b-256 f4f8e90820dca25902d35f5633024044890cbc7b18923bff9c81fb29654a3fc8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for chesslib-1.0.340199386-cp37-cp37m-manylinux_2_5_x86_64.manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 934c3dbabaf79cd009a4d29135e908ae68de2e8fdac31ea3b71cdf7d15b6cc49
MD5 7bc39c08914fed1782ffd1867ea95a7f
BLAKE2b-256 3cee0dde3452d80e9fb5c57d52670206e382d1c764c1207904d0f1a56f02a347

See more details on using hashes here.

File details

Details for the file chesslib-1.0.340199386-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.340199386-cp37-cp37m-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 4291471a6324f0931e79c03db556da51196d5ef3fe168cb7e5ad5f5db3205d27
MD5 838a11aa8c8f5a18ef5d8578e740579d
BLAKE2b-256 3a48b6502dc6b1cfd43cf08a60e10d1b386f84380a72460039c8ec695ce6c1e1

See more details on using hashes here.

File details

Details for the file chesslib-1.0.340199386-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.340199386-cp37-cp37m-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl
Algorithm Hash digest
SHA256 0b55dd54cb12d23d7afe34d96621821eb542de11d016cda631cec40039a36695
MD5 9e4eb7d56f8df4d49ace294d38d5148a
BLAKE2b-256 bdf4f3bf996e0bdd50131fd45c3c3f3db90fb20e3c3fda88153a75af0e5dbc65

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for chesslib-1.0.340199386-cp36-cp36m-manylinux_2_5_x86_64.manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 4ac6fb5755728196d14da892926fe3f75f48cdb5e016ca66d82a830ac938e087
MD5 e4bfb3c7f104dc59fe287de9bf9c3328
BLAKE2b-256 44581fef435785e375448490bf4505243f5f7da9fc3021507924db4a3fb022dd

See more details on using hashes here.

File details

Details for the file chesslib-1.0.340199386-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.340199386-cp36-cp36m-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 7496d1fe8cbc5dee3af45d3791b06d3428a597e5dcf7fc5778501cdaf180a696
MD5 23dd625f4e3f1d0a2545b70181229839
BLAKE2b-256 11d43c1bfa52b47dd0ad10816a3cc380fa38ebf291fa9bb811d1bbc33e04ee26

See more details on using hashes here.

File details

Details for the file chesslib-1.0.340199386-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.340199386-cp36-cp36m-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl
Algorithm Hash digest
SHA256 cc0acaa732bd21aff29787e2380ab1c3706ea301b1806dac5c2a87f643d257f1
MD5 890db20ae96f8f7ed37425c01043af97
BLAKE2b-256 69479518ea0af569579416eb67607b3ae3fd8b585df2ecaa2807388b406687e2

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