Binary Deployment

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:

# 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

here is my new code

# 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)

I need help with my code:

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.

sorry if you got interrupted in anything

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

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

1 Like