When I play this level my Griffin Riders just get completely destroyed by the enemies within about 15 seconds and then they hit the mine field. Can someone look at my code and see where I can improve? I’m stumped.
// The goal is to survive for 30 seconds, and keep the mines intact for at least 30 seconds.
this.toggleFlowers(false);
this.chooseStrategy = function() {
var enemies = this.findEnemies();
var fangrider = this.findNearest(this.findByType("fangrider"));
if(this.gold >= this.costOf("griffin-rider")){
this.summon("griffin-rider");
}
if(fangrider && fangrider.pos.x < 36){
return "fight-back";
} else {
return "collect-coins";
}
};
this.commandAttack = function() {
var griffins = this.findByType("griffin-rider");
var enemies = this.findEnemies();
for(var i = 0; i < griffins.length; i++){
var griffin = griffins[i];
var enemy = griffin.findNearest(enemies);
if(enemy && enemy != "fangrider"){
this.command(griffin, "attack", enemy);
}
}
};
this.pickUpCoin = function() {
var coin = this.findNearest(this.findItems());
if(coin){
this.move(coin.pos);
}
};
this.heroAttack = function() {
// Your hero should attack fang riders that cross the minefield.
var fangrider = this.findNearest(this.findByType("fangrider"));
this.attack(fangrider);
};
loop {
this.commandAttack();
var strategy = this.chooseStrategy();
if(strategy === "fight-back"){
this.heroAttack();
} else if(strategy === "collect-coins"){
this.pickUpCoin();
}
}