
David Horne's 1K Chess on the ZX81 (2001) - jermo
http://thad.frogley.info/archive/the_greatest_program.html
======
tzs
Impressive, as is the recent smaller program from a French programmer.

Note, however, that both have sacrificed some rules. I believe they have both
left out en passant, which is rare enough that it won't be missed most of the
time. It's rare enough that if you use it against a casual player, there is a
good chance they will accuse you of making it up.

Castling, however, which is also omitted, is much more serious. Getting
castled is one of the top goals of the opening. It happens in nearly every
game. Taking that out is huge.

Pawn promotion is missing from the older program but included in the smaller
French program (making the French achievement even more impressive). I'd say
that is not as big an omission as castling, because many games end before it
gets to that point. Still, the threat of promotion has a major influence on
endgame play.

So what we actually have are very impressive implementations of chess-like
games. That raises the question of what is the smallest implementation of full
chess?

I suspect that both these programs could be expanded to the full rules without
making them a lot bigger, by taking the short cut of allowing the human to
make those moves, but not the computer (especially if the computer trusts the
human and does not check for legality of the human's move). I think you could
defend a claim that you then have full chess, and it is just a limitation of
the AI that it does not consider those moves.

~~~
sago
There's a rather ahistoric perspective at work here. I wonder if there's a
generational thing.

ZX81 chess was so impressive because it was impossible (at least, I'd have bet
real money on it). It was the thing programmers namechecked as the kind of
thing you couldn't do on these newfangled cheap home computers. You need a
'real' computer to do it. There's just no way. I was writing programs for the
ZX81, and the kind of simplicity they had to have is difficult to communicate
to someone who grew up with 8086 PCs on their desks. Get an emulator and have
a go.

So, you're right, it wasn't 100% total, complete, full chess. But, good god,
it was chess, recognizably chess, playably chess! And the guy did it on a
freakin' ZX81. It is difficult to explain how astounding that was.

Pointing out it's lack of castling and en-passant (and stalemate, repetition,
etc) seems a bit churlish then.

That's why the challenge for the smallest chess program uses the ZX81 rules,
not the full rules. It is why smaller programs make it to HN, whereas you
don't know about the smallest program to play FIDE chess. It is a historical
honoring of that great achievement.

~~~
david-given
I _strongly_ recommend playing with this sort of machine. It's an utter
education. It will, literally, change the way you think about programming.
Also, the ZX81 had the best cover art for any programming manual EVER:

[http://uknet.com/gallery2/d/16341-2/076_Sinclair_ZX81_Manual...](http://uknet.com/gallery2/d/16341-2/076_Sinclair_ZX81_Manual.jpg)

(Paintings by John Harris. He sells prints!
[http://www.alisoneldred.com/thumbsJohnHarris-
Prints-3-1.html](http://www.alisoneldred.com/thumbsJohnHarris-
Prints-3-1.html))

However, you _will_ be frustrated with the ZX81; the built in BASIC was
utterly appalling and also didn't have a tokeniser --- instead each key (plus
modifiers) produced a different keyword. And it was stateful, so the keys did
different things depending on what you were typing. This made typing on it
horribly frustrating even when you had the keywords written on the keyboard,
which you won't have with an emulator.

It may be of interest to check out the Jupiter Ace --- it's basically the same
machine, made by some ex-Sinclair staff, except running Forth instead of
Basic; you can at least type on that, and using Forth means you're much closer
to the machine. (Plus, Sinclair's Basic was seriously horrifying. Any
alternative is better.) There's a bunch of emulators. But it's a much more
esoteric machine, so there'll be very limited resources around.

There are Javascript emulators for all the above, but they're pretty naff.

~~~
Almaviva
> Also, the ZX81 had the best cover art for any programming manual EVER:

Nope.

[http://globalnerdy.com/wordpress/wp-
content/uploads/2007/09/...](http://globalnerdy.com/wordpress/wp-
content/uploads/2007/09/forth_on_the_atari.jpg)

~~~
david-given
That's... um... wow.

I think I shall resort to a strictly pedantic definition of the word 'best'
here.

