top of page

Chess AI

Overview

Personal
2 weeks
C++
Unity
Unreal

Summary

   My chess AI project. After making this one, I felt a strange feeling that you may know too as an AI dev : May I be happy to keep loosing against my own AI ?... I made a great job right... Or am I so bad at chess ? ... Frustrating!

​

   Well anyway, more seriously, this is a project of a generic chess AI integrated in both Unreal Engine and Unity. It can play only with basic rules of chess, so no rock nor fancy en passant.

​

  It's implemented with a dll loaded in both engines as integration examples, but it could be done with any chess user interface that can load C++ dll. The targeted platform is Windows 64 bit architecture to make a good use of the bitboards (more details below). The AI can compute up to 6 plies (= half-move = someone plays) in a decent time for the user, "decent" meaning that it's good for the player to wait this time even if he is totally angry because he is loosing against a god damn AI! I have a lot to say on this project, so let's jump in!

Context

  This project came from a simpler project from my school, in which I had to create the most performant AI possible on Unity in 2 week. The thing is, I wanted to practice my C++ skills, so I asked my teachers if I could do this on Unreal Engine, and sadly, they refused (no problem my revenge is coming...). Asking this, I came up with an idea to merge my needs of engine/C++ training and the obligation to use Unity.

​

  I found the solution in a obscure feature of Unity (at least for most gameplay programmers) : the native plug-in handle. After a quick Google on that question, helped by the fact that I already linked C# and C++ with Mono like Unity in a previous project, I came to ask myself why should I limit myself with the default Unity = C# with that plug-in tech? The only limitation was the fact that I would have to use a specific plug-in for each new platforms I would like to target, but in that case, the only one needed was 64 bit Windows architecture, so it was good to go.

Global features

  After I implemented this idea, I ended up with a really simple interface on my dll, with only two functions needed to make it work basically : something to init the AI (doing all the precomputation stuff) and something to compute a move given a board, returning it (the only 2 structures that have to be passed from managed to unmanaged code). I added some more functions to handle the AI vs AI case, allowing to create easily multiple instances of the AI, but that's all the functions I needed.

 

  To make another example on how to use my AI in a generic way, I implemented an Unreal chess UI using it. The biggest problem with that was being able to handle the DLL itself from Unreal, because I couldn't find any good way to make Unreal package the project with it, so I still have to use a custom script that simply copy/past the dll to do it. I sadly hadn't the time to search a really long time because I still had a good AI to do, but if you know how to do it, I'm still curious, so feel free to contact me in that case.

AI

  As I said before, for my board, I used a bitboard representation to get the basic operations as fast as possible. I didn't get too far with it (didn't had time to implement fancy magic bitboards for example), but it still helped in the overall performances. For the best move search, I made an alpha-beta pruning, improved later in PVS search. For heuristics, I tried multiple things, like different material counting, some simple and fast, some more complex that can change in the end game. I finally kept the last one for a more 'strategic' AI, but from my tests, I learned sadly that the best way to get a good AI was by making it gain performance, resolving the problem faster in a near brute-force way, so I kept the most simple material counting for my best AI.

Debugging

  The DLL idea was unexpectedly really helpful to debug the AI. I could simply save the dll of passed AI versions  and make the current version play against them to be able to compare the different iterations and perform easy regression tests. I also could make my AI play against the AIs of other students, just giving them a class to wrap the marshalling stuff, and the current version of the dll, so it was quite trivial.

Conclusion

  I think wrapping this AI in a dll was a great idea, even if I was only on Unity first, because I could more easily go on perfs and debug everything. Furthermore having only one file for every version of the AI was a really good thing. Thanks to this, as you can see if you get the package, I ended up with a Unity project, an Unreal project, and three different versions of the AI. Basically, it was a tough project because I had to deal with multiple problems within a deadline made for only pure chess AI problems, but the time passed to setup everything was totally worth the trouble in term of features added and skills earned.

Builds

Windows 64

Hey!

It's Benjamin.

    Thanks for looking at my website! I hope you liked my projects so far! I've done a lot of different things, and I used some cool stuff that you could try in your own projects. Don't hesitate to wander a little in the project pages!

   

    If you have anything to ask me about or if you have some ideas to share, please do, I love chatting about technical stuff ;-) .

A Job?

A Question?

bottom of page