// Practice using modulo to loop over an array
// Choose the mix and order of units you want to summon by populating this array:
var summonTypes = ["soldier","soldier","soldier","archer","griffin-rider"];
var i = 0;
this.summonTroops = function() {
// Use % to wrap around the summonTypes array based on this.built.length
while (i < summonTypes.length) {
var type = summonTypes[i];
if(this.costOf(type) >= this.gold){
this.summon(type);
i++;
}
}
};
this.commadToops = function(){
var enemy = this.findNearest(this.findEnemies());
var friends = this.findFriends();
var i = 0;
while(i > friend.length){
var soldie = friends[i];
if(enemy && soldie){
this.command(soldie, "attack", enemy);
}
}
};
this.collectCoins = function() {
var item = this.findNearest(this.findItems());
var x = item.pos.x;
var y = item.pos.y;
if(item){
this.moveXY(x, y);
}
};
loop{
this.collectCoins();
this.summonTroops();
this.commandTroops();
}
It says that the hard execution loop of 3000000 exceeded I think this a failure in the API protection
It is nice of you to think that, but it is more likely errors in your codeā¦
such as:
while (i < summonTypes.length) {
var type = summonTypes[i];
if(this.costOf(type) >= this.gold){
this.summon(type);
i++;
}
}
Which seems like it will loop forever, since āiā only changes when you summon somethingā¦
What is the value of āiā anyway? it is not set to zero inside the function. Does javascript auto zero things? or is it an ever increasing global variable? (there is a āglobalā var i= 0;)
There is also this function:
var i = 0;
while(i > friend.length){
var soldie = friends[i];
if(enemy && soldie){
this.command(soldie, "attack", enemy);
}
}
No change in āiā here at all, but that is OK, in the sense that, it (i=0) should never be ā>ā than friends.length therefore it never goes into the loop.
In this part I mean I only increment when I actually do the summon
Shouldnāt the function just pass without doing anything if the if condition is not met
Lets say you have 5 gold when you enter the summon functionā¦
while i < number of summonables
If not enough gold to summon first type
never increment i
i always and forever less number of summonables
while-loop never ends
The while-loop, loops so long as the condition is met.
The if, only fires if the condition is met.
You need to change it so it does what ever you trying to accomplishā¦?
What are you trying to accomplish?
If you want to check and summon all things on the list that you have gold to summon, then just move the increment out of the loop. and DONE, it will check each item in turn summoning what you have the money for starting with your prefered units (the order checked) and when it has checked and summoned one of each (that you can afford) it will exit the while loop.
while (i < summonTypes.length) {
var type = summonTypes[i];
if(this.costOf(type) >= this.gold){
this.summon(type);
}
i++;
}
That is what Iād guess you had in mind. Your comment about it passing if the condition is not met almost makes me think you only want it to check the first thing in the summon list, which doesnāt really make sense as then you wouldnāt have bothered to make a list.
(another possibility) If you want it to only summon one unit, the first thing on the list that you can afford then you have to add some sort of success variable to the while.
notSummoned = true;
while (i < summonTypes.length && notSummoned) {
var type = summonTypes[i];
if(this.costOf(type) >= this.gold){
this.summon(type);
notSummoned = false;
}
i++;
}
Iād have used the variable as āsummonedā but I couldnāt remember if javascriptās not was ā!summonedā or ānot summonedā, so if you prefer āpositive logicā then feel free to switch all the assignments and fix the name and condition.)
Hmm, you still have the āi=0ā?
hmm, it shouldnāt say that unless āvar type = summonTypes[i]ā is somehow pointing to something that is not āunitā.
To summon one of each unit in order you either need to have enough money FIRST, before you run the function or you need to ārememberā where you left off last time you ran out of money, so when you come back you start there instead of starting over at the top of the listā¦
// Practice using modulo to loop over an array
// Choose the mix and order of units you want to summon by populating this array:
this.summonTroops = function() {
// Use % to wrap around the summonTypes array based on this.built.length
summonTypes = ["soldier","soldier","soldier","archer","griffin -rider"];
i = 0;
notSummoned = true;
while (i < summonTypes.length && notSummoned) {
var type = summonTypes[i];
if(this.costOf(type) >= this.gold){
this.summon(type);
notSummoned = false;
}
i++;
}
};
this.commadToops = function(){
var enemy = this.findNearest(this.findEnemies());
var friends = this.findFriends();
var i = 0;
while(i > friend.length){
var soldie = friends[i];
if(enemy && soldie){
this.command(soldie, "attack", enemy);
}
}
};
this.collectCoins = function() {
var item = this.findNearest(this.findItems());
var x = item.pos.x;
var y = item.pos.y;
if(item){
this.moveXY(x, y);
}
};
loop{
this.collectCoins();
this.summonTroops();
this.commandTroops();
}
Do you have the boss-star 3? otherwise you cant summon griffin riders
Need to fix the spacing āgriffin -riderā if so
You were supposed to use the % operator to loop around the array
you are looping i back to 0, so my guess from reading it is you will only get soldiers unless you have gold for 3 soldiers and an archer
this.commadToops = function() should be this,commandTroops
and you have solie instead of solider but it should still work anyways
Hi there! Iām sorry to bother but Iām having a few errors with this piece of code
var friends = this.findFriends();
My issue seems to lie here
this.command(friend, "move", ({x:43, y:37}));
var enemy = this.findNearest(this.findEnemies());
if (enemy){
this.command(friend, "attack", enemy);
}
It says that I am trying to control the accompanying palisades as if they were movable units. If someone could help explain to me how to correct this it would be much appreciated
My apologies about the legibility of my code, it was my first time posting on the forums.
But thank you for putting up with it because youāve really helped me out . Iāll be sure that the next time I post everything is in order
In commandTroops, you have an unconventional for-loop that doesnāt work. You should loop over all friends, then check if the friend type is not a palisade.