The Dunes (JS) - Hero on union break before victory

In this code I’m using my Senick Steelclaw (has a throw method), but I ran into the problem with my normal Hero Sir Tharin Thunderfist using “cleave” as well.

The code seems to be working for me up until I kill the final enemy, an Ogre. Then, he just stands there not picking up coins for the last quarter of the countdown: adding in a !enemy didn’t seem to make any difference.

Since I’m not getting any errors, I’m assuming I have a logic error. Could anyone point out where I went wrong?

// Collect coins. Ignore sand yaks and burls. Fight throwers and ogres.
loop {
    var enemy = this.findNearestEnemy();
    var item = this.findNearestItem();
    if (enemy) {
        if (enemy.type == "sand-yak" || enemy.type == "burl") {
            
            var position = item.pos;
            var x = position.x;
            var y = position.y;
            this.moveXY(x, y);   
            // Don't fight sand yaks or burls! Just keep collecting coins.
            
        }
        // But if the enemy is type "thrower" or "ogre", attack them.
        else if (enemy.type == "thrower" || enemy.type == "ogre") {
            if (this.isReady("throw") && this.distanceTo(enemy) < this.throwRange) {
                this.throw(enemy);
            }else {
                this.attack(enemy);
            }
                
        } else if (!enemy) {
            // Collect coins.
            if (item) {
                position = item.pos;
                x = position.x;
                y = position.y;
                this.moveXY(x, y);
            }
            }
        }
    
    }

Thanks for any assist!

1 Like

You have one big logic section that’s checking to see if an enemy exists. Inside that, you’re checking to see if an enemy no-longer exists, to begin collecting coins. So you’re checking to see if an enemy exists, and no longer exists, but there is no logic for checking is there are no enemies.

1 Like

Thanks Serg,

You hit the nail on the head. I didn’t see that my scope was all wrong. After I moved the <!enemy> condition outside the <if(enemy)>, and changed it from to a regular , it worked fast and clean.