[SOLVED] Help! Toil and Trouble

Here’s something to get you started. This level only really takes 7 lines to beat it legitimately.
But I don’t want to make things too confusing, so we’ll go with the long method.

Step 1: Change the attackWitch function:
Right now your attackWitch function doesn’t take any parameters, but here’s the catch – You can take an argument “friend”. So instead of having just def attackWitch(), you can do def attackWitch(friend). This will save you lines of code.

Step 2: Edit the inside of the new attackWitch function:
First, remove the soldiers = hero.findByType("soldier") line, you won’t need it because we’ll have it included in the arguments Next, remove the for loop as it’s now obsolete to have – As I said before, you already received a unit to command. In the witch = soldier.findNearest(hero.findByType("witch")) line, replace soldier with whatever you named the argument in the function attackWitch(argument). After that, command that unit to attack the witch.

Step 3: Remove attackNearest():
Remove the attackNearest() function since it’s unneeded.

Step 4: Remove chooseTarget()
As above, it’s obsolete to have it, so feel free to delete it.

Step 5: Edit the while True loop. Part A:
Keep the findFriends() and also keep the for-loop, but instead of getting the target, check if the type of the friend is an archer. If so, then get the friend.findNearestEnemy() for the archer, and have them attack it using the hero.command() function.

Step 6: Edit the while True loop. Part B:
Next, check if the friend.type is soldier, if so, then run our attackWitch function with the argument friend in it, so we’ll have attackWitch(friend).

If you do the above steps, then you should be able to finish the level :+1:

7-line of code proof:

5 Likes