Determine closest out of three positions

#1

In Sarven siege level, I’m trying to make hero walk to the closest tower once he has collected 20 coins:
I wasn’t really expecting the following to work, I just took a wild guess how it might be done and tried it:

while True:
while hero.gold < 20:
coin = hero.findNearestItem()
hero.moveXY(coin.pos.x, coin.pos.y)
pass

distance1 = hero.distanceTo(84,78) #position of 1st tower
distance2 = hero.distanceTo(84, 51) #position of 2nd tower
distance3 =  hero.distanceTo(84, 22) #position of 3rd tower

distances = [distance1, distance2, distance3]
closest = min(distances)
#then, something like:
if closest == distance1:
hero.moveXY(84, 78)
elif closest == distance2:
hero.moveXY(84, 51)
else:
hero.moveXY(84, 22)
1. for starters, I don’t even know if distanceTo works like that, i.e. can I ask it for the distance to certain coordinates or do I have to store that position in a variable. If so, how do I do that?

2. I’m not sure how to determine the lowest integer out of three (or any number but 2) I found suggestions that you could just use min(), and on the other hand, that you need to use a dictionary

Next

#2

coordinates written like that would not work with DistanceTo
But it would work with a dictionnary.

position = {"x": 80, "y": 40}
hero.say(distanceTo(position))

Now I don’t want to mess you ups with all of this.
People are going to say: How it work I though distanceTo() always takes an object and not a dictionnary.

What I believe is when you call an object in the function: distanceTo the function takes the object.pos value which is a dictionnary.
For example if you put enemy.pos in hero.distanceTo() it should works just like enemy works.

#3

just say:

if distance1 < distance2 and distance1 < distance3:
hero.say("distance 1 is shortest distance")
#same for the others.

#4

tbh for sarven siege I just use my hero instead of summoning troops and stuff

#5

A link to a rough simulation to what you want to achieve without classes ( because I don’t know classes yet) pythontutor . Another document , see “Find Best Target” Poster
Ask questions if you have any.

#6

I think I stumbled upon other cases where position( Vector ) and object can be interchangeable. Can you remember any other examples?