[Adventurer] Queue Manager


#1

working3

Aaaaaand the mastery level about queues: Queue Manager.

There you will control the queue. That level can be confusing, so feel free to share your opinion about it. I will be grateful for any feedback.


#2

looks very hard i can’t do it (maybe)


#3

@Bryukh, can you help with my code please?

# Control the queue without mistakes.

# Useful string constants.
PHRASE_IN = " in"
PHRASE_OUT = " out"
IDLE = "idle"
# The list of workers.
workers = hero.findByType("peasant", hero.findFriends())
# Use this array to control the worker queue.
queue = []

while True:
    # Iterate all workers:
    for worker in workers:
        if worker.status=='idle':
            hero.say(worker.id+' in')
            queue.append(5)
        # If the worker's property "status" is "idle":
          
            # Say the free worker's name with the word " in":
            
            # Add the worker in the end of the queue:
            
    item = hero.findNearestItem()
    # If the item exists and the queue isn't empty:
    if item and queue != 5:
        hero.say(worker.id+' out')
        # Say the name of the first in the queue worker
        # with the word " out":
        
        # Remove (pop) the first one from the queue:
        


#4

Have you played Mountain levels about arrays?
I didn’t get why you append 5 in the queue and then you check if an array (queue) is a number (5) ? O_o


#5

Oh sorry I found my mistake.


#6

Can you guys help? My code keeps not removing the peasant from the array after I say “Peasant Name out”.
queue.pop(0)
doesn’t work. What did I do wrong?


#7

You have to do

first=queue.pop(0)

#8

Okay. Thanks! (20 characters)


#9

If you need help call @Chaboi_3000.


#10

Hmmm I’m getting a bug in this level…
Whenever i do queue.pop(n)
whatever number i put in place of n, or just leave it blank, it keeps calling out the name of the last worker of the queue, instead of the first…
i did say first = queue.pop(0)
Here’s my code:
while True:
# Control the queue without mistakes.
# Useful string constants.
PHRASE_IN = " in"
PHRASE_OUT = " out"
IDLE = “idle”
# The list of workers.
workers = hero.findByType(“peasant”, hero.findFriends())
# Use this array to control the worker queue.
queue = []
# Iterate all workers:
for worker in workers:
# If the worker’s property “status” is “idle”:
if worker.status == “idle”:
# Say the free worker’s name with the word " in":
name = worker.id
hero.say(name + " in")
# Add the worker in the end of the queue:
queue.append(name)
item = hero.findNearestItem()
# If the item exists and the queue isn’t empty:
if item and queue != []:
# Say the name of the first in the queue worker
# with the word " out":
first = queue.pop()
hero.say(first + " out")
# Remove (pop) the first one from the queue

Does anyone know what is wrong with my code…?
Also, how do you incase your code so it’s separate from the rest of the message?


How to use queue.pop
Pop(0) method bug
#11

never mind, i have fixed the problem
Just had to do queue = queue.reverse() and reverse the array


#12

Sorry, it’s a bug with the python engine in CoCo and pop(0) doesn work correctly. I thought that bug is fixed :frowning:


#13

I think I’m running into the same bug, but I don’t get how to get around it. Just in case, I’ll post my code:

Code
while True:
    # Iterate all workers:
    for worker in workers:
        # If the worker's property "status" is "idle":
        if worker.status == "idle":
            # Say the free worker's name with the word " in":
            hero.say(worker.id + " in")
            # Add the worker in the end of the queue:
            queue.append(worker)
    
    item = hero.findNearestItem()
    # If the item exists and the queue isn't empty:
    if item and queue:
        # Say the name of the first in the queue worker
        # with the word " out":
        first = queue[0]
        hero.say(first.id + " out!")
        # Remove (pop) the first one from the queue:
        first = queue.pop(0)

#14

I replaced pop with a while loop like this:

#I don't put the first element of queue(queue[0]) so this acts like queue.pop(0)
index = 1
queue1 = []
while index <= len(queue) - 1:
    queue1.append(queue[index])
queue = queue1

#15

Also queue.shift() works in CoCo python :slight_smile:


#16

I’m getting a different error before that though: cannot read id of undefined on the line after I declare first = queue[0], where I have hero.say(first.id + " out!") What gives?


#17

you need to use:

first=queue.pop(0)

#18

Do you mean instead of first = queue[0]? I tried it that way, and while I don’t get the error, they still only line up, without the command for them to move out working.


#19

Can you send the whole code? There I can help.


#20

@Chaboi_3000 It’s about six posts above. (I implemented your change, trying with and without removing the pop from the end. I’ve also tried a pile of other ideas.)

Thanks.