Mixed Unit Tactics C++ help

// 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","archer"];
this.toggleFlowers(false);
this.summonTroops = function() {
    // Use % to wrap around the summonTypes array based on this.built.length
    //var type = summonTypes[???];
    var type = summonTypes[this.built.length % summonTypes.length];
    if(this.gold >= this.costOf(type)) {
        this.summon(type);
    }
    //this.say("I should summon troops!");
};

this.pickUpNearestCoin = function() {
    var coin = this.findNearest(this.findItems());
    if (coin) {
        this.move(coin.pos);
    }
};

this.commandTroops = function() {
    var friends = this.findFriends();
    for(var i=0; i < friends.length; i++) {
        var friend = friends[i];
        var enemy = this.findNearest(this.findEnemies());
        if (friend && enemy && friend.type != 'palisade') this.command(friend, "attack", enemy);
    }
};

while (true) {
    this.summonTroops();
    this.pickUpNearestCoin();
    this.commandTroops();
}

Erorr line 4

erorr line 4 [20 character]

Can you please post your hero and equipment? Also, please label this as #level-help for the Discourse.

What error message are you recieving?

You need to define the functions correctly:

auto functionName () {
      //whatever was in the function before
}

and execute them correctly:

functionName();

and change all of the this to hero (don’t know why this made a difference)
and change the brackets around ["soldier","archer"] to curvy brackets ( { and } )
and change hero.built.length % summonTypes.length to hero.built.size() % summonTypes.size()
and then change the 'palisade' to "palisade"
and finally change while (true) to int main()
and then there are no more errors I think. But your hero freezes after doing each function once. I think it has something to do with the int main() but I don’t know how to fix it. (c++ is wack)

Also please don’t try to copy other people’s code; just go back to previous levels and learn how to use loops and stuff in c++

Summary
1 Like

put where (20 characters)

nvm i have new code

int main() {
    int soldiers = 20;
    int archers = 10;
    int ogres = 20;
    
    while (ogres > 0) {
        if (soldiers > 0) {
            ogres -= soldiers;
            soldiers -= ogres * 0.5;
        } else if (archers > 0) {
            ogres -= archers * 2;
            archers -= ogres * 0.5;
        } else {
            auto << "abc" << endl;
            return 0;
        }
    }
    
    auto << "abcd" << endl;
    
    return 0;
}

erorr line 19

its said auto not defined

nvm new code\

// 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
}
auto command(){
    
}

auto collect(){
    
}

auto attack(){
    
}

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


can u give me summon code line 6

nvm new code

// 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
}
auto command(){
    
}

auto collect(){
    
}

auto attack(){
    
}

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


can u give me summon code line 6