m8 Dev log and C# experiments

Discussion of chess software programming and technical issues.

Moderator: Ras

mathmoi
Posts: 290
Joined: Mon Mar 13, 2006 5:23 pm
Location: Québec
Full name: Mathieu Pagé

m8 Dev log and C# experiments

Post by mathmoi »

I was inspired to try and write a chess engine using C# in another thread and decided to give it a go.

I'm not sure how far I'll go, but I also wanted to start a dev log thread here a other have done and share my experience and some eperiments I might do along the way.

I just did a first performance experiment, I tried to test if it would be possible to make a struct wrapper around an integral type to make the code more type safe and more expressive. My very limited test seems to imply it will be possible.

I wrote a post about it on my blog. I will try and write about theses eperiments in blog form too as a way to try to become a better writer. As it's probably painfully obvious english is not my first language.

https://www.mathieupage.com/posts/sharper-chess-engine/

My next steps will be to write some bases types and a Board representation.
chessica
Posts: 922
Joined: Thu Aug 11, 2022 11:30 pm
Full name: Esmeralda Pinto

Re: m8 Dev log and C# experiments

Post by chessica »

mathmoi wrote: Tue Nov 14, 2023 4:58 am I was inspired to try and write a chess engine using C# in another thread and decided to give it a go.

I'm not sure how far I'll go, but I also wanted to start a dev log thread here a other have done and share my experience and some eperiments I might do along the way.

I just did a first performance experiment, I tried to test if it would be possible to make a struct wrapper around an integral type to make the code more type safe and more expressive. My very limited test seems to imply it will be possible.

I wrote a post about it on my blog. I will try and write about theses eperiments in blog form too as a way to try to become a better writer. As it's probably painfully obvious english is not my first language.

https://www.mathieupage.com/posts/sharper-chess-engine/

My next steps will be to write some bases types and a Board representation.
eduherminio
Posts: 76
Joined: Mon Apr 05, 2021 12:00 am
Full name: Eduardo Caceres

Re: m8 Dev log and C# experiments

Post by eduherminio »

Following closely Mathieu, please keep posting your findings.

You may want to consider using readonly struct in your experiments, where relevant.

PS:
I ended using the following global aliases for some types to make the code clearer, but they don't prevent one from making the type errors you mention in your blog:

Code: Select all

  <ItemGroup>
    <Using Include="System.Int32" Alias="Move" />
    <Using Include="System.UInt64" Alias="BitBoard" />
  </ItemGroup >
  
Author of Lynx chess engine (GitHub, Lichess)
User avatar
lithander
Posts: 915
Joined: Sun Dec 27, 2020 2:40 am
Location: Bremen, Germany
Full name: Thomas Jahn

Re: m8 Dev log and C# experiments

Post by lithander »

I look forward to following your devlog and already enjoyed the blog post.

