Chess Program Coded In 487 Bytes

bootchess

A new chess program takes up just 487 bytes. It appears to beat a record set on the ZX81 more than three decades ago.

BootChess can run on Windows, OS X or Linux systems. It’s a text only game, using a grid with letters to denote pieces and periods to denote empty squares. It appears to show one player’s pieces in capitals and the other’s in lower case.

Oliver Poudade told the BBC that he took on the project as a challenge to promote “sizecoding”, the idea of making code as efficient as possible.

In a detailed post about the project, Poudade explains that he was inspired by the game 1K ZX Chess, created by David Horne for the ZX81 computer in 1982. Despite the name (which actually referred to the one kilobyte of memory on the computer) that game only took up 672 bytes.

Horne once noted that the memory constraints were so great that even an ending sequence (such as a congratulatory message for the winner) would have taken it over the limit.

Both games involve a human player against the computer. While the computer decides the “best” available legal move, it does so on simple criteria and neither learns from its experiences nor adapts to the opponent’s style of play. With Horne’s game, the criteria for assessing each move were:

  • will it take an opponents piece;
  • will it move a piece out of harm’s way;
  • will it move a piece into harm’s way; and
  • will it lead to check on the opponent.

Poudade says that in principle programming a chess game is not difficult: the challenge was to do so with a small amount of code while still making it visually recognizable. He noted that pawns were the hardest to program as their allowable moves varies so much depending on the circumstances.

As with 1K ZX Chess, BootChess is missing two key components that mean it’s arguably not a “real” chess game:

  • castling, in which a player can switch the positions of the king and castle (also called rook); and
  • en passant, in which if an opponent moves a pawn two spaces on its initial advance, a player can capture the pawn as if it had only moved one space.

There are also a few rules missing, such as the option for a player to claim a draw if the game goes 50 moves without a piece being captured or a pawn moves, or if the exact same board position occurs three times.

BootChess does have one advantage over its predecessor: it allows a pawn that reaches the other side of the board to be replaced by a queen. That’s still a compromise however as real chess also allows replacement by a castle, bishop or knight.