# How do chess engines "think"?

What I want to know is how engines are programmed to find moves. I'm sure they first calculate the most forcing lines such as captures and checks. But what about subtle, deep positional moves? They seem to find them very quickly as well (Generally speaking. Of course they miss such moves now and then). As in, how are they programmed to look for quiet moves/positional ideas? They can't just brute force every move, since that'd take too long, so there should be some clever way for them to arrive at the best moves really quickly. I'm interested in knowing this because I think it'd help players to think over the board in the real world as well.

• Chess engines look at several million positions per second, so they can in fact look at all them until they get a few moves deep. Mar 26, 2013 at 11:23
• This is an awesome question, I really enjoyed the answers. Mar 27, 2013 at 0:16
• Basically they search ahead with a search algorithm (e.g., minimax) and evaluate the positions using pre-programmed heuristics. Although some recent engines (e.g., AlphaZero) develop their own methods of evaluation through playing themselves. Sep 29, 2019 at 0:21
• To add to the comment of @InertialIgnorance, many machine learning (ML) based engines play against itself (or train with a game database). But, we have no idea how they actually 'think'; they are indeed blackboxes! Mar 10, 2023 at 6:03

In a general way chess engines use a decision tree. The root of the tree is the current position and has a child node for each position that can be made by making a legal move. Each of these nodes in turn have a child node for the positions that can be reached by making a legal move from them. The engine pushes the tree out to a depth defined by its capabilities and the time it is allowed to "think". Positions that can be reached in more than one way are simply cross referenced so that they will not have to be considered more than once. Once the tree is created the computer uses a set of weighted rules to analyze the final positions in the tree and starts to remove those that are undesirable or that the opponent can prevent it from reaching. The tree is cut down this way until only one move remains and the computer makes that move.

http://www.chess.com/blog/zaifrun has series or articles on creating a chess engine if you want a more in depth look at how they work.

• Nice answer, so when you see depth of 30, does that mean the engine has searched 30 nodes deep? Also, what does it mean by Ply? Mar 26, 2013 at 14:50
• I am not sure what Ply stands for without doing some more research, I have never been good with acronyms. Rather depth refers to how many nodes deep, each node would be a half move, or to moves deep might depend on the programmer. However I think that convention would have it be the number of moves deep. Mar 26, 2013 at 20:37
• A ply is a half of a move. A move in chess is one complete 'set' if you will by white + black. A ply is half of that, so just one color. Mar 27, 2013 at 14:57

You're asking a pretty complex question, but it's good to go back to basics. There are a couple of concepts to consider:

## Evaluation

If a (real) player is shown a position and asked "who is winning this game?", how do they go about deciding? Most likely, they will check a few basic things, such as: material differences, the degree to which pieces have been developed or are positioned "well", doubled / isolated / connected / passed pawns, (controlled) open files, how far up the board the pawns are.

Now, if you had to, you could come up with a systematic way to calculate a position score based on the above. You could decide, for example, that a pawn is worth 1 point, and that a passed pawn is worth 0.3 points more. Isolated or doubled pawns might be worth a bit less, etc. If you add up everything, you get an estimate value for the immediate position at hand.

This is known as evaluation, and basically all chess programs have a way to evaluate positions (ignoring novelty AI chess engines which are typically very weak).

But what about subtle, deep positional moves?

Well, we've only barely scratched the surface of position evaluation. The actual implementation of an evaluation function could be simplistic, to allow for more positions to be evaluated per second (albeit in a rough fashion), or more complex, leading to less positions evaluated, but with a higher degree of confidence. It's not unusual for the evaluation function to take into account hundreds or even thousands of separate pieces of information.

## Search

I've specifically left out something from the above, which most real players will immediately think about - is there any way to immediately win the game for either side? Any mates or "hanging" pieces visible? Although it's easy to trivialise this, it's anything but trivial.

