Clash of Clones, Javascript {SOLVED}

This is my code

while (true) {
    var enemy = hero.findNearestEnemy();
    if (enemy && enemy.health > 0) {
        if (hero.health < hero.health / 4) {
            hero.shield(enemy);
        } else if (hero.isReady) {
            hero.bash(enemy);
        }
    }
}

My hero will keep bashing until it died

“else if (hero.isReady)” to do what?

// You'll need good strategy to win this one!
// Your clone will have the same equipment you have!
// But, they're not very skilled at using special powers.
while (true) {
    while (hero.health > hero.health / 4) {
        var enemy = hero.findNearestEnemy();
        if (enemy && enemy.health > 0) {
            if (enemy && hero.isReady("cleave")) {
                hero.cleave(enemy);
            } else if(hero.isReady("bash")){
                hero.bash(enemy);
            }
            else {
                hero.attack(enemy);
            }
        }
    }
}

How do I let my hero run away after it’s health is lower then 100

@SuperSmacker

if hero.health < 100 {
hero.moveXY…

That code wouldn’t let u beat the clone tho.

I suggest buying emperor’s gloves, go behind the enemy’s archers and chain-lightning them. Then let your archers and soldiers do the rest.

I don’t have any gems to buy them, how do I get them @SuperSmacker?

Do more replayable levels, do some previous levels that u didn’t finish, or jump ahead to the mountains (kind of cheating but u can use codecombat.com/play/level/(level name here) to access any level. :stuck_out_tongue: This also allows you to bypass the “Restricted Equipment” check.

Thanks! What are replayable levels?

Dungeon Brawl, Backwoods Brawl, Backwoods Treasure, Sarven Brawl, Sarven Treasure, Sarven Siege, and more. You can play them over and over again. Each time you beat the level, you get more gems and exp but the level gets harder.

1 Like

Levels like sarven brawls

Any way I can pass the level without buying those gloves?

This is my inventory:


image

About gems see this How to get GEMS, Stuck in Sarven Desert
the while loop you have is some kind of while True loop
If you have 800 health -> 800>200 then 400>100 then… you die

 while (hero.health > hero.health / 4)

replace with

hero.health > hero.maxHealth / 4:
1 Like

@xython good catch didn’t see that

You can pass every level with this equipment:


/ I never use flags/

My Mac can see it. :wink:

Oh wait nevermind I can see it now. Internet lag b4. Wasn’t loading.

I dont have any pets…

@BrendGroen that’s needed for like Arrowproof Wolf level and stuff. Not needed for clash of clones.

@xython btw sometimes griffin pet or pugicorn is needed instead of the wolf pet

Stuck on Clash of Clones. New day - new problem lol. So I really don’t want to use flags or kill enemies or do an array of teleports, I want to understand why my while loops (with crosses) here don’t work(I know it’s quite lengthy). I also see that the positions and teleports are buggy… Is there a way of beating the level with my idea?

appreciate any help.

hero.moveXY(35, 42);
while (hero.gold < 150) {
    var item = hero.findNearestItem();
    var enemy = hero.findNearestEnemy();
    var cross1 = {
            x: 76,
            y: 51
        };
    var cross2 = {
            x: 76,
            y: 19
        };
    var cross3 = {
            x: 5,
            y: 49
        };
    var cross4 = {
            x: 6,
            y: 19
        };
    if (enemy) {
        var heroX = hero.pos.x;
        var heroY = hero.pos.y;
        var enemyX = enemy.pos.x;
        var enemyY = enemy.pos.y;
        if (enemy && hero.distanceTo(enemy) < 20 && enemyX < heroX && enemyY < heroY) {
            while (hero.pos.x !== cross1.x && hero.pos.y!== cross1.y) {
                hero.move(cross1);
            }
        } else if (enemy && hero.distanceTo(enemy) < 20 && enemyX < heroX && enemyY > heroY) {
            while (hero.pos.x !== cross2.x && hero.pos.y== cross2.y) {
                hero.move(cross2);
            }
        } else if (enemy && hero.distanceTo(enemy) < 20 && enemyX > heroX && enemyY < heroY) {
            while (hero.pos.x !== cross3.x && hero.pos.y!== cross1.y) {
                hero.move(cross3);
                break;
            }
        } else if (enemy && hero.distanceTo(enemy) < 20 && enemyX > heroX && enemyY > heroY) {
            while (hero.pos.x !== cross4.x && hero.pos.y!== cross4.y) {
                hero.move(cross4);
            }
        }
    } else {
        hero.say("no enemies");
    }
    if (item && hero.distanceTo(item) < 10) {
        hero.moveXY(item.pos.x, item.pos.y);
    }
}

Hi! I am also having issues with this level. I already completed it in Python, but I started to work with JavaScript. I modified the code from the Python code I used the first time I beat the level, but it still gives me an error “Code never finished. It’s either really slow or has an infinite loop.” Any help would be greatly appreciated.

Here’s my code:

var index = 0;

while (true) {
    var archers = hero.findByType("archer", hero.findEnemies());
    if (index < archers.length) {
        while (index < archers.length) {
            index += archers;
        }
    }
    var archer = archers[index];
    if (archer) {
        if (hero.isReady("bash")) {
            hero.bash(archer);
        } else {
            while (archer) {
                hero.shield();
                if (hero.isReady("bash")) {
                    hero.bash(archer);
                }
            }
        }
    }
}

Here’s my equipment: