I do not know what to do so far. This is what my code looks like:
# Ogres are trying to take out your reindeer!
# Keep your archers back while summoning soldiers to attack.
def pickUpCoin():
# Collect coins.
coin = hero.findNearestItem()
hero.move(coin.pos)
pass
def summonTroops():
# Summon soldiers if you have the gold.
if hero.gold >= hero.costOf("soldier"):
hero.summon("soldier")
pass
# This function has an argument named soldier.
# Arguments are like variables.
# The value of an argument is determined when the function is called.
def commandSoldiers():
for soldier in hero.findFriends():
enemy = soldier.findNearestEnemy()
if enemy:
hero.command(soldier, "attack", enemy)
# Write a commandArcher function to tell your archers what to do!
# It should take one argument that will represent the archer passed to the function when it's called.
# Archers should only attack enemies who are closer than 25 meters, otherwise, stay still.
while True:
pickUpCoin()
summonTroops()
friends = hero.findFriends()
for friend in friends:
if friend.type == "soldier":
# This friend will be assigned to the variable soldier in commandSoldier
commandSoldiers(friend)
elif friend.type == "archer":
# Be sure to command your archers.
pass
Firstly donāt bother with Archipelago-Goldās suggestion. Your statement says āIf hero.gold is greater than or equal to the cost of the soldier thenā, and that should be fine.
You need to understand a new concept in functions in this level. The code when you started will have had this in it.
def commandSoldiers(soldier):
If you imagine that a function is a machine. Up until now, the machine has been using materials that it already has to process and produce an output. Now however weāre putting an input into the machine, which is āsoldierā.
So you need to remove the for loop in your commandSoldiers(soldier) function (youāll need to sort the indentation, but keep the rest of the code the same). Wherever the code comes across the word āsoldierā it will use the input.
If you look at when you call the code (in the while True loop) you then put a specific friend into the function, so the āmachineā knows what to operate the function on.
You need to write the same code for the commandArcher(whateverArguementYouPutInHere) function, and to then call the function in the while True loop.
Have a go at that, and post your code again if you need more help.
Hmmm, sorry Iāve only just seen that youāve asked for more explanation.
So your function with an argument might be something like:
def heroAttack(greenAliens):
if greenAliens:
hero.attack(greenAliens)
Now, there arenāt greenAliens in Coco (not that Iāve found yet). But thatās OK, as when we call the function we put:
enemy = hero.findNearestEnemy()
heroAttack(enemy)
The function will then replace all the greenAliens with enemy, so the code will get the hero to check if thereās an enemy, and attack the enemy if there is one.
Does that help? Can you now write some code which starts:
enemy = soldier.findNearestEnemy()
if enemy:
hero.command(soldier, "attack", enemy)
Only you need to change āsoldierā to āgreenAliensā (or change āgreenAliensā into āsoldierā. It doesnāt matter which way round, as long as the input into the machine (function) is the same as the thing that itās operating on).