I have always been fascinated by computer programs that play chess! During my undergrad studies I decided to implement a chess engine of my own; mainly to learn so-called ‘AI’ algorithms. The result; an engine called Horus(I am a fan when it comes of Egyptian gods :p) can be seen here. Overall, it was a needlessly painful experience(but hey – I did get to learn a lot).

So here’s the idea: I plan to write a series of tutorials on developing a chess engine; i.e. an AI program that can play chess against a human. Along the way, we’ll review concepts of what exactly constitutes making the engine ‘intelligent’ and look into algorithms which can be potentially used to create something known as a General Game Player, a program which can play any game of perfect information(we’ll come to what that is later).

So how exactly would one go about building a chess engine? Well, the ideal way would be to simply implement a program that allows two users to play chess(while checking for all the rules as well), and then hand over the control of one of these users to the AI(yeah – I am making it sound dumb).

Let’s get into the details in an object oriented way. To build this; the following stuff is necessary for us

  • A chessboard
  • Chess pieces
  • Keeping track of which side is playing

So I am gonna begin with exactly that, a chessboard class. What does this consist of? How do we represent the board? Let’s just start with an 8×8 integer array. Another thing is we will be building this complete thing in single file(for the sake of simplicity). We’ll also be maintaining other basic variables(such as which side has to play) and auxiliary functions to initialize and print the board; all pretty self-explanatory.

#include <iostream>
#include <string>

using namespace std;

#define WHITE 0
#define BLACK 1

enum {
    BL = 0,
    WP, WN, WB, WR, WQ, WK,
    BP, BN, BB, BR, BQ, BK
};


int init_pos[8][8] = { 
    {BR, BN, BB, BQ, BK, BB, BN, BR},
    {BP, BP, BP, BP, BP, BP, BP, BP},
    {BL, BL, BL, BL, BL, BL, BL, BL},
    {BL, BL, BL, BL, BL, BL, BL, BL},
    {BL, BL, BL, BL, BL, BL, BL, BL},
    {BL, BL, BL, BL, BL, BL, BL, BL},
    {WP, WP, WP, WP, WP, WP, WP, WP},
    {WR, WN, WB, WQ, WK, WB, WN, WR}
};

std::string enum_to_piece[13] = {"--", 
                "WP", "WN", "WB", "WR", "WQ", "WK",
                "BP", "BN", "BB", "BR", "BQ", "BK"};

class chessboard {
private:
    int board[8][8];
    int side_to_play;
public:
    chessboard() {
        init();
    }
    
    void init() {
        for(int i = 0; i < 8; i++) {
            for(int j = 0; j < 8; j++) {
                board[i][j] = init_pos[i][j];
            }
        }
        side_to_play = WHITE;
    }
    
    void print() {
        for(int i = 0; i < 8; i++) {
            for(int j = 0; j < 8; j++) {
                std::cout << enum_to_piece[board[i][j]] << " ";
            }
            std::cout << std::endl;
        }
        
        std::cout << std::endl;
        std::cout << "Side to play: "
                  << (side_to_play == WHITE ? "WHITE" : "BLACK")
                  << "\n\n";
    }
};

int main() {
    chessboard board;
    
    board.init();
    board.print();
}

This code is also available here(the github repo for this tut).

The output is as follows:

BR BN BB BQ BK BB BN BR 
BP BP BP BP BP BP BP BP 
-- -- -- -- -- -- -- -- 
-- -- -- -- -- -- -- -- 
-- -- -- -- -- -- -- -- 
-- -- -- -- -- -- -- -- 
WP WP WP WP WP WP WP WP 
WR WN WB WQ WK WB WN WR 

Side to play: WHITE

Phew, that’s it for today. In the next article we’ll see how to generate and validate moves. See you soon!

About the Author Pranav Deshpande

Software Developer interested in AI, Reinforcement Learning and game programming.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s