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

Uploaded CPython 3.9 manylinux: glibc 2.17+ x86-64

chesslib-1.0.340699405-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.340699405-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl (66.4 kB view details)

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

chesslib-1.0.340699405-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (63.0 kB view details)

Uploaded CPython 3.8 manylinux: glibc 2.17+ x86-64

chesslib-1.0.340699405-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.340699405-cp38-cp38-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl (66.8 kB view details)

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

chesslib-1.0.340699405-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (62.7 kB view details)

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

chesslib-1.0.340699405-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.340699405-cp37-cp37m-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl (67.6 kB view details)

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

chesslib-1.0.340699405-cp36-cp36m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (62.7 kB view details)

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

chesslib-1.0.340699405-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.340699405-cp36-cp36m-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl (66.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.340699405-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for chesslib-1.0.340699405-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 1d55f7746630b1f4960dd15fd6319c631dcc38e048a0b677a40431e3702f7cc5
MD5 8e30b6a27f19e3b65a38257b7d648d74
BLAKE2b-256 4c84248dbfa853378dbd52c37235fd2bc168e2337b82a7b4b2ddb8cf3e2dffe5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for chesslib-1.0.340699405-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 dd51d924a0d85797e025e608c388376bd9ba666b6a75748a0635688da6b04190
MD5 7881cbb31b7ebbc8f5a4f680cba3e6f0
BLAKE2b-256 a42227e1d359cf676b54ce8f6f2aee1e409718eaf00b38f6b6c33a10fe35d4d2

See more details on using hashes here.

File details

Details for the file chesslib-1.0.340699405-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.340699405-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl
Algorithm Hash digest
SHA256 3977bea599f20ce3fa4d83d168d3d0c394d0beadc18e361cafa8b43dc6cea7e3
MD5 6599a1e1ab385447964f83998f6c6c01
BLAKE2b-256 9755f9fd8993679c521e3e90bfcfc235fd69891112864b297a2369b3d028a7f1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for chesslib-1.0.340699405-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 a70a65153303e336939af6533ba60b81d043db156a10fedec624345cf71b4f1c
MD5 d2abd8a654ec1dd83e7e5d628f05f00f
BLAKE2b-256 6aa89b0202797694fc8792e7afd6c6e12c162735ee894981292a824bec3b520d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for chesslib-1.0.340699405-cp38-cp38-manylinux_2_5_x86_64.manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 e9c87e4032b1870548b60d380eb4bf0c67f295bc428cdc78f8149a3914c4f7ef
MD5 2cf69188e6ddb7f870f4128768c5a478
BLAKE2b-256 9eec11feefee12cf2f2d68ab635aa30e13b7a33841c2b275a54476170d7c90bb

See more details on using hashes here.

File details

Details for the file chesslib-1.0.340699405-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.340699405-cp38-cp38-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl
Algorithm Hash digest
SHA256 d18e4823c1723a2840119c899f330434d279f4749db42b04efa6efe7271c1c53
MD5 e2a8cb325f6c6677fb0d3c604e8c87f6
BLAKE2b-256 c2e229d02117a17a39d8d21b0197be0c6a9626569c3e680cb821d874bf9c727f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for chesslib-1.0.340699405-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 73764a01997d2320448f48cc57051676f04f36255c250ff2b66d94a66657fe36
MD5 ce2f3892b3636c9abc4180818f975c2d
BLAKE2b-256 db6d639b51aee90ea8358e46a5350149037c1fe0a0e7713b1745a2dd317fe96f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for chesslib-1.0.340699405-cp37-cp37m-manylinux_2_5_x86_64.manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 6c058da907596872196755f57420fd95990f35eaf3d1bfb32ab75a609ed77d54
MD5 d80bcc7d709eb63195765378ecc796d4
BLAKE2b-256 9bd4ca3ce7a5614e44fbed6c474588d0090eae48d0759f44de2416884c1674d7

See more details on using hashes here.

File details

Details for the file chesslib-1.0.340699405-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.340699405-cp37-cp37m-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl
Algorithm Hash digest
SHA256 8338b295a7293e52ab30ce16d87a8362d886d97683f56b46c54ac24cc493c813
MD5 7c9888aa064ad95cc0fa0ac6462cd263
BLAKE2b-256 f21cfb2858596e8c22fed7956b24c3a97e625aab5e41100cd20961b5782f14cc

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for chesslib-1.0.340699405-cp36-cp36m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 9c79ac7ac1506b276bd5a9ca0bff06823f07a1d02deb77e6a781684edb943b12
MD5 cb59d98e4ea1b65cc8c028fa1721ecc9
BLAKE2b-256 4a93e1042b714de3445c60bf3f1cec384df603735f361bc06fab0c773d72f2bb

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for chesslib-1.0.340699405-cp36-cp36m-manylinux_2_5_x86_64.manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 23d15f96a6468682c005cd3c4a90c457ba4a4367949e2ac994531be266d478b6
MD5 0a6113190c3b08590a532a95c63a371d
BLAKE2b-256 884f66b63ce098a5ccee91562f72bec4d2fd45091cf5bdaf55979925389c9700

See more details on using hashes here.

File details

Details for the file chesslib-1.0.340699405-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.340699405-cp36-cp36m-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl
Algorithm Hash digest
SHA256 8d58a540f4c03b846a3b6ee93999fe5c16d8ff131b53fd606958688b1d8a90c2
MD5 4265dcfa9e30e46409e2a0994b776b44
BLAKE2b-256 749c02b9ff32aa2be37e32cead0a223ce50ff1b06ad1bc31f85b5641e7c7e439

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