Implementing a Chess engine from scratch

Implementing a Chess engine from scratch

Chess is an ancient two-player strategy board game. There are a huge number of possibilities since, after 5 moves each, there are 69,352,859,712,417 possible games. It is thus practically impossible to predict every single move.

As a humble Chess amateur, I gave myself this challenge: *to develop a simple, good-looking Chess game with AI that can beat me, *without Machine-Learning. This article is about my journey to achieve it and is composed of 4 parts: rules, computation, strategy, and playing.

And just to make things clear, I also decided not to read theoretical or algorithmic explanations on Chess engines, I wanted to build my own algorithm, based on my common sense and personal experience.

I named it Bobby, as a tribute to Robert “Bobby” James Fischer, who has been World Chess Champion and one of my heroes as a young player.

1. Rules


Chess is played by two opponents, one color for each, white and black pieces. Then for each side, there are the following pieces:

  • ♔ — A king
  • ♕ — A queen
  • ♖ — Two rooks
  • ♗— Two bishops
  • ♘ — Two knights
  • ♙ — Eight pawns

Initial positions

At the beginning of the game, the initial position is always the same, strictly defined. A range of pawns in front of the other pieces, the royal couple in the center, then symmetrically come the bishops, knights, and finally rooks.

The board is a squared grid of 8x8 cases (i.e. two-dimensional array), alternating dark and light backgrounds. Now is the time for a visual representation of that board.

Visual display

When I thought about drawing the pieces, I first started considering free graphic resources. But that would force me to manage pictures, which was not desirable for quick development. Fortunately, I discovered that each symbol is part of the Unicode: a basic text label is thus sufficient to draw a piece! Perfect, I was already able to print the board in the console logs to visualize it:

♜ ♞ ♝ ♛ ♚ ♝ ♞ ♜ 
♟ ♟ ♟ ♟ ♟ ♟ ♟ ♟ 

♙ ♙ ♙ ♙ ♙ ♙ ♙ ♙ 
♖ ♘ ♗ ♕ ♔ ♗ ♘ ♖

Funny, but not as good-looking as what I had in mind, so I decided to create a basic GUI: the board is a single frame, with a grid layout, driven by a MVC architecture.

I started with the definition of the dark and light backgrounds, where each square is a label containing either an empty value or the Unicode symbol of a piece.

If you are familiar with Chess you probably know that the board grid has a coordinate system made of letters (horizontal axis, or files) and digits (vertical axis, ranks) which is used for game notation, i.e. to script the moves. I finally added them on the sides of my board, and here is the result, a simple 10x10 grid:

Image for post

Basic GUI of my Chess game


Let’s continue. What are the rules? What actions are allowed? There are basically 4 types of moves, that may be combined according to the piece:

  1. Straight (used for rooks, queen and king)
  2. Diagonal (bishops, queen, king)
  3. «L» (knights)
  4. Pawns moves

Note that special moves en passant, pawn promotion, and castling are deliberately ignored for the sake of simplicity but have been implemented later. The exhaustive description of moves can be found on Wikipedia.

Some additional constraints to implement a move: a piece cannot jump over another piece, except for knights, and when a piece reaches an enemy piece, it may capture it and take its place on the board.

I also designed an optional colored border around squares to highlight moves:

  • Red — currently selected piece

  • Blue — possible destination of the selected piece

  • Green — last move played by the opponent

    Image for post

    Image for post

Colored borders around squares for a better view of moves


Now that we can move pieces, we must take into account the rules of the game: there are things that can be done, as well as things that must be done (typically, escaping check).

It is out of scope to explain every single rule here but I tried to implement most of the constraints, among others:

  • Players’ turns;
  • Detection of whether a king is in check, and when there is no way to escape from this, determining that the king is checkmate (game lost);
  • Both kings always being spaced a square apart;

Game ending

Now how does the game finish? There are two scenarios: either a player wins, or neither win (draw).

As already mentioned, you lose when your king is in check and that there is no possible move to escape being in check.

But you draw if:

  • There is no possible move but your king is not in check (stalemate); or
  • Both players repeat the 3 same moves in a row; or
  • Both kings are alone on the board (or with insufficient material to win)

I also implemented another rule to avoid never-ending games: if there has been neither pawn move, nor a capture within the last 50 moves, the game is a draw. Note that this rule is largely accepted by the Chess community.

With the above rules, two humans can play a game, but we want to play against the computer, so let’s get to the heart of the matter.

chess programming artificial-intelligence software-engineering games

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Software Developer vs Software Engineer — Differences: Bogus or Real?

In this article, see if there are any differences between software developers and software engineers. What you’re about to read mostly revolves around my personal thoughts, deductions, and offbeat imagination. If you have different sentiments, add them in the comment section, and let’s dispute! So, today’s topic…

AI Innovations in Artificial Intelligence

Innovations in Artificial Intelligence - Various sectors in which AI is remarkably used & has brought changes in humanity - Education, Healthcare,automobile

From Software Engineering To Artificial Intelligence — Making The Career Move

Technology has been an unstoppable force over the years, with particularly astounding growth and breadth of its presence across all…

Making the Transition from Software Engineer to Artificial Intelligence

Making the Transition from Software Engineer to Artificial Intelligence. Even if you're already a Software Engineer, making the transition to AI engineer isn't straightforward. It takes time and a lot of work to successfully transition and make an impact on the industry.

Average Salary of an Artificial Intelligence Engineer?

In this blog we will walk with A broad salary breakup for the role of an artificial intelligence engineer. Keep reading to know more…