Clash of clones (VERY VERY RAGED BY THIS LEVEL)

in the code here the “enemy” is just from hero.findNearestEnemy() and not from “targets”
also you should name the lists differently otherwise you’re just replacing them

like this?

while True:
    here = hero.findNearestEnemy()
    if here:
        archer = hero.findByType("archer")
        if archer:
            hero.attack(archer)
        else:
            tharin = hero.findByType("tharin")
            if tharin:
                hero.shield()
                ready = hero.isReady("bash")
                if ready:
                    hero.bash(tharin)
                else:
                    hero.shield()
                zap = hero.isReady("electrocute")
                if zap:
                    hero.electrocute(tharin)
            else:
                scout = hero.findnearest(scout)
                if scout:
                    hero.attack(scout)
                else:
                    nearest = hero.findNearest(ogre)
                    if nearest:
                        hero.attack(ogre)
                    
                    

yeah that should work
you can also use elif so you don’t need to nest the if statements

so I kinda did this

def findEnemy():
    enemies = hero.findEnemies()
    nearestArcher = None
    nearestTharin = None
    nearestScout = None
    nearestOgre = None
    nearestOther = None
    archerDistance = 99999
    tharinDistance = 99999
    scoutDistance = 99999
    ogreDistance = 99999
    otherDistance = 99999

    for enemy in enemies:
        if enemy.type == "archer":
            dist = hero.distanceTo(enemy)
            if dist < archerDistance:
                archerDistance = dist
                nearestArcher = enemy
        elif enemy.type == "tharin":
            dist = hero.distanceTo(enemy)
            if dist < tharinDistance:
                tharinDistance = dist
                nearestTharin = enemy
        elif enemy.type == "scout":
            dist = hero.distanceTo(enemy)
            if dist < scoutDistance:
                scoutDistance = dist
                nearestScout = enemy
        elif enemy.type == "ogre":
            dist = hero.distanceTo(-1*enemy)
            if dist < ogreDistance:
                ogreDistance = dist
                nearestOgre = enemy
        else:
            dist = hero.distanceTo(enemy)
            if dist < otherDistance:
                otherDistance = dist
                nearestOther = enemy

    if nearestArcher:
        return nearestArcher
    elif nearestTharin:
        return nearestTharin
    elif nearestScout:
        return nearestScout
    elif nearestOgre:
        return nearestOgre
    elif nearestOther:
        return nearestOther
    else:
        return None

movePoints = [
    {"x": 20, "y": 20},
    {"x": 60, "y": 20},
    {"x": 60, "y": 40},
    {"x": 20, "y": 40},
    {"x": 40, "y": 30}
]

movePointIndex = 0

while True:
    enemy = findEnemy()
    currentMovePoint = movePoints[movePointIndex]

    hero.say(currentMovePoint) #prints the location the hero is moving to.
    if hero.distanceTo(currentMovePoint) < 10: #increased distance check
        movePointIndex = (movePointIndex + 1) % len(movePoints)

    hero.moveXY(currentMovePoint["x"], currentMovePoint["y"])

    if enemy:
        if hero.isReady("electrocute"):
            hero.electrocute(enemy)
        elif hero.isReady("bash"):
            hero.bash(enemy)
        elif hero.isReady("attack"):
            hero.attack(enemy)
        elif hero.isReady("shield"):
            hero.shield()
    else:
        if hero.isReady("shield"):
            hero.shield()

its not working
20 charachters

What part is not working?
(also you’re always ready to attack or shield)
some of the code you have will likely make your hero just be moving instead of attacking

You also had one syntax error in findEnemy()

dist = hero.distanceTo(-1*enemy)

You have some repetitive code here. I see you are trying to implement a method that returns the optimal enemy to fight, but this way is very confusing.

Your code:

    for enemy in enemies:
        if enemy.type == "archer":
            dist = hero.distanceTo(enemy)
            if dist < archerDistance:
                archerDistance = dist
                nearestArcher = enemy
        elif enemy.type == "tharin":
            dist = hero.distanceTo(enemy)
            if dist < tharinDistance:
                tharinDistance = dist
                nearestTharin = enemy
        elif enemy.type == "scout":
            dist = hero.distanceTo(enemy)
            if dist < scoutDistance:
                scoutDistance = dist
                nearestScout = enemy
        elif enemy.type == "ogre":
            dist = hero.distanceTo(-1*enemy)
            if dist < ogreDistance:
                ogreDistance = dist
                nearestOgre = enemy

I see you are using the part dist = hero.distanceTo(enemy) for every statement, so consider moving it outside. By the way, float('inf') is the form for infinity, if that is what you meant by setting everything to 99999.
Utilizing the and operator is also a good way to clean up that mess, an example is shown below.

def findEnemy():
    enemies = hero.findEnemies()
    archer = None
    tharin = None
    scout = None
    ogre = None
    other = None

    archerDist = 99999
    tharinDist = 99999
    scoutDist = 99999
    ogreDist = 99999
    otherDist = 99999

    for enemy in enemies:
        # Set the distance to enemy outside, less repetitive
        dist = hero.distanceTo(enemy)
        if enemy.type == "archer" and dist < archerDist:
            archer = enemy
            archerDist = dist
        elif enemy.type == "tharin" and dist < tharinDist:
            tharin = enemy
            tharinDist = dist
        elif enemy.type == "scout" and dist < scoutDist:
            scout = enemy
            scoutDist = dist
        elif enemy.type == "ogre" and dist < ogreDist:
            ogre = enemy
            ogreDist = dist
        elif dist < otherDist:
            other = enemy
            otherDist = dist

    if archer:
        return archer
    elif tharin:
        return tharin
    elif scout:
        return scout
    elif ogre:
        return ogre
    else:
        return other

Furthermore, you do not need to check isReady("attack"), because basic attack are always ready without cooldown, so just attack if other abilities are not ready, and that’s enough.

enemy = findEnemy()
if enemy:
    if hero.isReady("electrocute"):
        hero.electrocute(enemy)
    elif hero.isReady("bash"):
        hero.bash(enemy)
    else:  # attack if none of the abilities are ready
        hero.attack(enemy)
# .shield() is also no-cooldown, no need to check if it is ready
else:
    hero.shield()

I believe that can temporarily clean the code, and when you have more problems, a screenshot alongside with code would be more helpful, thanks.

2 Likes