[SOLVED] Clash of clones Javascript

Hi, I’ve been trying lots of different things (without flag, i would like to find specifically a solution of passive win) and i feel like im starting to scrabmle lots of stuff and my brain is becoming mashed potato, pls help ^^

function killArcher() {
    i = 0;
    while (i < archers.length) {
        archer = archers[i];
        if (archer && archer.health > 0) {
            hero.throw(archer);
            hero.attack(archer);
        }
        i++;
    }
}
var enemy = hero.findNearestEnemy();
var enemies = hero.findEnemies();
var archers = hero.findByType("archer", hero.findEnemies());
var self = hero.findByType("ninja");
hero.jumpTo({
    x: 65,
    y: 70
});
hero.moveXY(78, 70);
while (true) {
    while (enemies[self] && enemies[self].health > 0) {
        if (hero.isReady("chain-lightning")) {
            hero.cast("chain-lightning", self);
        } else {
            if (hero.isReady("throw") && hero.distanceTo(self) < hero.throwRange) {
                hero.throw(self);
            } else {
                hero.attack(self);
            }
        }
    }
    while (enemies[archers] && enemies[archers].health > 0) {
        killArcher();
    }
    if (enemy && enemy.health > 0) {
        if (hero.isReady("chain-lightning")) {
            hero.cast("chain-lightning", enemy);
        } else {
            hero.throw(enemy);
            hero.attack(enemy);
        }
    }
}

after passing the first enemy line for some reason hero chain lightenings the enemies behind and not focusing on self > archers> in the end only closest enemy

  • because gear is low & i get chop chopped fastu (I use low gear for my clone to be weak too)

Here you only need self, not with the enemies part.

Here you make self an array, but you want a specific element of it (like array[0] that gives you the first element of the array called array).

You should firstly kill the archers in my opinion, and after the archers kill your clone.

Andrei

hi i only now set to it again :c i think my function is not good because after the hero jumps to center and cast lightning it stops and does nothing until it dies

Javascript

function killArcher() {
    i = 0;
    while (i < archers.length) {
        archer = archers[i];
        if (archer && archer.health > 0) {
           if (hero.isReady("chain-lightning")) {
            hero.cast("chain-lightning", archers);
          }  else {
              if (hero.isReady("throw") && hero.distanceTo(archer) < hero.throwRange) {
                hero.throw(archer);
            } else {
                hero.attack(archer);
            }
        }
        i++;
    }
}}
var enemy = hero.findNearestEnemy();
var enemies = hero.findEnemies();
var archers = hero.findByType("archer", hero.findEnemies());
var self = hero.findByType("ninja");
hero.jumpTo({
    x: 68,
    y: 70
});
while (true) {
        while (enemies[archers] && enemies[archers].health > 0) {
        killArcher();
    }
    while (enemies[self] && enemies[self].health > 0) {
        if (hero.isReady("chain-lightning")) {
            hero.cast("chain-lightning", self);
        } else {
            if (hero.isReady("throw") && hero.distanceTo(self) < hero.throwRange) {
                hero.throw(self);
            } else {
                hero.attack(self);
            }
        }
    }

    if (enemy && enemy.health > 0) {
        if (hero.isReady("chain-lightning")) {
            hero.cast("chain-lightning", enemy);
        } else {
            hero.throw(enemy);
            hero.attack(enemy);
        }
    }
}

Here try to attack the archers while archers.length > 0 (there are still more archers to attack).

Here self is the element, so you can only use self wherever you have enemies[self].

Before this try to put that enemy = enemies[0].

Andrei

And you can move this outside the while true loop.

Andrei

1 Like

I made it, cant edit posts title for [solution] and cant edit my comments to remove code (well, dunno if needed since the final code that worked isnt here)
tyvm <3

1 Like

I edited the title for you. And congratulations for completing the level! :partying_face:

Andrei

This topic was automatically closed 12 hours after the last reply. New replies are no longer allowed.