{"id":19350693,"url":"https://github.com/slevin48/chess","last_synced_at":"2025-11-05T03:03:06.227Z","repository":{"id":112495733,"uuid":"315075311","full_name":"slevin48/chess","owner":"slevin48","description":"Analyzing chess games and rendering as a web app","archived":false,"fork":false,"pushed_at":"2021-02-15T12:56:38.000Z","size":3646,"stargazers_count":7,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-13T03:13:32.955Z","etag":null,"topics":["chess","docker","python","stockfish","streamlit"],"latest_commit_sha":null,"homepage":"","language":"Jupyter Notebook","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/slevin48.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-11-22T15:58:10.000Z","updated_at":"2024-07-06T13:44:10.000Z","dependencies_parsed_at":"2023-05-15T09:15:12.214Z","dependency_job_id":null,"html_url":"https://github.com/slevin48/chess","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/slevin48%2Fchess","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/slevin48%2Fchess/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/slevin48%2Fchess/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/slevin48%2Fchess/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/slevin48","download_url":"https://codeload.github.com/slevin48/chess/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248657923,"owners_count":21140846,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["chess","docker","python","stockfish","streamlit"],"created_at":"2024-11-10T04:33:33.843Z","updated_at":"2025-11-05T03:03:06.131Z","avatar_url":"https://github.com/slevin48.png","language":"Jupyter Notebook","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Chess [![Open in Streamlit](https://static.streamlit.io/badges/streamlit_badge_black_white.svg)](https://share.streamlit.io/slevin48/chess/main/chess-app.py)\n\nAnalyzing chess games and rendering as a [web app](https://share.streamlit.io/slevin48/chess/main/chess-app.py)\n\n![game1](images/game1.svg)\n\nMatch plot:\n\n![matchplot](images/matchplot.png)\n\n## Stockfish\n\n[Stockfish](https://stockfishchess.org/) is an open source chess engine\n\n```python\nimport chess\nimport chess.pgn\nimport chess.engine\n\ndef stockfish_evaluation(board, color='white', time_limit = 0.01):\n    engine = chess.engine.SimpleEngine.popen_uci(\"/usr/games/stockfish\")\n    info = engine.analyse(board, chess.engine.Limit(time=time_limit))\n    if color == 'white':\n        score = info['score'].white().score()\n    else:\n        score = info['score'].black().score()\n    engine.quit()\n    return score\n\npgn = open('games/slevin48_vs_GraciasSenior_2020.11.22.pgn')\nfirst_game = chess.pgn.read_game(pgn)\n\n# Move 48\nboard = first_game.board()\nmoves = [move for move in first_game.mainline_moves()]\n\n# Iterate through all moves and play them on a board\nfor move in moves[0:48]:\n    board.push(move)\nresult = stockfish_evaluation(board)\nprint(result)\n```\n\n[Analysing and evaluating a position](https://python-chess.readthedocs.io/en/latest/engine.html#analysing-and-evaluating-a-position):\n\n```python\nimport chess\nimport chess.engine\n\nengine = chess.engine.SimpleEngine.popen_uci(\"/usr/games/stockfish\")\n\nboard = chess.Board(\"r1bqkbnr/p1pp1ppp/1pn5/4p3/2B1P3/5Q2/PPPP1PPP/RNB1K1NR w KQkq - 2 4\")\ninfo = engine.analyse(board, chess.engine.Limit(depth=20))\nprint(\"Score:\", info[\"score\"])\n# Score: PovScore(Mate(+1), WHITE)\n\nengine.quit()\n```\n\nIt enables to perform analysis plots of the match\n\n![matchplot](images/matplot.png)\n\n\n## Stockfish on demand\n\n🐋[Dockerfile](Dockerfile) installing Stockfish + Python Chess library, exposing analysis as a REST API:\n\n```\ndocker build -t chess-engine .\ndocker run -p 5000:5000 chess-engine\n```\n(run command can also be detached: `--detach` , `-d`)\n\n```\ncurl http://localhost:5000/score -d '{\"data\":\"rnbqkbnr/pppppppp/8/8/3P4/8/PPP1PPPP/RNBQKBNR b KQkq - 0 1\"}' -X POST -v -H \"Content-Type: application/json\"\n```\n(unnecessary use of `-X POST` and of optional use of `-v` for verbose output)\n\n🚀*Deploy to Heroku* \n[Dockerize Your Python Flask application and deploy it onto Heroku](https://medium.com/analytics-vidhya/dockerize-your-python-flask-application-and-deploy-it-onto-heroku-650b7a605cc9)\n```\n$ curl https://stockfish48.herokuapp.com/score -d '{\"data\":\"rnbqkbnr/pppppppp/8/8/3P4/8/PPP1PPPP/RNBQKBNR b KQkq - 0 1\"}' -H \"Content-Type: application/json\"\n{\n  \"data\": 40, \n  \"isError\": false, \n  \"message\": \"Success\", \n  \"statusCode\": 200\n}\n```\n## AlphaZero\n\nMentally play through possible future scenarios, giving priority to promising paths, whilst also considering how others are most likely to react to your actions and continuing to explore the unknown.\n\nAfter reaching a state that is unfamiliar, evaluate how favourable you believe the position to be and cascade the score back through previous positions in the mental pathway that led to this point.\n\nAfter you’ve finished thinking about future possibilities, take the action that you’ve explored the most.\n\nAt the end of the game, go back and evaluate where you misjudged the value of the future positions and update your understanding accordingly.\n\n[How to build your own AlphaZero AI using Python and Keras\n](https://medium.com/applied-data-science/how-to-build-your-own-alphazero-ai-using-python-and-keras-7f664945c188)\n\n## AlphaZero vs Stockfish\n\n![AlphaZeroVsStockfish](images/AlphaZeroVsStockfish.jpeg)\n\n![AlphaZeroVsStockfishGame](images/AlphaZeroVsStockfishGame.png)\n\n## Resources\n\nCode:\n* [Python-chess](https://github.com/niklasf/python-chess) + ([Doc](https://python-chess.readthedocs.io/en/latest/))\n* [Programming a Chess Player](https://jupyter.brynmawr.edu/services/public/dblank/CS371%20Cognitive%20Science/2016-Fall/Programming%20a%20Chess%20Player.ipynb)\n* [Deep Pink](https://github.com/erikbern/deep-pink)\n* [Chess annotator](https://pypi.org/project/chess-annotator/)\n* [python-chess-analysis](https://github.com/mptedesco/python-chess-analysis)\n* [Chess-Graphical-Evaluation](https://github.com/rafaelmcam/Chess-Graphical-Evaluation)\n* [Python-Easy-Chess-GUI](https://github.com/fsmosca/Python-Easy-Chess-GUI)\n* [Chess analysis D3js](https://github.com/m-hou/Chess-Analysis)\n* [Python chess graph](https://github.com/Destaq/chess_graph)\n* [Chess web app](https://github.com/SayonB/Predicting-Pro-Chess-Moves)\n\nArticles:\n* [Using Deep Learning to train a Deep Search Chess Algorithm](https://towardsdatascience.com/creating-a-chess-engine-with-deep-learning-b9477ff3ee3d)\n* [How to build your own AlphaZero AI using Python and Keras\n](https://medium.com/applied-data-science/how-to-build-your-own-alphazero-ai-using-python-and-keras-7f664945c188)\n* [Analyzing Chess with Pandas to Learn from the Best and Raise My Rating](https://medium.com/@crawftv/analyzing-chess-with-pandas-to-learn-from-the-best-raise-my-rating-1bf22f28b83)\n* [Modeling a ChessBoard And Mechanics Of Its Pieces In Python](https://impythonist.wordpress.com/2017/01/01/modeling-a-chessboard-and-mechanics-of-its-pieces-in-python/amp/)\n* [A Visual Look at 2 Million Chess Games](https://blog.ebemunk.com/a-visual-look-at-2-million-chess-games/)\n* [Chessviz](https://andreasstckl.medium.com/chessviz-graphs-of-chess-games-7ebd4f85a9b9)\n* [Machine Learning for Chess — AlphaZero vs Stockfish](https://mmsubra1.medium.com/machine-learning-for-chess-alphazero-vs-stockfish-b58638e73fee)\n* [AlphaZero Crushes Stockfish In New 1,000-Game Match](https://www.chess.com/news/view/updated-alphazero-crushes-stockfish-in-new-1-000-game-match)\n* [Predicting Professional Players’ Chess Moves with Deep Learning](https://towardsdatascience.com/predicting-professional-players-chess-moves-with-deep-learning-9de6e305109e)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fslevin48%2Fchess","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fslevin48%2Fchess","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fslevin48%2Fchess/lists"}