------
VonGuard
Just for the record, the Atari 2600 also had a chess game with AI. It also had
only 1024 bits of RAM, but it had access to all of it because there was no OS
or underlying hardware support, like in the Speccy. So while this is
definitely amazing, the 2600 version is very close in the "insanely well
coded" category, especially because the 2600 had some SERIOUSLY quirky
hardware to deal with. For example, on the 2600, you could only draw half the
screen's background: the other side had to be a mirror image. Additionally,
the columns of pixels that you drew on the screen were enumerated, to the
halfway point on the screen as follows:

12345678,87654321,1234

So you were drawing forward, backward, then forward again, and that only got
you half the screen!

~~~
T-hawk
Not exactly; this overstates the complexity of the chessboard display.

The order isn't exactly what you wrote, it's bits 4567 / 76543210 / 01234567
for the three playfield registers for the left half of the screen. This can be
either repeated or mirrored for the right half. A common trick as 2600 Video
Chess did was to simply not use the leftmost (mirrored to rightmost) register,
which still gets you 16x2 bits of mirrored playfield in the middle, trading
computational cost for a slightly smaller play area.

Now an 8x8 checkered board is not symmetrical for mirroring. 2600 Video Chess
actually draws only 7 of the files with the mirrored playfield registers, with
the center one of the 7 (the 'e' file) straddling the mirror line. The
additional (leftmost) file is actually a sprite object (the "ball"). Pretty
clever.

The real trick in 2600 Video Chess is drawing 8 sprites per row on a system
whose hardware only has 6. It does this with the "venetian blinds" trick,
where each sprite is striped onto alternating scanlines. That way there's only
ever 4 actual sprites at a time, which are moved back and forth by a
square's-width on alternating lines.

~~~
sago
This reply is exactly what I love about HN. I'd like to buy you a drink and
talk of vintage hardware :)

------
samatman
Thanks to the Archive, the code and commentary is available:

