[antipodes]code don't work

> def areAntipodes(unit1, unit2):
>     reversed1 = ""
>     for i in range(len(unit1.id) - 1, -1, -1):
>         reversed1 += unit1.id[i]
>     return reversed1 == unit2.id
> 
> friends = hero.findFriends()
> enemies = hero.findEnemies()
> 
> for friend in friends:
>     for enemy in enemies:
>         if areAntipodes(friend, enemy):
>             hero.command(friend, "move", enemy.pos)
> 
> enemy = hero.findNearestEnemy()
> if areAntipodes(friend, enemy) is False:
>     for friend in friends:
>         hero.command(friend, "attack", enemy)
>     hero.attack(enemy)

Archers move but after all the antipodes were down, they donā€™t attack the enemy, and neither the hero. Help!

i think problem is here, but i donā€™t know why and how to change

I will check if the nearest enemy is a warlock and put all last lines in a loop because you need to command every one of your archers to attack the enemy continuously. Donā€™t think the line ā€œareAntipodes(friend, enemy) is Falseā€ is needed.

@xythonļ¼Œ

> for friend in friends:
>     for enemy in enemies:
>         if areAntipodes(friend, enemy):
>             hero.command(friend, "move", enemy.pos)
>         else:
>             hero.command(friend, "attack", enemy)

why it couldnā€™t deal like this? when i use this, all is in fight.:disappointed_relieved:
and if i look for the warlock, all is in fightā€¦

Iā€™m sorry if you know all this. I will have to explain this to my 9 years old son! :grinning:
I will try to visualize approximately your code:

In your case everyone of your friends will attack every enemy only once and that will be insufficient. Put the code in ā€œwhile Trueā€ loop and see if you need the line
if areAntipodes(friend, enemy):
the link to above ā€˜codeā€™ is https://goo.gl/uDgSbt
if you want to loop only 3 times: https://goo.gl/h8C8wd
the site is http://pythontutor.com/ , VISUALIZE Python, Java, JavaScript, TypeScript, Ruby, C, and C++

1 Like

@xython That is really helpful. Thank you very much.