My Beginner’s Guide to Chess Programming

Discussion of anything and everything relating to chess playing software and machines.

Moderators: hgm, Rebel, chrisw

User avatar
lithander
Posts: 881
Joined: Sun Dec 27, 2020 2:40 am
Location: Bremen, Germany
Full name: Thomas Jahn

My Beginner’s Guide to Chess Programming

Post by lithander »

I have written a minimal chess engine from scratch in C# to learn about chess programming.

With “minimal” I don’t mean least lines of code or smallest executable size. Instead tried to make it “minimal” in the sense of being as simple and easy to understand as possible.

For example I use an 8x8 array of pieces to represent my board as it is conceptually simpler than padded 10x12 or even bitboards. My move generator is also very simple to understand, I think. There’s no necessity yet for undoing moves. Everything was written from scratch to solve the current problem that I got confronted with. You won't learn a lot about the established best practices but I think the process I followed is playful and intuitive and easy to follow.

With the educational purpose in mind I decided to document the development with four explanatory Youtube videos. These videos are really the heart of the project - at least they took much longer to edit than writing the code! ;)

At this point I’m not sure what I’m still missing that would be considered essential for even the simplest viable engine?
In a way I feel like it’s done: everything I can come up with now seems to be a somewhat arbitrary step towards a stronger engine, but also a step away from the ‘minimal’ version that just does what’s really essential. For example if I borrow some texel-tuned PSTs from other engines I’ll gain approximately 300 ELO and win most games against Stockfish set to 1350 ELO. (Just summing up piece values like I do now I win only 10% of the games.) But there’s nothing original about that and it gives the impression that auto-tuned PSTs are somehow a required component for all chess engines.

So please be my judge: Do you think there is any merit in a simple, educational engine like I present it here especially if you consider the accompanying tutorial videos? Is it “good enough” when it plays at only ~1000 ELO? If not let me know what you think the critical rating threshold would be and I’ll try to reach it in the simplest/easiest way I can come up with and make a 5th video about that.

When I was trying to find opponents for my engine I clicked through the last percentiles of the CCRL lists but a lot of the links were dead or the engines didn’t support fast time controls or had other issues. To finalize this project, I would like to submit my engine to be included in tournaments so that other beginners browsing the lists can find it as an opponent! But I’m not sure that I’m ready yet to apply there. I suppose I would have to at least implement some kind of time control support? Anything else?

The code is on github, here are the 4 videos on Youtube and I’ve provided a Windows build of the engine. If you encounter problems with running it on Windows please let me know! I’m not sure if the dependencies are installed with a normal Windows 10. If you want a Linux or Mac build that should be possible in theory, too.

