Level: Zone of Danger

There’s something off here. What is the function of the code’s tabs? Whatever it is, it doesn’t work. The max I can do with these buttons is to reset the code.
On the same subject, yesterday I tried to use constructor to redefine the functions and it worked fine. Now I can’t do that. Furthermore, when I try to just write the function, it displays a message that says the function is already defined. I can only assume that’s because the tabs above the code suppose to give me access to write different parts of the code. Something that the’re not doin’ as far as I can tell.

On another note, thank you very much for developing this game! It’s awesome and fun even for adults. I’ve been trying to teach my younger brother to program for ages now and he never seemed interested till your game showed up.

Edit: Okay. I found out how to divide my code and write these functions using only the given ui and commands. But it’s far from obvious: I needed to click the refresh button in all of the tabs and change the casting settings to “manual”. But even after Iv’e done all of this, there seems to be some sort of bug with the casting option - it stays grey and doesn’t say what’s wrong at some point.

Also, when I open getNearestEnemy tab and then return to setAction, it adds up lines and erases others in setAction. and so I always need to refix the setAction. And there’s some series problems with the copy-paste function.

Edit2: Most of the bugs seems to be fixed :slight_smile: Also, there’s some funny business Dronk orge, but I’ll work it out :wink:

I can’t beat it. Can’t even try. That’s probably because I’m really horrible at JavaScript.

But I really like the level, it seems awesome! Now if I could only find the correct way to kill a single guy…

Great job Aftermath, nicely done!
Might be interesting to improve this algorithm for more complex fights, when a faster target becomes closer.

For instance:

  • check if the faster (now closer) target has less or equal hit points than the previously attacked enemy
  • if yes, attack it

The logic behind this - the faster targets usually have less hit points as well. Suicide units (like banelings in SC2) with lower HP than large targets should be targeted first anyways. So that’s two flies with one swat! :slight_smile:

This was pretty fun. For kicks, you can technically survive the entire level by adding just this one line to getNearestEnemy():

edit - I can’t seem to work the spoiler tag, so I removed the code.

I hope I used the spoiler correctly.

Also, people were mentioning targeting by health and speed, etc. A series of increasingly difficult challenges in that vein would be very, very fun. Perhaps it could culminate in an unwinnable scenario where you optimize for the most possible kills before dying.

@rnprdk that’s a great idea! A “Targeting Campaign”, so to speak. Any interest in helping to create it?

What’s the one line that can beat it? Feel free to spoil, since I probably need to just fix the balance so you actually have to shoot the nearest guy.

Yeah I figured it kind of wasn’t really a spoiler, but I try to play it ultra-safe with spoiler-y things. I’m living in dread right now of someone spoilering the second season of House of Cards for me …

nearestEnemy = enemies[(enemies[1] && enemies[0].id[4] == “’”) ? 1 : 0 ];

You can just barely (barely!) survive shooting only at enemies[0] until you get to the big bad, at which point you have to shoot at enemies[1] if there is one.

I might try to! I found you guys while I was searching for a coffeescript project to contribute to, and I was very happy to find a project so welcoming to beginners. I really admire a lot of the people I see out there hacking on OSS, but I find myself quite nervous about beginning to contribute myself.

Oh, well, that’s sufficiently clever that I think I’ll let it stand for this level! :wink:

Don’t be nervous about contributing, especially to this project. We will be excited to get you set up and expressing your ideas in code, since that is, after all, our mission! Feel free to stop by our HipChat room any time.

@nick Hi nick, Im fairly new at coding and so when I got to this level I copied and pasted your code from above into the getNearestEnemy “spell” and I copied and pasted code from another fellow on the alternate thread for this level, however when I run the game, the tower explodes and repairs as if it were in a loop. Please advise

It sounds like there might be some error message showing–the explode/repair thing would make sense if the tower ran into a programming problem. Were there any error messages in either of the programmable methods?

Sorry @nick, but I can’t seem to get the code. Any tips or suggestions?

Thanks,
TJ

@makertech81 Have you checked the Guide button at the top? There are some hints in there.

Thanks! It works now! What a great level @nick!

TJ

Oh, I was so frustrated, I’m just learning, I think I should move forward and revisit this level later.

I feel as though my code on this level is doing what I want it to do, but that the swarm is just too fast for the tower to keep up.

Does somebody want to run my code and see?

http://pastebin.com/RvxD2fxY

@chrisman did you also implement the distanceTo() method from the spell dropdown? If not, it won’t be able to correctly find the nearest guy, even if the getNearestEnemy() is correct.

This has happened twice, and I’ve had to restart the whole mission or write a lot of null-checking code in the distanceTo method - basically the error just reads “Cannot read property ‘x’ of undefined” - either meaning I’m not passing in a valid Thang as target - which I don’t think is right… or some funny business is happening.

getNearestEnemy()

var enemies = this.getEnemies();
var nearestEnemy = null;
var nearestDist = 10000;

// TODO: Find the nearest enemy with the distanceTo method.

var cur = null;

for(var e in enemies) {
    cur = this.distanceTo(e);
    
    if( cur < nearestDist) {
        nearestEnemy = e;
        nearestDist = cur;
    }
}

return nearestEnemy;

distanceto(target)

// TODO: Find the distance between this.pos and target.pos.
return Math.sqrt(Math.pow(target.pos.x - this.pos.x) + Math.pow(target.pos.y - this.pos.y));

I haven’t been able complete this puzzle with this code, which I find perplexing. I’ve tried using the debugging hover stuff, and it’s just not working. this.getEnemies(); never yields a result in the debugger, but I can force the tower to attack the first ogre if I assign enemies[0] to nearestEnemy. All the other logic doesn’t seem to affect any outcome.

I’ve also tried using a while loop and an explicit for loop using an integer counter for the index of enemies - that makes the error go away, but the behavior is still not correct (usually the tower just continues random fire).

I’ll tinker with it and update and see where my mistakes where, because I’m fairly certain that this is solvable and these solutions should work… but something somewhere is messed up. Thanks for any feedback!

JavaScript for-in loops over arrays don’t work like one would hope. This part:

for(var e in enemies) {
    cur = this.distanceTo(e);

should be either:

for(var enemyIndex in enemies) {
    var e = enemies[enemyIndex];
    cur = this.distanceTo(e);

or:

for(var enemyIndex = 0; enemyIndex < enemies.length; ++enemyIndex) {
    var e = enemies[enemyIndex];
    cur = this.distanceTo(e);

It’s possible that some other problem happened when you tried the explicit while-loop and for-loop?

I had a lot of trouble with this one =(
Once you add some extra calculations for danger level - javascript runs slower and Big And Brawler kills you.

Hmm, still should be working. If you want to see the solution, you can join my multiplayer game to see what I did (spoiler):

Hey,
I can’t find the bug in my code: http://pastebin.com/Z0b6vJm6
Could someone please have a look? :smiley:
Thanks