JSChessAI
Repos of code used in my JSDayES 2018 talk about JS, Chess and AI
Install / Use
/learn @gonzaloruizdevilla/JSChessAIREADME
Source code of my JSDayES talk about AI and Chess
You can find the slides of the talk here: https://slides.com/gruizdevilla/chess-ai-with-javascript/
What you've just checkout is the final version of the code. If you want to replay, checkout the tag base_for_exercise.
You can replay the talk, advancing commit by commit from base_for_exercise.
All the code has to be implemented in main.js.
The files included in the project are the following:
index.html: where you can see everything happeningmain.css: few custom stylesmain.js: where you should code everythingjs\chess.js: Chess.js library. Game enginejs\chessboard.js: Library to draw the chessboard on screenjs\jquery.js: dependency of chessboard.jsjs\tfjs.js: TensorFlow.jsjs\labels.js: Labels of all available movements in Chess for DNN modeljs\boardstate.js: function that converts FEN string to an array for the DNN model.models\chess-model-1.json: Tensorflow model that predicts next best move for a chessboard.models\chess-model-1.weights.json: Tensorflow weights for the previous model.
Some tips for the different steps
Chessboard
Constructor receives two parameters:
- fist one is the id of the HTML element where the chessboard is going to be drawn
- for the second one use the string "start" or the next config object, and then implement:
let config = {
draggable: true,
position: 'start',
onMouseoutSquare,
onMouseoverSquare,
onDragStart,
onDrop,
onSnapEnd
}
It is very easy to get a certain square because all the squares had a class like square-b4, being b4 the square coordinates.
Chess.js
These methods can be useful:
moves([options]): returns a list of legal moves from the current position. Options is optional and the attributes are:squareto limit to movements of the piece in that square, andverbose, a boolean to indicate if you want FEN notation of movement or an object with{from,to}move(move): to apply a move. returns null if movement is not valid. Accepts string or object (like the ones returned bymoves)undo(): undoes the last movehistory(): returns a list containing the moves of the current gamefen(): returns the FEN string for the current positiongame_over(): returns true if the game has ended via checkmate, stalemate, draw, threefold repetition, or insufficient material.
Boardstate
getBoardState(fenStr): given a FEN string, returns the array that should be used with the TFJS model.
TensorFlow.js
tf.loadModel(modelUrl): loads the model and it's weights. Needs only the model url.model.predict(tensor): make a predictiontf.tensor4d(arr, [numBoards, 8,8,8]): the model has 888 (board size * 8 features)tf.argMax(arr): return a tensor with a scalar of the position with the hightest value of the array
