Clash of Clones, Javascript {SOLVED}

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:

The main question I have is with the first line:

var index = 0;

Do I really need the var? When I remove it, the error box tells me “Missing var. Use var index = to make a variable.”

I think CodeCombat is running in JavaScript “strict mode”:

The "use strict" directive was new in ECMAScript version 5.
It is not a statement, but a literal expression, ignored by earlier
versions of JavaScript. The purpose of "use strict" is to indicate
that the code should be executed in "strict mode". With strict 
mode, you can not, for example, use undeclared variables.
All modern browsers support "use strict" except Internet Explorer 9 and lower:

If your python code is successful I don’t think you rewrote it right in JS. Try to follow the logic of your original code and use the same formatting as in python.

    if (index < archers.length) {
        while (index < archers.length) {
            index += archers;
        }
    }

here you get the archers[archers.length] warrior - why are you doing this?

Originally, I was planning to use

for (archer in archers) {

when I used this statement, I decided to do something else, and I never deleted the

if (index < archers.length) {
    while (index < archers.length) {
            index += archers;
    }
}

I will delete it now since it’s essentially a useless line of code. And thank you for responding so quickly!!!

Would someone please compare this code and see if I translated it from Python to JS correctly? It would be very appreciated!!!

Python:


def findArcher(enemies):
    for enemy in enemies:
        if enemy.type == "archer":
            return enemy
while True:
    enemies = hero.findEnemies()
    archer = findArcher(enemies)
    if archer:
        if self.isReady("bash"):
            hero.bash(archer)
        else:
            while archer:
                archer = findArcher(enemies)
                hero.shield()
                if self.isReady("bash"):
                    hero.bash(archer)

JavaScript:

function findArcher(enemies) {
    for (var enemy in enemies) {
        if (enemy.type == "archer") {
            return enemy;
        }
    }
}
while (true) {
    var enemies = hero.findEnemies();
    var archer = findArcher(enemies);
    if (archer) {
        if (hero.isReady("bash")) {
            hero.bash(archer);
        } else {
            while (archer) {
                hero.shield();
                if (hero.isReady("bash")) {
                    hero.bash(archer);
                }
            }
        }
    }
}

One last thing - I’m not getting any errors with the JavaScript code, but my hero won’t do anything at all, so there must be some other issue. Thanks to anyone who can tell me how I translated it incorrectly!

# python
            while archer:
                archer = findArcher(enemies)
                hero.shield()
// js
            while (archer) {
                // missing statement
                hero.shield();