April 27, 2014

Touchdevelop pt 2

I the first part I started with programming on the phone using touchdevelop.

This time I will go a little bit deeper and finish my bird song quiz app.

Basic functionality is to randomly get six bird sounds from my media list on the phone, display all six and play one of them letting the user guess which one is right. After choosing the app will tell you if it was correct.

In the last part we started out with this to get a random song:

action main()
      var mysongs := media→ songs
      var chosensong := songs→random
      player→play(chosensong)
end action


This time we do the same stuff but six times with a a while clause that adds stuff to a collection.

First we add the chosen song:

var quizlist:=collections→create string collection
quizlist→add(▷ getbirdname(chosensong→name))
var x:=0
while x < 5 do
   var decoy := ▷getbirdname(songs→random→name
   if quizlist→contains(decoy) then
   else
      quizlist→add(decoy)
      x := x+1
   end if
end while

As I want a bird song quiz I have to convert the mp3-name (for example white-tailed swallow.mp3) to a bird name. I do this by removing the .mp3 from the name together with all numbers I can find (sometimes I have many numbered sounds for a bird).  This is done in getbirdname and the symbol ▷  means that it calls a function that returns the bird name from the mp3 file name.

The function looks like this:

private atomic action getbirdname(s:string)
returns (out:string)
do
   out:=s→replace regex("[\\d-]","")→replace(".mp","")
end action

Simple regex to get rid of the digits and then I just remove the .mp-part and return the name.

Then I show the names from the quizlist and plays the chosen one on my phone with:

quizlist→sort 
player→play(chosensong)
"Choose bird:"→post to wall
□ chosen:= -1
while □ chosen < 0 do
   □ chosen := wall→pick string("Which bird is this?", "Choose an alternative.", quizlist)
end while
if quizlist→at(□ chosen)→equals(▷ getbirdname(target→name)
then
   "Correct"→post to wall
else
   "Incorrect"→post to wall
   ▷ getbirdname(target→name)→post to wall
end if

The  chosen means that it can save the result between rounds as a global persistant variable that can be stored in the cloud.

Oh by the way....
the symbol □ looks like this with a bit magnification:

I've just touched on what you can do with touchdevelop (try for example language→speak text("en-US", "female", ▷ getbirdname(chosensong→name)), but since I still haven't found any customer who wants his business system written in the language I will move on to graph databases next time.

See you!




March 25, 2014

Finally I can code on my way to work...

I have a Windows Phone and I love it. Sure, there are a bit fewer apps and in Sweden only one phone company has the Nokia 1520 as a phone of choice. The 1520 is a big phone, most people actually question if its a phone or a tablet when they see it.

For me that is a perfect size.

It barely slips down in my pocket and it is big enough to be used as a small tablet.
When I then discovered TouchDevelop a few weeks ago everything came together. I could finally do some serious coding on my phone!

So... what is TouchDevelop?

It is a programming language by Microsoft Research that lets you create software by clicking. It is made for developing by touch and it lets you use your programs on a windows phone, iPhone, iPad, android phone or tablet or on your PC.

What is it not?

Even though the tutorials use the LOGO-turtle it is not a simple language. TouchDevelop has a lot of power under the hood and it takes a while to get in to. You could use the language to do just about anything, but the more complex the task is the more you would wish that you made it in c# instead. It is simply a perfect tool for quick hacks or games that needs to run on your phone.


So over to the case:
I am a bird watcher and I travel a lot. To be able to recognize birds I need to learn their sounds. This is seriously boring and I usually end up with listening to a couple of songs and then giving up.
This time it should be more fun to learn sounds so I want to create a bird quiz-application on my phone that plays a sound and gives me a number of birds to choose from and in the end gives me a percentage on how many correct answers I made so I can be proud of the results.

Since I only have bird songs in my song list on my phone the first step would be to play a random song.

It is a simple task but it is something that still would take some time to do in a traditional language (c#).

In TouchDevelop this would look like this:

action main()
      var mysongs := media→ songs
      var chosensong := songs→random
      player→play(chosensong)
end action

Just run this and a random song. And all this by clicking on your phone.

To be continued...