Digging through old code: Chess program

There is a lot of waiting for parents at a chess tournament (see Eliminating Fire Alarm sound at Chess Tournament Awards Ceremony). I had my computer, and I was asked if I had a chess program on it.

The answer is a little bit complicated: I wrote one over 2 decades ago that still works on Windows XP.

I looked in a zip file of old programs that I wrote and I found the source code of my old moldy chess program dated 1984 for the original IBM PC In those DOS days, the OS didn’t do very much for the programmer.

I had to write my own mouse driver and pulldown menu system because this was way before Windows. I remember trying to decide what happens when a mouse click pulls down a menu and moves off the menu: should the menu close? Should there be a single mouse down to activate the menu and a single mouse up over the current item would choose that item?

I had to decode the serial inputs of the 1200 baud optical VisiOn mouse (with its own metal mouse pad).. I used my Heathkit Oscilloscope to look at the raw RS-232 serial signals coming from the mouse and figuring out what they meant.

My code wrote directly to the IBM PC CGA Graphics card. 0xB800 was the base address.

I used my cartoon animation program (I wrote a cartoon animation program around 1982 on an original 4.77 MHz IBM PC (it’s reincarnated in Visual FoxPro: start the Task Pane, Solution Sample, Forms, Form Graphics, Line Animation). You could draw a single picture, save it, draw another picture, save it, then choose to animate, which would morph one picture to the other using interpolation) to draw the chess pieces with the mouse and record the vectors that made up a chess piece.

In fact, here’s what my Pawns looked like in vector graphics:

struct dat {

  char x0,y0,x1,y1;

} pawn[] = {

  2, 22, 23, 22,

 23, 22, 21, 20,

 21, 20, 14, 19,

 14, 19, 16, 16,

 16, 16, 17, 12,

 17, 12, 14, 8,

 14, 8, 18, 6,

 18, 6, 17, 5,

 17, 5, 14, 4,

 14, 4, 14, 3,

 14, 3, 13, 2,

  2, 22, 4, 20,

  4, 20, 11, 19,

 11, 19, 9, 16,

  9, 16, 8, 12,

  8, 12, 11, 8,

 11, 8, 7, 6,

  7, 6, 8, 5,

  8, 5, 11, 4,

 11, 4, 11, 3,

 11, 3, 12, 2,

  0, 0, 0, 0

I wrote my version of the Bresenham algorithm for drawing vectors.

It’s actually not too hard to write the chess program logic itself: you just need a move generator to create all possible moves, a position evaluator to give each position a score, and a tree pruning algorithm because the trees are so huge.

The program was written for a 4.77 MHz PC (see Why was the original IBM PC 4.77 Megahertz?), which had 256 k RAM and no hard disk. I remember trying to get each position to fit into exactly 16 bytes, so that memory management was simple: recall that with the 16 bit segmented architecture of the 8088 processor, a segment register pointed to 16 byte long segments.

Some of the more difficult chess issues:

  • Detecting a 3 duplicate move draw
  • Representing castling queenside and kingside
  • Capturing En-Passant (only allowed on the very next move after the opponent moves a pawn)
  • Changing the static board evaluator for beginning, middle, and end games. Control of center squares has less weight at the end game
  • Bishop vs knight value in opening, middle, end games.
  • When to prune the search tree: in some cases it would look a dozen moves ahead (a dozen levels down the tree).

The program was designed to “think” for about 3 minutes, then move. It would generate positions and use the static board evaluator to rate each position.

Interestingly enough, and a testament to Windows backward compatibility, I can run the same binary code on my machine today. It runs blazingly fast and finishes a 40 move game in about 3 seconds because the processor is way faster today (see My toys over the years) However, the pulldown menus and the mouse driver predate and are thus incompatible with Windows.