Eight Queens Puzzle in Python

Click me to play

The Eight Queens Puzzle is a classic problem whose goal is to place 8 queens on an 8x8 chessboard in such a way that none of the queens share a row, column or diagonal. The version discussed here allows exploration of not just an 8x8 but an arbitrary size nxn board, and hence the program is called N Queens.

The problem is very interesting from a Computer Science point of view, as is raises many issues to do with data representation, algorithmic efficiency and more. You can explore some of these issues in How to think like a Computer Scientist.

This article focuses on a Python implementation of the game using a fantastic tool for building graphical user interfaces (GUIs) called simplegui. simplegui is a minimal GUI framework for Python which keeps things simple so you can focus on the idea you are trying to implement without getting bogged down in detail. It provides:

  • A canvas area where you can draw shapes and text, and display images
  • A control area where you can easily add buttons and labels
  • keyboard press and mouse click event handling
  • Timers
  • Audio playback functionality

There are two ways you can use this tool:

Play the 8 Queens Puzzle in Python Online

Check out the puzzle here

Please note you may have to run the code a couple of times to get it to work in Codeskulptor. This is to do with image/sound loading times when running in a browser.

Try and solve the puzzle on different size boards. Be aware that a couple of the smaller sizes do not have solutions. Can you tell which ones?

Now that you are familiar with the puzzle, no doubt you are chomping at the bit to write the program for yourself, or at least to peek at the code to see how it works.

You can find the complete code for the puzzle on my GitHub The code is comprised of two files:

  • n_queens.py contains attributes and methods for the logic and data representation of the game, and is completely independent of the GUI code. This separation is generally a very good idea when working with GUIs, as you will learn from experience if you try and build similar games mixing together display logic and game logic.

  • n_queens_gui.py uses attributes and methods from n_queens.py along with the tools available from the simplegui module to make a graphical version of the game.

You will notice that both files use object oriented programming. For some this might be considered an advanced topic. It would be possible to write the program with using classes, but at the level of complexity of this game that approach could quickly become unwieldy, particularly when it comes to keeping track of global variables.

That said, there is a great deal that you can do with Codeskulptor/SimpleguiCS2Pygame without using OOP, so don’t be discouraged if the code here seems a bit beyond your level of understanding. In future articles I may well cover the basics of GUI programming for people who haven’t yet ventured into the world of classes and objects.

Python implementation of the classic Eight Queens Puzzle

Leave a Repl​​​​​y

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

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

Get the code for the classic Retro Alien Blitz Game using Python Turtle Graphics when you join our mailing list