How to get started with chess programming

Discussion of chess software programming and technical issues.

Moderator: Ras

ScottMoore
Posts: 1
Joined: Mon Mar 14, 2022 8:25 pm
Full name: Scott Moore

How to get started with chess programming

Post by ScottMoore »

Hey folks. I started playing chess around December 2020, and kept it up for several months. I kinda fell out of practice with it because there were other hobbies I was focusing on, and because I live somewhere quite rural there weren't any in-person chess clubs near me.

However, this past while I have become interested in the properties of chess as a game, and in the idea of the programming side of it. For example, there are a few things I'd be interested in looking into:
  • Analysis of games ending in mate-in-n after x plies from the starting position- there is some info on it here I've seen looking at mate-in-5 games up to 5 plies in, and mate-in-1 games up to 10 plies in, but I'd be interested to see if we could find how many mate-in-6, mate-in-7 and so on games there are among the first plies in chess. Related to this is obviously the question of finding perft(16), etc, how far that can be realistically pushed, and having the games up to a given perft(n) mapped and available, in a searchable format (if this isn't already the case?), so that they can be analysed like tablebases can be analysed
  • Analysis of the existing 7-man tablebase to find out what percentage of legal positions are checkmate positions, what percentage are mate-in-1, mate-in-2, etc, or draw-in-1, draw-in-2, etc, and to compare those stats across 7-man positions, 6-man positions etc.
  • Analysis of random positions in chess (8-man or greater positions) to find what percentage of legal positions in chess are checkmate positions, what percentage of legal positions are mate-in-1, what percentage are mate-in-2, or draw-in-1 or draw-in-2, etc. Even the question of how you generate a random position, while ensuring there isn't any skew in your sample, is a difficult one - I've seen some of John Tromp's work on this and it's clearly complicated.
  • Overall - mapping chess. Figuring out what percentage of legal chess positions are 'solved'/known, as checkmate, mate-in-1, mate-in-2, draw-in-1, draw-in-2 etc, figuring out what percentage of legal chess positions aren't solved, and mapping all of this. A further example - many chess positions exist (8-man or greater) where one side has a significant material advantage (with the other side having insufficient material to checkmate) and likely have a short mate-in-x sequence available, though likewise many of these positions will be stalemate positions instead - one could categorise these positions and produce estimates for how many are mate-in-x for the winning side and how many are stalemates/draws, thereby further mapping out how many chess positions are 'solved', and reducing the number that aren't.
I realise that the people working on these topics are pretty advanced and knowledgeable in their fields, and have access to beefy computing power to conduct this research properly. I'm also conscious I don't have programming experience myself, but this is an area I'm interested in and I intend to do a Master's in computer science or a related field at some point down the line (my Bachelor's is in a humanities subject). So I don't expect to be leading any breakthroughs but I'd be interested in making whatever contribution I can, however I can, and I'd appreciate any advice on how I could get started. From what I've read on the Chess Programming Wiki the recommendation to beginners is that they work on something simple like building a chess engine, but given that plenty of chess engines already exist and it doesn't directly relate to the things I'm interested in looking into, I don't think I'd feel enthused about that. I'd appreciate any pointers anyone can provide.
User avatar
j.t.
Posts: 263
Joined: Wed Jun 16, 2021 2:08 am
Location: Berlin
Full name: Jost Triller

Re: How to get started with chess programming

Post by j.t. »

https://github.com/tromp/ChessPositionRanking

Maybe that's not really what you're thinking about, but maybe it's interesting anyways.
pedrojdm2021
Posts: 157
Joined: Fri Apr 30, 2021 7:19 am
Full name: Pedro Duran

Re: How to get started with chess programming

Post by pedrojdm2021 »

First of all, before get started doing chess programming, you need first to learn programming in general, you need to be able to write programs with a good structure / good performance in general.

That's something that you can learn even in youtube ( im a self-taught programmer ) or in the university.
Also you need to choose what language you want to learn? C# is very powerful currently and is not too hard to learn for example, but also there are C / C++ and even Rust is used to write programs, those others are hard to learn and master, but gives you more control about everything.

So after you feel confortable with your coding skills, you can start your journey with chess programming:
You can start by following one of these youtube playlists:



Or this one:



any of these should teach you at least the basic knowledge about chess programming, after that is just matter of reading other engines source code, reading the chessprogramming wiki , and asking questions here.

one very important thing you need to learn also is bitwise operations, they are used a lot in chess programming, specially if you want to do something performant.

Good luck!
Chessnut1071
Posts: 313
Joined: Tue Aug 03, 2021 2:41 pm
Full name: Bill Beame

Re: How to get started with chess programming

Post by Chessnut1071 »

pedrojdm2021 wrote: Wed Mar 16, 2022 1:12 am First of all, before get started doing chess programming, you need first to learn programming in general, you need to be able to write programs with a good structure / good performance in general.

That's something that you can learn even in youtube ( im a self-taught programmer ) or in the university.
Also you need to choose what language you want to learn? C# is very powerful currently and is not too hard to learn for example, but also there are C / C++ and even Rust is used to write programs, those others are hard to learn and master, but gives you more control about everything.

So after you feel confortable with your coding skills, you can start your journey with chess programming:
You can start by following one of these youtube playlists:



Or this one:



any of these should teach you at least the basic knowledge about chess programming, after that is just matter of reading other engines source code, reading the chessprogramming wiki , and asking questions here.

one very important thing you need to learn also is bitwise operations, they are used a lot in chess programming, specially if you want to do something performant.
bitwise operations improved my search function over 7x. Most of the increase in speed; however, is the evaluation function. In mate solutions it gave me a 100x increase. I have no experience with ELO optimization YET, but, there's a huge difference on what's important between those two objectives.
Good luck!
zamfofex
Posts: 26
Joined: Wed Feb 16, 2022 6:21 am
Full name: P. M. Zamboni

Re: How to get started with chess programming

Post by zamfofex »

I am relatively new to chess programming in general too, so take my advice with a grain of salt!

I think it would be useful to know how much you are invested into programming and familiar with it to give an accurate advice.

At any rate, I think the first thing that is worthwhile is to investigate how to find legal moves for a position. First, you can start with something simple like pseudo‐move generation, then eventually move onto more complicated (yet faster) approaches such as using bitboards. This will be able to help you become familiar with programming (if you aren’t yet) and also with how chess programs are usually written and used.

After that, you can start coming up with challenges to solve, like the ones you posed in your OP, for example.