There are no errors in my code but somehow I still cannot protect the minefields or survive in 30 seconds.
Source code:
// The goal is to survive for 30 seconds, and keep the mines intact for at least 30 seconds.
this.chooseStrategy = function() {
var enemies = this.findEnemies();
// If you can summon a griffin-rider, return "griffin-rider"
if (this.gold >= this.costOf("griffin-rider")) {
return "griffin-rider";
}
// If there is a fangrider on your side of the mines, return "fight-back"
for (var i = 0; i < enemies.length; i++) {
var enemy = enemies[i];
if (enemy.type == "fangrider") {
return "fight-back";
} else {
return "collect-coins";
}
}
// Otherwise, return "collect-coins"
};
this.commandAttack = function() {
// Command your griffin riders to attack ogres.
var friends = this.findFriends();
for (var j = 0; j < friends.length; j++) {
var friend = friends[j];
var enemy = friend.findNearest(friend.findEnemies());
if (enemy && enemy.type != "fangrider") {
this.command(friend, "attack", enemy);
}
}
};
this.pickUpCoin = function() {
// Collect coins
var items = this.findItems();
var nearest = this.findNearest(items);
if (nearest) {
this.move(nearest.pos);
}
};
this.heroAttack = function() {
// Your hero should attack fang riders that cross the minefield.
var enemy = this.findNearest(this.findEnemies());
if (enemy && enemy.type == "fangrider" && enemy.pos.x < 38) {
if (this.isReady("bash")) {
this.bash(enemy);
} else {
this.attack(enemy);
}
}
};
loop {
this.commandAttack();
var strategy = this.chooseStrategy();
// Call a function, depending on what the current strategy is.
if (strategy == "griffin-rider") {
this.summon("griffin-rider");
} else if (strategy == "fight-back") {
this.heroAttack();
} else if (strategy == "collect-coins") {
this.pickUpCoin();
}
}