Blog about software development


Minesweeper in Python for iPad

03 Jun 2016 - by 'Maurits van der Schee'

Pythonista iOS app allows you to write Python on your iPad. I wrote Minesweeper 2 years ago and with version 2.0 of Pythonista it did not work anymore. I fixed that and also added three features (first click is not a bomb, timer starts on first click, support chording) contributed on the OMZ forum by a user named "git-bee" on github.

Pythonista version 2.0

A lot has changed in version 2.0.

The completely revamped scene module gives you a lot more possibilities for building 2D games and animations in Pythonista. You can even use custom OpenGL fragment shaders. Lots of new sample code and a tutorial for building a simple game are available in the included Examples folder. source

In my case I only had to change a few small things in the script. Debugging was the hardest part as it seems the functions that called the Python Image Library (PIL) were terminated without a warning and the game kept running. It turned out the Size object could no longer be used to indicate size in PIL as Size contains floats and PIL needs ints.

If you’ve used a previous version of Pythonista, you may also be familiar with the Layer and Animation classes that were conceptually similar to Node and Action. These classes are still available for backwards compatibility, but it is strongly recommended to use the new Node/Action APIs instead because they provide significantly better performance. source

The performance of games has increased, especially if you use the new Node classes instead of the old Layer classes. My game is so simple that you don't really notice this.

3 Minesweeper features

The following features are part of the original Microsoft Minesweeper and were missing from my implementation. I only merged the implementation that was suggested by git-bee, so that's where all credits should go.

1. First click is not a bomb

It is very annoying when you start playing minesweeper and you lose the game on the first move. The original, Microsoft Windows, version of the game prevents this. My Python implementation was lacking this feature and in the latest release this functionality was added.

2. Timer starts on first click

While the game should only start the timer when you click the first time on the board, my Python implementation was starting the timer right away. This way you are never able to achieve that 4 second world record. It may also not help that you have to hold the tile to right click, costing you precious milliseconds.

3. Support chording

Chording is the thing that happens when you right click on an open tile. I didn't know that you could even do this, but after reading the minesweeper wiki I learned about this important technique. It helps you to play faster and it suddenly also makes sense to mark bombs. Until I learned about "chording" I never understood why you would mark bombs at all.

Pythonista version 3 (Edit: 2016-06-15)

On June 14th, 2016 OMZ released Pythonista version 3. This new version runs on Python 3.5 and is thus not fully backwards compatible with the Python 2.7 based Pythonista 2.0. Github user "mncfre" has made Minesweeper compatible with this new version and you find this version it in the file "Mines3.py".


Check out Pythonista Minesweeper on Github.

PS: Liked this article? Please share it on Facebook, Twitter or LinkedIn.