- Blind mode tutorial
lichess.org
Donate

I was bored and decided to build my own chess engine. Because why not?

As the title says while watching some WC games between Magnus and Nepo, I started slowly coding away.

My first objective was to simply code all the rules of chess- so I could move pieces around. Pawns were super annoying to code as black/white pawns move differently (they move opposite directions which is something you'd never even think about unless you formally try to code the rules and the captures and en passant and promotions... ugh pawns are duuumb). Castling was also somewhat painful to get right - so many conditions etc...

Once I had most of the rules of chess set in code. The next step was to make an AI. The first one was just:

  1. get all available moves
  2. play one of them at random
  3. ???
  4. PROFIT!

Technically it is an AI just a very bad one. So I did the next best thing and quickly coded up an evaluation function which does two things for now:

  1. counts all the piece material (pawn 1, B, N = 3, R = 5, Q = 9, K = ???)
  2. Then I tried to determine piece quality by multiplying the base value by a coefficient depending on the number of available moves and captures

I didn't know what to do with the Kings. What value should a king be? So I gave the King a value of 100. Thinking it would do it's best not to lose it. But a piece worth hundred was just too delicious for the AI and it tried to activate it immediately. So now my kings are worth 0, but losing ones king gives a score of +/- infinity, with the idea that I am allowing the capture of kings but any move that would prevent it would have a higher value. So in theory it will avoid checkmate as much as possible.

So having a rudimentary eval function. I just put it in a minmax algorithm - an algorithm that tries to find the best move (maximize the eval function for you), while simultanously minimizing it for the opponent (making sure that while you maximize it from your side the opponent has the weakest possible replies). Hence the name minmax.

Now my biggest challenge is to improve my evaluation function and also improve the performance of the program so I can search and evaluate more positions per second. The current number varies between 40k-200k positions per second depending on some changes of my algorithm and the position too.

I am most happy about the choice of implementing FEN import/export as one of the first features I coded, allowing me to initialize the AI at any state of the game.

Here is a sample game it played against itself just now:

  1. e3 e5 2. Qf3 Qf6 3. Qxf6 Nxf6 4. b3 Bb4 5. Ba3 a5 6. c3 Bxa3 7. Nxa3 a4 8. b4 Ke7 9. Bd3 d6 10. Ne2 Bd7 11. O-O e4 12. Bc2 Bg4 13. Nd4 Nc6 14. Nab5 Nxd4 15. Nxd4 h5 16. Kh1 h4 17. f3 exf3 18. gxf3 Bd7 19. Rg1 c5 20. bxc5 dxc5 21. Nf5+ Bxf5 22. Bxf5 g6 23. Bh3 Rhd8 24. Rab1 b5 25. Rxb5 Rxd2 26. Rxc5 Rxa2 27. Rc7+

Every move here took from 30-130s to make. I am actually quite pleased with the result. It's not very strong play but given the simplicity of the eval function and relatively low performance of positions per second. It seems to play reasonably and so far no insanely stupid moves. Although after the check black "blundered his king", which is not something that should happen but I guess it's work in progress and a bug that needs fixing.

My current goals are to make a smarter evaluation function, adding some strategic elements like doubled pawns, king safety etc

Also the performance needs to improve, more positions per second allows deeper search.

Has anyone of you guys ever attempted to make a chess AI? I actually found it to be relatively easy and I am thinking of keeping updates to this thread with my progress, problems I encounter funny bugs etc

As the title says while watching some WC games between Magnus and Nepo, I started slowly coding away. My first objective was to simply code all the rules of chess- so I could move pieces around. Pawns were super annoying to code as black/white pawns move differently (they move opposite directions which is something you'd never even think about unless you formally try to code the rules and the captures and en passant and promotions... ugh pawns are duuumb). Castling was also somewhat painful to get right - so many conditions etc... Once I had most of the rules of chess set in code. The next step was to make an AI. The first one was just: 1. get all available moves 2. play one of them at random 3. ??? 4. PROFIT! Technically it is an AI just a very bad one. So I did the next best thing and quickly coded up an evaluation function which does two things for now: 1. counts all the piece material (pawn 1, B, N = 3, R = 5, Q = 9, K = ???) 2. Then I tried to determine piece quality by multiplying the base value by a coefficient depending on the number of available moves and captures I didn't know what to do with the Kings. What value should a king be? So I gave the King a value of 100. Thinking it would do it's best not to lose it. But a piece worth hundred was just too delicious for the AI and it tried to activate it immediately. So now my kings are worth 0, but losing ones king gives a score of +/- infinity, with the idea that I am allowing the capture of kings but any move that would prevent it would have a higher value. So in theory it will avoid checkmate as much as possible. So having a rudimentary eval function. I just put it in a minmax algorithm - an algorithm that tries to find the best move (maximize the eval function for you), while simultanously minimizing it for the opponent (making sure that while you maximize it from your side the opponent has the weakest possible replies). Hence the name minmax. Now my biggest challenge is to improve my evaluation function and also improve the performance of the program so I can search and evaluate more positions per second. The current number varies between 40k-200k positions per second depending on some changes of my algorithm and the position too. I am most happy about the choice of implementing FEN import/export as one of the first features I coded, allowing me to initialize the AI at any state of the game. Here is a sample game it played against itself just now: 1. e3 e5 2. Qf3 Qf6 3. Qxf6 Nxf6 4. b3 Bb4 5. Ba3 a5 6. c3 Bxa3 7. Nxa3 a4 8. b4 Ke7 9. Bd3 d6 10. Ne2 Bd7 11. O-O e4 12. Bc2 Bg4 13. Nd4 Nc6 14. Nab5 Nxd4 15. Nxd4 h5 16. Kh1 h4 17. f3 exf3 18. gxf3 Bd7 19. Rg1 c5 20. bxc5 dxc5 21. Nf5+ Bxf5 22. Bxf5 g6 23. Bh3 Rhd8 24. Rab1 b5 25. Rxb5 Rxd2 26. Rxc5 Rxa2 27. Rc7+ Every move here took from 30-130s to make. I am actually quite pleased with the result. It's not very strong play but given the simplicity of the eval function and relatively low performance of positions per second. It seems to play reasonably and so far no insanely stupid moves. Although after the check black "blundered his king", which is not something that should happen but I guess it's work in progress and a bug that needs fixing. My current goals are to make a smarter evaluation function, adding some strategic elements like doubled pawns, king safety etc Also the performance needs to improve, more positions per second allows deeper search. Has anyone of you guys ever attempted to make a chess AI? I actually found it to be relatively easy and I am thinking of keeping updates to this thread with my progress, problems I encounter funny bugs etc

Nice. I suppose its kinda hard. But i do have a project in mind. I have a 3d printer, ill print a chess set and i plan on buying some sensors.
In short, ill try to make my own dgt as im poor and dont have 500 usd to spare lol, but im also starting to learn carpentry and i am seriously considering building chess and go boards.

If i succeed with the prototype (ill give myself like a 1 year deadline to become decent at different skills, building boards, and maybe even learn how to work with plastic for 1 man "mass" production) i may even consider entering the market.

Any pointers on where to start with the coding part to interpret the moves?

Nice. I suppose its kinda hard. But i do have a project in mind. I have a 3d printer, ill print a chess set and i plan on buying some sensors. In short, ill try to make my own dgt as im poor and dont have 500 usd to spare lol, but im also starting to learn carpentry and i am seriously considering building chess and go boards. If i succeed with the prototype (ill give myself like a 1 year deadline to become decent at different skills, building boards, and maybe even learn how to work with plastic for 1 man "mass" production) i may even consider entering the market. Any pointers on where to start with the coding part to interpret the moves?

@Alientcp hah nice to hear about your project. Good luck with it!

The coding part for that should not be hard I think and if you are inexperienced with coding a project like that is an excellent way to learn it as you have a concrete goal to work towards and to motivate you solve specific problems.

The question is what kind of sensors are you going to use? I think NFC sounds like something you could potentially use for this. They are not expensive and you can program in codes for pieces in basic stickers that you can put it under the pieces. More difficult would be to put the sensors in the actual board. Which means you would need a matrix of those sensors so it is a rather nonstandard set up and I guess you better be a good DIY guy to make it happen. The challenge is def in the hardware for you. Code will be child's play after that.

@Alientcp hah nice to hear about your project. Good luck with it! The coding part for that should not be hard I think and if you are inexperienced with coding a project like that is an excellent way to learn it as you have a concrete goal to work towards and to motivate you solve specific problems. The question is what kind of sensors are you going to use? I think NFC sounds like something you could potentially use for this. They are not expensive and you can program in codes for pieces in basic stickers that you can put it under the pieces. More difficult would be to put the sensors in the actual board. Which means you would need a matrix of those sensors so it is a rather nonstandard set up and I guess you better be a good DIY guy to make it happen. The challenge is def in the hardware for you. Code will be child's play after that.

@likeawizard said in #1:

  1. e3 e5 2. Qf3 Qf6 3. Qxf6 Nxf6

In my childhood I got one of the first chess computers as a christmas gift (the Chess Champion Mk1 if anyone remembers this thing) and it would play something similar.

The question is what kind of sensors are you going to use? I think NFC sounds like something you could potentially use for this.

Until the DGT boards came out it was rather common to use simple magnets under the pieces and reed contacts under the squares. You don't need to recognize the type of a piece, only the source and the destination field, provided you start with a known position.

@likeawizard said in #1: > 1. e3 e5 2. Qf3 Qf6 3. Qxf6 Nxf6 In my childhood I got one of the first chess computers as a christmas gift (the Chess Champion Mk1 if anyone remembers this thing) and it would play something similar. > The question is what kind of sensors are you going to use? I think NFC sounds like something you could potentially use for this. Until the DGT boards came out it was rather common to use simple magnets under the pieces and reed contacts under the squares. You don't need to recognize the type of a piece, only the source and the destination field, provided you start with a known position.

@Katzenschinken no idea of the chess computer you're talking about. I'm guessing that's something from the early nineties? But ye since my engine makes no use of opening books such aggressive play makes sense - activate your strongest piece and make immediate threats. I guess it would need to see somewhat deeper for it to see threats being made against the queen and see it as a bad move.

About the magnets- do you know what mechanism was used to read the magnetic field? Was it just some metallic switch that got actuated by the magnet or a Hall sensor? And as you mentioned - it only sees a piece leave one square and land on another so your software needs to keep track of what was where along the way. A digitized signal would have the ease of just reading what piece actually stands where. Accidentally knocking over some pieces while moving or pushing them on different squares could potentially 'corrupt' the game sate

@Katzenschinken no idea of the chess computer you're talking about. I'm guessing that's something from the early nineties? But ye since my engine makes no use of opening books such aggressive play makes sense - activate your strongest piece and make immediate threats. I guess it would need to see somewhat deeper for it to see threats being made against the queen and see it as a bad move. About the magnets- do you know what mechanism was used to read the magnetic field? Was it just some metallic switch that got actuated by the magnet or a Hall sensor? And as you mentioned - it only sees a piece leave one square and land on another so your software needs to keep track of what was where along the way. A digitized signal would have the ease of just reading what piece actually stands where. Accidentally knocking over some pieces while moving or pushing them on different squares could potentially 'corrupt' the game sate

@likeawizard
That chess computer came out even earlier, in the late 1970s. It had only a very rudimentary evaluation and thus very weak positional play, it had no opening library and you were even allowed to play illegal moves. As I said: it was one of the very first chess computers on the market.

The sensors for picking up a magnetic field are these:
https://en.wikipedia.org/wiki/Reed_switch
They are very cheap, only a few cents per piece.

And as you mentioned - it only sees a piece leave one square and land on another so your software needs to keep track of what was where along the way.

Yep, that's a slight problem. You mustn't slide a piece over the board otherwise you could disrupt your move recognition. Instead you have to pick up the piece, lift it above the board and put it down exactly on the square where it is supposed to go. But this was standard routine with all chess computers that used magnets and reed switches, pretty much any chess computer with a wooden board. You get used to it.

@likeawizard That chess computer came out even earlier, in the late 1970s. It had only a very rudimentary evaluation and thus very weak positional play, it had no opening library and you were even allowed to play illegal moves. As I said: it was one of the very first chess computers on the market. The sensors for picking up a magnetic field are these: https://en.wikipedia.org/wiki/Reed_switch They are very cheap, only a few cents per piece. > And as you mentioned - it only sees a piece leave one square and land on another so your software needs to keep track of what was where along the way. Yep, that's a slight problem. You mustn't slide a piece over the board otherwise you could disrupt your move recognition. Instead you have to pick up the piece, lift it above the board and put it down exactly on the square where it is supposed to go. But this was standard routine with all chess computers that used magnets and reed switches, pretty much any chess computer with a wooden board. You get used to it.

@greysensei said in #8:

That video actually appeared on my nerd YouTube feed recently, however, I was reluctant to watch it as I didn't want to spoil my challenge of doing truly by myself. Will probably check it out at some point.

I'm wondering what kind of Glicko rating would you rate my bot from the sample game?
I'm in the mid 1300s for blitz and I can easily crush it y chasing it's queen around. So I guess it could be in the 900s?

@greysensei said in #8: That video actually appeared on my nerd YouTube feed recently, however, I was reluctant to watch it as I didn't want to spoil my challenge of doing truly by myself. Will probably check it out at some point. I'm wondering what kind of Glicko rating would you rate my bot from the sample game? I'm in the mid 1300s for blitz and I can easily crush it y chasing it's queen around. So I guess it could be in the 900s?

@Katzenschinken okay so it is a metallic switch in there as I guessed. Still even in the nepo carlsen match there were moments where the board misread a move and it needed to be corrected by someone manually as it stayed on the live feed for almost a minute.

And the chess computer looks gangsta and old school.

@Katzenschinken okay so it is a metallic switch in there as I guessed. Still even in the nepo carlsen match there were moments where the board misread a move and it needed to be corrected by someone manually as it stayed on the live feed for almost a minute. And the chess computer looks gangsta and old school.

This topic has been archived and can no longer be replied to.