![Docker CI](https://github.com/Bonifatius94/ChessLib.Py/workflows/Docker%20CI/badge.svg)
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():
last_draw = chesslib.ChessDraw_Null
board = chesslib.ChessBoard_StartFormation()
drawing_side = chesslib.ChessColor_White
state = chesslib.GameState_None
game_over_states = [chesslib.GameState_Checkmate, chesslib.GameState_Tie]
print('start of game')
print(chesslib.VisualizeBoard(board))
while state not in game_over_states:
next_action = choose_action(board, drawing_side, last_draw)
board = chesslib.ApplyDraw(board, next_action)
last_draw = next_action
drawing_side = 1 - drawing_side
state = chesslib.GameState(board, last_draw)
print()
print(chesslib.VisualizeDraw(next_action), f'state={state}')
print(chesslib.VisualizeBoard(board))
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():
board = chesslib.ChessBoard_StartFormation()
draws = chesslib.GenerateDraws(board, chesslib.ChessColor_White, chesslib.ChessDraw_Null, True)
draw_to_apply = draws[random.randint(0, len(draws) - 1)]
new_board = chesslib.ApplyDraw(board, draw_to_apply)
print(chesslib.VisualizeDraw(draw_to_apply))
print(chesslib.VisualizeBoard(board))
print(chesslib.VisualizeBoard(new_board))
rev_board = chesslib.ApplyDraw(new_board, draw_to_apply)
board_hash = chesslib.Board_ToHash(board)
board_reloaded = chesslib.Board_FromHash(board_hash)
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).
sudo apt-get update && sudo apt-get install -y git docker.io
sudo usermod -aG docker $USER && reboot
git clone https://github.com/Bonifatius94/ChessLib.Py
cd ChessLib.Py
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.
docker build . --file Dockerfile-dev -t "chesslib-dev"
docker run -it "chesslib-dev" bash
docker run -v $PWD:/scripts "chesslib-dev" python3 /scripts/test.py
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.