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
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
      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)
   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:

"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)
   "Correct"→post to wall
   "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!

No comments:

Post a Comment