# Binary Deployment

**URL:** <https://discourse.codecombat.com/t/binary-deployment/21942>\
**Category:** Level Help\
**Created:** [May 31, 2020, 5:10pm UTC](https://discourse.codecombat.com/t/binary-deployment/21942 "2020-05-31T17:10:11Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![tv337](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/tv337/32/12147_2.png) [@tv337](https://discourse.codecombat.com/u/tv337)\
**Post date:** [May 31, 2020, 5:10pm UTC](https://discourse.codecombat.com/t/binary-deployment/21942/1 "2020-05-31T17:10:11Z")

</div>

I understand that there are many topics on this but I am having trouble understanding how to make it work. here is my code but I know that the code is incomplete:

```python
# Recruit soldiers and archers to fill out each squadron.
# Each paladin has a decimal number stored in her deployment attribute.
# Convert these to binary and represent them with lines of soldiers and archers next to each paladin.
# Soldiers are 0s, archers are 1s.
# For the bonus goal, add griffins as 2s for trinary number lines next to the warlocks.
# Check the guide for help with binary numbers.

def Dec2Bin(g):
    i=7
    res = []
    while i >= 0:
        res[i] = g // 2**i
        if res[i]:
            g -= 2 ** i
        i -= 1
    return res
binary = Dec2Bin(49)

hero.say("my number is " + binary[7] + " " + binary[6] + " " + binary[5] + " " + binary[4] + " " + binary[3] + " " + binary[2] + " " + binary[1] + " " + binary[0])

xArr = [15, 20, 25, 30, 35, 40, 45, 50]
def commands(paladin):
    z = binary[7, 6, 5, 4, 3, 2, 1, 0]
    if z == 0:
        hero.summon("soldier")
    elif z == 1:
        hero.summon("archer")
    elif z == 2:
        hero.summon("griffin-rider")
paladin = hero.findNearest(hero.findByType("paladin"))
commands(paladin)

```

I have got the part of getting the number into binary but I need help on the rest of the level.  
just realized I need to call the function

---

<div class="post-metadata">

**Author:** ![tv337](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/tv337/32/12147_2.png) [@tv337](https://discourse.codecombat.com/u/tv337)\
**Post date:** [May 31, 2020, 7:01pm UTC](https://discourse.codecombat.com/t/binary-deployment/21942/2 "2020-05-31T19:01:13Z")

</div>

here is my new code

```python
# Each paladin has a decimal number stored in her deployment attribute.
# Convert these to binary and represent them with lines of soldiers and archers next to each paladin.
# Soldiers are 0s, archers are 1s.
# For the bonus goal, add griffins as 2s for trinary number lines next to the warlocks.
# Check the guide for help with binary numbers.

def Dec2Bin(g):
    d=7
    res = []
    while d >= 0:
        res[d] = g // 2**d
        if res[d]:
            g -= 2 ** d
        d -= 1
    return res
#binary = Dec2Bin(49)

#hero.say("my number is " + binary[7] + " " + binary[6] + " " + binary[5] + " " + binary[4] + " " + binary[3] + " " + binary[2] + " " + binary[1] + " " + binary[0])

xArr = [15, 20, 25, 30, 35, 40, 45, 50]
def commands(paladin):
    g = paladin.deployment
    z = Dec2Bin(g)
    hero.say("my number is " + z)
    friends = hero.findFriends()
    
    for j in range(len(xArr)):
        x = xArr[j]
        hero.command(friends, "move", {'x': x, 'y': paladin.pos.y})
    for i in range(len(z)):
        z1 = z[i]
        if z1 == 0:
            hero.summon("soldier")
        elif z1 == 1:
            hero.summon("archer")
        elif z1 == 2:
            hero.summon("griffin-rider")
paladin = hero.findNearest(hero.findByType("paladin"))
commands(paladin)

```

---

<div class="post-metadata">

**Author:** ![Flamekey\_42](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/flamekey_42/32/19618_2.png) [@Flamekey\_42](https://discourse.codecombat.com/u/Flamekey_42)\
**Post date:** [February 18, 2021, 9:13pm UTC](https://discourse.codecombat.com/t/binary-deployment/21942/3 "2021-02-18T21:13:34Z")

</div>

I need help with my code:

```python
posx = [17,26,35,44,53,57,62,69]
oo = []
def commandPaladin(paladin):
    g = paladin.deployment
    y = paladin.pos.y
    gg = g
    j = 0
    ii = 0
    h = 0
    k = 7
    kk = 128
    while g <= kk:
        if g < kk:
            hero.summon("soldier")
            friend = hero.findNearest(hero.findByType("soldier"))
            hero.command(friend, "move", {"x":posx[h],"y":paladin.pos.y})
            h += 1
            kk = kk / 2
            
        else:
            hero.summon("archer")
            friend = hero.findNearest(hero.findByType("archer"))
            hero.command(friend, "move", {"x":posx[h],"y":paladin.pos.y})
            h += 1
            while h <= 7:
                hero.summon("soldier")
                friend = hero.findNearest(hero.findByType("soldier"))
                hero.command(friend, "move", {"x":posx[h],"y":paladin.pos.y})
                h += 1
                
        k = 9 - h
    while g != 0:
        if g % 2 == 0:
            hero.summon("soldier")
            friend = hero.findNearest(hero.findByType("soldier"))
            hero.command(friend, "move", {"x":posx[k],"y":paladin.pos.y})
        else:
            g -= 1 
            hero.summon("archer")
            friend = hero.findNearest(hero.findByType("archer"))
            hero.command(friend, "move", {"x":posx[k],"y":paladin.pos.y})
        
        g = g / 2
        k -= 1

    

paladins = hero.findByType("paladin")
for paladin in paladins:
    commandPaladin(paladin)
my hero summons many soldiers but only commands a some of them.

```

---

<div class="post-metadata">

**Author:** ![Flamekey\_42](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/flamekey_42/32/19618_2.png) [@Flamekey\_42](https://discourse.codecombat.com/u/Flamekey_42)\
**Post date:** [February 18, 2021, 9:14pm UTC](https://discourse.codecombat.com/t/binary-deployment/21942/4 "2021-02-18T21:14:08Z")

</div>

sorry if you got interrupted in anything

---

<div class="post-metadata">

**Author:** ![jka2706](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/jka2706/32/13636_2.png) [@jka2706](https://discourse.codecombat.com/u/jka2706)\
**Post date:** [February 19, 2021, 9:02am UTC](https://discourse.codecombat.com/t/binary-deployment/21942/5 "2021-02-19T09:02:02Z")

</div>

Hi Flamekey\_42,

Good work with the code - looks impressive. Agreed that it looks as though all the units get summoned, but then only some get commanded. I ended up using something like

```python
lastBuilt = hero.built[hero.built.length - 1]

```

in order to command the right friend - if you just instruct the nearest one then there’s so many units then sometimes the wrong one gets given the command.

Post again if you want more information.

Jenny
