Mixed unit tactics [SOLVED]

// Practice using modulo to loop over an array
auto summonTypes = {"soldier", "archer"};
auto summonTroops() {
    // Use % to wrap around the summonTypes array based on hero.built.length
    auto type = summonTypes[hero.built.length % summonTypes.length];
    if(hero.gold >= hero.costOf(type)){
        hero.summon(type);
    }
}

auto command(){
    auto friends = hero.findFriends();
    for(int i = 0; i < friends.length; i ++){
        auto friend = friends[i];
        auto enemy = friend.findNearestEnemy();
        if(enemy && (friend.type != "palisade")){
            hero.command(friend, "attack", enemy);
        }
    }
}

auto collect(){
    auto item = hero.findNearestItem();
    if(item && hero.isReady("jump")){
        hero.jumpTo(item.pos);
    }else{
        hero.move(item.pos);
    }
}


auto attack(auto enemy){
    if(enemy){
        if(hero.isReady("jump")){
            hero.jumpTo(enemy.pos);
        }else if(hero.isReady("bash")){
            hero.bash(enemy);
        }else{
            hero.attack(enemy);
        }
    }
}

auto tactic(){
    if(hero.time > 30){
        attack(hero.findNearestEnemy());
    }else{
        while(true) {
            collect();
        }
    }
}


int main() {
    // Choose the mix and order of units you want to summon by populating this array:
    while(true) {
        summonTroops();
        command();
        tactic();
    }
    return 0;
}

Soldier not attacking enemies, what wrong with my code.

auto tactic(){
    if(hero.time > 30){
        attack(hero.findNearestEnemy());
    }else{
        while(true) {
            collect();
        }
    }
}

There shouldn’t be a while true loop around collect(); otherwise the hero would keep collecting forever and not command soldiers.

Thanks! @JustALuke
Solved the level, even got the bonus!

This topic was automatically closed 12 hours after the last reply. New replies are no longer allowed.