[Adventurer] Queue Manager

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.

4 Likes

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

2 Likes

@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:
        

2 Likes

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

3 Likes

Oh sorry I found my mistake.

3 Likes

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?

2 Likes

You have to do

first=queue.pop(0)
1 Like

Okay. Thanks! (20 characters)

1 Like

If you need help call @Chaboi_3000.

1 Like

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?

1 Like

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

1 Like

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

1 Like

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)
1 Like

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
2 Likes

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

2 Likes

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?

1 Like

you need to use:

first=queue.pop(0)
2 Likes

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.

1 Like

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

2 Likes

@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.

1 Like