Play Home Contribute Github Join Us! Discourse Staff Members Github Contribution Guides Team

Clash of Clones Help!

Ok, I’ll try. (20 characters)

1 Like

I can’t get the flags to work, this is my code for flags:

while True:
    flag = hero.findFlag()
    if flag:
        flagPos = flag.pos
        flagX = flagPos.x
        flagY = flagPos.y
        flag = hero.findFlag("green")
    self.moveXY(flagX, flagY)
    self.pickUpFlag(flag)

ok so I did do what you ask but I doesn’t work still, here is my full code (NO FLAG CODE INCLUDED):

while True:
    enemyIndex = 0
    enemies = self.findNearestEnemy()
    while enemyIndex < len(enemies):
        enemy = enemies[enemyIndex]
        enemyIndex += 1
        if enemy.type != 'sand-yak':
            while enemy.health > 0:
                if self.isReady("bash"):
                    hero.bash(enemy)
                if self.isReady("cleave"):
                    hero.cleave(enemy)
                if self.isReady("build"):
                    hero.buildXY("bear-trap", enemy.pos.x, enemy.pos.y)
                else:
                    self.attack(enemy)
    if self.health <= self.maxHealth:
        enemy = hero.findNearestEnemy()
        distance=hero.distanceTo(enemy)
        while enemy.health > 0 :
            if distance < 4:
                if self.isReady("cleave"):
                    self.cleave(enemy)
                if self.health < self.maxHealth / 3:
                    hero.shield()
                else:
                    self.attack(enemy)
            else:
                self.attack(enemy)
            if self.health < self.maxHealth / 5:
                hero.shield()

This is the first error I’ve found, this may be causing all the problems. You use self.findNearestEnemy(), that will only find you one enemy: the nearest one. You want a command which will find you an array of multiple enemies. (findEnemies()…)
Ah I’ve just seen where you went from that to nearest. Sorry for the misunderstanding.

I meant you should just use if enemy:, with the nearest enemy. You really don’t need to loop through enemies to attack them. There’s no need. Just find the nearest, check if it exists, then run the rest of your attack code.
Danny

Helps A LOT But it is still not working, Code:

while True:
    enemyIndex = 0
    enemies = self.findEnemies()
    while enemyIndex < len(enemies):
        enemy = enemies[enemyIndex]
        enemyIndex += 1
        if enemy.type != 'sand-yak':
            while enemy.health > 0:
                if self.isReady("bash"):
                    hero.bash(enemy)
                if self.isReady("cleave"):
                    hero.cleave(enemy)
                if self.isReady("build"):
                    hero.buildXY("bear-trap", enemy.pos.x, enemy.pos.y)
                else:
                    self.attack(enemy)
    if self.health <= self.maxHealth:
        enemy = hero.findNearestEnemy()
        distance=hero.distanceTo(enemy)
        while enemy.health > 0 :
            if distance < 4:
                if self.isReady("cleave"):
                    self.cleave(enemy)
                if self.health < self.maxHealth / 3:
                    hero.shield()
                else:
                    self.attack(enemy)
            else:
                self.attack(enemy)

(P.S.) now I have 632 gems from daily stuff.

Like I said, you don’t need if hero.isReady()
I find using for loops much easier for enemies or friends, I don’t know if you can use for loops but try it out! I think it is much easier than enemyIndex.
Lydia

Well, you haven’t actually done what I said yet.
Why this:

Instead of this:

enemy = hero.findNearestEnemy()
if enemy:

You code attacks enemies in a specific order which is not good for tactics. Just find the nearest and use that. No loops necessary except for the while True!

Where do you put that? I can’t if a place that says enemy = hero.findNearestEnemy() if enemy:.

talk later, I got to go to online class.

I think you may be overthinking this a little bit. For some reason you’re using a loop to attack. That hasn’t been taught and is not very successful. The traditional way of attacking is finding the nearest enemy. Checking if they exist, then doing stuff like:

if hero.isReady("cleave", enemy):
    hero.cleave(enemy)
else:
    hero.attack(enemy)

Look at earlier levels in the forest if you’re struggling with that.
Danny

Sure (20 characters)