Click here to Skip to main content
15,893,401 members
Articles / Programming Languages / C#

My C# Chess Engine on BitBucket

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
27 Nov 2011CPOL3 min read 15.5K   4   2
My C# chess engine on BitBucket

I decided to publish my chess engine plays on bitbucket. Well, it is almost a redo from scratch of a complete but buggy chess engine I wrote in the past that I decided to rewrite just because it was difficult to stack into the old code that I learned. The version present when I write this post contains just the move generator and the complete test for it (I used another nice engine: roce to compare my perft test results against. What is a perft test? Well, it is a test to prove our engine produces, from a starting board, all the possible different boards in a certain number of ply, accordingly to the chess game rules. This test also gives an idea on how fast is the strategy we use to generate moves, even if this can affect just in part the overall performance of the alpha/beta pruning, we should not write a slow blobby monster. Let's see below a session of the test working:

image

The strange string showing the positions are board situations expressed in FEN Notation, that is almost the standard notation we use to talk about board situations. How many tests does FelpoII move generator pass? Well, here is the file containing the FEN boards, with the depth plies move counts shown, there are a lot of positions, even tricky and generally challenging positions (in term of rules).

What is the performance? Since almost all chess engines are written in C++ or in C, can a C# engine work at the same level of magnitude of performance? Here below the performance, we have for the starting position:

Depth: 6 119060324 moves 5,34 seconds. 22283422,048 Move/s. Hash Hit=1400809 

The same test with roce (that is a C++ chess engine):

Perft (6): 119060324, Time: 4.208 s 

So almost the same, that is good if we remember that we wrote in C# Smile. We just used a little hack: as you probably know (or will know if you will start playing with chess engines development) chess engines use hash tables to store information about a board (by using hashes against Zobrist Keys). This table is stored in an unmanaged big memory array, this achieved a really sensible increase in performances.

Well, some more details about the engine:

  • It uses a 0x88 board representation.
  • It uses object oriented code (so it is easier to understand compared to traditional C++ engines).
  • The internal random numbers for the zobrist key are generated by a Mersenne Twister Generator, that really solved some nasty bug due to wrong hash conflicts when I used the standard random algo of C#.
  • It uses a transposition table in unmanaged memory to increase performance.
  • It has a performance comparable (at least in move generation) with traditional C/C++ engines.

What We Can Do Next?

Complete the engine with a good working Negamaxalpha – beta pruning algorithm.

What Can We Do with the Code As Is?

We can use the move generator as is to validate game moves in a two human player UI, or for generating fancy images from FEN positions, write a WPF (another) chess board (winboard compatible? So a lot of engines are already written for it) and so on.

Enjoy!

Image 3

Image 4
Image 5

Image 6

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Italy Italy
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 5 Pin
Logi Guna26-Jan-13 5:57
professionalLogi Guna26-Jan-13 5:57 
GeneralMy vote of 5 Pin
Abinash Bishoyi29-Nov-11 7:00
Abinash Bishoyi29-Nov-11 7:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.