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

[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