Chess Programming


Like the bullet said on the way in, chess is my favorite excuse to program—not that I need an excuse! I've done everything from utilities to typesetting, from game readers to commercial chess software, and I've enjoyed every minute of it. It really does combine all (well mostly all) of the things I enjoy and lets me write code to make it better. And as a side effect, usually my play improves. Just as an example, what follows is a list of steps taken from the playing of a game to its posting on my chess games web page:

If my goal was to involve as many computers as possible, three machines and two networks isn't a bad start! In the rest of this article I'll expand and explain the middle four steps, hopefully so that anyone who wants to, can do the same.

UPDATE! This was written quite some time ago and I no longer own let alone use a Palm Pilot. Between then and now I've used two other hand helds, an HP and more lately a Palm Z22. Why the Z22, simple I lost the HP! The Z22 is easily available for $60 or so (including shipping) on eBay so it seemed like the thing to do.

Transcribe and proofread game from score sheet to Palm Pilot Z22

One of the problems I share with a lot of other chess players lies in keeping an accurate score. Worse having that score be legible after the game. When you throw in a slight case of dyslexia, what you end up with is a recipe for frustration. My solution tries to remedy as much of this process as I can. The first thing I do before leaving the board, is transcribe the game using a program called 'Chess Recorder' (no longer available, but you can get its successor, ChessPad V2.1  at the authors web site) HiArcs, running on my Palm Pilot Z22. The advantages here are many, but several of them are particularly important. First, when you do this immediately, your memory is at its freshest and second, the application is visual mimicking the physical actions used during the game. Third, Chess Recorder HiArcs won't allow illegal moves, acting as sort of digital copy editor, insuring that at least the game you record is legal and complete even if your score sheet wasn't.

Archive games from Palm Pilot Z22 to PC

From the point of view of common sense computer precautions, always back up your data. In addition since the software to convert from a Palm Z22 memo record to a game in a .pgn file lives on a PC anyway this step is pretty much required. It is also, thanks to good design from Palm, dead bang easy. Just snap your usb cable into your Palm Pilot Z22 into its cradle and hit the hot link button. Every thing after that is automatic.

Create .pgn game file from PC archive

This next step move the game from Palm PC database to an access database using a program called accurately enough, 'Palm2Pgn'. This program is written in Visual Basic and uses a third party control from Consult US GmbH called 'Jet Pilot'. This is fairly easy to come by, either here or their own web site. Come to think of it, Palm2Pgn also uses a data table control from Sheridan, mostly because the ones that come with VB are so lame. If I remember correctly, the two things I wanted that I couldn't get out of box with VB5 were alternating background colors and selection memory. 

The first is mostly cosmetic and something I wanted but could live without, but the second was a major project killer. Ok, an exaggeration, but it was something I needed—you see, data entry on a Palm Pilot Z22 is enough of an annoyance so that you try and minimize it as much as you can. In particular make no entries you don't have to, like round number for instance. My plan from the first was to allow the user to select games in the order they are played, leaving the program to fill out the missing information automatically. Round numbers were obvious in context, but only if the selection order from the data table was preserved. VB of course throws this information away (or did then.)  At any rate what I wanted looks something like this:

You will note that since this is quick and dirty, I haven't been too concerned with little finishing touches. For instance the word 'Data1' visible in the data control at the bottom of the screen certainly shouldn't be there. Also the default point size for all of the text is too small, it should be at least 10 point instead of just 8. But the bottom line here is that it works, so the rest can wait until I get around to it.

As you can see, you select games in order (first to last) then hit the button on the bottom. That leads to a screen like this:

There after, the Save as button leads to a file dialog with the normal options. Similarly for the Email button. What might not be so obvious from this screen is the work done behind the scenes as a part of building the file. First there are some cosmetics:

  1. Eliminate memo name—'{R One}' and the like. Palm insists that all memos be named uniquely and that name should be in curly braces as the first thing in the file.
  2. Wipe out the ECO tag. Usually it will look like '[ECO "?"]', so remove it for the moment.
  3. Customize all reference to me by changing 'hsmyers' to 'Myers, Hugh S (ID)'.
  4. Replace the contents of the ROUND tag with the current round.
  5. Find the game result and save it for later.
  6. If its the first game find and save the playing site and event name for later. If it is not the first game then replace the site and event tags with the saved information. In effect, this means that you only fill out this information once when entering the first game.
  7. Convert all zeros used to indicate castling to letter 'ohs ' to match the PGN standard.
  8. Now edit the move text so that all lines are less than 80 character long, again to match the PGN standard.

