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

Kelvintaph Crusader hlp plz

here is my code:

# You can find friends through walls, but not enemies.
# Watch out for smooth, frictionless ice patches!
def moveTo(position, fast=True):
    if (hero.isReady("jump") and fast):
        hero.jumpTo(position)
    else:
        hero.move(position)

def commandTroops():
    for index, friend in enumerate(hero.findFriends()):
        enemies = hero.findEnemies()
        witch = hero.findNearest(hero.findByType('witch'))
        if len(enemies) > 0 and witch:
            if friend.type == 'paladin':
                CommandPaladin(friend)
            else:
                CommandSoldier(friend)
            if (friend.canCast("heal")):
                hero.command(friend, "cast", "heal", friend)
            elif hero.now() < 7:
                hero.command(friend, "move", {'x': 31, 'y': 40})
            elif hero.now() < 12:
                hero.command(friend, "move", {'x': 7, 'y': 40})
            if (worst and friend.pos.x - worst.pos.x < 10):
                hero.command(friend, "move", {'x': 50, 'y': 58})
        elif friend.type == 'archer' and hero.now() < 15:
            while True:
                hero.command(friend, "move", {'x': 49, 'y': 58})
            else:
                hero.command(friend, "move", {'x': 78, 'y': 40})


def CommandPaladin(paladin):
    if (paladin.canCast("heal")):
        target = lowestHealthFriend()
        if target:
            hero.command(paladin, "cast", "heal", target)
    elif (paladin.health < 100):
        hero.command(paladin, "shield")
    else:
        target = findWorstEnemy()
        if (target):
            hero.command(paladin, "attack", target)

def CommandSoldier(soldier):
    target = findWorstEnemy()
    if (target):
        hero.command(soldier, "attack", target)

def findWorstEnemy():
    witch = hero.findNearest(hero.findByType('witch'))
    ogre = hero.findNearest(hero.findByType('ogre'))
    skeleton = hero.findNearest(hero.findByType('skeleton'))
    if witch:
        return witch
    elif ogre:
        return ogre
    elif skeleton:
        return skeleton
    else:
        return None

def lowestHealthFriend():
    lowestHealth = 99999
    lowestFriend = None
    friends = hero.findFriends()
    for friend in friends:
        if friend.health < lowestHealth and friend.health < friend.maxHealth:
            lowestHealth = friend.health
            lowestFriend = friend

    return lowestFriend

def attack(target):
    if target:
        if (hero.distanceTo(target) > 10):
            moveTo(target.pos)
        elif (hero.isReady("bash")):
            hero.bash(target)
        else:
            hero.attack(target)

while True:
    commandTroops()
    brawler = hero.findNearest(hero.findByType('brawler'))
    catapult = hero.findNearest(hero.findByType('catapult'))
    if brawler and hero.distanceTo(brawler) > 15:
        moveTo(brawler.pos, False)
    elif brawler:
        runaway = Vector.subtract(hero.pos, brawler.pos)
        runaway = Vector.normalize(runaway)
        runaway = Vector.multiply(runaway, 15)
        direction = Vector.add(runaway, hero.pos)
        moveTo(direction, False)
    elif catapult:
        attack(catapult)
    else:
        hero.move({'x': 78, 'y': 15})

the error says: “else: statment for whiile unsupported”
here is a link to the lvl: https://codecombat.com/play/level/kelvintaph-crusader?
thanks for the help

btw, idk what line the error is on

Maybe else doesn’t fits while True here?

so what should i change the else to?

just looked at it and i dont rly understand it…

do i need progimation 5 for this lvl? is so i have progimation 4 on right now.

ok so i got it to work but my soldiers just die here is my new code:

# You can find friends through walls, but not enemies.
# Watch out for smooth, frictionless ice patches!
def moveTo(position, fast=True):
    if (hero.isReady("jump") and fast):
        hero.jumpTo(position)
    else:
        hero.move(position)

def commandTroops():
    for index, friend in enumerate(hero.findFriends()):
        enemies = hero.findEnemies()
        witch = hero.findNearest(hero.findByType('witch'))
        if len(enemies) > 0 and witch:
            if friend.type == 'paladin':
                CommandPaladin(friend)
            else:
                CommandSoldier(friend)
            if (friend.canCast("heal")):
                hero.command(friend, "cast", "heal", friend)
            elif hero.now() < 7:
                hero.command(friend, "move", {'x': 31, 'y': 40})
            elif hero.now() < 12:
                hero.command(friend, "move", {'x': 7, 'y': 40})
            if (worst and friend.pos.x - worst.pos.x < 10):
                hero.command(friend, "move", {'x': 50, 'y': 58})
            while True:
                hero.command(friend, "move", {'x': 49, 'y': 58})
                hero.command(friend, "move", {'x': 78, 'y': 40})


def CommandPaladin(paladin):
    if (paladin.canCast("heal")):
        target = lowestHealthFriend()
        if target:
            hero.command(paladin, "cast", "heal", target)
    elif (paladin.health < 100):
        hero.command(paladin, "shield")
    else:
        target = findWorstEnemy()
        if (target):
            hero.command(paladin, "attack", target)

def CommandSoldier(soldier):
    target = findWorstEnemy()
    if (target):
        hero.command(soldier, "attack", target)

def findWorstEnemy():
    witch = hero.findNearest(hero.findByType('witch'))
    ogre = hero.findNearest(hero.findByType('ogre'))
    skeleton = hero.findNearest(hero.findByType('skeleton'))
    if witch:
        return witch
    elif ogre:
        return ogre
    elif skeleton:
        return skeleton
    else:
        return None

