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

Pieces

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

Moves

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

Rules

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

Implementing a Chess engine from scratch
9.35 GEEK