[Solved] Coded Orders Python Help

# Read the message on the sign to determine which units to summon and where to place them.
# Check the guide for instructions on interpreting the orders.

sign = hero.findByType("sign")[0]
message = sign.message

# Tip: parse the sign, then summon the units, then send each unit to the right position.
while True:
        
    if message:
        if message == "axxyy":
            hero.summon("archer")
            friend = hero.findByType("archer")
            hero.command(friend, "move", {'x': int(xx), 'y': int(yy)})
        elif message == "sxxyy":
            hero.summon("soldier")
            friend = hero.findByType("soldier")
            hero.command(friend, "move", {'x': int(xx), 'y': int(yy)})
        elif message == "pxxyy":
            hero.summon("peasant")
            friend = hero.findByType("peasant")
            hero.command(friend, "move", {'x': int(xx), 'y': int(yy)})
        elif message == "gxxyy":
            hero.summon("griffin-rider")
            friend = hero.findByType("griffin-rider")
            hero.command(friend, "move", {'x': int(xx), 'y': int(yy)})
        else:
            continue

This is my code so far, but I notice that I can’t figure out how to read the sign without the quotes around something like pxxyy, but then that doesn’t work because xx and yy are integers, not xx and yy, so it doesn’t work. Can someone help? Oh and here is the link https://codecombat.com/play/level/coded-orders

Hi @CryptiCrystal.
I’m afraid you’re going to have to change the way you go about solving this level.
This comment says:

# Tip: parse the sign

Do you know what parse means? If not that’s absolutely fine, I’ll tell you. In coding, and in English, it means “to break something up into smaller parts, based on syntax, grammar or a pattern, for a certain purpose”, or something along those lines.
What it means here is that you have to go through the big long message bit by bit. This is what the structure of the message looks like:
“a1536s4532s2341P2455a2333p4455P1311…etc.”
You get the picture. It goes in series of 5 characters with the pattern ?XXYY, with ? being either a, s, p, g, A or P (archer, soldier, peasant, Artillery or Paladin) and XX being a two-digit x coordinate e.g. 34. The same for YY. Remember at this point those numbers are still strings.
So, what are the most important bits of that message? What are the characters you could access with a for loop that could tell you what troop type to summon, and, from that point in the array, find the X and Y coordinate to command the troop to move to? Which part of the 5 character pattern?
Remember, once you’ve found this character, you can make a for loop do something very cool:

alphabet = "abcdefghijklmnopqrstuvwxyz"
# a normal for loop can do this:
for i in range(0, len(alphabet):
    print(alphabet[i])
# output:
# -->a
# -->b
# -->c
# -->d
# -->e
# -->....etc....etc.

# however, you can add something to a for loop to make it print only certain parts of an array (remember the message you're looking at in the level is an array)
alphabet = "abcdefghijklmnopqrstuvwxyz"
for i in range(0, len(alphabet), 3): #--> this is called a step, you have probably come across it.
    # what the step does is add three to i every loop, when you'd normally add 1.
    print(alphabet[i])
    
# output:
# -->a
# -->d
# -->g
# -->j
# -->m

# Do you see how it only prints every 3rd character in the array?

How could you put this to use in this level?
Danny

2 Likes

So I could do for i in range(0, len(message)) or something

Almost… What about this section of my post:

I’m sure you could work out a way of doing it with just:

for i in range(0, len(message)):

But I think adding a step is easier.
Danny

hmmmmmmm, so do I have to have both of xx and yy?
Self Edit: Yes

So now I have

# Read the message on the sign to determine which units to summon and where to place them.
# Check the guide for instructions on interpreting the orders.

sign = hero.findByType("sign")[0]
message = sign.message

# Tip: parse the sign, then summon the units, then send each unit to the right position.
for i in range(0, len(message)):
    if message[0] == "a":
        hero.summon("archer")
        targets = hero.findByType("archer")
        for target in targets:
            hero.command(target, "move", {'x': int(message[1] + message[2]), 'y': int(message[3] + message[4]}))
    if message[0] == "s":
        hero.summon("soldier")
        targets = hero.findByType("soldier")
        for target in targets:
            hero.command(target, "move", {'x': int(message[1] + message[2]), 'y': int(message[3] + message[4]}))
    if message[0] == "p":
        hero.summon("peasant")
        targets = hero.findByType("peasant")
        for target in targets:
            hero.command(target, "move", {'x': int(message[1] + message[2]), 'y': int(message[3] + message[4]}))
    if message[0] == "g":
        hero.summon("griffin-rider")
        targets = hero.findByType("griffin-rider")
        for target in targets:
            hero.command(target, "move", {'x': int(message[1] + message[2]), 'y': int(message[3] + message[4]}))
    else:
        continue

but the duck tells me that I need to separate parameters by commas. It’s in the lines with the int() function. I’m confused on how to fix it

Umm, I don’t really get what you mean. Look, what I’m saying is that you could loop through the long message so that only the troop type indicators are listed. E.g.

"asspgAsPa"

What step number would you use if you wanted to get every fifth character.
0-5-10-15-20 etc. (it should be pretty obvious at this point)

ohhhhh, so you want to step it by five, remeber the xx and yy, then command them after summoning all of the troops

ok, so I did it, but the result looks very funny. I end up summoning the same troop over and over again until I run out of gold, and then command them to go to one spot. Doesn’t seem right…

1 Like

Ok and the problem was I was switching the orders of two brackets. Not saying where

2 Likes

This topic was automatically closed 12 hours after the last reply. New replies are no longer allowed.