If I were to type the following line of Javascript
var best;
how would it translate to Python?
If I were to type the following line of Javascript
var best;
how would it translate to Python?
best = None
this is how you should right it i think
Thanks (post must be 20 characters)
// Ogres are trying to take out your reindeer!
// Keep your archers back while summoning soldiers to attack.
hero.pickUpCoin = function() {
// Collect coins.
if (hero.findNearestItem()) {
var coin = hero.findNearestItem();
hero.moveXY(coin.pos.x, coin.pos.y);
}
};
hero.summonTroops = function() {
// Summon soldiers if you have the gold.
if (hero.gold >= hero.costOf("soldier")) {
hero.summon("soldier");
}
};
// This function has an argument named soldier.
// Arguments are like variables.
// The value of an argument is determined when the function is called.
hero.commandSoldier = function(soldier) {
// Soldiers should attack enemies.
var soldiers = hero.findByType("soldier", hero.findFriends());
if (soldiers) {
for (var i = 0; i <= soldiers.length; i++) {
var soldier1 = soldiers[i];
var enemy = hero.findNearestEnemy();
if (enemy) {
hero.command(soldier1, "attack", enemy);
}
}
}
};
// Write a commandArcher function to tell your archers what to do!
// It should take one argument that will represent the archer passed to the function when it's called.
// Archers should only attack enemies who are closer than 25 meters, otherwise, stay still.
hero.commandArcher = function() {
var archers = hero.findByType("archer", hero.findFriends());
for (var i = 0; i <= archers.length; i++) {
var archer = archers[i];
if (archer) {
var enemy = archer.findNearestEnemy();
if (enemy && archer.distanceTo(enemy) < 25) {
hero.command(archer, "attack", enemy);
}
}
}
};
while(true) {
hero.pickUpCoin();
hero.summonTroops();
var friends = hero.findFriends();
for(var i=0; i < friends.length; i++) {
var friend = friends[i];
if(friend.type == "soldier") {
// This friend will be assigned to the variable soldier in commandSoldier
hero.commandSoldier(friend);
} else if(friend.type == "archer") {
// Be sure to command your archers.
hero.commandArcher();
}
}
}
Whenever I press run, it pops up with an error in the function to command soldiers that there was no enemy even when I placed it in an if loop. Level is Hunters and Prey.
change this to
hero.commandSoldier = function(soldier) {
// Soldiers should attack enemies.
var enemy = soldier.findNearestEnemy();
if(enemy){
hero.command(soldier,"attack",enemy);
}
};
and do the same for the archers
it should be hero.commandArcher(friend); inside the if statement instead of hero.commandArcher only
it should be function (archer)
delete all of this and type
var enemy = archer.findNearestEnemy();
if(enemy&&archer.distanceTo(enemy)<=25){
hero.command(archer,"attack",enemy);
}
put 2 parenthesis after soldier.findNearestEnemy so it will be soldier.findNearestEnemy();
Oh I didn’t catch that, thanks
you are welcome
(Post must be at least 20 characters)
I still do not get the reason behind the for loops and stuff, can you explain this to me?
the for loop is the same thing as a while loop, it just have extra parts like the var i = 0 and the i++, it helps you do a task for a number of times, inside it you will see that we are looking at the type of the current friend to decide if he is a soldier of archer and command him
So for loops are basically complicated versions of the loop 5: thing in which it would loop the given code 5 times
yes, and you can also say that they are while loops but with extra parts