Reindeer Tender Help


#1

Hey Guys,

I have been trying this level for a couple days, I think I know where my problem is, but I don’t quite know how to write it the way it should be written. I think I am having challenges either removing the reindeer from the friends array, or the code on the bottom that checks if the pen is empty. Anyways, my code is below.

# This array contains the positions of the pens that we want to put the reindeer in.
penPositions = [ {"x":20,"y":24}, {"x":28,"y":24}, {"x":36,"y":24}, {"x":44,"y":24}, {"x":52,"y":24} ]

# This array is used to track which reindeer have been asssigned to which pen.
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]

    # Go through each position and see if it matches the reindeer's position.
    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 the assignments array in slot posIndex.
            reindeer = penOccupants[penIndex]
            # Remove the reindeer from the friends array.
            friends.remove(reindeer)
            # break out of the inner loop here to avoid confusion.
            break
            pass

# Assign the remaining reindeer to new positions.
for deerIndex in range(len(friends)):
    # If the reindeer in this array slot is null, skip this and continue to the next one.
    reindeer = friends[deerIndex]
    if not friends[deerIndex]:
        pass
    # Look for the first pen with nothing already assigned to it.
    for occIndex in range(len(penOccupants)):
        # If there's nothing in this slot of the assignments array, then the pen is open.
        if not penOccupants[occIndex]:
            # Put the reindeer in the assignments array.
            penOccupants[occIndex] = reindeer
            # Command the reindeer to move to the pen position.
            hero.command(reindeer, "move", penPositions[occIndex])
            # break out of the inner for loop here so we don't reassign the deer.
            break
        pass

#2

The reindeer seem to get assigned to random pens, some with sleeping reindeer inside.


#3

You need “# Put the reindeer in the assignments array in slot posIndex.”


#4

I changed my code to this but it didnt work, posIndex isnt initially defined, so I don’t necessarily understand where to declare it and how to manipulate it if we are supposed to be keeping track of penIndex in this for loop.

# This array contains the positions of the pens that we want to put the reindeer in.
penPositions = [ {"x":20,"y":24}, {"x":28,"y":24}, {"x":36,"y":24}, {"x":44,"y":24}, {"x":52,"y":24} ]

# This array is used to track which reindeer have been asssigned to which pen.
penOccupants = [ None, None, None, None, None, ]
posIndex = 0
# 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]

    # Go through each position and see if it matches the reindeer's position.
    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 the assignments array in slot posIndex.
            reindeer = penOccupants[posIndex]
            # Remove the reindeer from the friends array.
            friends.remove(reindeer)
            # break out of the inner loop here to avoid confusion.
            posIndex += 1    
            break
            pass

#5

Looks like there is a mistake in the comment. But I meant another problem

penOccupants[penIndex] = reindeer;

instead

reindeer = penOccupants[penIndex]