What does it mean for a player to have full confidence in a combination? In the end, it boils down to having calculated all options. Real players typically won't do this (except for trivial or very forced mates), most of the time we will only consider a handful of options, and rule out others which seem to be "non-constructive" or obviously leading to a loss. We often make mistakes during this calculation, e.g. we may realise that a change in move orders makes threats evaporate, etc. The point is that to be completely sure of a combination, you actually need to calculate all the way to its conclusion, assuming each player will only make the best possible move available to them (this is referred to as "min/max").

Now, given that chess has a much larger search space (this is what "all possible moves in the future" is referred to) than what is feasible for a computer to calculate, compromises need to be made. Just like humans, computers can decide to disregard entire lines of thought based on certain criteria. This is known as heuristics. It's worthwhile noting that while you can only be really sure of a combination if you brute force it, a complex evaluation function can often detect the presence of threats (e.g. we could count forks, skewer opportunities, etc, to guide a search in that direction).

In the end, although computers are extremely fast, it's the heuristics which allow them to calculate so deep. That said, you may be surprised how deep modern engines calculate fully, typically it's beyond 3 moves, even in quick games.

## Conclusion / combining it all

So, to summarise - evaluation functions have a lot of intelligence built into them (i.e. they take into consideration more things than your average human player), heuristics allow the computer to cull lines of thought it decides probably aren't going to end well, and computers are extremely, extremely fast. Add them up, and they're pretty difficult to beat.

• Just an update: "AI"-based algorithms are in some of the best engines nowadays - AlphaZero, Lela, and Stockfish. With the aid of algorithms from machine learning, Stockfish was able to gain some hundreds extra in ELO when playing against other top engines. These engines also contributed interesting data to chess theory as some were trained with zero information fed in - learning from purely random moves at first, playing itself. AlphaZero was able to arrive at many openings played by super GMs. Also, the heuristics used are often alpha-beta pruning (just for a little more info). May 4, 2022 at 10:16

It is just impossible for computers to look deep enough (25 ply and more) and check every possible move.

What makes is possible is the technique called Alpha-beta pruning which means that computers, similar to humans (but way better) follow only the promising continuations.

They evaluate the positions constantly (based on some precoded rules, valuing material, king safety, activity, pawn structures etc) and look into variations that seem to be leading them towards the best position.

It is still close to magic how they manage to do it efficiently enough to evaluate millions of these positions in a second.

So to summarise- you are correct, they can not look at all the moves if they play strategic chess, but they can very quickly look at decent moves. The problem is still with the very long term plans and horizon to which they can see, but this is being worked on (Rybka analyses much slower, but plays much more positional chess, while Houdini is romantic at it's 'mechanical heart' calculating more moves and playing more aggressively). Even computers have their own styles!

• It is impossible to brute force most positions, but I'll add to your answer that there are 6-piece end tables for all combinations of legal pieces (6 total pieces e.g. 3 white & 3 black or 5 white & 1 black). Engines look up the solved answer to evaluate any position with 6 or fewer pieces. I'm not sure what its criteria are when they have a losing position (maximize number of moves before checkmate? Attempt tricks? Attack, going for a checkmate or for a profitable capture?). People are working on 7-piece tables although I don't know its progress / whether it is feasible to use (too many GB). May 4, 2022 at 10:28

I agree with the answers.

I remember GM Roman Dzindzichashvili talking about it, in one of Roman's labs videos, i don't remember what video it was (if anyone knows the details please edit my answer).

Roman said that the developer of Fritz engine is his friend. So Roman tested Fritz to see how good it was, and the developer told Roman that in order for fritz to make complex decisions (sacrificing materials in exchange for positional advantage for example) they had to change the value of the pieces, like telling the program that a bad bishop is worth 1 point, a bishop on an open diagonal is worth 7 points, knights are worth 5 points in close positions...

I don't know the exact numbers for each piece but that's how it works, and now your engine won't have any problem sacrificing a bad bishop or whatever if you can tell him the value of each piece in each position.

EDIT