Reindeer Tender Help! [SOLVED]

I need help:

// This is the array of pen positions
var 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.
var penOccupants = [ null, null, null, null, null ];

// And this array contains our reindeer.
var friends = hero.findFriends();

// Figure out which reindeer are already in their pens.
var reindeer = null;
for (var deerIndex = 0; deerIndex < friends.length; deerIndex++) {
    reindeer = friends[deerIndex];
    
    // For each position check if it matches a reindeer.
    for (var penIndex = 0; penIndex < penPositions.length; penIndex++) {
        var penPos = penPositions[penIndex];
        
        if (penPos.x == reindeer.pos.x && penPos.y == reindeer.pos.y) {
            // Put the reindeer in occupants at penIndex
            penOccupants[penIndex] = reindeer;
            // Remove the reindeer from the friends array.
            reindeer = null;
            // break out of the inner loop here:
            break;
        }
    }
}

// Assign the remaining reindeer to new positions.
for (deerIndex = 0; deerIndex < friends.length; deerIndex++) {
    // If the reindeer is null, use continue:
    if(reindeer === null) {
     continue;   
    }
    
    // Look for the first pen with nothing.
    for (var occIndex = 0; occIndex < penOccupants.length; occIndex++) {
        // If there is nothing, the pen is open:
        if (penOccupants[occIndex] === null) {
            // Put the reindeer in the occupants array.
            penOccupants[occIndex] = reindeer;
            // Command the reindeer to move to the pen.
            hero.command(reindeer, "move", penPositions[deerIndex]);
            // break out early so we don't reassign:
            break;
        }
    }
}

Please tell me what I’m doing wrong.

Hi Ozakesus87,

Three changes:

  • Line 23 - change “reindeer = null” to “friends[deerIndex] = null”
  • Line 32 - you need to redefine reindeer, otherwise it will just keep the one reindeer already in the variable, and the hero will command that reindeer to 3 different pens.
  • Line 45 - “hero.command(reindeer, “move”, penPositions[deerIndex])” should be “hero.command(reindeer, “move”, penPositions[occIndex]);”.

Post again if you need more help.

Jenny

I fixed what you said was wrong, and now all the reindeer move. But some of them still go to occupied pens. Is there something else I’m missing?

Can you post your new code?

// This is the array of pen positions
var 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.
var penOccupants = [ null, null, null, null, null ];

// And this array contains our reindeer.
var friends = hero.findFriends();

// Figure out which reindeer are already in their pens.
var reindeer = null;
for (var deerIndex = 0; deerIndex < friends.length; deerIndex++) {
    reindeer = friends[deerIndex];
    
    // For each position check if it matches a reindeer.
    for (var penIndex = 0; penIndex < penPositions.length; penIndex++) {
        var penPos = penPositions[penIndex];
        
        if (penPos.x == reindeer.pos.x && penPos.y == reindeer.pos.y) {
            // Put the reindeer in occupants at penIndex
            reindeer = penOccupants[penIndex];
            // Remove the reindeer from the friends array.
            friends[deerIndex] = null;
            // break out of the inner loop here:
            break;
        }
    }
}

// Assign the remaining reindeer to new positions.
for (deerIndex = 0; deerIndex < friends.length; deerIndex++) {
    // If the reindeer is null, use continue:
    reindeer = friends[deerIndex];
    if(reindeer === null) {
     continue;   
    }
    
    // Look for the first pen with nothing.
    for (var occIndex = 0; occIndex < penOccupants.length; occIndex++) {
        // If there is nothing, the pen is open:
        if (penOccupants[occIndex] === null) {
            // 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;
        }
    }
}

Line 21:

reindeer = penOccupants[penIndex];

This should be the other way round!

Thanks for helping me I solved it now!!!

This topic was automatically closed 12 hours after the last reply. New replies are no longer allowed.