[SOLVED] Problem with Rectangle Formation

Error found in the level:

image

after running what looks like a problem-free code (although I believe that is not the problem). Anyone experienced the same thing?
:raised_hands:

1 Like

Actually, that is most likely a bug in your code, not the levelā€¦please post your code so we can verify.

2 Likes
# Form up soldiers and archers in rectangle formations.

# The distance between units.
step = 8

# First form up soldiers.
sergeant = hero.findNearest(hero.findByType("soldier"))
# The coordinates of the bottom left corner.
soldierX = 8
soldierY = 8
# The width and height of the formation.
width = sergeant.rectWidth
height = sergeant.rectHeight

for x in range(soldierX, soldierX + width + 1, 8):
    for y in range(soldierY, soldierY + height + 1, 8):
        hero.summon("soldier")
        lastUnit = hero.built[len(hero.built)-1]
        # Command the last built unit by using the  x/y variables:
        hero.command(lastUnit, "move", x, y)

# Next form up archers.
sniper = hero.findNearest(hero.findByType("archer"))
# The coordinates of the bottom left corner.
archerX1 = 48
archerY1 = 8
# The coordinates of the top right corner.
archerX2 = sniper.archerX2
archerY2 = sniper.archerY2

for x in range(archerX1, archerX2 + 1, 8):
    for y in range(archerY1, archerY2 + 1, 8):
        # Summon an archer.
        hero.summon('archer')
        # Find the last built unit.
        lastUnit = hero.built[len(hero.built) -1]
        # Command the last built unit by using the  x/y variables:
        hero.command(lastUnit, "move", x, y)
        pass

:raised_hands:

1 Like

Can you send me the link to the level?

Andrei

2 Likes

How do I do that? I havenā€™t done it before.
:raised_hands:

1 Like

https://codecombat.com/play/level/rectangle-formation

2 Likes

The problem is here:

        hero.command(lastUnit, "move", x, y)

Twiceā€¦you have this statement in two places, which is fine, once it is corrected.

The ā€˜moveā€™ method is not expecting individual x/y componentsā€¦it needs a vector, or ā€˜.posā€™ instead.

3 Likes

Here is the problem. Try to put this after ā€œmoveā€:

{"x": x, "y": y}

Andrei

3 Likes

Thanks a lot for the help ā€“ it worked! However Idonā€™t understand why: the error appeared the first time and what it was saying; but also why I needed the format:

Could you explain?
:raised_hands:

2 Likes

The command ā€œmoveā€ wants an position, not coordontes. x and y are cordonates, and {ā€œxā€: x, ā€œyā€: y} is an position. Do you understand?

Andrei

4 Likes

Yep, understood, what about ā€˜Donā€™t know how to transform: Setā€™ ā€“ is it just telling me that it doesnā€™t understand

?
:raised_hands:

2 Likes

That is correctā€¦you sent it a ā€˜setā€™ of instructions that it does not know how to handle.

4 Likes

Thank you, all, for clarification!
:raised_hands:

3 Likes