Friday, 11 March 2011

Chess in 1K

Since it's the 30th anniversary of the ZX81 computer, I thought I'd write about what I think is one of the most amazing pieces of programming ever: 1K ZX Chess.

In 1983, David Horne wrote a program that would play a game of chess that ran on the Sinclair ZX81 computer using just 1K of memory. As one of the smallest versions of the game to have a visual display of the board, it stands as a remarkable achievement. If sculpture can be described as starting with a block and whittling away anything that doesn't "feel" like the thing you're trying to create, then David has done something similar to the ZX81's tiny memory. It's hard to believe that there's a single byte left in the program that doesn't have some element of “chessness” to it.

Of course, to fit the greatest game ever into such a small space necessitated certain limitations. The program always plays as white and only makes one of two opening moves, depending which side of the cassette was used to load the game... king's pawn to king's pawn three (van't Kruijs' Opening), or queen's pawn to queen's pawn three (Miese's Opening).

The artificial intelligence of 1K ZX Chess could only look one move in advance, and the program had no concept of the newer rules of the game (ie, after the sixteenth century) such as castling, en passant, and promotion of pawns. Nevertheless, within these 672 bytes of code lie the barest bones of chess, as if this is the absolute zero of chess and below this it cannot exist in any recognisable form.

Playing the game isn't that satisfying, as it has a fondness for moving the rook back and forth between a1 and a2. If you want to break this cycle then you have to attack the square a2, and only then does it turn its attention to other things. So it's possible for it to play a good game, but only if the human opponent acts as a kind of sheepdog, guiding it towards the right move. Or at least away from a really wrong one.

If it takes one monkey seventy thousand years to type “TOBEORNOTTOBE”, then how long before 1K ZX Chess accidentally plays like a grandmaster? Having no memory of the previous game to learn from, it would be a fleeting success, never to be seen again but is there at least the potential for this game to mimic the style of every master who ever studied the game? I hope so. In one game, it suddenly pushed its queen into the fray in what seemed like a very bold and romantic move. It didn't last, though, but for a moment I felt like Prof Higgins in My Fair Lady, and I wanted to call out “By George, I think it's got it!”

(Oh, and here's a blog post in which 1K ZX Chess goes up against a newcomer to the 1K chess world, Tiny Chess.)


Anonymous said...

Yes I played this game when it came out. It was so exciting to play against a computer on our tv screen that the limitations of the game didn't matter. The screen display disappeared while every byte was used by the computer to work out its next move, and you had to wait anxiously for the board to reappear for it to be revealed.. Of course, occasionally the computer crashed and, like playing against an aged uncle, you were never sure if it was in deep thought or had fallen asleep!

John said...

I was a young teenager when I played this. I don't remember whether I actually bought the game. I suspect I did. It did actually play real chess but at a complete beginner level. Once I improved I tried to force it to win but it never did, sadly. Still, 30 years later I find myself searching the net for this game and stumble across this excellent page. It really is one of the great achievements of programming, both then and now. I still own my ZX81 and this game on tape. John.

Ersby said...

Thanks for the kind comment. And the fact that this is one of the most read posts on this blog indicates that there are plenty of people who remember it fondly. Not the greatest player of chess, but certainly the most remarkable.