Minesweeper game for iOS in Python (with Pythonista)

In one of my previous posts on Python, I wrote about how I like to program games when learning a new language. In my initial post, I have written “tic-tac-toe” with some AI. In my second post, I went for a very ambitious AI on the famous “Connect 4” game. In this third post, I will release a “minesweeper” game for iOS written using the “Pythonista” App. The picture below shows the game on my iPad mini:

minesweeper pythonista

A rainy day

Last week I was on holiday, and since it rained almost the entire week (I stayed in the Netherlands) I had plenty of time to sharpen my Python skills. The house we were staying in had a good Wi-Fi Internet connection and although I did not bring my laptop, I did bring my iPad.

Normally I use the iPad for reading websites on the couch while listening to some streaming music. I also like to play small games or type drafts for blog posts (like this one) on it. For the latter, I use the (free) WordPress iOS App. But now I had to do some Python Minesweeper coding.

I expected having to connect to one of my Linux boxes using a terminal application and then coding remotely using a text mode editor, like Vim. But when I explored the possibilities I ran into the Pythonista App. It is a full featured run-time and IDE for Python on iOS. It allows to both program (type) and execute the code on the iPad.

Although it is a paid app, I liked the idea of coding Python on the iPad so much that I decided to buy it. Pythonista comes with some sample applications that will get you up to speed in no time. Pythonista also has a fairly good help system so that you can get context sensitive help on Python functions.

After many hours of typing, Googling, and thinking, I came to the point that the code was ready. I ran into a few nice Python best practices: generators, enums, namedtuple and genuinly had a good time. I especially liked the “yield” (generator) construct a lot and as was happy to find yield is included in PHP (since version 5.5).

Windows XP graphics

Minesweeper X is a nice minesweeper clone written by Curtis Bright. On the site you find a set of skins for his game. Instead of inventing my own skin system, I decided to implement his. To load the skin I converted the file from BMP to PNG using Gimp. After that I converted the file to a base64 string to be able to paste it (with minimal length) in the code. For the game, I chose the familiar “Windows XP” skin, shown below:

winxpskin

Pythonista is not free

Pythonista” is a paid app (6 eur/7 usd). It is comparable to the Lua programming app “Codea” (9 eur/10 usd). For people that like PHP there is “Kodiak” (9 eur/10 usd). And finally, for people that code JavaScript there is “Procoding” (4.5 eur/5 usd). All four applications allow you to run scripts on your iOS device. Although geeking out on your iOS device with your favorite programming language is lots of fun, typing on-screen is not. That’s why I recommend a blue-tooth iPad keyboard for any serious programming effort.

There also is a very innovative app called “ScriptKit” (11 eur/12 usd), that allows for touch-enabled “drag-and-drop” programming in a Lua-like language.

Download the source code

Downloading the source code is as simple as following the link below and copy-pasting the code into your Pythonista app:

https://raw.githubusercontent.com/mevdschee/pythonista-minesweeper/master/Mines.py

Further reading

If you want to dive deeper into Pythonista, you may want to check out the following links:

Python games on GitHub

With this last post I have finished the three games I normally write when learning a new language:

  1. Tic-tac-toe (github) (blog)
  2. Connect 4 (github) (blog)
  3. Minesweeper (github) (blog)

I have published all three games on my GitHub account and discussed each of them in a blog. I hope it inspired you to pick up Python as well.

Share

One thought on “Minesweeper game for iOS in Python (with Pythonista)”

Leave a Reply

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