Can an ordinary person like me create a chess engine?

Sort:
PyriteDragon

I am a very average chess player who is interested in creating my own engine. I don't expect it to be immensely powerful, but it would be cool to see how it would work. I guess I want to experiment to see if the engine will be a stronger player than me and by how much. Is there any way to do that? I am in no way talented at computer programming, which I think would be an obstacle preventing me from creating an engine. Maybe there's someone who can pitch in their opinion here and also it would be cool to talk to people who have been involved in creating an engine. So if you're one of those people, please comment. Or if you're interested in discussing this type of thing, also comment. Thanks!

itrenix
Hi. There are lots of resources out there. I tinkered with this many years ago as I started as a developer.

Perhaps take a look here to get started, however there are many other resources as well.

Although you could write an engine in potentially any language, C and C++ tend to be the more common choices, however, I wrote a very simple engine in C# as an experiment. Nothing special but it beat me and was a lot of fun :)

http://www.fam-petzke.de/cp_getstarted_en.shtml
PyriteDragon

Thanks! I'll take a look. If I do end up doing it, I would need to make sure someone takes a look at it, in particular for making sure the engine doesn't make illegal moves.

 

itrenix
Well if you want any help give me a shout. However, I must point out that as long as you know the rules of chess and can program, you can write an engine. Writing a really good engine, however, normally requires input from strong chess players. Many engines have input from Grandmasters. Like I said mine worked well but was in no way really strong and def not commercial strength in any way. It was more a fun project rather than anything else. There maybe others with more experience in this area who can chip in, perhaps.

You could also download some open source engines (example Stockfish) and take a look at the code.

If you can’t program you may struggle so perhaps earn some basics of your chosen language first.

Thanks
PyriteDragon

Thanks!

itrenix
NP
Dsmith42

You might want to look at the written works of Claude Shannon, since he wrote the first chess program of any reasonable strength.

krazeechess

lol you could do this in python but i would also suggest c++. I know python (I'm not an expert tho) and i could imagine how difficult it would be.

rock303

Do you guys know how to program an engine with machine learning? That sounds really interesting to me (bc of AlphaZero).

 

Also, is it much harder in Java?

rock303

is Tensorflow good for it

Bussho

interested parties might check out open source machine learning AI chess engine designed to play human style chess by training on millions of human games instead of the more typical approach of playing millions of games against itself.  

https://github.com/CSSLab/maia-chess

"Aligning Superhuman AI with Human Behavior: Chess as a Model System"

dude0812

I created my own engine. I myself am a 1900 rated player, my engine is weaker than me, but it plays like a 4 digit player. If I just make moves fast without bothering to think about my moves then it can beat me. I see you are rated 1400 on this website. You are probably a little bit stronger than my engine. 

You don't need to be an expert at programming nor at chess in order to make an engine which plays like a 4 digit player.

 

I have 2-3 years of experience with computer programming. I made the engine in Rust and that was my first project ever in Rust (you only need to know the basics of a language's syntax in order to make a chess engine). 

dude0812

 

Here are some tips. 

 

I use a recursive function in order to evaluate how good each legal move is. The recursive function goes deeper until it reaches one of the following criterias:

1) There are no legal moves (checkmate or stalemate has occured)

2) 50 move rule has been reached

3) a certain depth has been reached

 

Then, in the last recursive call of the function you can non recursively evaluate the position. You can do this by counting material, king safety, piece activity etc.

I actually don't immediately call this non recursive evaluation function. 

I first call another recursive function which only looks at captures. This recursive function keeps track of the material count on the last and second to last turn to move. This second recursive function stops going deeper when the player whose turn it is to move is not in a worse material position than he was when it was his previous turn to move. It also stops going deeper if there are no legal moves that capture pieces. This is different from what most chess engines do. Most chess engines analyse deeper as long as there are captures available in the position. They don't have the condition to stop going deeper when a player is not in a worse material situation than he was at his previous turn to move.

The reason why I have this second recursive function is because imagine that you analyse the position one move deep. You play a random legal move which is not a blunder. Your opponent responds by capturing your pawn which is protected. The engine would decide that this variation is bad for you because you "lost" a pawn. It wrongly thinks that you lost a pawn because it didn't analyse far enough to see that you can take the pawn back. I deal with this problem by telling the engine to continue analysing until it reaches a position where a player whose turn it is to move is not in a worse material situation than he was at his previous turn to move.

Most chess engines deal with this situation by analysing deeper as long as there are captures available.

dude0812

When it comes to my first recursive function I increase the depth if there is a check in the variation. So, for instance, 1st recursive function would normally look 2 moves ahead for the engine, 2 moves for its opponent, but if one of those moves is a check then the 1st recursive function would look 3 moves ahead for the engine and 3 moves for the opponent. 

The reason why I added this feature is because checks would really mess up the horizont effect. For instance, let's say I play against my engine and I attack its knight with my pawn. Instead of moving the knight my engine gives a check with a bishop. I block the check with a pawn and my engine has 2 pieces hanging (imagine Bb4+ and I respond with c3. I am attacking both the bishop and the knight). The engine then gives a check with a queen (Qe7+) and I block it (Be2). My engine stops analysing here and it concludes that this variation is good for the engine (it has developed 2 minor pieces, it hasn't lost any material, my engine "thinks" it is doing great), while in reality I am winning a piece on the next move as I am attacking both its bishop and its knight.

I prevent this from happening by telling the engine to increase the depth a little bit if a variation that it is analysing contains a check.

dude0812

Here is a very useful optimization. I call it the "one refutation is sufficient" principle.

Let's say that after 1.e4 e5 you first analyse the move h3 (h3 isn't a great move but it is not a blunder either).

