Originally posted (a few minutes ago) at extreme_nerdery.
GAME: 1K Chess
"I was in love once. A Sinclair ZX81. People said 'No, Holly, she's not for you. She's cheap, she's stupid, and she wouldn't load.' Not for me, anyway."
- Holly, Red Dwarf series II: Stasis Leak
That last post from aiden_macleod on extreme_nerdery in which I attempted to derail the thread with further dribblings about my favourite ancient microcomputers has convinced me to write this latest article in the Codex Electro series.
Conoisseurs of dorkitude, I present to you, the greatest achievement in the history of computer programming.
Let us put it into context. In 1982, the ZX Spectrum arrived. It had colour, sound, and 16K of memory as standard, and a 48K model was available if you really wanted to push the boat out. All this made its predecessor, the ZX81, look as stone-age as the ZX81 had done to the ZX80 before it. The ZX81 was silent, operated in black and white only, had horrible chunky graphics (in as much as there were graphics), and its standard memory was only a kilobyte. Yes, everyone who ever owned a ZX81 had the external 16K RAM pack, but this came with its own hazards; RAM pack wobble was the most common, which would cause the computer to crash as it suddenly lost all its memory, but the worst scenario was completely disconnecting the pack without turning the computer off. This would usually murder both the pack and the computer in one shot, and I found that out all-too-painfully when my then two-year-old brother ripped the 16K pack off my ZX81 without a second thought. The computer survived, the pack didn't, and by that stage the Spectrum's dominance was such that a replacement 16K pack was damn near impossible to get hold of, and that's before Sinclair Research's notorious unreliablility was factored in.
If only I could have had 1K Chess to entertain me. I had no experience of chess at the time, but my grandad did and he taught me when I was a bit older. Only problem was, all the chess programs for the Spectrum were so unfailingly brilliant that they'd beat me every single game. Not so, this one. Because, and you may have guessed it by the title, what we have here is a working game of chess crammed into a single kilobyte. Let me run that past you again: one kilobyte. It should never have been possible. Chess is such a complicated game. It's not like draughts where every piece moves diagonally forwards on the black squares, thus simplifying the code considerably; no, there are six different pieces with eight different movement patterns (you'll see how that's worked out later), and every turn the computer has to check for the legality of the move. That on its own should far exceed the memory of the primitive, unexpanded ZX81. But these were the days when programmers were superheroes. These old micros had so little memory available that programmers devised ingenious tricks to extract every last scrap of computing power from what was available. As an example, this is what BASIC programmers used to do on the ZX81: LET n=3 would always be substituted by LET n=INT PI - the two functions which returned the equivalent of 3 took less bytes of memory than the digit itself, which was a floating-point variable stored as 3.0000000E+00. NOT PI and SGN PI were the equivalents of 0 and 1 respectively, and the use of character codes (LET n=CODE "$" for 13, for instance) substituted for other numbers. But I digress, because fairly obviously this was written in machine code. Even then, such memory-pinching techniques were heavily employed.
And, of course, there had to be compromises. For a start, the computer always plays white, and had only two opening moves; king's or queen's pawn one square forwards. (Incidentally, the two opening moves were on opposite sides of the tape.) Then, with the human player playing black, the usual first move might be king's pawn two squares forwards; in the usual A-H/1-8 notation, this is E7-E5. Only this has to be entered backwards, i.e. the key sequence is 7E5E. Weird... it appears on the screen "reversed" as E5-E7, and the pawn moves. It's time for the computer to move again... and, stupid though the AI may have been - it is really very easy to beat the computer! - you can actually see it thinking, as it plays out all its possible moves right in front of you... and usually makes the wrong move. To save further memory, castling, pawn promotion and en passant were also eliminated, but very few games would ever have called on the last two moves anyway. And, once you've beaten the computer, as you inevitably will, even if you're as crap at chess as I am, it hangs and needs to be reloaded. But, as it only took 40 seconds to load, which was the blink of an eye in those days (a standard 48K Spectrum game took four and a half minutes), who was going to complain about that?
So, there you have it. The limitations of the ZX81 make this game crude, stupid and ugly (as you'll see from the screenshot below), but, let me say it again, it plays a game of chess with one kilobyte of memory. Actually, that's something of a lie. The actual amount of memory available after the admittedly minimal system overheads were in place was only 672 bytes. For comparison, the PNG screenshot below is 700 bytes, and there are only two colours in the pallette. And I saved the text of this post as 1kchess.txt and found it took up 7,294 bytes (at least the original extreme_nerdery post did) - far more than the ZX81 could handle, and about half the memory of the first of the Spectrums.
It seems I'm not the only one to consider this program a work of staggering genius - this article was linked from, of all pages, Wikipedia's 1K Chess article. The programmer, David Horne, went on to publish the source code in a later issue of Your Computer, explaining to those who knew ZX81 machine code how the program worked and all the tricks he'd used to fit it into the minuscule amount of memory available.
And finally, the obligatory screenshot. Not only that, but if you click it, it'll redirect you to an online ZX81 emulator, so you can actually play the game for real!
Checkmate! I win!
Incidentally, here's the series of moves that led (very quickly) to this victory...