I like how you tried to replace integers with custom types that are more typesafe. Personally I didn't go all the way as to make a Square type (because here I think using integer values is well justified and not a hack) but for example Move has always been a struct for me. Oftentimes there is another (lighter) alternative to using structs though: For a Piece-type and many other things I define an enum which is basically defines a set of constants that is stored as integer (or byte) behind the scenes.
Minimal Chess (simple, open source, C#) - Youtube & Github
Leorik (competitive, in active development, C#) - Github & Lichess
mathmoi
Posts: 290
Joined: Mon Mar 13, 2006 5:23 pm
Location: Québec
Full name: Mathieu Pagé

Re: m8 Dev log and C# experiments

Post by mathmoi »

lithander wrote: Wed Nov 15, 2023 12:02 am I like how you tried to replace integers with custom types that are more typesafe. Personally I didn't go all the way as to make a Square type (because here I think using integer values is well justified and not a hack)...
I agree that it might be somewhat excessive to create structs for File, Rank, and Square, as I plan to do, but there could be advantages. For example, I will override the ToString method for these types, so that while debugging, the debugger will display 'e2' instead of '12'. This approach could also enable something like looping over squares without going over the edge.

Code: Select all

Square sq = Square.a2;
while(sq.IsValid)
{
   // Do Something interesting with sq
   sq = sq.Right;
}
Chessqueen
Posts: 5685
Joined: Wed Sep 05, 2018 2:16 am
Location: Moving
Full name: Jorge Picado

Re: m8 Dev log and C# experiments

Post by Chessqueen »

chessica wrote: Tue Nov 14, 2023 9:54 am
mathmoi wrote: Tue Nov 14, 2023 4:58 am I was inspired to try and write a chess engine using C# in another thread and decided to give it a go.

I'm not sure how far I'll go, but I also wanted to start a dev log thread here a other have done and share my experience and some eperiments I might do along the way.

I just did a first performance experiment, I tried to test if it would be possible to make a struct wrapper around an integral type to make the code more type safe and more expressive. My very limited test seems to imply it will be possible.

I wrote a post about it on my blog. I will try and write about theses experiments in blog form too as a way to try to become a better writer. As it's probably painfully obvious English is not my first language.

https://www.mathieupage.com/posts/sharper-chess-engine/

My next steps will be to write some bases types and a Board representation.
NOTE: Next match will be Monarch 1.7 Vs Mamoi both rated around 2050

BikJump Vs Matmoi

Rank Engine Score Bi Ma S-B
1 Bikjump 35.5/51 · ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· · 10=01011=11==111==111111111111110111100011011010=0 540.00
2 Matmoi 14.5/51 01=10100=00==000==000000000000001000011100100101=1 · ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· ·· · 540.00


50 of 50 games played

Tournament start: 2023.11.10, 06:28:46
Latest update: 2023.11.15, 16:24:07
Site/ Country: DESKTOP-4QNC0GS, United States
Level: 10 Seconds
Hardware: Intel(R) Core(TM) i7-4770 CPU @ 3.40GHz with 15.9 GB Memory
Operating system: Windows 10 Home Home Edition (Build 9200) 64 bit
PGN-File: Stockfish14 Vs Berserk12.pgn
Table created with: Arena 3
Chessqueen
Posts: 5685
Joined: Wed Sep 05, 2018 2:16 am
Location: Moving
Full name: Jorge Picado

Re: m8 Dev log and C# experiments

Post by Chessqueen »

These 2 engine will kill each other before they reach 50 games, there will be 2 funerals soon, but I am learning as I watch them destroy each other :mrgreen:

455‑456 Monarch 1.7 2124 +21 −21 50.1% +0.1 18.8% 836
51.7%
457 FrankyGo 1.0.2 64-bit 2123 +19 −19 51.5% −9.8 30.3% 990
53.2%
458 Bodo 0.2b 2122 +21 −22 50.5% −3.8 20.8% 808
54.5%
459 Uragano 3D 0.92 2120 +27 −27 43.7% +46.5 18.0% 533
57.4%
460 Cupcake 1.1c 64-bit 2116 +27 −27 52.6% −16.9 20.4% 529
53.6%
461 Alcibiades 0.3.0 64-bit 2115 +22 −22 47.8% +16.8 18.1% 802
51.7%
462 Honzovy Sachy 2 64-bit 2114 +17 −17 54.6% −34.3 16.7% 1326
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Monarch 1.7 Vs Matmoi

Rank Engine Score Mo Ma S-B
1 Monarch(v1.7) 2.0/4 · ·· ·· 1001? 3.75
2 Matmoi 2.0/4 0110? · ·· ·· 3.75


4 of 50 games played

Tournament start: 2023.11.15, 19:01:59
Latest update: 2023.11.15, 20:49:53
Site/ Country: DESKTOP-4QNC0GS, United States
Level: 10 Seconds
Hardware: Intel(R) Core(TM) i7-4770 CPU @ 3.40GHz with 15.9 GB Memory
Operating system: Windows 10 Home Home Edition (Build 9200) 64 bit
PGN-File: Monarch 1.7 Vs Matmoi.pgn
Table created with: Arena 3.5.
User avatar
Steve Maughan
Posts: 1276
Joined: Wed Mar 08, 2006 8:28 pm
Location: Florida, USA

Re: m8 Dev log and C# experiments

Post by Steve Maughan »

I'm pleased to see Monarch 1.7 getting some airtime after all these years! :D

— Steve
http://www.chessprogramming.net - Juggernaut & Maverick Chess Engine
JoAnnP38
Posts: 253
Joined: Mon Aug 26, 2019 4:34 pm
Location: Clearwater, Florida USA
Full name: JoAnn Peeler

Re: m8 Dev log and C# experiments

Post by JoAnnP38 »

eduherminio wrote: Tue Nov 14, 2023 2:48 pm

Code: Select all

  <ItemGroup>
    <Using Include="System.Int32" Alias="Move" />
    <Using Include="System.UInt64" Alias="BitBoard" />
  </ItemGroup >
  
I've never seen aliases used like this in the project file. I am going to remember that for my future projects. Thanks for sharing!!
User avatar
emadsen
Posts: 440
Joined: Thu Apr 26, 2012 1:51 am
Location: Oak Park, IL, USA
Full name: Erik Madsen

Re: m8 Dev log and C# experiments

Post by emadsen »

mathmoi wrote: Tue Nov 14, 2023 4:58 am I was inspired to try and write a chess engine using C# in another thread and decided to give it a go.
Welcome to the chess programming community. Good luck with your engine.
eduherminio wrote: Tue Nov 14, 2023 2:48 pm

Code: Select all

  <ItemGroup>
    <Using Include="System.Int32" Alias="Move" />
    <Using Include="System.UInt64" Alias="BitBoard" />
  </ItemGroup >
  
That doesn't provide type safety (contrasted with defining an enum or struct, which does). You can pass a Move alias to a method that requires a int parameter and the compiler won't complain.
Erik Madsen | My C# chess engine: https://www.madchess.net