# \[SOLVED\] Need Help with Rectangle Formation

**URL:** <https://discourse.codecombat.com/t/solved-need-help-with-rectangle-formation/20857>\
**Category:** Level Help\
**Created:** [April 7, 2020, 7:34pm UTC](https://discourse.codecombat.com/t/solved-need-help-with-rectangle-formation/20857 "2020-04-07T19:34:28Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ry4n\_17](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/ry4n_17/32/11794_2.png) [@Ry4n\_17](https://discourse.codecombat.com/u/Ry4n_17)\
**Post date:** [April 7, 2020, 7:34pm UTC](https://discourse.codecombat.com/t/solved-need-help-with-rectangle-formation/20857/1 "2020-04-07T19:34:28Z")

</div>

I did not understand very well what I have to do at this level can someone please help me  
Here is the code:

```python
# 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:
        
# 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.
        
        # Command the last built unit by using the x/y variables:
        
        pass

```

---

<div class="post-metadata">

**Author:** ![dedreous](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/dedreous/32/13849_2.png) [@dedreous](https://discourse.codecombat.com/u/dedreous)\
**Post date:** [April 7, 2020, 9:16pm UTC](https://discourse.codecombat.com/t/solved-need-help-with-rectangle-formation/20857/2 "2020-04-07T21:16:47Z")

</div>

You basically have two code blocks that you need to add code to. In the first block (the first big yellow arrow), you just need to command the soldier to move to x, y.

Using that first block as a template, you need to flesh out the second block. They are almost identical, except for one targets soldiers and the other archers.

You only need to add code where the arrows indicate:

 ![image](https://us1.discourse-cdn.com/flex016/uploads/codecombat/original/2X/a/a6b9ec590385be1c0fceb8060dca74cf47236b26.png)

---

<div class="post-metadata">

**Author:** ![Ry4n\_17](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/ry4n_17/32/11794_2.png) [@Ry4n\_17](https://discourse.codecombat.com/u/Ry4n_17)\
**Post date:** [April 9, 2020, 6:14pm UTC](https://discourse.codecombat.com/t/solved-need-help-with-rectangle-formation/20857/3 "2020-04-09T18:14:07Z")

</div>

This is my code now

```python
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:
        soldiers = self.findFriends()
        soldierIndex = 0
        soldier = soldiers[soldierIndex]
        hero.command(soldier, "move", {"x": 24, "y": 25})
# 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:
        archers = self.findFriends()
        archerIndex = 0
        archer = archers[archerIndex]
        hero.command(archer, "move", {"x": 48, "y": 24})
        pass

```

I get this error

 ![Desktop Screenshot 2020.04.09 - 15.12.54.98](https://us1.discourse-cdn.com/flex016/uploads/codecombat/original/2X/9/94718849518028101ceaa9ede570e4f6f50c1b29.png)

---

<div class="post-metadata">

**Author:** ![dedreous](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/dedreous/32/13849_2.png) [@dedreous](https://discourse.codecombat.com/u/dedreous)\
**Post date:** [April 9, 2020, 6:30pm UTC](https://discourse.codecombat.com/t/solved-need-help-with-rectangle-formation/20857/4 "2020-04-09T18:30:03Z")

</div>

Looks like you are missing a few lines…what you just posted starts at line 15…please post all of your code.

---

<div class="post-metadata">

**Author:** ![Ry4n\_17](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/ry4n_17/32/11794_2.png) [@Ry4n\_17](https://discourse.codecombat.com/u/Ry4n_17)\
**Post date:** [April 9, 2020, 10:18pm UTC](https://discourse.codecombat.com/t/solved-need-help-with-rectangle-formation/20857/5 "2020-04-09T22:18:24Z")

</div>

Here is all the code

```python
# 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:
        soldiers = self.findFriends()
        soldierIndex = 0
        soldier = soldiers[soldierIndex]
        hero.command(soldier, "move", {"x": 24, "y": 25})
# 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:
        archers = self.findFriends()
        archerIndex = 0
        archer = archers[archerIndex]
        hero.command(archer, "move", {"x": 48, "y": 24})
        pass

```

---

<div class="post-metadata">

**Author:** ![Deadpool198](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/deadpool198/32/16753_2.png) [@Deadpool198](https://discourse.codecombat.com/u/Deadpool198)\
**Post date:** [April 10, 2020, 8:02am UTC](https://discourse.codecombat.com/t/solved-need-help-with-rectangle-formation/20857/6 "2020-04-10T08:02:12Z")

</div>

Hi @Ry4n_17, you’re not quite following the instructions here.  
It says:

> [@Ry4n\_17](#):
>
> ```python
> # Command the last built unit by using the x/y variables:
> 
> ```

And you wrote:

> [@Ry4n\_17](#):
>
> ```python
> soldiers = self.findFriends() 
> soldierIndex = 0 
> soldier = soldiers[soldierIndex] 
> hero.command(soldier, "move", {"x": 24, "y": 25})
> 
> ```

Your code won’t work here, soldierIndex doesn’t increment so you will always command the same soldier. Make sure you follow the instructions…  
Use the variables `lastUnit`, `x` and `y`.  
You only need **one** line after the comment.  
Danny

---

<div class="post-metadata">

**Author:** ![Ry4n\_17](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/ry4n_17/32/11794_2.png) [@Ry4n\_17](https://discourse.codecombat.com/u/Ry4n_17)\
**Post date:** [April 10, 2020, 6:36pm UTC](https://discourse.codecombat.com/t/solved-need-help-with-rectangle-formation/20857/7 "2020-04-10T18:36:22Z")

</div>

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

```

dont understand the x, y part

---

<div class="post-metadata">

**Author:** ![dedreous](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/dedreous/32/13849_2.png) [@dedreous](https://discourse.codecombat.com/u/dedreous)\
**Post date:** [April 10, 2020, 6:53pm UTC](https://discourse.codecombat.com/t/solved-need-help-with-rectangle-formation/20857/9 "2020-04-10T18:53:32Z")

</div>

The x and y are variables that can be used to represent the coordinates you want to move to. These are being calculated via the 'for loop’s at the beginning of each code block. Remember, each block is very similar, except for either targeting soldier, or archer.

In the full code you posted above, you have:

> [@Ry4n\_17](#):
>
> hero.command(soldier, “move”, {“x”: 24, “y”: 25})

This is very close, but instead of using literal coordinates (the 24 and 25), you want to use the variables instead.

---

<div class="post-metadata">

**Author:** ![Ry4n\_17](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/ry4n_17/32/11794_2.png) [@Ry4n\_17](https://discourse.codecombat.com/u/Ry4n_17)\
**Post date:** [April 10, 2020, 8:20pm UTC](https://discourse.codecombat.com/t/solved-need-help-with-rectangle-formation/20857/10 "2020-04-10T20:20:49Z")

</div>

This is my code now

```python
# 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, soldierX + width + 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":soldierX, "y": soldierY})
# 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": archerX1, "y": archerY1 })
        pass

```

---

<div class="post-metadata">

**Author:** ![dedreous](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/dedreous/32/13849_2.png) [@dedreous](https://discourse.codecombat.com/u/dedreous)\
**Post date:** [April 10, 2020, 8:49pm UTC](https://discourse.codecombat.com/t/solved-need-help-with-rectangle-formation/20857/11 "2020-04-10T20:49:32Z")

</div>

The x and y that you want to use are from the for loops:

```python
for x in range(soldierX, soldierX + width + 1, 8):
    for y in range(soldierY, soldierX + width + 1, 8):

```

They are the variables being used to calculate the coordinates.

---

<div class="post-metadata">

**Author:** ![Ry4n\_17](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/ry4n_17/32/11794_2.png) [@Ry4n\_17](https://discourse.codecombat.com/u/Ry4n_17)\
**Post date:** [April 13, 2020, 5:41pm UTC](https://discourse.codecombat.com/t/solved-need-help-with-rectangle-formation/20857/12 "2020-04-13T17:41:20Z")

</div>

But how do i use the code

```python
hero.command(lastUnit, "move", {"x":soldierX, "y": soldierY})

```

thats the code that i used but the soldiers and archers move to one x only.

---

<div class="post-metadata">

**Author:** ![dedreous](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/dedreous/32/13849_2.png) [@dedreous](https://discourse.codecombat.com/u/dedreous)\
**Post date:** [April 13, 2020, 6:35pm UTC](https://discourse.codecombat.com/t/solved-need-help-with-rectangle-formation/20857/13 "2020-04-13T18:35:24Z")

</div>

That’s because those are not the correct variables and you are only defining them once, so they will only point to one coordinate (location). You **literally** need to use the x and y variables.

---

<div class="post-metadata">

**Author:** ![Ry4n\_17](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/ry4n_17/32/11794_2.png) [@Ry4n\_17](https://discourse.codecombat.com/u/Ry4n_17)\
**Post date:** [April 14, 2020, 4:33pm UTC](https://discourse.codecombat.com/t/solved-need-help-with-rectangle-formation/20857/15 "2020-04-14T16:33:27Z")

</div>

it said success in the run but it dosent work now

 ![Desktop Screenshot 2020.04.14 - 13.32.54.74](https://us1.discourse-cdn.com/flex016/uploads/codecombat/original/2X/8/8f5c1d457d0e4e19677d0a98487fac57b9a7b553.png)

---

<div class="post-metadata">

**Author:** ![Ry4n\_17](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/ry4n_17/32/11794_2.png) [@Ry4n\_17](https://discourse.codecombat.com/u/Ry4n_17)\
**Post date:** [April 14, 2020, 4:35pm UTC](https://discourse.codecombat.com/t/solved-need-help-with-rectangle-formation/20857/16 "2020-04-14T16:35:20Z")

</div>

Nevermind i refreshed and it worked  
Thanks again ded

---

<div class="post-metadata">

**Author:** ![brooksy125](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/brooksy125/32/9966_2.png) [@brooksy125](https://discourse.codecombat.com/u/brooksy125)\
**Post date:** [April 14, 2020, 4:49pm UTC](https://discourse.codecombat.com/t/solved-need-help-with-rectangle-formation/20857/17 "2020-04-14T16:49:12Z")

</div>

The Soldier for loop with the y range is not quite correct. You happened to get a seed (arrangement of variables to change the level and test reliability) that worked with the error.

The y value: does it expect width or height?

---

<div class="post-metadata">

**Author:** ![dedreous](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/dedreous/32/13849_2.png) [@dedreous](https://discourse.codecombat.com/u/dedreous)\
**Post date:** [April 14, 2020, 5:02pm UTC](https://discourse.codecombat.com/t/solved-need-help-with-rectangle-formation/20857/18 "2020-04-14T17:02:18Z")

</div>

Good catch!..completely missed that one 🙂
