Skip to content

Getting started

Teodor Elstad edited this page Oct 17, 2017 · 8 revisions

If you haven't already, visit the cheslie-player readme and install cheslie-player. Then come back here.

Writing an AI that make random moves

Open ai.js in your favorite code editing tool (those of you using the JSFiddle are already viewing ai.js). Clear out the file so you're starting with a clean slate.

Please note that the following code snippets are written with a local installation using nodejs in mind. There will be some differences if you're using other players. For instance exports.name is ai.name in the JSFiddle player, but the overall code should be very similar.

Naming your AI

The first thing we want to do is to name your AI. It could be anything as long as it's a string. I'll choose 'rnd-jesus'.

exports.name = 'rnd-jesus';

Please don't try some fancy XXS-thing. It'll probably work :-P

Creating a function that makes moves

Now we want to move chess pieces. Most of the batteries are included with cheslie-player, but we have to supply a function that makes moves.

The move function takes one argument. Let's print it!

exports.name = 'rnd-jesus';

exports.move = function (board) {
    console.log(board);
};

Test it out by running the command npm start or adding runner.run(ai, 220); at the bottom of the JSFiddle and pressing run.

You should get an output similar to this:

...
rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1
...
TypeError: Cannot read property 'then' of undefined

We obviously get an type error. That's not surprising, since we're not yet making a move, but what's the strange sequence rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1?

This is the argument to the move function. It's a chessboard, complete with black and white pieces, represented in Forsyth-Edwards Notation or just FEN for short.

Using chess.js to represent the board

Working with raw FEN-strings is hard, so let's use chess.js to get a more readable representation.

var Chess = require('./modules/chess-extended.js').Chess;

exports.name = 'rnd-jesus';

exports.move = function (board) {
	var chess = new Chess(board);

	console.log(chess.ascii());
};

Run the code again. We still get the type error since we're not making a move, but instead of a cryptic FEN-string you should get something like this:

   +------------------------+
 8 | r  n  b  q  k  b  n  r |
 7 | p  p  p  p  p  p  p  p |
 6 | .  .  .  .  .  .  .  . |
 5 | .  .  .  .  .  .  .  . |
 4 | .  .  .  .  .  .  .  . |
 3 | .  .  .  .  .  .  .  . |
 2 | P  P  P  P  P  P  P  P |
 1 | R  N  B  Q  K  B  N  R |
   +------------------------+
     a  b  c  d  e  f  g  h

Finding all legal moves

Chess.js can do other tricks as well. Let's get a list of all valid moves for white.

var Chess = require('./modules/chess-extended.js').Chess;

exports.name = 'rnd-jesus';

exports.move = function (board) {
	var chess = new Chess(board),
		moves = chess.moves();

	console.log(moves);
};

Now you should get something like this when you run it:

[ 'a3', 'a4', 'b3', 'b4', 'c3', 'c4', 'd3', 'd4', 'e3', 'e4', 'f3', 'f4', 'g3', 'g4', 'h3', 'h4', 'Na3', 'Nc3', 'Nf3', 'Nh3' ]

This is a list of all the moves white can make in Standard Algebraic Notation (SAN).

At first glance it can look like some information is missing. What move is 'a3'? Where is the move from and to, and what piece is moved? The reason is that when using SAN, only the coordinate of the square the piece is moved to is recorded, if it can be inferred what piece can move there. In our example the pawn on a2 is the only piece that can move to a3. In addition a single letter indicates if any other piece than a pawn makes the move. In our example Nf3 is a move the kNight (not to be confused with King) can make. In addition there is a lot of special notation and rules that makes this a consistent system for chess notation, but we'll skip the details. Just let chess.js handle it and you'll be fine

Picking a random move

In order to finish our random move making AI, we just have to pick a random move and return it:

var Chess = require('./modules/chess-extended.js').Chess;

exports.name = 'rnd-jesus';

exports.move = function (board) {
	var chess = new Chess(board),
		moves = chess.moves(),
		move = moves[Math.floor(Math.random() * moves.length)];

	return move;
};

Run the code with npm start, and you should se your new AI play a game against another AI making random moves.

Also try out the command npm test. This will run some very simple tests checking that your AI makes legal chess moves. It's not as thorough as playing a full game against another AI, but it's a lot quicker for simple testing.

If you're running locally, you can try out the command npm run dev-browser to get a visualization of your AI playing a game in the browser.

Where to go from here

You can head over to How to use the sample players and start enhancing your AI with functionality from the sample players.

Alternatively you could check out the page on The Chessboard API to learn more about Chess.js, the special extensions to Chess.js in cheslie-player and get some ideas to other interesting information you can harvest to make your AI the best.