Actually, any kind of feedback would be much appreciated! Even negative opinions would help me to decide how to move forward with it. I would be very happy if the videos would find a small niche audience that can appreciate them! But I'm not sure how to find and address them. You guys are well beyond what I have to say but maybe you have good ideas? Let me know of you thoughts!
Minimal Chess (simple, open source, C#) - Youtube & Github
Leorik (competitive, in active development, C#) - Github & Lichess
JohnW
Posts: 381
Joined: Thu Nov 22, 2012 12:20 am
Location: New Hampshire

Re: My Beginner’s Guide to Chess Programming

Post by JohnW »

wow, that's a great idea and I am glad you did it in C# instead of C++
User avatar
Roland Chastain
Posts: 640
Joined: Sat Jun 08, 2013 10:07 am
Location: France
Full name: Roland Chastain

Re: My Beginner’s Guide to Chess Programming

Post by Roland Chastain »

lithander wrote: Sat Jan 23, 2021 4:17 pm I have written a minimal chess engine from scratch in C# to learn about chess programming.

With “minimal” I don’t mean least lines of code or smallest executable size. Instead tried to make it “minimal” in the sense of being as simple and easy to understand as possible.
I like this idea. This is also what I tried to do, with more or less success. :)

If you look for opponents that your engine can beat, you could take a look at my GitHub page.
lithander wrote: Sat Jan 23, 2021 4:17 pm If you want a Linux or Mac build that should be possible in theory, too.
If you think that it is possible to build your engine for Linux, I would like to try. If someone knows how to do, I would be glad to learn.

Regards.

Roland
Qui trop embrasse mal étreint.
User avatar
Guenther
Posts: 4607
Joined: Wed Oct 01, 2008 6:33 am
Location: Regensburg, Germany
Full name: Guenther Simon

Re: My Beginner’s Guide to Chess Programming

Post by Guenther »

lithander wrote: Sat Jan 23, 2021 4:17 pm I have written a minimal chess engine from scratch in C# to learn about chess programming.

...

When I was trying to find opponents for my engine I clicked through the last percentiles of the CCRL lists but a lot of the links were dead or the engines didn’t support fast time controls or had other issues. To finalize this project, I would like to submit my engine to be included in tournaments so that other beginners browsing the lists can find it as an opponent! But I’m not sure that I’m ready yet to apply there. I suppose I would have to at least implement some kind of time control support? Anything else?
Did you also look at my XB/UCI chronology? As you have realized the main crux with most of the programs in that region is that they often are unstable, cannot win with huge material plus, don't support all time controls, don't know repetition rules, or left out some 'edge' cases (underpromotions, stalemate etc.).
This makes them often unusable for rating lists (also ratings will be somehow polluted depending on the opponents in that region).

OTH engines are not only created for rating lists and might be useful for weaker Humans as sparring partner as an example and ofc for a first learning experience in programming, as mentioned in your first part I left out. (I am just no fan of C#, sorry)

I pondered already about adding a 'stability' column too after the thread created by OliThinks author last year.
FWIW strength alone is no criterium to be added to the chronology as long as it is no random or near random mover.

Monchester should be a good sparring partner, if your estimation is correct.

Ah and yes for your question about being included in rating lists, it should at least support mps (moves per (repeating) sessions) time controls
and being able to play from arbitrary positions (fed moves), as nowadays people mostly feed a few book moves from GUIs/CLIs to get some
variance and the game starts from there.
lithander wrote: Sat Jan 23, 2021 4:17 pm Actually, any kind of feedback would be much appreciated! Even negative opinions would help me to decide how to move forward with it. I would be very happy if the videos would find a small niche audience that can appreciate them! But I'm not sure how to find and address them. You guys are well beyond what I have to say but maybe you have good ideas? Let me know of you thoughts!
So, does it have any limitations mentioned above?
https://rwbc-chess.de

trollwatch:
Talkchess nowadays is a joke - it is full of trolls/idiots/people stuck in the pleistocene > 80% of the posts fall into this category...
User avatar
Guenther
Posts: 4607
Joined: Wed Oct 01, 2008 6:33 am
Location: Regensburg, Germany
Full name: Guenther Simon

Re: My Beginner’s Guide to Chess Programming

Post by Guenther »

It is a bit puzzling that it extracts to a folder named
'Sunfish PST Depth 4' as Sunfish is a know Python chess engine
OTH I could not find at a first glance that you were using the PST of Sunfish, just some generic piece values?

Moreover it doesn't run here anyway, this is in Win7-64 with DotNet 4.8. Does it require the latest one?
The error messages are a bit cryptic too, as the mentioned installation folder for DotNet is not the one used here.
Normally it's always in a Win folder, may be it has changed after Win7 who knows.

Code: Select all

C:\Engines\UCI\MinimalCE_01-64\Sunfish PST Depth 4>MinimalChessEngine.exe
A fatal error occurred. The required library hostfxr.dll could not be found.
If this is a self-contained application, that library should exist in [C:\Engines\UCI\MinimalCE_01-64\Sunfish PST Depth 4\].
If this is a framework-dependent application, install the runtime in the global location [C:\Program Files\dotnet] or use the DOTNET_ROOT environment variable to specify the runtime location or register the runtime location in [HKLM\SOFTWAR
E\dotnet\Setup\InstalledVersions\x64\InstallLocation].

The .NET Core runtime can be found at:
  - https://aka.ms/dotnet-core-applaunch?missing_runtime=true&arch=x64&rid=win7-x64

C:\Engines\UCI\MinimalCE_01-64\Sunfish PST Depth 4>
https://rwbc-chess.de

trollwatch:
Talkchess nowadays is a joke - it is full of trolls/idiots/people stuck in the pleistocene > 80% of the posts fall into this category...
User avatar
lithander
Posts: 881
Joined: Sun Dec 27, 2020 2:40 am
Location: Bremen, Germany
Full name: Thomas Jahn

Re: My Beginner’s Guide to Chess Programming

Post by lithander »

Thanks for the feedback Guenther! I'll get back to your other comments soon, let me just quickly address the issues with the release.

I uploaded the wrong Zip. :roll: As I mentioned in my original post I tried my super simple eval vs Sunfish's PSTs but never meant to release it.

To allow multi-platform support I used the .Net Core 3.1 Framework. You can find it here: https://dotnet.microsoft.com/download/dotnet-core/3.1

But I don't want to force every potential user to install a Framework. So I have uploaded two alternative release bundles that are a bit bigger but should contain all necessary dependencies. Could you let me know if downloading the Release.v1.Big.Executables.zip or Release.v1.Standalone.zip
allow you to run the two executable (UCI Engine + ASCII Board) without installing any additional dependencies?

Sorry, for the inconvenience! :oops:
Minimal Chess (simple, open source, C#) - Youtube & Github
Leorik (competitive, in active development, C#) - Github & Lichess
User avatar
Guenther
Posts: 4607
Joined: Wed Oct 01, 2008 6:33 am
Location: Regensburg, Germany
Full name: Guenther Simon

Re: My Beginner’s Guide to Chess Programming

Post by Guenther »

lithander wrote: Sat Jan 23, 2021 8:30 pm Thanks for the feedback Guenther! I'll get back to your other comments soon, let me just quickly address the issues with the release.

I uploaded the wrong Zip. :roll: As I mentioned in my original post I tried my super simple eval vs Sunfish's PSTs but never meant to release it.

To allow multi-platform support I used the .Net Core 3.1 Framework. You can find it here: https://dotnet.microsoft.com/download/dotnet-core/3.1

But I don't want to force every potential user to install a Framework. So I have uploaded two alternative release bundles that are a bit bigger but should contain all necessary dependencies. Could you let me know if downloading the Release.v1.Big.Executables.zip or Release.v1.Standalone.zip
allow you to run the two executable (UCI Engine + ASCII Board) without installing any additional dependencies?

Sorry, for the inconvenience! :oops:
Downloaded both, but after extracting them I saw one of them had too much files and dlls for my taste ;-)
So I started the other one (big binaries) for now (just cmd - will try a GUI later):

Code: Select all

uci
id name MinimalChessEngine
uciok
ucinewgame
isready
readyok
position startpos
go 5000
Attack index buffers computed in 3,3927ms
bestmove g1f3
The cmd board version also started ok.
https://rwbc-chess.de

trollwatch:
Talkchess nowadays is a joke - it is full of trolls/idiots/people stuck in the pleistocene > 80% of the posts fall into this category...
User avatar
Guenther
Posts: 4607
Joined: Wed Oct 01, 2008 6:33 am
Location: Regensburg, Germany
Full name: Guenther Simon

Re: My Beginner’s Guide to Chess Programming

Post by Guenther »

Its first game in Winboard vs. Monchester 0.99
Monchester has also no tc management but it can be compiled to change the fixed depth.
I can try one ply more later.

One thing I noticed that should be available, would be showing output (eval/depth).

[pgn][Event "Test_2019 Winboard 4.9.1"]
[Site "CAPPUCCINO"]
[Date "2021.01.23"]
[Round "-"]
[White "MinimalChessEngine (UCI2WB)"]
[Black "Monchester 0.99"]
[Result "1-0"]
[TimeControl "40/120"]
[Annotator "1... -0.42"]

1. d3 c6 {-0.42/4 0.15} 2. g4 Nf6 {-0.22/4 0.17} 3. f3 Nd5 {-0.28/4 0.19}
4. Bg2 e6 {-0.17/4 0.23} 5. Bd2 Qf6 {-0.14/4 0.41} 6. Qc1 Qh4+
{-0.20/4 0.51} 7. Kf1 Bc5 {-0.25/4 0.44} 8. e3 a6 {-0.42/4 0.46} 9. a4 Nf6
{-0.40/4 0.49} 10. Bc3 Nd5 {-1.28/4 0.44} 11. d4 Be7 {-0.45/4 0.44} 12. b3
Nf6 {-0.28/4 0.41} 13. Bb2 Qh6 {-0.25/4 0.32} 14. Ke2 Bb4 {-0.25/4 0.44}
15. a5 Qg5 {-0.22/4 0.41}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Kh7 {+2.14/4 0.04} 58. Rh2+ Kg7 {+2.14/4 0.02} 59.
Rf2 Kg6 {+2.14/4 0.05} 60. Rg2 Kh6 {+2.14/4 0.03} 61. Kc3 Kh7
{+1.88/4 0.03} 62. Kb4 Kh6 {+2.11/4 0.03} 63. Ka3 Kh7 {+2.14/4 0.02} 64.
Kb2 Kh6 {+2.11/4 0.03} 65. Be2 Kh7 {+1.80/4 0.02} 66. Bd3+ Kh6
{-4.05/4 0.02} 67. Rg8 b6 {-12.54/4 0.02} 68. Rxc8 bxc5 {-13.71/4 0.03} 69.
Bxf4+ Kh5 {-14.65/4 0.02} 70. Be2+ Kg6 {-14.54/4 0.02} 71. Rxb8 Kf5
{-14.85/4 0.04} 72. Be3 Kf6 {-40.17/4 0.05} 73. Bxc5 Ke5 {-40.62/4 0.06}
74. Bc4 Kf4 {-41.20/4 0.04} 75. Bxa7 Ke5 {-41.22/4 0.05} 76. Bc5 Kf6
{-41.82/4 0.03} 77. Ka3 Ke5 {-41.85/4 0.04} 78. Rb7 Ke4 {-42.22/4 0.02} 79.
Rxd7 Kf4 {-42.82/4 0.01} 80. Bd3 Kg3 {-42.85/4 0.03} 81. Kb4 Kf4
{-42.91/4 0.03} 82. Bb6 Kg5 {-42.85/4 0.02} 83. Be2 Kf4 {-42.88/4 0.02} 84.
Kb3 Ke4 {-42.85/4 0.03} 85. Ka2 Kd5 {-42.85/4 0.02} 86. Bf3+ Ke5
{-42.91/4 0.02} 87. Rd8 Ke6 {-42.88/4 0.02} 88. Kb3 Kf7 {-42.88/4 0.03} 89.
Be2 Kf6 {-42.82/4 0.02} 90. Bc7 Kf5 {-42.74/4 0.03} 91. Rh8 Ke6
{-42.85/4 0.03} 92. Rf8 Kd5 {-42.97/4 0.02} 93. d7 Kc5 {-84.00/4 0.02} 94.
Bxa6 Kd4 {-2857.14/4 0.01} 95. d8=Q+ Kc5 {-2857.14/4 0.01} 96. Ka2 Kb4
{-2857.14/2 0.01} 97. Qd3 Ka4 {-2857.14/4 0.02} 98. Rc8 Kb4
{-2857.14/4 0.02} 99. Bb5 Kc5 {-2857.14/4 0.01} 100. Ba6 Kb4
{-2857.14/4 0.01} 101. Qb3+ Kc5 {-2857.14/4 0.02} 102. Bf4 Kd4
{-2857.14/4 0.01} 103. Bh2 Kc5 {-2857.14/4 0.02} 104. Qa4 Kd5
{-2857.14/2 0.02} 105. Re8 Kc5 {-2857.14/4 0.01} 106. Rc8 Kd5
{-2857.14/2 0.02} 107. Qc4#
{Xboard adjudication: Checkmate} 1-0[/pgn]
https://rwbc-chess.de

trollwatch:
Talkchess nowadays is a joke - it is full of trolls/idiots/people stuck in the pleistocene > 80% of the posts fall into this category...
Gerd Isenberg
Posts: 2250
Joined: Wed Mar 08, 2006 8:47 pm
Location: Hattingen, Germany

Re: My Beginner’s Guide to Chess Programming

Post by Gerd Isenberg »

lithander wrote: Sat Jan 23, 2021 4:17 pm I have written a minimal chess engine from scratch in C# to learn about chess programming.

With “minimal” I don’t mean least lines of code or smallest executable size. Instead tried to make it “minimal” in the sense of being as simple and easy to understand as possible.

For example I use an 8x8 array of pieces to represent my board as it is conceptually simpler than padded 10x12 or even bitboards. My move generator is also very simple to understand, I think. There’s no necessity yet for undoing moves. Everything was written from scratch to solve the current problem that I got confronted with. You won't learn a lot about the established best practices but I think the process I followed is playful and intuitive and easy to follow.
...
Actually, any kind of feedback would be much appreciated! Even negative opinions would help me to decide how to move forward with it. I would be very happy if the videos would find a small niche audience that can appreciate them! But I'm not sure how to find and address them. You guys are well beyond what I have to say but maybe you have good ideas? Let me know of you thoughts!
You should take the next step to use alpha-beta instead of pure minimax. Iterative deepening as well.
Too many news - better use pre-allocated boards indexed by ply for copy-make. Negamax does not imply multiplying with color all the time.
User avatar
lithander
Posts: 881
Joined: Sun Dec 27, 2020 2:40 am
Location: Bremen, Germany
Full name: Thomas Jahn

Re: My Beginner’s Guide to Chess Programming

Post by lithander »

Thanks for the many comments everyone!
JohnW wrote: Sat Jan 23, 2021 5:59 pm Wow, that's a great idea and I am glad you did it in C# instead of C++
C++ has it's strengths but beginner friendliness isn't one of them! :)
Roland Chastain wrote: Sat Jan 23, 2021 6:42 pm If you look for opponents that your engine can beat, you could take a look at my GitHub page.
Wow, Pascal, Basic, Lua? That's like meeting old friends from school you haven't seen for decades. (literally^^)
I had MinimalChess play 50 games against alouette64! Here are the results!

Code: Select all

Score of alouette64 vs MinimalChess: 7 - 27 - 16 [0.300]
...      alouette64 playing White: 3 - 14 - 8  [0.280] 25
...      alouette64 playing Black: 4 - 13 - 8  [0.320] 25
...      White vs Black: 16 - 18 - 16  [0.480] 50
Elo difference: -147.2 +/- 85.0, LOS: 0.0 %, DrawRatio: 32.0 %
50 of 50 games finished.
Roland Chastain wrote: Sat Jan 23, 2021 6:42 pm If you think that it is possible to build your engine for Linux, I would like to try. If someone knows how to do, I would be glad to learn.
I picked DotNet Core specifically because it's supposed to be cross platform but I never used it before. Apparently you don't even need Linux to make a Linux build from Visual Studio. :shock: Then I downloaded my own zip from within an emulated Ubuntu and the only thing I had to do was to add the permission to execute the file. After that it responded to my 'uci' command as exepcted. Give it a try ;)
Guenther wrote: Sat Jan 23, 2021 6:58 pm Did you also look at my XB/UCI chronology?
I was not aware of your chronology! But that's a really cool resource. Also the list is mind-blowingly long, I'll need to dig into that. Also, when I looked, MinimalChess was added already! Thank you :)
Guenther wrote: Sat Jan 23, 2021 6:58 pm As you have realized the main crux with most of the programs in that region is that they often are unstable, cannot win with huge material plus, don't support all time controls, don't know repetition rules, or left out some 'edge' cases (underpromotions, stalemate etc.).

This makes them often unusable for rating lists (also ratings will be somehow polluted depending on the opponents in that region).

Ah and yes for your question about being included in rating lists, it should at least support mps (moves per (repeating) sessions) time controls
and being able to play from arbitrary positions (fed moves), as nowadays people mostly feed a few book moves from GUIs/CLIs to get some
variance and the game starts from there.

So, does it have any limitations mentioned above?
I think it's fairly stable, it supports starting from a position given as FEN string, and I have tested the move generator against the testsuite from http://www.rocechess.ch/perft.html. Book moves work too - at least in CuteChess.

But at this point MinimalChess will ignore any time control parameters and always performs a hardcoded depth 4 search, basically always playing blitz even if it shouldn't. So that's a limitation it has. Also the repetition rules are not implemented. And you're right about the lack of output in the GUI, too.
Gerd Isenberg wrote: Sat Jan 23, 2021 10:55 pm You should take the next step to use alpha-beta instead of pure minimax. Iterative deepening as well.
I'm looking forward to trying that next!

But I'm considering to keep MinimalChess at it's current minimal scope and instead just make a 2nd repository with a different name where I don't have to worry about the goal of simplicity and how I would explain each bit of code in Youtube videos. ;)
Gerd Isenberg wrote: Sat Jan 23, 2021 10:55 pm Too many news - better use pre-allocated boards indexed by ply for copy-make.
The garbage collector of the .Net runtime is incredibly efficient and will reuse the same instances. But you made me curious. I just tried a view variants where I explicitly reused the same handful of boards (both with Array and Stack) and the best result I could get was 5.3s instead of 5.9s for a 5 ply search on a complicated mid game position. 10% faster searches isn't nothing.
But I'm sure in a non-managed language like e.g. C++ the difference would more noticeable.
Gerd Isenberg wrote: Sat Jan 23, 2021 10:55 pm Negamax does not imply multiplying with color all the time.
To quote CPW "In order for negaMax to work, your Static Evaluation function must return a score relative to the side to being evaluated" so the multiplying with color just happens somewhere else. But I shouldn't have called my implementation negamax, I guess. I'll remove the mention from the comments.

Thanks for your input! Each point has been very thought provoking!
Minimal Chess (simple, open source, C#) - Youtube & Github
Leorik (competitive, in active development, C#) - Github & Lichess