def lowestHealthFriend():
    lowestHealth = 99999
    lowestFriend = None
    friends = hero.findFriends()
    for friend in friends:
        if friend.health < lowestHealth and friend.health < friend.maxHealth:
            lowestHealth = friend.health
            lowestFriend = friend

    return lowestFriend

def attack(target):
    if target:
        if (hero.distanceTo(target) > 10):
            moveTo(target.pos)
        elif (hero.isReady("bash")):
            hero.bash(target)
        else:
            hero.attack(target)

while True:
    commandTroops()
    brawler = hero.findNearest(hero.findByType('brawler'))
    catapult = hero.findNearest(hero.findByType('catapult'))
    if brawler and hero.distanceTo(brawler) > 15:
        moveTo(brawler.pos, False)
    elif brawler:
        runaway = Vector.subtract(hero.pos, brawler.pos)
        runaway = Vector.normalize(runaway)
        runaway = Vector.multiply(runaway, 15)
        direction = Vector.add(runaway, hero.pos)
        moveTo(direction, False)
    elif catapult:
        attack(catapult)
    else:
        hero.move({'x': 78, 'y': 15})

I’m just curious, what is this part for?
moveTo(direction, *False*)

Hm, thanx. I used to write only something like hero.move(moveToPos) and it works fine. As far as I often need to write as less as possible I didn’t even think to put anything else there.

Oh my. I tried it and got several errors.

My solution for this lvl was larger and more complicated I think. But in the end it looks like this:

while True:
    heroMove()
    friendCommand()

My tip:

let your archers and paladin to kill the witch first, then let all of your troops attack their nearest enemy. If soldier.health is lower than some number, move it beside the archers. When you finished killing all of them, use the flags to move them to the end. Also, try use cast chain lightning to kill one ogre first.

1 Like

i need it there or else my hero just goes through the ogres and gets demolished

and how do u make a soldier go for a specific thing?

I used time to define what my troops should do. As for moving it was by
hero.command(friend, "move", {"x": xx,"y": yy})

are the 2 x’s the corrdinates of where the enemy is? @Alexbrand

and where would i put it in the code…

so i changed the code a little bit…

# You can find friends through walls, but not enemies.
# Watch out for smooth, frictionless ice patches!
def moveTo(position, fast=True):
    if (hero.isReady("jump") and fast):
        hero.jumpTo(position)
    else:
        hero.move(position)

def commandTroops():
    for index, friend in enumerate(hero.findFriends()):
        enemies = hero.findEnemies()
        witch = hero.findNearest(hero.findByType('witch'))
        if len(enemies) > 0 and witch:
            if friend.type == 'paladin':
                CommandPaladin(friend)
            else:
                CommandSoldier(friend)
            if (friend.canCast("heal")):
                hero.command(friend, "cast", "heal", friend)
                hero.command(friend, "move", {'x': 31, 'y': 40})
                hero.command(friend, "move", {'x': 7, 'y': 40})
            worst = findWorstEnemy()
            if (worst and friend.pos.x - worst.pos.x < 10):
                hero.command(friend, "move", {'x': 50, 'y': 58})
                hero.command(friend, "move", {'x': 6, 'y': 58})
            else:
                hero.command(friend, "move", {'x': 49, 'y': 58})
            hero.command(friend, "move", {'x': 50, 'y': 39})
        else:
            hero.command(friend, "move", {'x': 78, 'y': 40})


def CommandPaladin(paladin):
    if (paladin.canCast("heal")):
        target = lowestHealthFriend()
        if target:
            hero.command(paladin, "cast", "heal", target)
    elif (paladin.health < 100):
        hero.command(paladin, "shield")
    else:
        target = findWorstEnemy()
        if (target):
            hero.command(paladin, "attack", target)

def CommandSoldier(soldier):
    target = findWorstEnemy()
    if (target):
        hero.command(soldier, "attack", target)

def findWorstEnemy():
    witch = hero.findNearest(hero.findByType('witch'))
    ogre = hero.findNearest(hero.findByType('ogre'))
    skeleton = hero.findNearest(hero.findByType('skeleton'))
    if witch:
        return witch
    elif ogre:
        return ogre
    elif skeleton:
        return skeleton
    else:
        return None

def lowestHealthFriend():
    lowestHealth = 99999
    lowestFriend = None
    friends = hero.findFriends()
    for friend in friends:
        if friend.health < lowestHealth and friend.health < friend.maxHealth:
            lowestHealth = friend.health
            lowestFriend = friend

    return lowestFriend

def attack(target):
    if target:
        if (hero.distanceTo(target) > 10):
            moveTo(target.pos)
        elif (hero.isReady("bash")):
            hero.bash(target)
        else:
            hero.attack(target)

while True:
    commandTroops()
    brawler = hero.findNearest(hero.findByType('brawler'))
    catapult = hero.findNearest(hero.findByType('catapult'))
    if brawler and hero.distanceTo(brawler) > 15:
        moveTo(brawler.pos, False)
    elif brawler:
        runaway = Vector.subtract(hero.pos, brawler.pos)
        runaway = Vector.normalize(runaway)
        runaway = Vector.multiply(runaway, 15)
        direction = Vector.add(runaway, hero.pos)
        moveTo(direction, False)
    elif catapult:
        attack(catapult)
    else:
        hero.move({'x': 78, 'y': 15})

my troops dont attack anymore why they just try to go to the location at the end… why?

@Deadpool198 can you help figure out what is wrong?

Throughout your code you have a big mistake. This is the first comment on the level:

# You can find friends through walls, but not enemies.

Well. You haven’t exactly stuck to that. You need to find a friend, then use friend.findEnemies() and use functions you make yourself to find specific types.
Danny

what do u mean @Deadpool198?