Try this level - Binary Search

I figured this site needed a level about binary search so I went about making one.

The general idea was this: The level could be a number guessing game and your character says the numbers. Since it takes time to say something, too many guesses would make you run out of time.

The yaks are supposed to provide a visual representation of the guesses. I originally considered having each of them represent one number, but that would cap the max number too low, so they represent a range of numbers.

Try it here:

I want feedback in particular on the system of using the paladin’s position for the higher/lower responses. I’m considering changing it to another property like maxNum if the positions are too much of an issue.

1 Like

I like it, nice work.

Though, as the level currently stands, I feel it is probably too hard even for the glacier area. Perhaps laying out some logic structure in the sample code and commenting it, as well as providing an example of how the algorithm should work in the guide section would make the level more manageable for learners.

Very nice level. Can be even be a mountain or even a desert with some extra help:

  1. Does not work with JavaScript. Paladin properties such as pos or maxNum are not defined.
  2. You need to mention using int(value) or Math.round(value) to convert to an integer
  3. Add a small pre-written code portion such as:
if(answer < 40): 
  maxrange=middle-1
    if(maxrange < minrange):
      maxrange=minrange

Not sure about sample code, but I agree that there should be an explanation of the algorithm in the guide. At the moment there is just a link to a wikipedia page but a step-by-step example would be helpful. Finishing the guide should be one of the later steps of building the level though.
Relative to other algorithms, however, this one should be easier to conceptualise. Give someone this problem in real life and they will soon figure out the optimal way to do it.

Now, did anyone have issues with the positions? I’ll try the level in javascript.

Edit: tried the javascript and was able to pass: pos was defined, but maxNum was not defined until after the first frame (I think?). Then I changed that in the editor.

Nice level. The yaks were a nice touch. :smile_cat:

I like that you covered the edge cases. I was able to complete this using JavaScript. I was able to access the paladin position and maxNum properties.

I’d agree with others, providing some pre-written code would probably be helpful for learning the concept.

Immediately got an error upon start of the level FYI (before modification). I am doing a clojure run.

Parse error on line 2

Because the line comment syntax differs from clojure to javascript.

That is expected – translating the text and code to other languages should be done as part of the finishing touches for the level. So just change the initial comments to your preferred syntax.
However, configuring the comments to work for other languages (in the level editor) doesn’t take very long at all, I just haven’t done it yet.

Yep, that sounds right.

Hey, I gave the level a play through (and finally beat it.) Some things I’ll note:

  • Since binary-search is usually centered around array indexes, it would be helpful if non-integer inputs were rejected. This would point players to managing integers so they don’t fall into the same traps I did: (49.5, 49.75, 49.875, 49.9125 etc always being floored to 49)
  • Some sample code would be nice to have, even if it is just:
// This would help point players in the right direction.
while(true) {
    var friend = hero.findFriends()[0];
    var lowerBound = 0;
    var upperBound = friend.maxNum;
    hero.say(upperBound);
}
  • If not sample code above, then maybe put this information in code format in the guide?
  • The fact that the Paladin starts at 40 results in inelegant code. Would it mess up anything if the Paladin started at 41?

I like the Yaks being a debugging tool. I hope you find time to complete it, we’d love to add it to the campaign. I don’t think it’s too hard for the Glacier.

2 Likes

Bug: the paladin position doesn’t work at all, she just keeps alternating between left and right regardless of whether the number is lower/higher.

2 Likes