Upgrade to Chess.com Premium!

Creating a chess engine from scratch (Part 1: Basics)

Hi.

I have a master degree in computer science and mathematics. As a hobby project I will blog about the design and implementation (writing software code) of what goes into a chess engine - I am creating my own engine for fun. For those who wants to learn how a chess engine actually works this will probably be interesting as I will also talk about general principles of chess engines. 

So how does a chess engine work:

Well, there are basically two components of all chess engines:

1. position evaluation

2. Searching for the next move (and choosing the best)

The first is a piece of code that evaluates any position and gives it a numerically value (positive means white is better, negative means black is better, while a evaluation value of 0.0 means the position is equal). How is a position evaluated by a computer? Well, basically it adds up all pieces on the board (for instance 1.0 for a pawn, 3.0 for knight and bishop, 5.0 for a Rook, 9.0 for a Queen - negative values for black) and then applies a lot of heuristic rules. These rules are known as rules of thumb such as a double pawn is usually not good, a knight on the rim is dim (bad), castling is usually good etc etc. All these heuristics are usually hard-coded by the programmers with help from Grand Masters. A good chess program can easily have 100s or 1000s of these heuristics that modifies (up or down) the evaluation of the position. 

2. Searching for the next move

Since there are about 10 raised to the power of 120 different chess games (compare that to the estimated number of particles in the universe - around 10 raised to the power of 80 is the estimate) then even a computer cannot simply look at all moves. So all engines generate a tree of moves where the root is the current position, then for each legal move there is a branch from the root to all the new positions resulting from one move, then from each of these branches you add new branches for all legal moves etc etc. 

Obviously this search tree gets extremely big very fast (it grows exponentially) so you have to cut off branches that looks unpromising and concentrate on the promising branches - i.e. the critical lines. This is what humans are extremely good at - we usually only look at a very narrow search tree with 1 or 2 branches for each move. Computers use brute force - i.e. computational speed to evaluate a much wider tree (because they are too stupied to know what the narrow branches should be automatically).

A computer can easily evaluate a few million positions per second, a human probably 1-2 positions per second!! Usually speed is measured in MNodes/sekund which means million positions (Nodes in computer science jargon) per second. Fritz running on my old laptop does about 2.5 MNodes while Deep Blue did about 200 MNodes per second. Raw power is not all - the evaluation function is also very important. Pretty much all engines use the same algorithm for searching through the search tree of possible moves to find the next move. This algorithm is known as alfa-beta search or it is some variant of this. This technique builds on a simpler method known as min-max method, which I will discuss in part 2. 

Basically, results have shown that this brute-force way of playing chess works extremely well - massive calculations are what computers do best, which is why all engines are remarkably similiar in design. 

To further enhance an engine you will usually add a database of opening moves - an opening book and end game databases as well. For instance now there are databases for ALL positions with 6 or fewer chess pieces which will tell you whether any move from any position will lead to draw, win or lose. If we will ever get a 32-men database then we will have solved chess. i.e we will know if chess is draw, win for white or win for black (with perfect play from both sides). Recently checkers (or draughts as it is know in British English) was solved. We know now that with perfect play checkers is always a draw. Checkers has about 10^20 legal positions, while chess has about 10^40 or so legal positions. So we are looooong way from solving chess - will probably not happen in my life time (my guess is that chess is actually a draw game with perfect play from both sides. You can tell me if I am wrong or right in the year of 2134 or so!)

 

In my next blog I will describe my basic design (which is a bit different from the standard method described above - I want to try a new approach to the move evaluation part - no reason to make yet another rykba-crafty-fritz-ivanhoe clone. My engine will be a hybrid of several ideas)

Comments


  • 5 months ago

    hrecho

    Hi i was thinking. What about to use some decent algorythm for choosing moves and let the pc play again himself randomly. Than record the kardinality for every position that lead to the win. After some time of playing randomly some positions would get bether grades some worse. Like a map position -> number of wins it led to. So this way the algorythm could improve himself for a price of time and capacity of hdd :) has this aproach been used? or is it leading to unrealistic databases or too big time for pc to evolve?

  • 11 months ago

    Tom_Hindle

    I have never created a chess engine but I would be interested in doing but I wouldn‘t know what app/program to be able to create it in and it would be good if the program was free due to it just being for fun why I want to create one

  • 11 months ago

    mkbd4

    Dear Zaifrun. Thank you for introducing this neophyte to new worlds. Could you please suggest an efficient path to required basic programming ie what do i need before i commence learning how to design chess engine. Kindest regards. Murray

  • 12 months ago

    LogicCrazy

    If you would like further details/examples check out my step-by-step YouTube tutorials ranging from simple to advanced chess engines.  I discuss both the 'why' and the 'how' of programming a chess engine.  I hope this helps as an extra resource.

  • 14 months ago

    deankellam

    How about a war against two identical chess engines one as white the other as black. With a search based on scoring threats in the positions.

  • 15 months ago

    THOMASWSCHERZER31337

    The great transposintional move in chess is peace. Simply offer a draw before you move a piece or after you move a peice depending on what rules you play by.

    34men?

    It only takes two! 

    Why is something so simple as the greatest move in chess is to not move and offer a draw! ? SO Complex of a problem? 

    oh btw im "technically" a college dropout so 

    (W+.5|B+.5) is not 0(zero) You do have the ability to make peace before war! as nobody has made a move or white has made the first move you cant base your objection on denying a draw to position! Therefore the only transpositional move in chess (a win/lose game) is PEACE/DRAW!

    Why dont you test that out nexttime you design a program to play a game, add it in your logic tree most people forget that they can draw by choice or force a draw! Laughing Just a thought, hope it helps!

  • 15 months ago

    manthan12345

    okay.. i think u r the guy i m looking for.i m medicine student but i love mathematics.. i do want to make chess engine..but i know nothing about it..how to create chess engine? what basic things i must know before creating it? i dnt knw also how to make programming language..lol.. in my 10th grade i have learned logo and pascal language..that's it..plz guys do help me..who knows if in future my chess engine may beat houdini ?!! :)

  • 23 months ago

    Jamalov

    brilliant

    tak

  • 3 years ago

    LookDontThink

    Hi.  I've read almost the entire series  you have written on writing a chess engine, and I must say I am impressed.  You have simply outlined a great way to make your own engine, not giving away too many details, and letting the reader come away with a good idea of how chess engines work in general.

    I am in the middle of writing my own chess engine.  It has alpha-beta pruning with simple position evaluation, and am currently looking at ways to improve it.  Your list on Advanced Position Evaluation is going to help me tremendously, and if you have any other ideas you want to share with me, I would love to hear them.  I will also let you know if I think of anything.

    Again, thank you for writing this blog, and I hope to hear from you soon.

    -Dan-

  • 4 years ago

    tranminhkhoi2

    And in 2134, chess960 will replace the normal chess Cool

Back to Top

Post your reply: