My Beginner’s Guide to Chess Programming

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

Moderators: hgm, Dann Corbit, Harvey Williamson

Forum rules
This textbox is used to restore diagrams posted with the [d] tag before the upgrade.
User avatar
lithander
Posts: 353
Joined: Sun Dec 27, 2020 1:40 am
Location: Bremen, Germany
Full name: Thomas Jahn

My Beginner’s Guide to Chess Programming

Post by lithander » Sat Jan 23, 2021 3: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.

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. A simple engine written in C#! Details on Youtube & Github

JohnW
Posts: 316
Joined: Wed Nov 21, 2012 11:20 pm
Location: New Hampshire

Re: My Beginner’s Guide to Chess Programming

Post by JohnW » Sat Jan 23, 2021 4:59 pm

wow, that's a great idea and I am glad you did it in C# instead of C++

User avatar
Roland Chastain
Posts: 476
Joined: Sat Jun 08, 2013 8:07 am
Location: France
Full name: Roland Chastain
Contact:

Re: My Beginner’s Guide to Chess Programming

Post by Roland Chastain » Sat Jan 23, 2021 5:42 pm

lithander wrote:
Sat Jan 23, 2021 3: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 3: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: 4045
Joined: Wed Oct 01, 2008 4:33 am
Location: Regensburg, Germany
Full name: Guenther Simon
Contact:

Re: My Beginner’s Guide to Chess Programming

Post by Guenther » Sat Jan 23, 2021 5:58 pm

lithander wrote:
Sat Jan 23, 2021 3: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 3: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?

User avatar
Guenther
Posts: 4045
Joined: Wed Oct 01, 2008 4:33 am
Location: Regensburg, Germany
Full name: Guenther Simon
Contact:

Re: My Beginner’s Guide to Chess Programming

Post by Guenther » Sat Jan 23, 2021 6:43 pm

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>

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

Re: My Beginner’s Guide to Chess Programming

Post by lithander » Sat Jan 23, 2021 7: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:
Minimal Chess. A simple engine written in C#! Details on Youtube & Github

User avatar
Guenther
Posts: 4045
Joined: Wed Oct 01, 2008 4:33 am
Location: Regensburg, Germany
Full name: Guenther Simon
Contact:

Re: My Beginner’s Guide to Chess Programming

Post by Guenther » Sat Jan 23, 2021 7:53 pm

lithander wrote:
Sat Jan 23, 2021 7: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.

User avatar
Guenther
Posts: 4045
Joined: Wed Oct 01, 2008 4:33 am
Location: Regensburg, Germany
Full name: Guenther Simon
Contact:

Re: My Beginner’s Guide to Chess Programming

Post by Guenther » Sat Jan 23, 2021 8:06 pm

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).


Gerd Isenberg
Posts: 2246
Joined: Wed Mar 08, 2006 7:47 pm
Location: Hattingen, Germany

Re: My Beginner’s Guide to Chess Programming

Post by Gerd Isenberg » Sat Jan 23, 2021 9:55 pm

lithander wrote:
Sat Jan 23, 2021 3: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: 353
Joined: Sun Dec 27, 2020 1:40 am
Location: Bremen, Germany
Full name: Thomas Jahn

Re: My Beginner’s Guide to Chess Programming

Post by lithander » Sun Jan 24, 2021 1:32 am

Thanks for the many comments everyone!
JohnW wrote:
Sat Jan 23, 2021 4: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 5: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 5: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 5: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 5: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 9: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 9: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 9: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. A simple engine written in C#! Details on Youtube & Github

Post Reply