[SOLVED] Reindeer tender help

# This is the array of pen positions
penPositions = [ {"x":20,"y":24}, {"x":28,"y":24}, {"x":36,"y":24}, {"x":44,"y":24}, {"x":52,"y":24} ]

# Use this array to keep track of each pen's reindeer.
penOccupants = [ None, None, None, None, None, ]

# And this array contains our reindeer.
friends = hero.findFriends()

# Figure out which reindeer are already in their pens.
for deerIndex in range(len(friends)):
    reindeer = friends[deerIndex]
    
    # For each position check if it matches a reindeer.
    for penIndex in range(len(penPositions)):
        penPos = penPositions[penIndex]
        if penPos.x == reindeer.pos.x and penPos.y == reindeer.pos.y:
            # Put the reindeer in occupants at penIndex
            reindeer = penOccupants[penIndex]
            # Remove the reindeer from the friends array.
            friends.remove(reindeer)
            # break out of the inner loop here:
            break
            pass

# Assign the remaining reindeer to new positions.
for deerIndex in range(len(friends)):
    # If the reindeer is null, use continue:
    if reindeer is None:
        continue
    
    # Look for the first pen with nothing.
    for occIndex in range(len(penOccupants)):
        # If there is nothing, the pen is open:
        if not penOccupants[occIndex]:
            # Put the reindeer in the occupants array.
            penOccupants[occIndex] = reindeer
            # Command the reindeer to move to the pen.
            hero.say(reindeer.id)
            # break out early so we don't reassign:
            break
            pass

My hero only goes through one pen, and then stops. I think I have something wrong with my loops and I can’t exactly find where.

The first issue I see is on line 19. What is intended is to populate penOccupants with the coordinates of the reindeer that is in that specific stall. If there is no reindeer, that index is already assigned the value None, so no change needed.

Line 21. Here, you don’t want to truly ‘remove’ the element, but set it to None, instead.

Line 29. Where have you defined reindeer? Remember, you already have an array containing a list of your friends and at this point, you are iterating thru that list.

for the line 21, do you mean like reindeer = None

Kind of, yes…like with 29, you have an array, so you want to set that specific index to None.

I don’t quite get what you mean for line 29. Could you explain more?

Certainly…on line 8, you define friends; this is an array that lists all of the reindeer. You use ‘friends’ in several places throughout the code. You can manipulate a specific element of this array, simply by naming it…something like:

for x in range(len(friends)):
    if someCriteria = thatCriteria:
        friend[x] = somethingElse

I’m intentionally trying to be vague, but am hoping to help get the concept across. The code you need is nothing like the demo I just showed, but the concept is the same. If you need something more explicit, I will PM it to you.

I think I did something wrong while trying to follow your directions.

# This is the array of pen positions
penPositions = [ {"x":20,"y":24}, {"x":28,"y":24}, {"x":36,"y":24}, {"x":44,"y":24}, {"x":52,"y":24} ]

# Use this array to keep track of each pen's reindeer.
penOccupants = [ None, None, None, None, None, ]

# And this array contains our reindeer.
friends = hero.findFriends()

# Figure out which reindeer are already in their pens.
for deerIndex in range(len(friends)):
    reindeer = friends[deerIndex]
    
    # For each position check if it matches a reindeer.
    for penIndex in range(len(penPositions)):
        penPos = penPositions[penIndex]
        if penPos.x == reindeer.pos.x and penPos.y == reindeer.pos.y:
            # Put the reindeer in occupants at penIndex
            reindeer = penOccupants[penIndex]
            # Remove the reindeer from the friends array.
            reindeer = None
            # break out of the inner loop here:
            break
            pass

# Assign the remaining reindeer to new positions.
for deerIndex in range(len(friends)):
    # If the reindeer is null, use continue:
    if not friends[deerIndex]:
        continue
    reindeer = friends[deerIndex]
    # Look for the first pen with nothing.
    for occIndex in range(len(penOccupants)):
        # If there is nothing, the pen is open:
        if not penOccupants[occIndex]:
            # Put the reindeer in the occupants array.
            penOccupants[occIndex] = reindeer
            # Command the reindeer to move to the pen.
            hero.command(reindeer, "move", penPositions[occIndex])
            # break out early so we don't reassign:
            break
            pass

My hero now commanded all the reindeer, including the ones already in a pen, to move into a pen. level completion status is incomplete

as it turns out, doing that counts as incomplete when you run it, but submitting it works.

1 Like