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

3 Likes

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

2 Likes

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)


3 Likes

So what is wrong with my code?

2 Likes

I finished the level plus the bonus

2 Likes

Hello. Yes I know that this is several months old but you seem to have solved the level and I was wondering if you could help me with my code in binary deployment. Thank you for your consideration.

Howdy…go ahead and post your code and explain the issue you are seeing. If you are getting any errors, pics of those would help too.

And can you tell us if you do not understand the algorith of this level so we will try to explain it to you? (and format your code as it is described here)

Andrei

2 Likes

I will gladly post my code. Here it is

    # 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 toTernary(number):
    # Start with an empty string.
    string = ""
    # Then, while the number isn't zero:
    while number != 0:
        # We grab the remainder of our number.
        remainder = number % 3
        # This is our iterator method. 'number' decrements here.
        number = (number - remainder) / 3
        # Append the string to the remainder.
        string = remainder + string
    # Finally we want to return our constructed string.
    return string
    
def toBinary(number):
    string = ""
    # Go through the steps again:
        # Get remainder, decrement, and append string.
    # Remember that binary is another way of saying '2'!
    while number != 0:
        # We grab the remainder of our number.
        remainder = number % 2
        # This is our iterator method. 'number' decrements here.
        number = (number - remainder) / 2
        # Append the string to the remainder.
        string = remainder + string
    # Finally we want to return our constructed string.
    
    return string  

while True:
    paladins = hero.findByType("paladin")
    warlocks = hero.findByType("warlock")
    
    for warlock in warlocks:
        deployment1 = warlock.deployment
        message1 = toTernary(deployment1)
        y = warlock.pos.y
        if len(message1) < 6:
            message1 = 0 + message1
        if len(message1) < 7:
            message1 = 0 + message1
        if len(message1) < 8:
            message1 = 0 + message1
        #hero.say(message1)
    for paladin in paladins:
        deployment = paladin.deployment
        message = toBinary(deployment)
        y = paladin.pos.y
        if len(message) < 5:
            message = 0 + message
        if len(message) < 6:
            message = 0 + message
        if len(message) < 7:
            message = 0 + message
        if len(message) < 8:
            message = 0 + message
        #hero.say(message)
    for i in range(len(message)):
        word = message[i]
        #hero.say(word)
        if word == 0:
            hero.summon("soldier")
        if word == 1:
            hero.summon("archer")

    for j in range(len(message1)):
        word1 = message1[j]
        hero.say(word1)
        if word1 == 0:
            hero.summon("soldier")
        if word1 == 1:
            hero.summon("archer")
        if word1 == 2:
            hero.summon("griffin-rider")
    friends = hero.built
    
    for friend in friends:
        xArr = [50, 45, 40, 35, 30, 25, 20, 15]
        for x in xArr:
            
            hero.command(friend, "move", {'x': x, 'y': y})

I don’t summon anything.

Sorry, but I have a pretty busy week and now I will have to go to bed soon.

Andrei

2 Likes

BTW @dedreous I have no errors it just won’t move past the summon code and the summon code does not run.

my guy summons nada and the command code runs perfectly I don’t know how to fix my summon code.

I have to go to bed soon. Sorry for the inconvinience!

Andrei

2 Likes

np if you could at some point check the code and tell me if anything is wrong that would be helpful.

1 Like

Ok. I will have a look at it tomorrow.

Andrei

2 Likes

OK, I mostly do JavaScript so I might be wrong. But I think this:

should be indented more, so it sits inside the ‘for paladin in paladins’ loop (and a similar bit of code inside the ‘for warlock in warlocks’ code).

Also, I had to command the new soldiers/archers to move straight away, so that I could command the right one to the right place; otherwise it turned into chaos of who was sent where.

2 Likes

You can remove the while True, you do not need it.

Andrei

2 Likes

First of all, remove the free lines 72 and 83.

Here try to put a while the length of message < 8 to add an 0.
The same thing for here

But with message1 instead of message.

Andrei

2 Likes