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

Kelvintaph Crusader Help Codecombat

Code
while True:
    command()
    move()
def command():
    friends = hero.findFriends()
    witches = hero.findByType("witch")
    targets = hero.findEnemies()
    for witch in witches:
        for friend in friends:
            for target in targets:
                if witch:
                    hero.command(friend, "attack", witch)
                elif target.type != "brawler" and target.health > 0:
                    hero.command(friend, "attack", target)
def move():
    hero.moveXY(69, 14)
    hero.wait(2)
    hero.moveXY(15, 14)
Link

https://codecombat.com/play/level/kelvintaph-crusader

My code runs like this. I move to make the catapults kill the brawlers and themselves, but after my troops kill the witch, they just stop attacking. Help?

This is quite a confusing structure. A triple for loop will not work very well.
Remember that the second and third for loops will only run when the first for loop runs. The first for loop will only run when there are witches. So if there are no witches will it run?
Danny

2 Likes

Hmmmm, I never thought about that. Now that I’ve changed my code, it does the same thing. The troops kill the witch and then just stand still. Here it is.

while True:
    command()
    move()
def command():
    friends = hero.findFriends()
    witch = hero.findNearest(hero.findByType("witch"))
    for friend in friends:
        enemy = friend.findNearestEnemy()
        if witch:
            hero.command(friend, "attack", witch)
        elif enemy.type != "witch":
            hero.command(friend, "attack", enemy)
def move():
    hero.moveXY(69, 14)
    hero.wait(2)
    hero.moveXY(15, 14)

try changing the elif to an if

Nope. It then just targets the nearest enemy and not the witch.

Why doesn’t this work?

while True:
    command()
    move()
def command():
    friends = hero.findFriends()
    witch = hero.findNearest(hero.findByType("witch"))
    for friend in friends:
        enemy = friend.findNearestEnemy()
        if witch:
            hero.command(friend, "attack", witch)
        else:
            hero.command(friend, "attack", enemy)
def move():
    hero.moveXY(69, 14)
    hero.wait(2)
    hero.moveXY(15, 14)

Hi CryptiCrystal,

I think part of the problem is that you’re using hero.moveXY. While the hero is moving, no other code runs, so your allies tend to just stand there and be killed. Try hero.move instead.

Do you have the Emperor’s Gloves? They’re useful for this level. I found I also needed to be clever about which friend to command to attack which enemy.

Hope that helps.

Jenny

I do have emperor’s gloves, the chain-lightning ones, right?

while True:
    command()
    move()
def command():
    friends = hero.findFriends()
    witch = hero.findNearest(hero.findByType("witch"))
    for friend in friends:
        enemy = friend.findNearestEnemy()
        if witch:
            hero.command(friend, "attack", witch)
        else:
            hero.command(friend, "attack", enemy)
def move():
    hero.move({'x': 69, 'y': 14})
    hero.wait(2)
    hero.move({'x': 15, 'y': 14})

Well, this doesn’t work, because then I move forward a little, then again forward a little, then I die.

Chain lightening works through the walls, so you can help your friends…

It took me 133 lines of code to solve this with the bonus. Admittedly my code isn’t the nicest - I’m sure there’s plenty of people who have done it with fewer lines (and theres a function in there that I didn’t use)! But keep playing with different ways to get different characters doing different things.

Jenny

Yes I did notice that chain lightning goes through walls. I didn’t test that until now. I think that will help :slight_smile:

I’m So CLOSE. I can’t figure out how to make the hero go to the X when the catapults are dead. I’ll show my code.

while True:
    command()
    move()
def command():
    friends = hero.findFriends()
    witch = hero.findNearest(hero.findByType("witch"))
    for friend in friends:
        enemy = friend.findNearestEnemy()
        if witch:
            if friend.type == 'paladin' and not witch:
                hero.command(friend, "shield")
            else:
                hero.command(friend, "attack", witch)
        else:
            if enemy:
                hero.command(friend, "attack", enemy)
            else:
                hero.command(friend, "move", {'x': 78, 'y': 40})
def move():
    enemy = hero.findNearestEnemy()
    catapults = hero.findByType("catapult")
    if hero.isReady("chain-lightning") and enemy:
        hero.cast("chain-lightning", enemy)
    else:
        hero.move({'x': 68, 'y': 14})
        if hero.pos.x == 68 and hero.pos.y == 14:
            for catapult in catapults:
                if catapult:
                    hero.wait(1)
                    hero.moveXY(11, 14)
                elif catapult.health <= 0:
                    hero.moveXY(78, 14)

I havent done this level yet but you could do

if enemy,health < 0:
#move to the x