"Linux Gazette...making Linux just a little more fun!"

Pysol: Python-Powered Solitaire

By Larry Ayers


Playing solitaire card games on a computer became popular when Microsoft bundled a Klondike game with Windows 3.1. Since then such programs have proliferated on nearly every platform which possesses a windowing interface. There is a certain appeal to dragging miniature representations of playing cards around the screen. A side benefit is that such games usually can keep track of scores, provide hints, and sometimes auto-play in demo mode.

There have been many solitaire games released for Linux. One of the older ones is xpat2, which has some of the nicest design-work of any of these games. Xpat2 shows its age due to the lack of card-dragging, which contributes greatly to the feel of a computer card-game. Clicking on a card instantly moves it to a legal destination; when there is more than one possible move, the one xpat2 chooses may not be the one you had in mind. Otherwise it's a fine game, with several solitaire variants to choose from and well-done on-line help.

Users of the GNOME and KDE desktop environments each have native solitaire games, both of which are quality applications. If you aren't a user of one of these desktop systems it's hardly worthwhile to keep the bulky shared libraries around just to play a simple game.

Recently I was browsing the incoming directory at the Sunsite FTP site; I happened across a small file which, according to its accompanying *.lsm file, purported to be an implementation of solitaire called Pysol written in the Python programming language. I was a little dubious of this claim. Python is a versatile and powerful interpreted programming language, but is it possible to write a card-game using Python which is as usable and pleasing to the eye as one written in C or C++?

It evidently is possible if the tkinter module is used to provide the graphical interface. Tkinter (which I assume stands for "interface to Tk") lets a Python script use John Ousterhout's versatile Tk toolkit to provide the windowing interface. Tk is normally used with the Tcl command language, but Tcl has several limitations. These have been sufficient to provide motivation for several replacements; Tkinter is widely used, but there are others. Perltk uses Larry Wall's Perl language as the command language; another is Stk, which uses Scheme as its scripting language. The Xxl spreadsheet is the first major project I've seen which uses this Tk/Scheme hybrid. (Perhaps I'll review Xxl one of these months).

Pysol was written by Markus F.X.J. Oberhumer and he has released it under the GNU license. The game is an extensive reworking and enhancement of a simple Python solitaire demo written by Guido van Rossum (creator and maintainer of the Python language) which is included as an example in the Python distribution.

Features and Game-Play

Here is a list of Pysol's features, adapted from the README file in the distribution:

Nine games can be played: Gypsy, Picture Gallery, Irmgard, 8X8, Freecell, Seahaven,Braid, Spider, and Forty Thieves. The rules and documentation are supplied in HTML format and are displayed in a separate window using a Python HTML extension. Card-dealing at the onset of a game is nicely animated, and the mouse-dragging of cards works smoothly.

If you have ever spent much time playing solitaire on a computer you probably have noticed that after a certain point in a game the outcome seems obvious. This intuition isn't always accurate when you suspect the game is lost, but sometimes it's obvious that several more card-moves will win the game. Pysol binds the a key so that, when pressed, it will automatically cycle through those moves and bring the game to completion. When you strongly suspect that the game can't be won, the menu-item Demo (in the Game menu) will ask if you want to abandon the current game; pressing the "Yes" button will start the demo mode and either finish the game or find that it can't be completed. I've found that about one-quarter of the times I resort to demo mode my intuition was wrong and the game could have been won. If a game is hopeless a pop-up window appears informing you that "This won't come out".

Pysol's help key is h; when it's pressed a black arrow appears extending from a card to a recommended destination. The same arrows appear when Demo mode is initiated, though in this case the cards are actually moved.

Here is a screenshot of a Pysol Klondike game:

Pysol Window

Installing Pysol

Pysol won't work at all if you don't have a current Python installation, including the tkinter module. A current Linux distribution will include all the Python stuff you would ever want, it's just a matter of installing it. As far as that goes, Python is one of those high-quality applications which is very likely to compile well from source, assuming you have the basic Linux development packages installed, such as gcc, make, etc.

Pysol is just a 75-kilobyte executable Python script; running make install will copy the script to /usr/games and the necessary data-files to /usr/share/pysol, after which the game is ready to run.

I'm impressed by this game's quality and playability. It does take several seconds to start up, probably due to the necessity of loading the Python interpreter and the Tkinter module into memory.

Last modified: Mon 28 Sep 1998

Copyright © 1998, Larry Ayers
Published in Issue 33 of Linux Gazette, October 1998