Package: chesslib
ChessLib Python ExtensionAboutThis 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).UsageInstall theofficial Python packageusing pip:pipinstallchesslibFor 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.importchesslibimportnumpyasnpdefchoose_action(board,drawing_side,last_draw):poss_actions=chesslib.GenerateDraws(board,drawing_side,last_draw,True)returnnp.random.choice(poss_actions)defmain():# init cache variableslast_draw=chesslib.ChessDraw_Nullboard=chesslib.ChessBoard_StartFormation()drawing_side=chesslib.ChessColor_White# init game statestate=chesslib.GameState_Nonegame_over_states=[chesslib.GameState_Checkmate,chesslib.GameState_Tie]# log the start formation to consoleprint('start of game')print(chesslib.VisualizeBoard(board))# continue until the game is overwhilestatenotingame_over_states:# choose an action and apply it to the boardnext_action=choose_action(board,drawing_side,last_draw)board=chesslib.ApplyDraw(board,next_action)# update the cache variableslast_draw=next_actiondrawing_side=1-drawing_side# alternate between white and black sidestate=chesslib.GameState(board,last_draw)# log the game progress to consoleprint()print(chesslib.VisualizeDraw(next_action),f'state={state}')print(chesslib.VisualizeBoard(board))# log the game's outcome to consolewinning_side="black"ifdrawing_side==chesslib.ChessColor_Whiteelse"white"print('tie'ifstate==chesslib.GameState_Tieelsef'{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:importchesslibimportnumpyasnpdefmain():# create a new chess board in start formationboard=chesslib.ChessBoard_StartFormation()# generate all possible drawsdraws=chesslib.GenerateDraws(board,chesslib.ChessColor_White,chesslib.ChessDraw_Null,True)# apply one of the possible drawsdraw_to_apply=draws[random.randint(0,len(draws)-1)]new_board=chesslib.ApplyDraw(board,draw_to_apply)# write the draw's nameprint(chesslib.VisualizeDraw(draw_to_apply))# visualize the board before / after applying the drawprint(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 hashboard_hash=chesslib.Board_ToHash(board)board_reloaded=chesslib.Board_FromHash(board_hash)# see tests/ folder for more examplesif__name__=='__main__':main()How to DevelopFor 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)sudoapt-getupdate&&sudoapt-getinstall-ygitdocker.io
sudousermod-aGdocker$USER&&reboot# download the project's source code (if you haven't done before)gitclonehttps://github.com/Bonifatius94/ChessLib.PycdChessLib.Py# install the 'Visual Studio Code' text editor (optional)sudosnapinstallcode--classicThe 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)dockerbuild.--fileDockerfile-dev-t"chesslib-dev"# attach to the build environment's interactive bash console (leave the session with 'exit')dockerrun-it"chesslib-dev"bash# mount a Python test script into the build environment and run itdockerrun-v$PWD:/scripts"chesslib-dev"python3/scripts/test.py# debug the GitHub release pipelinedockerrun-v$PWD/dist:/output-v$PWD:/build-it"chesslib-dev"bashSee the GitHub wiki for more details.CopyrightThis software is available under the MIT licence's terms.