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

"The one wizard" code never finishes

Hi everyone. So I thought I had solved the level “The one wizard”. Ran the code, killed all the bad guys and everything. Then I tidied up my code by removing some hero.say statements that I had put in for de-bugging. Now the code won’t run through, and I can’t figure out why. What’s worse is it freezes at different points, so I can’t identify the problematic bit of code.

# Defeat as many ogres as you can.
# Use 'cast' and 'canCast' for spells.
enemy = hero.findNearestEnemy()
while True:
    while enemy: #while there are enemies
        if enemy.type == "ogre":
            hero.moveXY(5, 31)
        else:
            if hero.distanceTo(enemy) > 30: #if it's beyond wand range cast litng-bolt
                if hero.canCast("lightning-bolt", enemy):
                    hero.cast("lightning-bolt", enemy)
            else: # otherwise use chain-lightning if it's ready
                if hero.canCast("chain-lightning", enemy):
                    hero.cast("chain-lightning", enemy)
                else: # otherwise use your wand
                    hero.attack(enemy)
        enemy = hero.findNearestEnemy() # check if there are any more enemies
    if hero.canCast("regen", hero): # if there are no enemies, cast regen
        hero.cast("regen", hero)
    enemy = hero.findNearestEnemy() #check again for new enemies

So I’ve managed to replicate my success by trail-and-error, adding in some hero.say statements, which seems to slow things down enough to prevent the program freezing. The only difference in the code is the added hero.say statements, so I think this might be a bug. Here’s the successful code:

# Defeat as many ogres as you can.
# Use 'cast' and 'canCast' for spells.
enemy = hero.findNearestEnemy()
while True:
    while enemy: #while there are enemies
        if enemy.type == "ogre":
            hero.moveXY(5, 31)
        else:
            if hero.distanceTo(enemy) > 30: #if it's beyond wand range cast litng-bolt
                if hero.canCast("lightning-bolt", enemy):
                    hero.cast("lightning-bolt", enemy)
                    hero.say("lightning")
            else: # otherwise use chain-lightning if it's ready
                if hero.canCast("chain-lightning", enemy):
                    hero.cast("chain-lightning", enemy)
                    hero.say("zzaap")
                else: # otherwise use your wand
                    hero.attack(enemy)
        enemy = hero.findNearestEnemy() # check if there are any more enemies
    hero.say("no enemies")
    if hero.canCast("regen", hero): # if there are no enemies, cast regen
        hero.cast("regen", hero)
    enemy = hero.findNearestEnemy() #check again for new enemies

Can anyone tell me what is going on here?

The reason you’re code is freezing is because of an “infinite loop” in the game, which, in reality, is just a very slow/inefficient one.
Codecombat can’t really deal with these kinds of while loops. And you do already have the code in a while True loop, so why use while enemy?
if worked fine for me (using your code).
In theory, your code does work but Codecombat doesn’t really like it when you do while someObject it gets a bit confused.
So you might just have to use if.
Thanks
Danny

Thanks Danny.

The while enemy: loop is there to identify an appropriate time to cast the regen spell (i.e. once the enemies are cleared). But I’ll try it without the second while loop, and see if that makes a difference.

John

1 Like

i had the same problem but i solved it anyways good luck john

@middleagedspread The coordinates for the Button to activate the fire traps is 5, 33. Change that and you will get the 44 ogres mark.

Jonathan