Mixed Unit tactics help

When I run this code

// 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.

1 Like

Iā€™m sorry but I canā€™t tell what you mean

Which part:

the you have errors?

the only i++ in the if statement?

or the no i++ in the while loop at all?

or the never goes into the loop because 0 is never greater than length of friends?

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

Sooooooooo how would I fix this?

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.) :smile:

JSā€™s not is !

(twenty chars happy now!)

What I and trying to accomplish is to summon all the units in the array in the order that they are listed

Okay I copy pasted the code you provided but itā€™s saying I need a string to check the cost of the thing

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ā€¦

hint

self.built() is an array of what you builtā€¦

This is the code I have at this point

// 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();
}





Shouldnā€™t the gold check work?

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 :smile:

Please read the FAQ and properly format your code so it is readable.

You have to do something like:

if friend.type != "palisade":
    # Do stuff
1 Like

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 :smile: . Iā€™ll be sure that the next time I post everything is in order

My code works, but doesnt command the ā€˜friendsā€™

def summonTroops():
    type=summonTypes[len(self.built) % len(summonTypes)]
    if self.gold >= self.costOf('archer'):
        self.summon('archer')
    elif self.gold >= self.costOf('soldier'):
        self.summon('soldier')
def commandTroops():
    friends=self.findFriends()
    for friend in friends != 'palisade':
        enemies=self.findEnemies()
        for enemy in enemies:
            self.command(friend, "attack", enemy)
def collectCoins():
    coin=self.findNearest(self.findItems())
    self.move(coin.pos)
loop:
    summonTroops()
    commandTroops()
    collectCoins()

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.

did that and i works now, thanks