Skip to main content

Building a chess game in Go

Context 

I have recently decided to focus my efforts on the Go programming language. I have used it professionally before and it is a pleasure to go back to it. I really enjoy working with it.

In order to practice, I have created a very crude chess game. You can see the code on Github here: https://github.com/nakurai/go-chess-game.


 

 I have tried several time to build the chess logic, and gave up every time. I am very happy to have completed it. I used the most naive data structures and algorithms in this logic, but that's how I was able to complete it.

To handle the UI, I used Ebitengine. The documentation is quite sparse (at least for now) but it is a very neat library. I would like to do more with it in the future.

As you can see in the video, the "AI" is for now very trivial. It just picks a move randomly among all the possible ones available.

Challenges 

In terms of challenges, learning how to use the game engine while ramping up my skills in Go was the main challenge I would  say. The logic for now is still quite straightforward, and having the unit tests helped a lot to detect edge cases.

Roadmap 

There are many ways to improve this project, and I think it's a very good playground. On my list of improvements are:

  • Make the AI incrementally smarter. I have a lot of ideas about this. For a starter, I would like to build a heuristic function able to assess the intrinsic "value" of a board in any specific state. That would help determine the next move. I know stockfish has one, so it should be feasible.
  • Make the UI part of the game better. For now, it does not even acknowledge a checkmate/draw state!
  • Make the art look better. I would like to learn how to use 2D sprites with Ebitengine and make the game as a whole better.
  • Optimize the chess logic. For now it is all very naïve, and given the simple "AI" it does not show its limitations. However, there are a lot of possible optimizations. As the logic gets more complicated, the idea would be to keep an eye for emerging bottlenecks and go from there.

Takeaways 

In my previous attempts, I tried to follow a tutorial on how to build an optimize version right away. I think that was a mistake, especially while learning more about a language, a library, etc.

"Make it work, make it good, make it fast" rules. Well, I made it work. 

Comments

Popular posts from this blog

How to generate a self-signed certificate with Openssl (using git windows bash)

I recently needed to generate a self-signed certificate to test a website locally. I wasn't familiar with the process, and I wanted to share here what I learned. First, we need a private key. A private key is a long series of characters that must be kept secret. In my context, it will be used to encrypt messages before the client and the server, in a way secure enough to prevent anybody to spy on them. Once the private key is created, we need to generate another file that will be the "signature" of our certificate. Among other data, this file will contains some information specific to the server's context: country, organization's name, email address of the organization's technical contact, etc. Once this signature is established, there are two paths: - Path A: If we want our server to be publicly accessible, every browser in the world must able to trust the certificate. In order for that to happen, we need to send our signature file to one of the official SSL ...

How to Compile a GTK4 program on Windows (using MSYS)

  I wanted to create a GUI program using C and GTK. I am using a windows operating system, so I had followed the instructions from the GTK Windows installation page . However, when trying to compile my program using: gcc main.c -o main.exe `pkg-config --cflags --libs gtk4` I had the following errors: gcc.exe: error: pkg-config: No such file or directory gcc.exe: error: gtk4`: No such file or directory gcc.exe: error: unrecognized command-line option '--cflags' gcc.exe: error: unrecognized command-line option '--libs' I noticed that the command  pkg-config --cflags --libs gtk4 was actually returning a list of required includes. It seemed that the gcc command simply was not able to recognize them. To solve the problem, I then created a Makefile, in which a variable called GTK_4_INCLUDES contains the list of includes.  The compilation command becomes:   gcc main.c -o main.exe $(GTK_4_INCLUDES) To compile the program in the console, I just had to type mingw32-make , a...

Fixing x-invalid-end-tag lint error on Vuejs

  My situation is: Coding with vue in a Vue file. Using the Vetur plugin in VS code It then generates an error of type x-invalid-end-tag in a quite random fashion. To solve it, I have found one simple trick in this github issue: // .eslintrc.js module.exports = {  //... rules:  { 'vue/no-parsing-error': [2, { 'x-invalid-end-tag': false }]  }  }