Boston Code Camp 21: How to Unit Test JavaScript with Jasmine

by Larry Spencer Saturday, June 21, 2014 10:06 AM
Later today, I will present a session at Boston Code Camp 21 on How to Unit-Test JavaScript with Jasmine. This post is the start of what would be the slide deck if I had any slides. (More posts are coming soon.) If you want to learn the basics of Jasmine, this post is for you, too.

First, to set the stage. We will spend our time in a world of sims -- simulated beings who wander around and do one of two things when they meet another sim: either strike him or heal him. When A meets B, A's choice is based on three things:

  • A's inherent level of aggressiveness.
  • B's reputation (how often he has chosen to strike rather than heal in the past)
  • A's reponsiveness to B's reputation.
Those factors combine in a simple way to give a probability that A will choose to strike. Then, with one call to Math.Random(), the decision is made. This little graph shows the process.
A negative responsiveness, meaning that the worse B's reputation, the less likely it is that A will strike him, corresponds to a downward slope of the line.

While A is striking or healing B, B is also striking or healing A.

A sim stars with 0 health points. He gains points when he is healed, and loses them when he is struck. When he gets healthy enough, he reproduces. When his health is too far in negative territory, he dies. 

We can set the parameters of the world. For example, we can say that if A and B both choose to heal each other, each one gains 500 points, but if A strikes B while B is healing A, B loses 600 points while A only gains 100. (Perhaps A doesn't get healed as much because he's too busy fighting, and B loses a lot because he's not trying to defend himself.)

Initially, we populate our world with 10,000 sims, with aggressiveness and responsiveness factors evenly distributed. Then, we randomly pair them off, let them do their thing, When that round is done, we pair them off again, and so on, until we decide to end the game.

If you press Play in the game below, you'll see some blue discs. The location of each disc represents aggressiveness and responsiveness. The area of each disc corresponds to the number of sims with those characteristics. You can also vary the parameters. After much tinkering, I've found a set of parameters that tends to produce two different outcomes, depending on how the random aspects of the game play out. (Try it several times and you'll see.) I've made those parameters the defaults. Enjoy!

Of course, the game below is hosted by Plunker, so you can inspect and edit the code to your heart's content. You'll be playing in your own sandbox; there's no danger of wrecking anything.

Starting with the next post, I'll get into the Jasmine and JavaScript aspects of all this. 

Tags: , ,

All | Talks

Pingbacks and trackbacks (1)+

Add comment

About the Author

Larry Spencer

Larry Spencer develops software with the Microsoft .NET Framework for ScerIS, a document-management company in Sudbury, MA.