Play Home Contribute Github Join Us! Discourse Staff Members Github Contribution Guides Team

Help on Antipodes [ SOLVED]

#1

So I am unsure as to why there is a code error on the code that it has given me. But when I sent my friend the code it worked on his but not mine:

// The warlock used the "clone" spell and created evil antipodes of our archers.
// But even that evil spell has weakness.
// If your archer touches his antipode, then it will disappear.
// If an archer touches the wrong clone or attacks one of them, then the clones start to fight.
// We can find antipodes by their names - they are each other's reverse.
// This function check two units whether they are antipodes or not.
function areAntipodes(unit1, unit2) {
    var reversed1 = unit1.id.split("").reverse().join(""); // error here
        for(var i =0; i < units.length; i++){
        // Iterate all units again with indexes "j".
        var iUnits = units[i];
        for(var j=0; j<units.length; j++){
            // If "i" is equal to "j", then skip (continue).
            if (i==j) {
                continue;
            }
            // Find the distance between the i-th and j-th units.
            var jUnits = units[j];
            var dist = iUnits.distanceTo(jUnits);
            // If the distance less than 'minDistance':
            if (dist < minDistance) {
                // Reassign 'minDistance' with the new distance.
                minDistance = dist;
                // Reassign 'nearestPair' to the names
                // of the current pair of units.
                nearestPair = [iUnits.id, jUnits.id];
            }
        }
    }
    return reversed1 === unit2.id;
}
var friends = hero.findFriends();
var enemies = hero.findEnemies();
// Find antipodes for each of your archers.
// Iterate all friends.
for (var friend = 0; friend < friends.length; friend++) {
    // For each of friends iterate all enemies.
    for (var enemy = 0; enemy < enemies.length; enemy++) {
        // Check if the pair of the current friend and the enemy are antipodes.
        if (areAntipodes(friend, enemy)) {
            // If they are antipodes, command the friend move to the enemy.
            hero.command(friend, 'move', enemy.pos);
        }
    }
}
// When all clones disappears, attack the warlock.
var enemies = hero.findEnemies();
while (enemies.length == 1) {
    hero.attack(hero.findNearestEnemy());
}

#2

@Johnny_Fuentes-McClu

Sometimes where the ‘debugger tool’ says there is a problem is not where the code actually needs to be changed.

When looking at your loops that cycle through the enemies and friends, what values are you sending into the function areAntipodes()? Are the values numeric, an object, or something else?

You can use hero.say(); to print the output from a variable to the screen like this:

hero.say(friend);
1 Like
#3

I get it now. I keep saying numbers when I need to say names instead right? So to complete this level you need to “convert” the numbers to names right?

#4

so now when i run it my hero wont attack the warlock

#5

I noticed that your while check is looking for exactly 1 enemy, but the skeletons pop up and make your array more than one which will prevent your hero from attacking the nearest enemy. I’d suggest a check to see if there are no more enemy archers or switch the while to an if check and add the while loop while just for attacking.

while (enemies.length == 1)
#6

i don’t think i understand what you mean by “checking to see if there are no more enemy archers”
and the “adding the while loop while just for attacking.” parts

#7

I understand the concepts your trying to tell me but not how I would get that done.

#8

Ok, not a problem, I’ll show you what I mean. Are you familiar with the hero.findByType()? You can look only for enemy archers using the code below. This will give you an array of all the enemy archers.

archers = hero.findByType("archer", hero.findEnemies())

Then see if there are any enemy archers left with:

if len(archers) == 0:

The findNearest() method may find the skeletons which makes it difficult to attack the warlock. You may want to focus your attack on the Warlock when the skeletons show up using the same findByType method. Adding the [0] at the end will make the variable only see the first object of the array so it is not an array variable and you can call the attack method on the warlock.

warlock = hero.findByType("warlock", hero.findEnemies())[0]
#9

Ok that makes a lot of sense actually. I didn’t know that by adding things in the hero.findByType() method you could specify certain allies or enemies. Thank you. knowing that will help me out in the future.

#10

well now my code runs but he moves at the begging and causes me to fail:

Updated code:

// The warlock used the "clone" spell and created evil antipodes of our archers.
// But even that evil spell has weakness.
// If your archer touches his antipode, then it will disappear.
// If an archer touches the wrong clone or attacks one of them, then the clones start to fight.
// We can find antipodes by their names - they are each other's reverse.
// This function check two units whether they are antipodes or not.
function areAntipodes(unit1, unit2) {
    var reversed1 = unit1.id.split("").reverse().join("");
    return reversed1 === unit2.id;
}
var friends = hero.findFriends();
var enemies = hero.findEnemies();
// Find antipodes for each of your archers.
// Iterate all friends.
for (var friend = 0; friend < friends.length; friend++) {
    var good = friends[friend];
    // For each of friends iterate all enemies.
    for (var enemy = 0; enemy < enemies.length; enemy++) {
        var bad = enemies[enemy];
        // Check if the pair of the current friend and the enemy are antipodes.
        var antiPode = areAntipodes(good, bad);
        if (antiPode) {
            // If they are antipodes, command the friend move to the enemy.
            hero.command(good, "move", bad.pos);
        }
    }
}
// When all clones disappears, attack the warlock.
var enemyArchers = hero.findByType("archcer", hero.findEnemies());
while (enemyArchers.length === 0) {
    var warlock = hero.findByType("warlock", hero.findEnemies())[0];
    if (warlock) {
        hero.attack(warlock);
    }
}

#11

This might just be a spelling error. Your “archer”, in the findByType is spelled incorrectly.

var enemyArchers = hero.findByType("archcer", hero.findEnemies());
#12

but when I change it he still wont attack the warlock.

#13

You have all of the pieces, just need to shuffle them a bit. Add a while (true) loop before all of the code with the enemyArchers and then switch the while (enemyArchers.length) to an if statement.

Since you have to wait for your archers to match up with their antipodes, you need to loop through until they dealt with their double. And then continue the loop to attack the warlock.

If you need more detail - Spoiler for last section
while (true){ 
    var enemyArchers = hero.findByType("archer", hero.findEnemies());
    if (enemyArchers.length === 0) {
        var warlock = hero.findByType("warlock", hero.findEnemies())[0];
        if (warlock) {
            hero.attack(warlock);
        }
    }
}

#14

That makes sense. I forgot that if the variables aren’t inside the while loop they will only pop up once and that’s it. Thank you. Ill make sure to keep that in mind for the future.