Then, you analyse the move Ba6 (this move losses a piece). As soon as you find out that after Ba6 your opponent can play bxa6 and win a piece for free you are done analysing Ba6. You have found one refutation of Ba6 and one refutation is enough, it would be pointless to search for another refutation (even if it exists, which in this case it doesn't, but whether another refutation exists is irrelevant, what's relevant is that after finding out that one refutation exists you already know you are not going to play Ba6).

To generalize, the reason you know bxa6 refutes Ba6 is because bxa6 would leave your opponent in a better spot than had you played h3 instead of Ba6 (remember, h3 is a move that you had already analysed). This bit is important because there will be positions where the best you can do is lose a bishop for nothing, the fact that Ba6 losses a piece is not enough to be discarded. What discards Ba6 is the fact that after Ba6 bxa6 you are in a worse spot than you would be if you play h3 (a move that you have already analysed).

I will now say the same thing in a language which is closer to my computer code.

When analysing moves keep track of the best move that you have found thus far (or 3rd, 4th, 5th etc. best move if you want to find 3,4,5 candidate moves).

 

Pass the best evaluation so far to the next recursive call of the function.

In  my concrete example, when the recursive function is called upon move Ba6 the evaluation of move h3 is passed as an argument. After finding out that bxa6 leaves you at -3.5, you check that -3.5 is a better evaluation for you than 0.0 (0.0 is the evaluation if white plays h3 instead of Ba6) you return immediately, because you have refuted the entire Ba6 line. You have found a response to Ba6 (the move bxa6) which leaves you at a better spot (-3.5) than had your opponent played the best move that he had analysed so far (h3 with evaluation 0.0).

For general case:

When you analyse a move that leaves you in the better spot, it gives you a better evaluation than the evaluation that you have received as an argument,  return immediately. Why? Because you have just refuted this whole variation. You have found a move which makes this whole variation bad for your opponent. You have found a move which leaves you in the better spot than you would be in had your opponent played the best move that he had analysed so far. Again, to make a parallel with the concrete example, you return immediatelly upon finding out that after Ba6 you have bxa6 which leaves you in a better spot (-3.5) than had your opponent played the best move that he had analysed thus far (h3 with evaluation 0.0).

 

Another optimisation which is related to this one is to quickly find the refutation of nonsense moves such as Ba6 so that you waste less time analysing moves like that. You can achieve this by telling your engine to first analyse captures. By analysing captures first you will not only help your engine refute nonsense moves faster but it will calculate capture chains faster (since often in a capture chain any move other than capturing is a nonsense move. This isn't always true, but it is true enough times to massively speed up your engine).

sansuk
dude0812 schreef:

 

The reason why I have this second recursive function is because imagine that you analyse the position one move deep. You play a random legal move which is not a blunder. Your opponent responds by capturing your pawn which is protected. The engine would decide that this variation is bad for you because you "lost" a pawn. It wrongly thinks that you lost a pawn because it didn't analyse far enough to see that you can take the pawn back. I deal with this problem by telling the engine to continue analysing until it reaches a position where a player whose turn it is to move is not in a worse material situation than he was at his previous turn to move.

OMG

You only look at material ?

No chess program stops analayzing as long as there are captures possible.

dude0812
sansuk wrote:
dude0812 schreef:

 

The reason why I have this second recursive function is because imagine that you analyse the position one move deep. You play a random legal move which is not a blunder. Your opponent responds by capturing your pawn which is protected. The engine would decide that this variation is bad for you because you "lost" a pawn. It wrongly thinks that you lost a pawn because it didn't analyse far enough to see that you can take the pawn back. I deal with this problem by telling the engine to continue analysing until it reaches a position where a player whose turn it is to move is not in a worse material situation than he was at his previous turn to move.

OMG

You only look at material ?

No chess program stops analayzing as long as there are captures possible.

No, the criteria to stop going deeper is different from the criteria to evaluate a position, although both of them take into account material situation.

When my engine evaluates a position it takes into account material, king safety, general chess principles that I have coded into it, piece activity etc.

 

Material is a criteria which decides when the 2nd recursive function stops going deeper (other criteria are having 0 moves that capture, or that the game is over). It is much faster to "stop going deeper when you reach a position when you are not in a worse material situation than you were on your last turn to move" than to "go deeper as long as there are captures available". Sure,it is less precise, but not by much. I lose a little bit of precision for a massive gain in speed. Even if my engine were to analyse deeper as long as there are captures available in the position it would still miss dumb things (such as the opponent trapping engines rook, the engine doesn't see that because it stops analysing when it has no captures, not seeing that the opponent has trapped its rook. This is a real life example that I have encountered, making the engine analyse deeper as long as there are captures available wouldn't solve this problem).

tygxc

The source code of Stockfish is in public domain.

dude0812
tygxc wrote:

The source code of Stockfish is in public domain.

Thank you. I will look into it, especially when I have less responsibilities, but it was fun to make my own chess engine. It was a fun challenge and I suspect that's why OP wants to make an engine as well.

dude0812
Bussho wrote:

interested parties might check out open source machine learning AI chess engine designed to play human style chess by training on millions of human games instead of the more typical approach of playing millions of games against itself.  

https://github.com/CSSLab/maia-chess

"Aligning Superhuman AI with Human Behavior: Chess as a Model System"

I have seen a guy on youtube who made a chess AI and it was rated 500-600. Apparently you need supercomputers in order for AIs to be strong. Traditional chess engines are apparently stronger on ordinary PCs. I have made a traditional chess engine which is easily 4 digit strength and I am an amateur programmer and a mediocre chess player. If you want to create a decent chess playing program and run it on your PC, your best bet is making a traditional chess engine.