'But wait—there's more' like Ginsu knives, the features just keep on coming. The next piece of magic involves chess taxonomy. Which means roughly the identification of chess openings. There are three codes commonly in use; NIC or New in Chess, ECO or Encyclopedia of Chess Openings and the traditional descriptive names. A little work is necessary in order to identify each game in these 3 systems. It is not horribly complicated on the face of it but its details are certainly convoluted. Simply put, for each position in the game create a 'FEN' diagram. Then working from the last position to the first, do a database lookup in an Access table. Not too surprisingly there is one table for each classification scheme where a particular position is the key to a given code. So you do this three times and with each lookup you insert the necessary PGN tag information. So using the first game above, you wind up with a NIC code of 'SD 3', an ECO code of 'B01' and an Opening name of 'Scandinavian defense'. Not only is this convenient, but from time to time I'm surprised by what opening I'm playing, at least in terms of its name!

After that, having taken the game apart, put it back together again. We add in the necessary white space like line breaks and such. Then we add on the saved result at the end of the move text, leaving us with pretty much just what you see in the previous illustration. Now repeat all of the previous until finished with each game.

UPDATE! Thank heaven that none of this (well mostly none) is necessary any longer. There are still bumps in the process, but the software that comes with the Z22 provides a fairly clean selection process for information that has been uploaded from it to the PC. It is now mostly a process of cut and paste to a .pgn file. Then I execute a batch file that looks like:

cleanPGN games\%1.pgn edits.txt
pgn2pal games\%1.pgn > games\%1.htm
np "C:\Program Files\Apache Software Foundation\Apache2.2\
➠htdocs\sdragons\Chess\chess.html"
copy /Y games\%1.pgn "C:\Program Files\Apache Software 
➠Foundation\Apache2.2\htdocs\sdragons\Chess\games\%1.pgn"
copy /Y games\%1.htm "C:\Program Files\Apache Software 
➠Foundation\Apache2.2\htdocs\sdragons\Chess\games\%1.html"
    














This allows me to test things before I upload to the actual web site.

  1. cleanPGN — Reformats the target file taking care of round numbers, missing site names and similar. It will also convert various aliases to uniform names using the edits.txt file. For instance ‘You’ will be changed to ‘Myers, Hugh S. (ID)’ cleanPGN also automatically fills in the blank on the three tags for taxonomy; ECO, NIC and Opening.
  2. pgn2pal — Converts the .pgn file to html using a variation of the idea from the very fine PalView software.
  3. np chess.html — Edit what amounts to the index for all of my games.
  4. copy … — Copy the new file(s) to my local web directory for testing.

Convert from .pgn to .xml

So if you've followed along so far, we now have a collection of games in order, enhanced and in a .pgn text file. You could certainly stop there if you wanted to. I doesn't take very much to display a text file on a web page, particularly since that is the built in default behavior. However since I've never know just when to stop (must be the lemming in me...) I thought it a good idea to take things to the next level so to speak. 

When you consider the display of chess games there are two problems. The viewer may not speak English and most viewers can't visualize the game positions in their heads. The solution to both of these problems is typographical and fairly straight forward. 

We can overcome the first problem by typesetting at least the game text in a special chess font that converts the game notation from normal algebraic to figurine. This is made easy by the mapping of piece characters to their corresponding symbol representation. The second problem is a little more difficult since I can't tell in advance what diagram a viewer might like to see. As a sort of compromise, I display the closing position of the game and leave it at that. The diagram itself is set in another chess font, this one designed for showing board positions. This last requires something more than a simple change of font, it requires both knowledge of the position in question and the correct mapping of font characters to each character of the diagram. Further since it must generate this from the game score, the program used must be able to play a long in effect, correctly updating an internal board with each move.

At this point the reader should remember that the VB application used to create the .pgn files in the first place also 'played along' in order to classify the openings. Quite true—but I decided to go another route for this portion of the process. I decided that this was a good time (read excuse) to learn another language. Python to the rescue! While the details of this program are related elsewhere, suffice it to say that the translation of .pgn into XML was both a challenge and extremely interesting as well.

UPDATE! While much of what was said in this section, kindly disregard everything mentioned about both Python (the link is still good as is the article) and XML. I've written better solutions that I've long since switched to.

Terms and definitions

FEN
Forsyth Edwards Notation
PGN
Portable Game Notation (text) or Portable Game Notation (html)
NIC
New in Chess opening code.
ECO
Encyclopedia of Chess Openings opening code.
Algebraic
Notation style used to mark chess moves in a game score. Each square on the chess board has a unique name, a1 through h8 where ranks (rows) are numbered 1-8 and files (columns) are lettered a-h. Pieces are single letters for each piece excepting pawns (a move without a piece designator is a pawn move). In English, pieces are K for king, Q for queen, B for bishop, N for knight and R for rook.
Figurine
Exactly the same as algebraic notation, except that each piece designator is replaced with an icon of the piece in question.

 Page last touched:
Send e-mail to: hsmyers@gmail.com 


Validated by W3C