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

Windows 11, bootable USB device not detected

 I recently needed to install a linux server on a new computer. The current operating system was windows 11. I decided to go with Ubuntu server. After following their instructions on how to download and create an .ISO image with Rufus , I had a problem: my USB key was not detected by the new computer as a bootable key. It turned our that the solution was the USB version. My USB key was 2.0. After doing exactly the same operation on a USB 3.0 device, everything worked fine. Bonus tip: on windows, to boot from a device, it's possible to restart the computer while holding the SHIFT key. The computer will restart and then display a menu from which you can access the different ways of booting your system. This menu will include any bootable device. Hope it helps!

How to use wxWidgets on Windows with a Makefile

 Hello! I wanted to learn GUI rpogramming in C++. One of the main libraries for this purpose is called wxWidgets. It's very popular and seem powerful enough to make even a complex graphical interface. I also liked the fact that it's multi platforms. I am on Windows, using MingW and I like coding with VS Code. I also like having a clear Makefile to generate my programs. So here is how I have managed to compile the Hello World program you can find on the wxWidgets website . First, go to their github repository and clone it in a folder you will keep on your computer. In the root directory, run the command git submodule update --init. If the clone operation has not done so already, this command will pull additional repositories needed by wxWidget. Go to build\msw. Run the command: mingw32-make -f makefile.gcc -j4 SHARED=1 UNICODE=1 BUILD=debug clean. It will delete any unwanted file. Once it's done, re run the command but without the clean instruction. This will build the lib...

Installing Postgres on Linux Lite (Ubuntu)

I have followed these instructions from the Postgres documentation : # Create the file repository configuration: sudo sh -c 'echo "deb https://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list' # Import the repository signing key: wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add - # Update the package lists: sudo apt-get update # Install the latest version of PostgreSQL. # If you want a specific version, use 'postgresql-12' or similar instead of 'postgresql': sudo apt-get -y install postgresql   After that, I was having trouble authenticating to Postgresql after installing the db server on Linux Lite.  This stackoverflow answer was very helpful. Open the file pg_hba.conf . For Ubuntu, use for example /etc/postgresql/13/main$ sudo nano pg_hba.conf and change this line at the bottom of the file, it should be the first line of the settings: local all ...