Let’s explore some basic concepts that will help us create a simple chess AI:

- move-generation
- board evaluation
- minimax
- and alpha beta pruning.

At each step, we’ll improve our algorithm with one of these time-tested chess-programming techniques. I’ll demonstrate how each affects the algorithm’s playing style.

You can view the final AI algorithm here on GitHub.

I’m having trouble beating a chess program I wrote. Not sure if I’m a bad player or the algorithm is decent.

## Step 1: Move generation and board visualization

We’ll use the chess.js library for move generation, and chessboard.js for visualizing the board. The move generation library basically implements all the rules of chess. Based on this, we can calculate all legal moves for a given board state.

Using these libraries will help us focus only on the most interesting task: creating the algorithm that finds the best move.

We’ll start by creating a function that just returns a random move from all of the possible moves:

var calculateBestMove =function(game) {

//generate all the moves for a given position

var newGameMoves = game.ugly_moves();

return newGameMoves[Math.floor(Math.random() * newGameMoves.length)];

};

Although this algorithm isn’t a very solid chess player, it’s a good starting point, as we can actually play against it:

## Step 2 : Position evaluation

Now let’s try to understand which side is stronger in a certain position. The simplest way to achieve this is to count the relative strength of the pieces on the board using the following table:

With the evaluation function, we’re able to create an algorithm that chooses the move that gives the highest evaluation:

var calculateBestMove = function (game) {

var newGameMoves = game.ugly_moves();

var bestMove = null;

//use any negative large number

var bestValue = -9999;

for (var i = 0; i < newGameMoves.length; i++) { var newGameMove = newGameMoves[i]; game.ugly_move(newGameMove); //take the negative as AI plays as black var boardValue = -evaluateBoard(game.board()) game.undo(); if (boardValue > bestValue) {

bestValue = boardValue;

bestMove = newGameMove

}

}

return bestMove;

};

The only tangible improvement is that our algorithm will now capture a piece if it can.

## Step 3: Search tree using Minimax

Next we’re going to create a search tree from which the algorithm can chose the best move. This is done by using the Minimax algorithm.

In this algorithm, the recursive tree of all possible moves is explored to a given depth, and the position is evaluated at the ending “leaves” of the tree.

After that, we return either the smallest or the largest value of the child to the parent node, depending on whether it’s a white or black to move. (That is, we try to either minimize or maximize the outcome at each level.)

A visualization of the minimax algorithm in an artificial position. The best move for white is b2-c3, because we can guarantee that we can get to a position where the evaluation is -50

With minimax in place, our algorithm is starting to understand some basic tactics of chess:

The effectiveness of the minimax algorithm is heavily based on the search depth we can achieve. This is something we’ll improve in the following step.

## Step 4: Alpha-beta pruning

Alpha-beta pruning is an optimization method to the minimax algorithm that allows us to disregard some branches in the search tree. This helps us evaluate the minimax search tree much deeper, while using the same resources.

The alpha-beta pruning is based on the situation where we can stop evaluating a part of the search tree if we find a move that leads to a worse situation than a previously discovered move.

The alpha-beta pruning does not influence the outcome of the minimax algorithm — it only makes it faster.

The alpha-beta algorithm also is more efficient if we happen to visit first those paths that lead to good moves.