[https://archive.org/stream/your-computer-
magazine-1982-12/Yo...](https://archive.org/stream/your-computer-
magazine-1982-12/YourComputer_1982_12#page/n67/mode/2up)
[https://archive.org/stream/your-computer-
magazine-1983-01/Yo...](https://archive.org/stream/your-computer-
magazine-1983-01/YourComputer_1983_01#page/n79/mode/2up)

~~~
Doctor_Fegg
Oh, wow: there are issues of Your Computer on archive.org. Thanks. I loved
that magazine.

~~~
didgeoridoo
There was a great interview on The Web Ahead recently with Jason Scott about
Archive.org and the challenge of preserving these amazing bits of history:
[http://5by5.tv/webahead/97](http://5by5.tv/webahead/97)

------
StavrosK
Why does the author say "memory" when he means "disk space"? Or does he
actually mean "memory", as in "RAM"? In that case, why would the size of the
executable matter?

~~~
ctdonath
There was no "disk" on a ZX81. Storage was audio cassette tape.

The size of the executable mattered because it all had to fit in 1024 bytes
RAM (no "K", no "M", certainly no "G") _with_ all video & operating system
memory.

~~~
StavrosK
Oh wow, I didn't know that, thanks.

By the way, to whomever downvoted a legitimate question: Nice.

~~~
ColinWright
I didn't downvote you, but you asked a question the answer to which is easily
available with the most trivial of web searches. You assumed he meant "disk
space", assuming the author made a mistake when you could easily have checked
and not asked the question here.

In some sense, it's not a legitimate question _for this context._

~~~
angersock
The question asked exactly is the sort of thing that "the most trivial of web
searches" wouldn't have answered as well as either of the replies already up.

One reply puts it into historical context, and educates the reader about the
memory/persistence model of the ZX81.

The other reply points out that the lack of virtual memory and whatnot means
that, in previous times, you needed to have the entire thing loaded into
memory to execute something.

Without some digging _and_ a knowledge of what to look for, those are non-
obvious but useful points.

~~~
TheOtherHobbes
Well - you could just search for "ZX81".

First thing that appears is a Wikipedia article which explains how the memory
and off-line storage worked.

So I don't think finding this out would have been rocket science.

Having said that, and having written my first code on a ZX81 (if you don't
count an even older TI programmable calculator with even less RAM) I'm old
enough to be amused by the question. :)

However you slice it, chess in 1K was _awesome_ coding.

------
j2kun
I'm not sure why the size of a program has any serious bearing on it's
greatness. Sure it's an incredible feat of engineering, but shouldn't the
"greatest program ever written" be primarily judged by what it allows us to
do, not need to do, to build, or what insights it provides?

For example, certainly the first widely used assembler was a "greater" program
than any chess game, not only because it stopped us from having to track
memory locations by hand, but because it allowed us to dedicate our brain
cycles to actual problem solving and writing more useful programs.

~~~
sago
The 4 minute mile was amazing because of the four minutes, not because someone
managed to run a mile. Amazing feats in the face of incredible constraints are
inherently worthy, I'd say.

~~~
SonicSoul
A better analogy would be 'greatest movie ever made' was so because it was
done on a budget of $1000.

Better analogy in context of this thread that is

~~~
sago
Why? Seems like a much poorer analogy to me, since it is trivial to make a
movie for much less than that. It falls into the same trap as the commenter
above, the 'greatness' is due to the quality of the program, rather than just
making it exist at all.

~~~
albedoa
The greatness of a run mile is measured by how fast it is run.

The greatness of a movie is not measured by its budget, nor is the greatness
of a program measured by its number of lines or memory footprint.

Do you see now why your analogy is not as good?

~~~
sago
Nope sorry. Do you see why your explanation is poor?

------
jonjacky
Also pertinent: Peter Jennings' Microchess for the 1K Kim-1. His page says it
"was the first game program sold for home computers", shipped in 1976.

[http://www.benlo.com/microchess/](http://www.benlo.com/microchess/)

~~~
phkahler
Was going to bring this up. My father keyed that into his Kim-1 by hand and
played it. Definitely predates the one here by several years and also fit into
1K.

On a side note, I wrote one that ran on a PIC16 and fit into 4K of flash,
while the part had only 176 bytes of RAM. It played over the serial port in a
terminal. The 4K is not impressive, but I wrote it in C with a compiler that
didn't support recursion because the stack is for return addresses only and is
only 8 levels deep. IIRC I could do a 5 level minimax search on it.

------
desireco42
Impressive, Elite game also comes to mind. For you who don't remember, it was
a really immersive game with huge universe that can fit on a floppy drive.

I remember when my friend show me proudly cd he just purchased with it, and
didn't believe game is small enough to not needing it. His face was precious
when he opened cd and saw how big the game was.

------
GregBuchholz
Olivier Poudade has recently written and even smaller version of the chess
program, clocking in at 468 bytes:

[http://olivier.poudade.free.fr/](http://olivier.poudade.free.fr/)

[http://www.bbc.com/news/technology-31028787](http://www.bbc.com/news/technology-31028787)

~~~
ctdonath
Prior HN discussion:
[https://news.ycombinator.com/item?id=8954630](https://news.ycombinator.com/item?id=8954630)

------
illsorted
Original source:
[http://www.kuro5hin.org/story/2001/8/10/12620/2164](http://www.kuro5hin.org/story/2001/8/10/12620/2164)

~~~
SwellJoe
Whoah, I'd almost completely forgotten Kuro5hin, and a lot of other people
obviously have, too, as the site is a ghost town. It was a really neat pre-
reddit user-generated social media site; kind of a more democratic Slashdot. I
knew the founder in some capacity (I don't even remember how/why, now, or
their name...maybe because I worked on Squid or because I worked in Perl), and
used to advertise my old company on Kuro5hin to be able to send some money
their way.

I quite liked it, and there was a brief time when I divided my time between
Slashdot and Kuro5hin, as I currently divide my time between reddit and HN.

~~~
bokchoi
My favorite Kuro5hin article is the intro to the K language:

[https://www.kuro5hin.org/story/2002/11/14/22741/791](https://www.kuro5hin.org/story/2002/11/14/22741/791)

~~~
Rodeoclash
Mine was the Casino story:
[http://www.kuro5hin.org/story/2001/7/19/181127/355](http://www.kuro5hin.org/story/2001/7/19/181127/355)

Almost 15 years ago now yet I still remember it. That was Kuro5hin at its
peak.

------
Supraperplex
By the standards of the linked article I would say the greatest program is the
original Elite game.

A whole universe complete with star systems and 3D models for spaceships plus
some other content like missions... I think the name says it: elite.

All in 52 kb on disk.

~~~
david-given
Having disk available is cheating --- you get to load chunks of code off disk
while the game runs!

 _Cassette_ Elite had to load the entire game into RAM. It had some omissions
--- fewer ships, no (or fewer?) missions, no docking computer, etc. But it was
completely self contained.

Source, BTW, is available from Ian Bell's website:
[http://www.iancgbell.clara.net/elite/bbc/index.htm](http://www.iancgbell.clara.net/elite/bbc/index.htm)

Here's an excerpt. It's machine code using BBC Basic's built in assembler as a
cheapo macro assembler:

    
    
      7000.SHPPT JSREE51:JSRPROJ:ORAK3+1:BNEnono:LDAK4:CMP#Y*2-2:BCSnono:LDY#2:jsrShpt:ldy#6:ldaK4:ADC#1:jsrShpt:LDA#8:ORAXX1+31:STAXX1+31:LDA#8:JMPLL81+2:PLA:PLA:.nono lda#&F7:andXX1+31:staXX1+31:RTS
      7010.Shpt STA(XX19),Y:iny:iny:STA(XX19),Y:LDAK3:DEY:STA(XX19),Y:ADC#3:BCSnono-2:dey:dey:STA(XX19),Y:rts
      8040.LL5 \2BSQRT Q=SQR(RQ)
      8045LDYR:LDAQ:STAS:LDX#0:STXQ:LDA#8:STAT:.LL6 CPXQ:BCCLL7:BNELL8:CPY#&40:BCCLL7:.LL8 TYA:SBC#&40:TAY:TXA:SBCQ:TAX:.LL7 ROLQ:ASLS:TYA:ROLA:TAY:TXA:ROLA:TAX:ASLS:TYA:ROLA:TAY:TXA:ROLA:TAX:DECT:BNELL6:RTS
      8065.LL28 \BFRDIV R=A*256/Q
      8070CMPQ:BCSLL2:LDX#254:STXR:.LL31 ASLA:BCSLL29:CMPQ:BCCP%+4:SBCQ:ROLR:BCSLL31:RTS:.LL29 SBCQ:SEC:ROLR:BCSLL31:RTS:.LL2 LDA#FF:STAR:RTS
      8085.LL38 \BADD(S)A=R+Q(SA)
      8090EORS:BMILL39:LDAQ:CLC:ADCR:RTS:.LL39 LDAR:SEC:SBCQ
      8095BCCP%+4:CLC:RTS:PHA:LDAS:EOR#128:STAS:PLA:EOR#255:ADC#1:RTS
      8100.LL51 \XX12=XX15.XX16
      8105LDX#0:LDY#0:.ll51 LDAXX15:STAQ:LDAXX16,X:JSRFMLTU:STAT:LDAXX15+1:EORXX16+1, X:STAS:LDAXX15+2
      8115STAQ:LDAXX16+2,X:JSRFMLTU:STAQ:LDAT:STAR:LDAXX15+3
      8120EORXX16+3,X:JSRLL38:STAT:LDAXX15+4:STAQ:LDAXX16+4,X:JSRFMLTU:STAQ:LDAT:STAR:LDAXX15+5:EORXX16+5,X
      8130JSRLL38:STAXX12,Y:LDAS:STAXX12+1,Y:INY:INY:TXA:CLC:ADC#6:TAX:CMP#17:BCCll51:RTS

------
owenversteeg
Wow, that's amazing. I love programs written in less than a kilobyte.
Personally, I developed a CSS framework that is only 995 bytes (not to compare
it to this program) - [http://mincss.com/](http://mincss.com/).

In the same vein as this program, there's JS1k. Some of the stuff on there is
also really amazing.

~~~
TylerJay
> Patients that switched from Bootstrap to Min reported up to a ninefold
> decrease in markup

hehe.

Actually though, as someone who learned CSS with the rule "Use Divs! No
Tables!" What does a page with less divs even look like? What are the
workhorses for page layout?

~~~
owenversteeg
That rule is good. Using divs normally is fine. However, it is possible to
overuse divs. Sometimes you'll have a div nested inside a div (and so on) six
levels deep. That's considered bad.

For example, <div id="div-holder"> <div id="inner-padding-div"> <div
id="right-align-div"> <div id="centering-div"> <div id="actual-content">

is bad - you could reduce that to one or two divs at most.

~~~
TylerJay
Gotcha. Thanks!

------
bhz
In homage to things like this, there are a number of 512 byte, 1k, etc. game
development competitions. You can find them allover for a variety of machines.
If you're interested these tiny masterpieces, you should enter a competition,
or at least check them out.

Years ago I made a 512 byte maze game, I put up the listing here:
[http://burninghorizon.com/pub/c64/tinyrinth/tinyrinth.htm](http://burninghorizon.com/pub/c64/tinyrinth/tinyrinth.htm)

I made that page before I ever saw the article (and much later webpage)
describing this 1k chess program. The article should enlighten anyone who
isn't sure just how small 1k of code actually is:
[http://users.ox.ac.uk/~uzdm0006/scans/1kchess/](http://users.ox.ac.uk/~uzdm0006/scans/1kchess/)

------
anjc
A lot of the comments here are depressing, showing no appreciation of
computing history or engineering challenges.

------
ctdonath
TL;DR - chess, in 1K RAM _including_ video and OS memory space.

------
shubhamjain
Amazing! it is hard to imagine to even barely scratch the surface with
constraints like that. Sinclair ZX81 does seem to have an emulator[1], would
it be possible to see it run? It seems the emulator would need a assembled
binary, something I am not sure would be possible without whole lot of grunt
work.

[1]: [http://www.chuntey.com/](http://www.chuntey.com/)

------
brownbat
Olivier Poudade's BootChess claims to be the new record holder, 487-bytes of
assembly: [http://mashable.com/2015/01/30/play-it-better-tiny-chess-
gam...](http://mashable.com/2015/01/30/play-it-better-tiny-chess-game/)

~~~
ctdonath
Now if someone could arrange a competition between the two...

------
3327
Holy smokes... blown away. 1K RAM? less then most email's we write... Now'a
days rockstars and Ninja's are the glorified ruby guys who "seemingly" do
Ruby/django magic... This is what counts...This is the real deal...

~~~
moron4hire
Weeeell...

The entire point of computers is to be able to do work that would be too
tedious for humans. Computers exist so people can be lazy.

Making better programming languages so programmers can be lazy is kind of the
whole point of the job.

~~~
sago
_The entire point of computers is to be able to do work that would be too
tedious for humans._

Really? News to me.

Like the entire point of cars is to take you on journeys that would be too
tedious to walk?

~~~
david-given
No, that's absolutely true. The first computers were for calculating logarithm
tables, which were incredibly tedious, slow and error prone for humans to do.

If you're ever in Bletchley Park in the United Kingdom, go visit the National
Museum of Computing. They'll show you (and you'll get to play with) the
Harwell Witch, a decimal computer designed to compete with humans who are
using mechanical hand-cranked calculators:

[http://www.tnmoc.org/special-projects/harwell-dekatron-
witch](http://www.tnmoc.org/special-projects/harwell-dekatron-witch)

A well-trained human can actually outperform the Witch; for twenty minutes.
(They tried it.) But the Witch doesn't make mistakes and it doesn't get tired.

It was only much later that they realised that computers could do things which
humans weren't _able_ to do.

~~~
sago
Not seeing where you're arguing that the _entire-point_ of computers is to do
tedious calculations.

That the first computers were used that way, I don't doubt. But not the same
thing, I'd say. The first cars were horseless carriages.

~~~
moron4hire
don't be pedantic

~~~
sago
I wasn't trying to be. I wasn't saying "yeah, but 'entire point' _strictly_
means... fnar fnar".

I was responding to a point above that seemed to imply that code like this
can't be considered great (or at least, no greater than a Rails website),
because its not what computers are for. They are for calculating things humans
can't. I disagree with that. And not on a pedantic, language level. It is a
genuine confusion, i think, between why something was made and what it can do.

Though on re-reading, I may have misinterpreted the OP.

~~~
moron4hire
Sorry, I was really just calling out the "real programmers fart assembly in
their sleep" sort of stuff. The chess game is certainly an accomplishment, but
there are other ways to be accomplished at programming.

------
jpalomaki
A bit related: David Braben's presentation about the original Elite.

[http://www.gdcvault.com/play/1014628/Classic-Game-
Postmortem](http://www.gdcvault.com/play/1014628/Classic-Game-Postmortem)

------
tibbon
Link to the code:
[http://users.ox.ac.uk/~uzdm0006/scans/1kchess/](http://users.ox.ac.uk/~uzdm0006/scans/1kchess/)

------
atlantic
And here are the rules that formed the core of the AI (in case you missed
them):

Score provides a move score based on the following: first, the "To" position
results in taking of a piece. Second, the "From" position is attacked. Third,
the "To" position is attacked. Fourth, "To" enables the computer to obtain a
check and finally the "From" position is defended.

------
brudgers
The other day, my son brought a discarded 3.5" floppy from PrintShop home from
school. It got me thinking about the fact that 1440kb or even 720kb is more
information than a person can wrap their head around unless it's dumbed down
to a simply interpreted bitmap...i.e. we can interpret it with our visual
cortex.

1024 bytes has 2^8192 permutations. And 3-SAT is NP-Complete.

------
angularly
Nice! ZX81 was my first computer. I didn't even have the tape storage. I just
typed some program in from a magazine and left it on for a couple of days. I
even got the Lampda 16K memory expansion, which was a big external module
mounted on the back :)

------
smoyer
I have that program! The cassette it came on is still in my basement (along
with a couple of ZX-81s and TS-2000s). While it might be rudimentary AI, I
also didn't beat it every time (maybe not even half the time when I started
playing against it).

~~~
pmontra
I believed to have it too but I checked and I've got the Chess program from
PSION instead, which is for the 16kB ZX81. I remember that I lost every single
game. I keep it on the old CDs bookshelf, with a cassette of Defender. They're
maybe the last two tapes left from those years. The ZX81 is somewhere at my
parents house. I also kept the BASIC Programming Manual. I develop with Ruby
and JS now, probably no less than 16 MB for any program I run :-)

------
vanderZwan
Given that the TI-8X series of calculators run on a z80 chip, we could port
this almost as-is to that little machine, right?

[http://en.wikipedia.org/wiki/TI-84_Plus_series](http://en.wikipedia.org/wiki/TI-84_Plus_series)

------
henrik_w
Kevlin Henney mentions this program in his talk "Cool Code". The whole talk is
really worth watching.

[http://www.infoq.com/presentations/Cool-
Code](http://www.infoq.com/presentations/Cool-Code)

------
ddorian43
See kkrieger. 96KB doom3-like fps-demo

------
DodgyEggplant
For the record, drawing on screen was poking bytes directly to the screen
memory.

------
Fastidious
Does anyone have a link to find out more about David Horne?

------
stillsut
This is the opposite of a great program.

A great program scales with Moore's Law: when you get 2x memory, the program
gets better, ideally more than 2x better.

Human limitations remain constant through the ages, that's why climbing Mt.
Everest or the 4 minute mile are so compelling. But the most basic fact about
computing capacity is its (exponential) upward trend.

------
luma
If you can't castle, you can't promote a pawn, and you can't en passant, it
isn't chess.

~~~
pavel_lishin
You are welcome to add these things; we'll give you a generous extra 100
bytes.

~~~
bsaul
Wonder if any student somewhere isn't trying to take up the challenge of
adding castling to the code, using an emulator. I like to think that's
something i would try to do as a student, because i'm pretty sure there's a
tremendous amount of skill to gain by doing it.

