pgChess code published

I have been lucky enough to be invited at the marvellous PGDay.eu 2012 conference in Stuttgart, which ended just yesterday.

The topic of the first of my two talks has been a collection of PostgreSQL objects that play chess, either between themselves or against a human.


My intention was to present the software, tidy up the code a bit (out of respect for the fellow PostgreSQL user 🙂 and then release it as free software, for the usual reasons, if there was any interest during the talk.
That being the case, I have named the program pgChess and published the code on GitHub so that the interested reader can verify my claim that the code weights just around 20KB.
I split the core of the code in two files: the pgchess library, which in turn invokes the 2podg library, which stands for “2-player open deterministic games”: open meaning that each player knows 100% of the status of the  game, and deterministic because the whole game is determined uniquely by the choices that the players make.
Two (counter)examples: most card games are not open; Backgammon is not deterministic.
The strategy is quite simple, and is tailored on PostgreSQL features. As short as I can:

  • custom types are defined both for the game state, and for a single move
  • the chessboard is defined as an 8×8 array
  • a quite large function computes the valid moves available from a given game state
  • a recursive algorithm populates the tree of possible moves, up to the specified level (to David Fetter: I am really looking forward to writable CTEs for this part!)
  • a recursive SQL query selects the “best” move, according to the desired strategy which is encoded in the query itself
  • a multi-transactional psql script displays the chessboard, thanks to the Unicode chess characters: ♟,♜,♞,♝,♛ and ♚ for the Black; ♙,♖,♘,♗,♕ and ♔ for the White.
  • players can type their move like “b2Pb4”, or “g8Nh6” (the piece letter always uppercase regardless of the colour).

The reason for the talk is to show that PostgreSQL is an excellent framework to develop application prototypes quickly (which is not a big secret; but providing one more example reinforces the claim). And speed prototyping is one key summand that I recall from Simon Phipps in his remarkable keynote speech, whose slides I recommend when they will become available.
If you attended the conference, please consider giving feedback: who knows, you could win a free entrance for the 2011 conference!
Please note finally that my talk slides will be shortly published on the conference website, and that this article will be updated accordingly.

0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *