Tuesday, August 13, 2013

Wrote a Sudoku solver in Python on an iPad

My BladeRF-board is burning in my hands. I want to connect the beast to my laptop, experiment with gnuradio, write some VHDL-code, program the FX3-USB-chip with C-code and eventually write some python-code to control the BladeRF from my laptop. And I need to understand GitHub, become familiar with unix again etc.

Well, I was thinking about where to start now when my wife got that (persistent?) idea to go out for a fortnight and go camping on that nice campsite without electricity and Internet. OK, no BladeRF for a few weeks. But life is about chances, so I took the iPad, found the 12V charger, checked my Smart phone for Internet-access (tethering!) and a few days later we went for the trip.

Between dividing my attention to my wife, our dog and the neighbours, I had ample time for python. Previously I installed Pythonista on the iPad. I had a marvellous fortnight. I spent hours in bed with Pythonista just before sleeping. I did the language tutorial a few times. Read most of the language reference and read some forums on the Internet (yes, tethering on the Smart phone and discovering that you 'burn' a lot of MB's from your 500 MB month-limit). Then I examined the examples from the Pythonista-app. It looked quite simple finally.

I decided to go for a Sudoku-solver. This is medium-difficult project with classes, functions, lists and drawing/animation. Just to get familiar with python.

First I programmed a white tile. I you touch the tile it changes to red, touch it again and it is white again. That is fun! The equivalent of Hello world or a blinking LED!

I programmed an array of 9 * 9 tiles. To make it interesting I programmed the tiles to flip the color of its 4 neighbours if you touch one. Then I had the value increment for every touch. Later I made a small modification to set the value back to 0 after reaching a value of 10.

I changed the graphics to show a subdivision into 9 bigger tiles, each divided into 3 * 3 tiles.

I programmed functions to check for doubles in a row, a column or a 3*3 tile according to the Sudoku-rules.

Finally I made a big green START-button and programmed a solver using backtracking. Believe me, this was the simplest part of the game!

Now I can touch my empty Sudoku and program a challenge from a newspaper. Few minutes later it shows the solution. But more than that it animates the progress. It is fun to watch the solver solve the Sudoku. You see the backtracking to the first tile and going up again to find a solution.

I found some hard Sudoku-problems on the Internet and my python-program could solve them all. The hardest one took more than half an hour, but perhaps it took that long because the iPad switched itself off after so many minutes. Later I changed the setup and got an empty battery after a long walk with the dog ;<((

I sent the python-program to my laptop using email and tried to run it from the python-implementation on the laptop. Nope: 'import scene' does not work. Of course, the class/module Scene is specific for the OS on an iPad, I presume. OK, a pity. If someone wants the code, comment on this article or send me an email. This Sudoku-program is really fun to play with! But quite distracting if you want to do something useful with your life...

I found an enormous lot of Sudoku-implementations on the internet! I knew that already because I programmed a solver in MathCad once and also in java in my worktime before retirement. To learn the language of course (my boss agreed ;>)) to that).

This Sudoku-project taught me a lot about python. Python looks a lot like java, but it is easier perhaps. I am quite surprised that python does not insist in declaring variables. I can't resist to program "x = 1.0" to explicitly declare x to be a floating point variable. The for loop is fantastic! Just write "for i in elem" where elem is a list, an array, a dictionary or whatever datastructure and you get every element in the variable i. From then on you can use i.x for its x-value or whatever attribute. The scope of variables puzzled me in the beginning. I still have some problems with the argument 'self'. In a class every function gets a pointer to itself automagically? I'll try to grasp that later, if I have to.

I sent this photo to my little grandson (he likes big
machines). However he thought it was a picture of
the present he was going to get from us...

No comments:

Post a Comment