Random Chess Moves in Javascript

by @billautomata


intro

In popular media skill at chess is often used as a device to portray the intelligence of a character in a story. As a youngster I considered myself pretty sharp. I got good grades, and my teachers gave me a lot of positive feedback. But whenever I played anyone at chess, I was basically always beaten.

Beaten is an understatement. I was and am destroyed by my chess playing peers. I deal with it a lot better today. I understand that there is a difference between smart and being educated. Also, that there is a difference between intelligence and experience.

My friends that defeat me at chess are educated in the game. They study past games and read books on strategy. "The middle of the board", "end game strategies", and "pawn structure" are things that are not a part of my mental chess move algorithms. After once being asked, "Are you just moving the pieces randomly?" I began to.

Chess gets way different when you make the first move you see. Sometimes the game ends very quickly, and sometimes the game can drag out into a draw. I wondered what would happen if two chess bots played against each other each making valid but random moves.

  • If all the moves are random, how many games would actually end in a checkmate?
  • If all the moves are random, how long is the average game?
  • If all the moves are random, how quickly can a game end?

The answers to those questions turned out to be pretty simple. There is a fantastic chess library for javascript called chess.js. It provides simulating a game of random moves as the first example of the API.

Leveraging this library, the node.js cluster module, and a big AWS instance I was able to simulate and record the moves of ~100,000 random games in about 3 hours. This would have taken me weeks on a single core on my laptop.

initial findings

~15% of random games end in a checkmate.

How does this compare to a sample of real chess games?

Chess matches are well documented events. At pgnmentor.com you can download hundreds of thousands of matches organized by player, year, opening moves, and match name. I downloaded the files for the players, then parsed and imported them into mongodb.

~55% of real games end in a checkmate.

digging deeper

how long is the random game? really fucking long

Moving the pieces randomly can't be efficient. I bet the random games drag on forever...

They do drag on forever. The above is a histogram of the occurences of the number of moves per game. The scale on the y-axis doesn't matter, just the overall shape of the graph and how far it trails to the right.

  • The average number of moves in a random game is 342.
  • The longest random game was over in 804 moves.
  • The average number of moves in a masters game is 80.
  • The longest masters game was over in 400 moves.

There is a noticeable spike in the histogram for the masters at 81 moves. If you know why, send me a message on twitter.

check

During the game the King can be in check. The above histogram displays the probability that the King is in check at some point in the duration of the game regardless of moves in the game. I normalized the moves for each game from 0% to 100% when calculating this histogram.

Unlike the 'moves histogram' above, the scales on the y-axis are the same, but still the shape is really all that matters.

  • During a random game, there is a 39% chance the King is in check.
  • During a random game, there is a 56% chance the King is in check when the game is 99% complete.
  • During a masters game, there is a 3.7% chance the King is in check.
  • During a masters game, there is a 13% chance the King is in check when the game is 99% complete.

An interesting note, at 100% through the game there is a dip in the check probability. This is because many games end in a draw where someone is not in check. Also it is because many of the games played by the masters are not played to completion but still counted as wins. One player forsees the inevitable and resigns, but the board remains in a peculiar state where the board is left in an unpredictable state. <1% of chess masters games actually end with someone in checkmate.

heatmaps

Position is everything, color is difficult. -Santiago Ortiz

Through the game it is likely that a piece will be at a given position. I quantized these probabilites into heatmaps corresponding to a point in time during the game from 0% to 100%. Below are corresponding heatmaps for the all chess pieces split between the random games (right) and the games of the chess masters (left).

vector fields

When a piece moves it has a velocity. Queens, bishops, and rooks potentially have the highest velocity. The Pawn and the King have the smallest velocity being able to only move one space (unless performing a Castle). I record these velocity values and store them on the grid where they occur. Over time I build up a vector field that describes the average behavior of the piece as it moves across the board.

What is a vector field without some particles? If you click the buttons below, you can see the vector fields for each piece type and start up the particle simulation. The differences in the shapes between the masters games (in gold) and the random games (blue) is quite striking for the Knight and the King. But on average most pieces trend toward the center of the board as expected. The vector fields for the Pawns has missing data because pawns don't ever actually exist at the top and bottom of the board, as they are promoted to a Queen piece.