I’ve been working on this level for days, coming back to it anew, reloading the code and trying and again and again. Nothing seems to work. The level says that the soldiers should attack the witches, and the archers should attack the nearest enemy. There are a few ways to implement this, most of them very wrong. Here is what I have tried, with the differences highlighted.
def chooseTarget(friend):
if friend.type == "archer":
return friend.findNearest(friend.findEnemies())
elif friend.type == "soldier":
return self.findNearest(self.findByType("witch"))
loop:
friends = self.findFriends()
for friend in friends:
# Use your chooseTarget function to decide what to attack.
self.command(friend, "attack", chooseTarget(friend))
This results in archers targetting the nearest throwers while the soldiers make a beeline for the bottom witch, getting whalloped by the ogres on their way past and then quickly eradicated after reaching the witch. Bummer.
def chooseTarget(friend):
if friend.type == "archer":
return friend.findNearest(friend.findEnemies())
elif friend.type == "soldier":
return ***friend***.findNearest(self.findByType("witch"))
loop:
friends = self.findFriends()
for friend in friends:
# Use your chooseTarget function to decide what to attack.
self.command(friend, "attack", chooseTarget(friend))
The archers take out the throwers, as per usual, and again they focus on the ogres as the ogres beat up the soldiers as they march past in two lines to attack both witches at the same time. There is a bloodbath resulting in a lot of humans on the ground, and no dead witches or ogres. Bummer.
def chooseTarget(friend):
if friend.type == "archer":
return friend.findNearest(***self***.findEnemies())
elif friend.type == "soldier":
return friend.findNearest(self.findByType("witch"))
loop:
friends = self.findFriends()
for friend in friends:
# Use your chooseTarget function to decide what to attack.
self.command(friend, "attack", chooseTarget(friend))
This is the best so far. The archers focus on the throwers at the top of the field, ignoring the bottom ones for some reason, and then the soldiers attack both witches at the same time. For some reason the bottom soldiers prevail before all the soldiers die and the remaining witch, both ogres and the bottom throwers turn on my archers. Slightly less of a bummer.
I’ve scoured the other post on this level and I really can’t figure out what I’m doing wrong. At one point I even just cut and pasted Midnight’s code and made the change he reported and still couldn’t make it work. Anyway, some help from one of you python gurus would be very much appreciated.