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

[SOLVED] Help on Binary Deployment

I think that I found out the correct algorithm, but I think that I pause too much, I get “ran out of time” and even with my waiting, I get “ran out of time”. What am I doing wrong? This is my 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.
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
            hero.wait(1)
        else:
            hero.summon("archer")
            friend = hero.findNearest(hero.findByType("archer"))
            hero.command(friend, "move", {"x":posx[h],"y":paladin.pos.y})
            hero.wait(1)
            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
                hero.wait(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})
        hero.wait(1)
        g = g / 2
        k -= 1

    

paladins = hero.findByType("paladin")
for paladin in paladins:
    commandPaladin(paladin)

Well, based on the first line of soldiers being the binary equivalent of the decimal number of the first paladin, it does look like your algorithm works for the binary…it doesn’t appear that you’ve gotten to the ternary part yet.

While I came up with a much different solution…a single code block that handles all three summon types, depending on the ‘paladin’ type…I’m having a hard time wrapping my head around your variables. Please understand this is not criticism, it’s just a recommendation…it is considered best practice to use descriptive words as variable names, especially when working with a team of developers, or if there is a chance of needing someone else to have a look. It is pretty much universally accepted that ‘for i in range’ is proper, but when naming a variable that is being used as a counter, or tracking mechanism, what is it counting? For example, just what is ‘oo = ’?

I understand. I will modify the code and resend it

Excellent…thank you! I can’t promise that I’ll be able to help you resolve, but I will certainly give it a try :slight_smile:

This is my new code:

# 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.
posx = [17,26,35,44,53,57,62,69]
def commandPaladin(paladin):
    g = paladin.deployment
    y = paladin.pos.y
    gg = g
    j = 0
    h = 0
    k = 7
    if paladin.type == "paladin":
        kk = 128
    else:
        kk = 2187
    while g <= kk:      # putting the zeros before the numbers
        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
            if paladin.type == "paladin":
                kk = kk / 2
            else:
                kk = kk / 3
            hero.wait(1)
        else:
            hero.summon("archer")
            friend = hero.findNearest(hero.findByType("archer"))
            hero.command(friend, "move", {"x":posx[h],"y":paladin.pos.y})
            hero.wait(1)
            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
                hero.wait(1)
        k = 9 - h
    while g != 0:  # the mutation from decimal numbers to binary or trinary
        if paladin.type == "paladin":
            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})
            hero.wait(1)
            g = g / 2
        else:
            if g % 3 == 0:
                hero.summon("soldier")
                friend = hero.findNearest(hero.findByType("soldier"))
                hero.command(friend, "move", {"x":posx[k],"y":paladin.pos.y})
            elif g % 3 == 1:
                g -= 1 
                hero.summon("archer")
                friend = hero.findNearest(hero.findByType("archer"))
                hero.command(friend, "move", {"x":posx[k],"y":paladin.pos.y})
            else:
                g -= 2 
                hero.summon("griffin-rider")
                friend = hero.findNearest(hero.findByType("griffin-rider"))
                hero.command(friend, "move", {"x":posx[k],"y":paladin.pos.y})
            hero.wait(1)
            g = g / 3
        k -= 1

    

paladins = hero.findByType("paladin")
for paladin in paladins:
    commandPaladin(paladin)
enemies = hero.findEnemies()
for enemy in enemies:
    commandPaladin(enemy)


So what is wrong with my code?

I finished the level plus the bonus