<aside> ♟️ This a guide on how to build a Chess engine from scratch, updated periodically as I further explore the space. This assumes some basic knowledge of Chess, search algorithms, and coding.
Want to know how chess engines work? Or how AI is now better than every human on earth in chess? Or maybe how you can beat your younger brother who is stupidly good at chess? This is the place.
</aside>
<aside> 👋 This is getting a lot more traffic than I thought it would! This is one of my weekly projects, so if you like it, consider following me on Twitter to get updates when I release something new.
</aside>
Before you start on your engine, you need a board representation and some form of visualization. I’ve chosen Chess.js and Chessboard.js to start.
Of course, you could build this yourself, but that isn’t really the point of this project. If you’re interested in learning about chess engine board representations, lookup “bitboards” and go from there - lots of interesting ways move generation and board state can be handled with maximum efficiency.
In looking for a good library to work in, I found the Chess.js package. This is a great place to start. It handles all move generation, board state, FEN notation (will get to that in a sec if you don’t know what that is), etc.
https://github.com/jhlywa/chess.js
I do most of my work in Javascript, so this is perfect for me. Super simple (not the fastest) but great for speed and learning fast. Lots of space to mess around, though I’d recommend porting to a strongly typed and compiled language once you start optimizing for performance.
Even if you don’t code in Javascript I would heavily recommend using chess.js, because of it’s super quick implementation with chessboard.js, a HTML chessboard representation.
These hook together so well it’s unbelievable. Being able to publish a website with your chess engine live in the browser is awesome. Here’s a link to a super simple starter repo you can download and run right now, which you’ll be able to follow along with the whole time.
https://github.com/0hq/starter_chess_engine
👆 Download and run this, it’ll load up what we’re starting with.
It’s important to start with a good baseline, so we’ll start with an engine that only makes random legal moves. Yeah, it’s dumb, but it’s a great starting point for the rest of the project.
If you download the above repo, you’ll see this. All it does is generate random moves for each player. Run this code! Boom, we have our first engine.
var board = null
var game = new Chess()
function makeRandomMove () {
// chess.js gives us all the possible moves in an array
// [ move1, move2, move3 ... ]
var possibleMoves = game.moves()
// exit if the game is over
if (game.game_over()) return
// choses a random index in the list
var randomIdx = Math.floor(Math.random() * possibleMoves.length)
// updates javascript board state
game.move(possibleMoves[randomIdx])
// changes html board state
board.position(game.fen())
// call this function again in 5 secs
window.setTimeout(makeRandomMove, 500)
}
board = Chessboard('myBoard', 'start')
window.setTimeout(makeRandomMove, 500)
https://0hq.github.io/starter_chess_engine/