Play Home Contribute Github Join Us! Discourse Staff Members Github Contribution Guides Team

Cloudrip Commander HELP

#1

Hi, I was wondering if you could help me with this level.

Here’s my code:

# Summon some soldiers, then direct them to your base.

# Each soldier costs 20 gold.
while hero.gold > hero.costOf("soldier"):
    hero.summon("soldier")
    
soldiers = hero.findFriends()
soldierIndex = 0
# Add a while loop to command all the soldiers.
while True:
    soldier = soldiers[soldierIndex]
    hero.command(soldier, "move", {"x": 50, "y": 40})
    # Go join your comrades!
    hero.moveXY(51, 41)

0 Likes

#2

Put the

in the second while loop

0 Likes

#3

Here is my new code:

# Summon some soldiers, then direct them to your base.

# Each soldier costs 20 gold.
while hero.gold > hero.costOf("soldier"):
    hero.summon("soldier")
    
# Add a while loop to command all the soldiers.
while True:
    soldiers = hero.findFriends()
    soldierIndex = 0
    soldier = soldiers[soldierIndex]
    hero.command(soldier, "move", {"x": 50, "y": 40})
    # Go join your comrades!
    hero.moveXY(51, 41)

0 Likes

#4

Make sure the soldierIndex = 0 is before the second While True loop (like your first code) or you will keep calling that one soldier. Keep in mind that you need to loop through the array of your soldier buddies, which means you need to increase the soldierIndex by 1 each loop after you command the current soldier.

soldierIndex += 1   # or soldierIndex = soldierIndex + 1

Heads up, with the While True: loop you will get an error when you try commanding a soldier that isn’t there. A simple if statement will prevent that.
You can trade out the While True loop with another version of the While loop using the index.

while soldierIndex < len(soldiers):

With this set up, you need to find the soldiers array before the second loop like your first code or you can’t find the length of the array.

0 Likes