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

Mixed-Unit-Tactics

#1

I don’t know what to do I tried to do it on my own and I cant figure out the “palaside” part of the 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","archer"];

function summonTroops() {
    // Use % to wrap around the summonTypes array based on hero.built.length
    //var type = summonTypes[???];
    var type = summonTypes[hero.built.length % hero.summonTypes.length];
    if (hero.gold >= hero.summon(type)) {
        hero.summon(type);
    }
}
function commandTroops(){
    var friends = hero.findFriends();
    var enemies = hero.findEnemies();
    for (var i = 0, i<friends.length, i++){
        var friend = friends[i];
        var enemy = hero.findNearest(enemies);
        if (friend.type == "palisade") {
            if (enemy) {
                hero.command(friend, "attack", enemy);
            }
        }
    }
}
function pickUpCoins(){
    var coins = hero.findItems();
    var coin = hero.findNearest(coins);
    if (coin) {
        hero.move(coin.pos);
    }
}

while(true) {
    summonTroops();
    commandTroops();
    pickUpCoins();
}


#3

A palisade can’t attack.

#4

what do you mean. It says in the hints that I should have type == “palisade”

#5

It says skip over friends who have type “palisade”, not to include them and not command any other types. (Hint: use != which means “doesn’t equal”)
:lion: :lion: :lion:

#6

so I made a few changes to my code and now when i run it, the soldiers will attack but my hero wont pick up coins until the enemies are attacking me.

// 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","archer"];

function summonTroops() {
    // Use % to wrap around the summonTypes array based on hero.built.length
    //var type = summonTypes[???];
    var type = summonTypes[hero.built.length % summonTypes.length];
    if (hero.gold > hero.summon(type)) {
        hero.summon(type);
    }
}
function commandTroops(){
    var friend = hero.findNearest(hero.findFriends());
    var enemy = hero.findNearest(hero.findEnemies());
        if (enemy && friend.type != "palisade"){
            if (friend){
                hero.command(friend, "attack", enemy);
        }
    }
}
function pickUpCoin() {
    // Collect coins
    var items = hero.findItems();
    var item = hero.findNearest(items);
        if (item) {
            hero.move(item.pos);
    }
}

while(true) {
    summonTroops();
    commandTroops();
    pickUpCoin();
}



#7

Call the pickUpCoin(); function first

#8

he still does the same thing

1 Like
#9

I also have this problem if i put everything in functions. Paste the code for pickUpCoin(); in the while loop

#10

he still wont move, he just stands there until the enemies get close

#11

I think palisade hurts enemies on contact?

#12


:slight_smile:

#13

Self-contradiction oof

Haven’t been on CC for a long time