Around 40 years ago there was a craze which took off around the world. It was a 3-dimensional puzzle called the Rubik’s Cube which kept people in a state of semi-trance while they tried to get the cube into the wining configuration.

Rubik's Cube

By a strange quirk of history, almost exactly 100 year before the Rubik’s Cube craze, there was another puzzle craze, this time for the 15 Puzzle. In some ways this was like a 2-dimensional version of the Rubik’s Cube. However, there was a twist – depending on the initial configuration the 15 Puzzle can be unsolvable. Prizes were offered which could never honestly be won and people racked their brains to find a solution where none existed.

In this article I want to share a Python implementation of the 15 Puzzle. It is a solvable version – but I guess if you are feeling mean you could modify the code and share the game with someone you want to annoy!

Programming the 15 Puzzle with Python

We will use the Python Turtle Graphics Module for this program, along with a tiny bit of Tkinter, which is the GUI (graphical user interface) library which the Turtle Module is built on. You will need an actual installation of Python to make this work rather than a browser-based version.

Here is the listing:

You will need the images to make this work – you can download them (and the code) from here.

A couple of points about the code:

  • The game logic and the display are tightly coupled in this implementation, which is not always a great idea – I was keen to get to playing the game so was hasty with the planning.

  • I have provided two ways to implement the scramble button. The Tkinter version could provide a gentle introduction to Turtle's parent library

  • There is a technique for passing additional parameters to the click callback which I discuss in this article about the 21 Game

  • There is a small amount of repetition in the code which I may refactor at some point, but for now as far as I can tell you have working version of the 15 Puzzle to play with.

Solving the 15 Puzzle

I don’t want to deprive you of the fun of solving this puzzle for yourself. What I have done is made it possible to work with a smaller version in order to develop strategies and work out what is possible. To to this just change the constants at the top of the code. One concept I find useful to think about when trying to be more effective than simply randomly moving squares, is cycles – see if you can see any cycles happening as you attempt to solve this puzzle.

So that was my implementation of the 15 Puzzle in Python. I hope you found it enjoyable. If you want a challenge, try writing the code for the game yourself. And let me know how you get on with either the strategy or the code in the comments below.

Happy puzzling!

Leave a Reply

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

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}

Join our mailing list

Join our mailing list to receive awesome articles about learning Python and Computer Science in a fun and accessible way, straight to your inbox.

 Take your Python skills to the next level!

with our free email course on object oriented programming with Python