Reindeer tender-adventurer


#1

i was trying out reindeer tender when it started giving me this error message

Error Message

Fix Your Code
Try self.pos
Line 14: Cannot read property ‘pos’ of undefined

# 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.
assignments = [ 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 posIndex in range(len(penPositions)):
        penPos = penPositions[posIndex]
        if penPos.x == reindeer.pos.x and penPos.y == reindeer.pos.y:
            # Put the reindeer in the assignments array in slot posIndex.
            assignments[posIndex] = reindeer
            # 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.
    if not reindeer:
        continue
    # Look for the first pen with nothing already assigned to it.
    for posIndex in range(len(penPositions)):
        # If there's nothing in this slot of the assignments array, then the pen is open.
        if not assignments[posIndex]:
            # Put the reindeer in the assignments array.
            assignments[posIndex] = reindeer
            # Command the reindeer to move to the pen position.
            hero.command(reindeer, "move", penPositions[posIndex])
            # break out of the inner for loop here so we don't reassign the deer.
            break


#2

the weird thing is that if i replaced reindeer.pos.x with self.pos.x it didn’t have a problem with ```reindeer.pos.y


#3

From the error message and what you said I am guessing that you are over itereating. self.say(reindeer) every loop to make sure what are looking for exists.

I am not sure how the complier handles this bit of code where you change the length of the array you are using to interate, but I am guessing that is where the problem is. It would also explain why using self.pos.x would make it work (becuase the code would never run)

friends = self.findFriends
for i in range(len(friends))
  if condition:
    friends.remove(friends[i])

#4

then why does it not have a problem with reindeer.pos.y?


#5

Because pen.pos.x is self.pos.x will never evaluate as true.
Python stops evaluating an if statement the moment it cannot be true. Thus you can do things like this:
if enemy and self.distanceTo(enemy) < 10
as the code stops with if enemy.

You may be able to fix your code by checking for reindeer first on line 14:
if reindeer and penPos.x is reindeer.pos.x and penPos.y is reindeer.pos.y:
It’s a lazy fix, but it may work or not depending on what the problem is, which is why it is a good idea to self.say(things) when you have errors. It helps isolate the problem so you can work on a solution.


#6

Maybe there’s a way to “remove” friends without changing the size of the friends array? The comment there could probably be phrased better.