[antipodes]code don't work


#1
> 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!


#2

i think problem is here, but i don’t know why and how to change


#3

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.


#4

@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…


#5

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++


#6

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