Chess engine code – the evaluation function

This article is part of a series – the contents page is here.

Throughout this series we’ve referred to the evaluation function and in the last article we saw how the minimax algorithm calls it.  Now, at last, we can reveal it.

You might be surprised by how simple it is.  Or maybe not.  It probably depends on how good you are at chess, and whether you’re a glass-half-empty or glass-half-full person.  Personally, I’m quite intrigued by how such a naïve function can sometimes pull off moves that aren’t half bad.  But yes, I have to concede that sometimes it does some pretty dumb things (although it can be very hard to tell how much of that is due to the over-simplification of the evaluation versus the horizon effect).

Continue reading

Chess engine code – minimax

This article is part of a series – the contents page is here.

In this article we cut to the chase and present the function that implements the minimax algorithm.  I’m going to cut out some miscellaneous stuff so that we can focus on the core, which is walking the tree and comparing the lines.  You can get the full version in the code base.

Continue reading

Chess engine code – running in the background

This article is part of a series – the contents page is here.

In earlier articles we’ve seen the code for generating moves.  Before we use it to build a playing engine, let’s take a detour and consider how we’re going to run that engine.

We’ve already identified that, thanks to the vastness of chess’s move tree, our engine could be slow to run.  Sophisticated modern engines can generate strong moves in just a few milliseconds, but unfortunately such speed and strength is beyond the scope of this project.  We’re going to have to wait a few seconds for Shallow Thought to calculate its moves.  (Incidentally, is this a good time to mention that Shallow Thought runs a lot faster on Chrome than on Edge?  Hmmm).

Continue reading

Chess engine code – laying the groundwork

This article is part of a series – the contents page is here.

In the last article we covered the very basic theory of a chess engine.  Time to write some code!  Before we can get to the interesting stuff we need to lay the groundwork with a few elementary types and enums.  You can see all of these in the ChessElements.ts file.  The main ones are PieceType, BoardSquare, Board and GameMove.

Continue reading

How to make your computer play chess

This article is part of a series – the contents page is here.

Let’s start somewhere near the beginning with a few definitions:

  • position is the state of the chess board at a given point.  It includes the locations of the pieces and the knowledge of whose turn it is to move next.  It also includes subtle stuff like whether the players have moved their kings or rooks, which affects their ability to castle.
  • move is a single move by one player, such as White pushing the pawn from e2 to e4.  Each move takes a position and creates a new position.
  • line is a sequence of moves.  For example, the line 1. e4 e5 2. Nf3 Nc6 3. Bb5 is the start of the Ruy Lopez opening.
Continue reading