Lesson 5 - Hide and Seek
Introduction
In the last Quest, you learned how to move your Dragon closer to one enemy Dragon to attack it. This time, there are 3 enemy dragons. DUN DUN DUUUUN!If you stay in one place, you won't be able to defeat them before time runs out. What you'll have to do is teach your Dragon how to move from one enemy Dragon to another... hunting each one down one by one.
Move toward enemy Dragon
You'll need to move your Dragon toward its closest enemy. In the beginning, "Enemy Dragon 1" is closest. After "Enemy Dragon 1" is defeated, the next closest enemy is "Enemy Dragon 2". Then "Enemy Dragon 3".To make your Dragon go from Enemy Dragon 1 to 2 to 3, you can update your
moveTo()
code to look like this:function run(state)
{
// get array of all my units
var myUnits = Helper.getMyUnits(state);
// get the first dragon in the array
var dragon = myUnits[0];
// get first dragon's ID
var dragonId = dragon.id;
// get this dragon's closest enemy
var enemyFound = Helper.getClosestEnemy(state, dragon);
if (!enemyFound)
{
// yay! no more enemies -- nothing left to do.
// calling "return" stops this function from
// executing any other code below.
return;
}
// get enemy dragon's ID
var enemyId = enemyFound.unit.id;
// ********* NEW! **********
// move my Dragon to the enemy Dragon's position
Game.moveTo(dragonId, enemyFound.unit.pos.x, enemyFound.unit.pos.y);
// attack!
Game.attack(dragonId, enemyId);
}
The new code is:
// ********* NEW! **********
// move my Dragon to the enemy Dragon's position
Game.moveTo(dragonId, enemyFound.unit.pos.x, enemyFound.unit.pos.y);
The code in the
run()
function above tells your Dragon to find its closest enemy, then move to exactly where it is. If you ran the code above, your Dragon will not stop until it is nose-to-nose with each enemy Dragon.There are two problems with this: It can be dangerous to move your Dragon too close to an enemy Dragon. Plus, you might still run out of time if you fly all the way to each enemy Dragon. We can do better.
We can move our Dragon just close enough to do damage. I.e., we want to move your Dragon only if it is too far from the enemy. To do this, we need
if-else
conditions.
if-else
conditions
The if-else
statement is a way for you to choose the best code to run -- depending on the condition. For example:if (enemyDragonIsTooFar)
{
// move closer toward enemy
}
else
{
// close enough to enemy -- no need to move
}
Dragon hunter code
Cool, huh? Now let's upgrade your code withif-else
to make your Dragon move smarter. Let's turn your Dragon into a Dragon hunter:function run(state)
{
// get array of all my units
var myUnits = Helper.getMyUnits(state);
// get the first dragon in the array
var dragon = myUnits[0];
// get first dragon's ID
var dragonId = dragon.id;
// get this dragon's closest enemy
var enemyFound = Helper.getClosestEnemy(state, dragon);
if (!enemyFound)
return; // no enemies; nothing left to do!
// get enemy dragon's ID
var enemyId = enemyFound.unit.id;
// ********* NEW! **********
// move my Dragon to the enemy Dragon's position
// (but only if enemy is too far away)
if (enemyFound.dist > 350)
{
// move toward enemy -- it's more than 350 units away
Game.moveTo(dragonId, enemyFound.unit.pos.x, enemyFound.unit.pos.y);
}
else
{
// close enough to enemy -- no need to move
}
// attack!
Game.attack(dragonId, enemyId);
}
Cheatcode
Spoiler alert! Click to reveal full solution.
<!DOCTYPE html>
<html>
<body>
<div id="display">hello</div>
<script>
function run(state)
{
// get array of all my units
var myUnits = Helper.getMyUnits(state);
// get the first dragon in the array
var dragon = myUnits[0];
// get first dragon's ID
var dragonId = dragon.id;
// get this dragon's closest enemy
var enemyFound = Helper.getClosestEnemy(state, dragon);
if (!enemyFound)
return; // no enemies; nothing left to do!
// get enemy dragon's ID
var enemyId = enemyFound.unit.id;
// ********* NEW! **********
// move my Dragon to the enemy Dragon's position
// (but only if enemy is too far away)
if (enemyFound.dist > 350)
{
// move toward enemy -- it's more than 350 units away
Game.moveTo(dragonId, enemyFound.unit.pos.x, enemyFound.unit.pos.y);
}
else
{
// close enough to enemy -- no need to move
}
// attack!
Game.attack(dragonId, enemyId);
}
Game.register(run);
</script>
</body>
</html>