Sunday, January 26, 2014

The Choice of Language and Libraries

I've spent several hours researching. What platform should we build our "Princess vs Lion" game on? Even though the game is seemingly simple, it is far from trivial to implement with the skillset we've built so far. The problem lies in asynchronicity: the Lion moves on a regular interval while the princess moves when the spacebar is pressed. The game result depends on both. 

As the main developer of the Bacon.js library I'd love to apply the Functional Reactive Programming (FRP) paradigm. The source code using Bacon.js and CoffeeScript would look something like this:


princessPos = spacePress.scan 4, inc
lionPos = Bacon.interval(500).scan 0, inc
lionWon = Bacon.combineTemplate { princessPos, lionPos }
               .filter ({princessPos, lionPos}) -> princessPos == lionPos
princessWon = princessPos.filter((pos) -> pos >= 13).take(1)

Fine? Well, to me this looks awesome, but how likely is a 4-year old to grok FRP while my colleagues are still struggling with it? Surely she beats 50% of them in her Vim skills already but FRP, really? Also, she's totally unspoilt by Object-Oriented Programming (OOP) too. The code above is full of object method calls. She can hardly understand functions.

Next, imperative Javascript without OOP, with some helper methods (key, tell, later) I wrote. (That's an image. Blogger is driving me crazy by consistently ruining my nice formatting)
Like that? Hmm. That doesn't seem daunting to me. Even my wife understood the code after I explained it to her. And she's never seen Javascript before. On the other hand, neither has my daughter. So, using this approach I'd have to expose her to Javascript with mutable variables. Would you do that to your daughter?

Then I tried the Elm programming language that has a syntax familiar to her already. The problem is I couldn't come up with a satisfying solution at all. See the Gist if you're into that kind of thing.

Currently I'm slightly leaned towards the Javascript solution even though it feels like domestic violence. The upside is that the code is indeed quite simple and it will teach her to write code that you can actually run in a browser without a complex toolchain. Because that's what she'd really want to learn. To write games she can run in her browser.

Maybe I'll teach her out of the bad habits later when she masters them already. I'd really really like to make this simple game happen... I started with BASIC and gotos and I come out pretty ok, didn't I? Ha.

Update: I got the Elm version to work too.

2 comments:

  1. Yeah, we've played with Scratch. It's nice. Not my choice for this excercise though, as it doesn't promote composing code from re-usable pieces.

    (http://girlscantcode.blogspot.fi/2013/12/reaktor-kids-kode-skool-iii-scratch.html)

    ReplyDelete