[SOLVED] How to write a loop-type method that will attack an enemy until they are dead

Hi All,

I am sharing how I solved a coding problem.
I was playing level 48 Kithguard Dungeon - Attack Wisely and I encountered a problem.
The problem was that I was unable to code a loop attack to attack an enemy until it was dead. I was using the ranged mage hero Omarn.

I could have bought a weapon that did more damage in order to code fewer
’hero.attack(enemy)’’ but I wanted to find a way to create a more beautiful and efficient code.

How I started troubleshooting -
My goal was to write a code that would attack the enemy and if the enemy died, then I would move onto the next area and defeat the next enemy.

Thinking Process -

  1. [CODE] Attack enemy until enemy dead [CODE]
  2. [CODE] Movement to Next Area [CODE]

I knew I had to loop an attack until the enemy died. However once the enemy was dead, the loops I wrote would continue running in order to find an enemy to attack.

I tried to use an if-else method. Similar to the one below and then I realised that my understanding of the if-else method was incorrect. The else statement will function only if the if statement is false, so it would forever run the loop because it is true.

if enemy:
    while True:
        hero.attack(enemy)
else:
    hero.moveDown(2)

I then tried an if - if not method and then realised again, my understanding of the concept was incorrect so I couldn’t use the if-if not method.

So then I had an ‘aha’ moment, what if I attacked based on the enemy’s health?
I was playing around with the while - True method and enemy.health properties however, the syntax was incorrect.

I then went to the forum and searched for an answer.
[Help request for possible bug with "Attack Wisely" (Kithgard Dungeon) [Python]]

User Hinkle, provided the exact answer I was looking for.
He suggested the following loop which worked perfectly.

while enemy.health > 0:
    self.attack(enemy)

The code worked and I was able to use my ranged hero to complete the mission without using many hero.attack(enemy) methods.

This was the code I used to beat the level.

hero.moveUp(2);
DoorA = hero.findNearestEnemy()
while DoorA.health > 0:
    hero.attack(DoorA)
hero.moveUp(2)
enemy = hero.findNearestEnemy()
while enemy.health > 0:
    hero.attack(enemy)
hero.moveDown(3)
hero.moveRight(2)
DoorB = hero.findNearestEnemy()
while DoorB.health > 0:
    hero.attack(DoorB)
hero.moveUp(3)
enemy = hero.findNearestEnemy()
while enemy.health > 0:
    hero.attack(enemy)
hero.moveDown(2)
hero.moveRight(2)
DoorC = hero.findNearestEnemy()
while DoorC.health > 0:
    hero.attack(DoorC)
hero.moveUp(2)
enemy = hero.findNearestEnemy()
while enemy.health > 0:
    hero.attack(enemy)
hero.moveDown(2)
hero.moveRight(2)
hero.moveUp(3)
hero.moveRight()
enemy = hero.findNearestEnemy()
while enemy.health > 0:
    hero.attack(enemy)
enemy = hero.findNearestEnemy()
while enemy.health > 0:
    hero.attack(enemy)
hero.moveDown(4)
hero.moveLeft(3)
hero.moveDown(1)
hero.moveLeft(2)

I then played a few more levels to discover the def - method to define functions.
I then had an idea to def the attack methods in order to clean up my code and make it even more beautiful.

Here is what it looks like after discovering a few more tools and some thinking.

def smashDoor():
    Door = hero.findNearestEnemy()
    while Door.health > 0:
        hero.attack(Door)

def murkEnemy():
    enemy = hero.findNearestEnemy()
    while enemy.health > 0:
        hero.attack(enemy)
    
hero.moveUp(2);
smashDoor()
hero.moveUp(2)
murkEnemy()
hero.moveDown(3)
hero.moveRight(2)
smashDoor()
hero.moveUp(3)
murkEnemy()
hero.moveDown(2)
hero.moveRight(2)
smashDoor()
hero.moveUp(2)
murkEnemy()
hero.moveDown(2)
hero.moveRight(2)
hero.moveUp(3)
hero.moveRight()
murkEnemy()
murkEnemy()
hero.moveDown(4)
hero.moveLeft(3)
hero.moveDown(1)
hero.moveLeft(2)

Question:
I was wondering, if there was a directory that lists all of the possible methods, objects, properties available to use in CodeCombat?

If I had known about the while method without the while-True and the enemy.health property, I might have come to the same answer that Hinkle gave.

If anyone can come up with a shorter and more beautiful code to beat this level with a ranged hero, I would love to see it.

Thanks :smile: