Ring bearer stuck


#1
# You must escort a powerful magical ring back to town to be studied.
# The goal is to escape, not fight. More ogres lurk in the surrounding mountains!
# Make a circle of soldiers around the peasant!
# We give you two functions to help with this:

# findSoldierOffset figures out the position a soldier should stand at in relation to the peasant.
# The first argument 'soldiers' should be an array of your soldiers.
# The second argument 'i' is the index of the soldier (in soldiers) you want to find the position for.
def findSoldierOffset(soldiers, i):
    soldier = soldiers[i]
    angle = i * 360 / len(soldiers)
    return radialToCartesian(5, angle)

# This function does the math to determine the offset a soldier should stand at.
def radialToCartesian(radius, degrees):
    radians = Math.PI / 180 * degrees
    xOffset = radius * Math.cos(radians)
    yOffset = radius * Math.sin(radians)
    return {"x": xOffset, "y": yOffset}

peasant = hero.findByType("peasant")[0]

# Use findByType to get an array of your soldiers.

while True:
    # Use a for-loop to iterate over range(len(soldiers)).
    soldier = hero.findByType("soldier")
    for i in range(len(soldier)):
        offset = findSoldierOffset(soldier, i)
    # Find the offset for a soldier.
    # Add the offset.x and offset.y to the peasant's pos.x and pos.y.
        hero.command(soldier, "move", {"x": peasant.pos.x + offset.x, "y": peasant.pos.y + offset.y})
    # Command the soldier to move to the new offset position.

    # The hero should keep pace with the peasant!
    hero.move({"x": hero.pos.x + 0.2, "y": hero.pos.y})

Any pointers as to where I have gone wrong? The soldiers don’t move. Thanks


#2

It would be less confusing for you if you used soliders, because here

you’re commanding the array of all the soldiers when you only want to command one.
You just need to make a soldier = soldiers[i] and command that soldier to move instead of the whole array.
Apart from that it’s right. :grin:
:lion::lion::lion:


#3
# You must escort a powerful magical ring back to town to be studied.
# The goal is to escape, not fight. More ogres lurk in the surrounding mountains!
# Make a circle of soldiers around the peasant!
# We give you two functions to help with this:

# findSoldierOffset figures out the position a soldier should stand at in relation to the peasant.
# The first argument 'soldiers' should be an array of your soldiers.
# The second argument 'i' is the index of the soldier (in soldiers) you want to find the position for.
def findSoldierOffset(soldiers, i):
    soldier = soldiers[i]
    angle = i * 360 / len(soldiers)
    return radialToCartesian(5, angle)

# This function does the math to determine the offset a soldier should stand at.
def radialToCartesian(radius, degrees):
    radians = Math.PI / 180 * degrees
    xOffset = radius * Math.cos(radians)
    yOffset = radius * Math.sin(radians)
    return {"x": xOffset, "y": yOffset}

peasant = hero.findByType("peasant")[0]

# Use findByType to get an array of your soldiers.

while True:
    # Use a for-loop to iterate over range(len(soldiers)).
    soldiers = hero.findByType("soldier")
    for i in range(len(soldiers)):
        soldier = soldiers[i]
        offset = findSoldierOffset(soldier, i)
    # Find the offset for a soldier.
    # Add the offset.x and offset.y to the peasant's pos.x and pos.y.
        hero.command(soldier, "move", {"x": peasant.pos.x + offset.x, "y": peasant.pos.y + offset.y})
    # Command the soldier to move to the new offset position.

    # The hero should keep pace with the peasant!
    hero.move({"x": hero.pos.x + 0.2, "y": hero.pos.y})

Thanks for the reply. I think I made the suggestions correctly but I still get an error:
move’s argument pos.x has a problem… thanks


#4

Look at the name of your first function. Then look at the variable definition for offset in your while True loop where you call that function. Compare the two.