Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Prerequisites

There’s a few more things we need to discuss before you can start on this journey. These are the… dreaded… prerequisites. They are not particularly exciting, but they are important to get right so you can avoid disappointments during this project.

This is gonna take a while

First: Let’s set expectations. This is not a weekend project (for most people) if you want to do it well. Writing a strong chess engine takes time, not only for writing it, but also for researching, debugging, and testing. Be prepared: you’re going to be at this for some time before you see the first results. Those results would be your engine playing a non-trivial game against another engine, and winning it.

Not for complete beginners

Second: This is not a project for someone who is just starting out with computers, computer science, or programming. You certainly don’t need a Ph.D. to understand the information in this book, but you will need to have strong intermediate knowledge about programming concepts (variables, constants, functions, data structures) to be able to get this project off the ground.

If there are two topics I recommend brushing up on, they are the binary system and bitwise operations. There are two chapters in the appendix that describe them:

The binary system
Bitwise operations

If you are not already well versed in these topics then I strongly urge you to study them first, thoroughly. Practice until you understand them without having to think too much. Feel free to find more information about this on the internet or YouTube if the explanations in the above two chapters are not enough.

If you have a strong background in low-level programming languages such as C, C++, Rust, or even classic Pascal, this book and the subject of chess programming will be easier for you. If you are coming from the web development world, used to JavaScript, Python or PHP, it will (probably) be harder, but certainly not impossible. (You can write chess engines in JavaScript and Python, but those languages would not be my personal choice.) If you have just learned to write “Hello World”, this probably will not be the project for you… yet. Of course, you’re invited to come back later.

Even if you have a strong foundation in bitwise operations, there is one more topic that is worth studying, especially if you’re going to use Rust: the newtype pattern. This means creating “new types” by wrapping existing types so they cannot be accidentally mixed up. For example, both a Square and a Piece can be represented by a u8 integer, but if you do that, you can accidentally mix them up when calling a function. Therefore Rustic uses wrapper types that prevent this. I mention this because, while the newtype pattern is available in many languages, Rust’s implementation of it is quite distinct.

Newtype pattern in the Rust Book
Newtypes as implemented by Rustic

Mind your language

Third: If you are not experienced in several different programming languages of different styles and low-level programming, then it would be best to pick a language you know well. This will spare you the burden of learning chess programming and a new programming language at the same time. Only pick a language you don’t know because you want to use this project to learn it, like I did with Rust. Even so, you should have intermediate to early-advanced programming skills in at least one other language, so you do not need to learn all the basics from scratch. Be prepared for a steep learning curve and some rewrites if you choose a language you do not know well.

Be happy with your environment

Four: Make sure your development environment is set up correctly. This is a topic which is not discussed in this book, apart from the chapter where instructions are given on how to build Rustic for different operating systems. It is impossible to give precise advice here, because there are so many IDEs (Integrated Development Environments), compilers, debuggers, operating systems, and programming languages to choose from.

Make sure you are able to compile and run “Hello World” in your programming language of choice. Make sure your editor or IDE has a debugger configured correctly, so you can set breakpoints and halt the program at that point. This lets you inspect variables and debug the program. If you have not yet set up a working development environment, search the internet for how to do so. My personal preference is to use Visual Studio Code as an IDE, Rust as a programming language (obviously), and the VSCode plugin rust_analyzer, which provides code completion and linting for Rust in VSCode.

Git those versions under control

Five: This is not strictly necessary per se, but I strongly recommend using a version control system such as Git, even if you are working alone. Versioning your code makes the development process a lot easier, not to mention safer. Safer? Yes. You can create a branch to write experimental code, and if it does not work, you can simply throw it away instead of trying to undo everything you did.

Do not skip this. Many IDEs and editors have built-in support for Git and other version control systems, or they provide plugins or extensions. If you do not like built-in version control or your favorite editor does not support it, there are many GUI applications available, both open-source and commercial. If you are a contrarian, you can also choose a different version control system. It does not matter, as long as you use something.

Know something about chess

Six: This is also not strictly necessary, but it would be very helpful if you know all the chess rules already. (If not, you can download and read the FIDE Laws of Chess Handbook.) As with other programming projects, it is good to know the basics of the domain you’re working in. Think about the movement of the pieces, castling, en-passant, and the various draw rules. You could just find all the rules and implement them, but you would not be able to easily understand what the engine is doing, and finding bugs will be harder. It would even be better if you can already play decent chess. Even beginner level between 1000–1200 Elo should be sufficient. At that point you are probably not going to make many rules mistakes anymore, so there is less chance you will implement the rules incorrectly.

Still here? Good. Now we start. Good luck.