[SOLVED] Trouble with code - Restless Dead level

Hi!
My character is not moving and I don’t know why. I thought that everything is good. Can someone tell me what is wrong with my code? :smiley: I tried to put simple coordinates to my “move” instead of putting array, but it didn’t work too.

var points = [{'x': 57, 'y': 11}, {'x': 50, 'y': 38}, {'x': 19, 'y': 40}, {'x': 51, 'y': 44}];

function chooseStrategy () {
    var enemies = hero.findEnemies();
    var items = hero.findItems();
    
    if (enemies) {
        return "fight";
    }
    else if (items) {
        return "collect";
    }
        return "move";
}

function attack (enemies) {
    var enemy = hero.findNearest(enemies);
    if (enemy) {
        while(enemy.health > 0) {
            hero.attack(enemy);
        }
    }
}

function pickUpCoins (items) {
    while(items) {
        var item = hero.findNearest(items);
        hero.moveXY(item.pos.x, item.pos.y);
    }
}


while(true) {
    var enemies = hero.findEnemies();
    var items = hero.findItems();
    
    var strategy = chooseStrategy;
    if (strategy == "fight") {
        attack(enemies);
    } 
    else if (strategy == "collect") {
        pickUpCoins(items);
    }
    else if (strategy == "move") {
        hero.moveXY(points[0].x, points[0].y);
        points.shift();
    }
}

Hi @cloud2012, welcome to the CodeCombat Discourse, Firstly, I’d like to say I like your tactic very much it’s pretty cool. :grin:
I think the reason your code is not working might be a few things:

I’m not that experienced in javascript, (i did codecombat in python) but isn’t there supposed to be an else?
Apart from that it could be your while loops slowing down your code:

I don’t normally use while loops like this because they always seem to cause ‘infinite’ loops (they’re not really infinite, I think codecombat just gets a bit confused)
And this one, which I think it the actual cause of the problem:

This is bound to cause a problem, your doing this while items exist, but then you only collect the nearest one and… I literally can’t explain for some reason! Maybe someone else can help, but what I’m saying is from my experience you shouldn’t use while ‘array’, use if item, it will work just as well.
Danny

There are many hard traceable errors of the same nasty kind :thinking:
See hero.findEnemies() Quiz

Thank you for advice, but I removed everything but “return move” from “chooseStrategy” function. Then I assigned a variable with “move” and if this variable == “move” then hero.moveXY and I put there one available point. It still don’t work D: It’s just ignoring whole variable and function.

    if (enemies) // error 
    else if (items)  // error
    while(items) // error
    var strategy = chooseStrategy;    // error

Could you post your new code?

Thanks :smiley: var strategy = chooseStrategy; didn’t cause the problem, but the rest did. I removed if (enemies) and while(items) and it works. Thank you guys for help!

I found the solution. Thank you for advice :smiley:

1 Like