Download the project: sliders.zip

Coding a simple GTK+ board game

Tutorial By: Ishan Chattopadhyaya
Example originally written for A Journey through GTK+, a book by Muttiah Annamalai

Introduction

In this small little example program, we'll be creating a small board game, i.e. the 15-puzzle (which was popularised by the great Sam Lloyd). We'll call this game Sliders. This example is based almost entirely upon the GtkTable widget.

The rules of the game are simple. There are 15 jumbled up bricks placed in a 4x4 square matrix. One slot is left empty & only bricks that are adjacent to the empty slot can be moved in to the slot (by clicking on the brick). Object is to rearrange the bricks in ascending order with the bottom-most slot as empty. Achieving the objective in lesser no. of moves is more creditable.

[Note for puzzle solvers: Due to lack of time, and my laziness of course ;-), checks for 14-15-problem (i.e. 14 & 15 reversed) are not done while randomising the bricks. If you are in modifying the prog. so as to include this, pls. mail me]

Coding the app

The program consists of 5 major sections/functions:

Here's the code listing: [for downloading this proj, click here]

The save and open features are not implemented. Maybe saving could involve saving the current board position and move no. to an external XML file. Another feature that could be added is a Hall of Fame sort of a scores dialog, whose data could be stored in XML.

Conclusion

This small program can hopefully be helpful for someone out there who's new to GTK+ and is learning. Comments at: ichattopadhyaya@gmail.com


Ishan Chattopadhyaya
Ishan Chattopadhyaya

Other tutorials by Ishan Chattopadhyaya

Links

Please Sign my guestbook here | Email me
SourceForge Logo