How a chess engine works: this is how the computer “thinks” when we play chess against it

A chess engineor chess engineis software designed to play chess with a skill level that surpasses even the best grandmasters. With each move, these programs evaluate the positions on the board and they decide the best move to make using complex artificial intelligence algorithms. Chess engines combine three fundamental components: the position representation, the search algorithm and a rating function. Their main goal is to calculate the optimal sequence of moves, based on analyzes that include thousands of positions in just a few seconds. In recent years, engines have made a leap in quality with the introduction of deep neural networkswhich simulate reasoning closer to that of the human brain.

The first engine to beat the world chess champion was Deep Blue of IBM, which in 1997 got the better of the then number one in the world Garry Kasparov over a 6 game match. However, chess engines don’t just play games: they can also analyze moves and strategies, helping players improve their game or discover new approaches with which to face future opponents. It must also be said that their use has raised ethical questions, due in part to the improper use used by some “players” to cheat during online games. At the same time, chess engines have given rise to competitions between machines, transforming the world of chess into a field where technology and chess tradition “fuse” together.

What’s behind a chess engine

But exactly what’s behind a chess engine? How does its algorithm work? Chess engines encode board positions using digital representationsoften through a “bitboard”where each bit corresponds to a piece or a square on the board and records crucial information such as the trait (who must move), castling possibilities and other rules, such as capturing en passant. With this information, the engine builds a complete view of the match, which can then be analyzed by evaluating game situations and looking deeply into matches.

There evaluation functionor heuristicsassign a numerical score to each position. This score reflects a side’s advantage or disadvantage based on factors such as material (piece value), king safety, and control of the center of the board. Traditionally, these functions rely on human knowledge of the game. However, in recent years, machine learning techniques have allowed engines to develop more sophisticated evaluation criteria, often surpassing human intuitions.

The engines also use additional tools such as opening books and the tablebase: the former are collections of initial moves taken from real games, which help the engine to open the game to start with a strategic advantage; the latter, however, contain all the possible combinations for positions with few pieces, offering perfect solutions in the final stages of the game.

In addition to evaluating the game dynamics, the best chess engines on the market try to look deep into matchesjust like a good chess player would do. The deeper the vision of the game, the better the move they can make at any given moment. To determine how deep the game vision of an algorithm (or a player) is, the so-called “ply” (or “layer”) which, to brutally simplify, is made up of two moves (one’s own and the opponent’s). Regarding this aspect, an interesting article on Chess.com (a point of reference for all online chess enthusiasts), dedicated precisely to chess engines, states:

At 20 ply (10 white moves and 10 black moves), most engines are already evaluating much deeper and stronger than humans. Depending on the time allowed and the complexity of the position, the engines can look more than 50 ply deep.

Some modern engines, moreover, like AlphaZero (by Google DeepMind) e Leela Chezz Zeroare based on deep neural networks or NN (Neural Networks). These systems learn the best moves by playing millions of games against themselves (or they are trained by feeding their algorithm training data, i.e. chess games (in many cases, those played by the Grand Masters). Once the training material has been provided training, the algorithm learns on its own how to approach matches, thanks to its ability to machine learning or machine learning.

“Traditional” engines, like Stockfishthey have usually used what is called research minimax alpha-beta (AB)which evaluates the best possible moves at a given moment. NN engines, on the other hand, use the so-called Monte Carlo tree search or MCTSwhere the algorithm plays a large number of quick games against itself at very high speeds and with random moves, then choosing the moves that seem to have the most probability of winning.

To optimize all these analyses, the algorithms also apply a technique known as “pruning”which eliminates disadvantageous steps. In this way, the engine can focus on the most promising options, reducing the number of calculations necessary and at the same time not compromising the quality of the moves themselves.

When chess engines were born

As you probably know, chess is a very ancient game (it is said to have originated in India around the 6th century AD). Chess engines aren’t quite as old, but you’ll probably be surprised to find that already in 1796 a fake chess-playing machine was devised – it was called Automaton – and its “algorithm” was actually a good human chess player hidden inside it. Over a century later, in 1912a machine was actually developed that was capable of checkmating a king and a rook against the opponent’s king. But it was only in 1951 that a certain Alan Turing he wrote a computer program that could actually play chess.