Skip to content
/ chess Public

Analyzing chess games and rendering as a web app

Notifications You must be signed in to change notification settings

slevin48/chess

Repository files navigation

Chess Open in Streamlit

Analyzing chess games and rendering as a web app

game1

Match plot:

matchplot

Stockfish

Stockfish is an open source chess engine

import chess
import chess.pgn
import chess.engine

def stockfish_evaluation(board, color='white', time_limit = 0.01):
    engine = chess.engine.SimpleEngine.popen_uci("/usr/games/stockfish")
    info = engine.analyse(board, chess.engine.Limit(time=time_limit))
    if color == 'white':
        score = info['score'].white().score()
    else:
        score = info['score'].black().score()
    engine.quit()
    return score

pgn = open('games/slevin48_vs_GraciasSenior_2020.11.22.pgn')
first_game = chess.pgn.read_game(pgn)

# Move 48
board = first_game.board()
moves = [move for move in first_game.mainline_moves()]

# Iterate through all moves and play them on a board
for move in moves[0:48]:
    board.push(move)
result = stockfish_evaluation(board)
print(result)

Analysing and evaluating a position:

import chess
import chess.engine

engine = chess.engine.SimpleEngine.popen_uci("/usr/games/stockfish")

board = chess.Board("r1bqkbnr/p1pp1ppp/1pn5/4p3/2B1P3/5Q2/PPPP1PPP/RNB1K1NR w KQkq - 2 4")
info = engine.analyse(board, chess.engine.Limit(depth=20))
print("Score:", info["score"])
# Score: PovScore(Mate(+1), WHITE)

engine.quit()

It enables to perform analysis plots of the match

matchplot

Stockfish on demand

🐋Dockerfile installing Stockfish + Python Chess library, exposing analysis as a REST API:

docker build -t chess-engine .
docker run -p 5000:5000 chess-engine

(run command can also be detached: --detach , -d)

curl 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"

(unnecessary use of -X POST and of optional use of -v for verbose output)

🚀Deploy to Heroku Dockerize Your Python Flask application and deploy it onto Heroku

$ 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"
{
  "data": 40, 
  "isError": false, 
  "message": "Success", 
  "statusCode": 200
}

AlphaZero

Mentally 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.

After 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.

After you’ve finished thinking about future possibilities, take the action that you’ve explored the most.

At the end of the game, go back and evaluate where you misjudged the value of the future positions and update your understanding accordingly.

How to build your own AlphaZero AI using Python and Keras

AlphaZero vs Stockfish

AlphaZeroVsStockfish

AlphaZeroVsStockfishGame

Resources

Code:

Articles:

About

Analyzing chess games and rendering as a web app

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published