I don’t know what I did wrong. Can somebody please help?
My code is below:
> // The goal is to survive for 30 seconds, and keep the mines intact for at least 30 seconds.
>
> function chooseStrategy() {
> var enemies = hero.findEnemies();
> // If you can summon a griffin-rider, return "griffin-rider"
> if (hero.gold >= hero.costOf("griffin-rider")) {
> return "griffin-rider";
> }
> // If there is a fangrider on your side of the mines, return "fight-back"
> var fangrider = hero.findByType("fangrider");
> if (fangrider) {
> return "fight-back";
> }
> // Otherwise, return "collect-coins"
> else {
> return "collect-coins";
> }
> }
>
> function commandAttack() {
> // Command your griffin riders to attack ogres.
> var friends = hero.findByType("thrower", hero.findFriends());
> for (var i = 0; i <= friends.length; i += 1){
> var friend = friends[i];
> var enemy = hero.findNearestEnemy();
> if (friend) {
> if (enemy && enemy.type == "ogre") {
> hero.command(friend, "attack", enemy);
> }
> }
> }
> }
>
> function pickUpCoin() {
> // Collect coins
> var coin = hero.findNearestItem();
> if (coin) {
> hero.moveXY(coin.pos.x, coin.pos.y);
> }
> }
>
> function heroAttack() {
> // Your hero should attack fang riders that cross the minefield.
> var enemy = hero.findNearestEnemy();
> if (enemy && enemy.type == "fangrider") {
> hero.attack(enemy);
> }
> }
>
> while(true) {
> commandAttack();
> var strategy = chooseStrategy();
> // Call a function, depending on what the current strategy is.
> if (strategy == "griffin-rider") {
> hero.summon("griffin-rider");
> commandAttack();
> }
> else if (strategy == "fight-back") {
> heroAttack();
> }
> else if (strategy == "collect-coins") {
> pickUpCoin();
> }
> }
> function commandAttack() {
> // Command your griffin riders to attack ogres.
> ` var friends = hero.findByType("thrower", hero.findFriends());`
> for (var i = 0; i <= friends.length; i += 1){
> var friend = friends[i];
> var enemy = hero.findNearestEnemy();
> if (friend)
> if (enemy && enemy.type == "ogre")
> hero.command(friend, "attack", enemy);
> }
> }
seems wrong to me. Your enemies aren’t your friends:
> // Command your griffin riders to attack ogres.
> ` var friends = hero.findByType("thrower", hero.findFriends());`
I think I know whats wrong. My hero doesn’t collect coins, so I keep running out of griffin riders. But I don’t understand why my hero isn’t collecting coins. Is there something wrong with the code below?
function chooseStrategy() {
var enemies = hero.findEnemies();
// If you can summon a griffin-rider, return "griffin-rider"
if (hero.gold >= hero.costOf("griffin-rider")) {
return "griffin-rider";
}
// If there is a fangrider on your side of the mines, return "fight-back"
var fangrider = hero.findByType("fangrider");
if (fangrider) {
return "fight-back";
}
// Otherwise, return "collect-coins"
else {
return "collect-coins";
}
}
function commandAttack() {
// Command your griffin riders to attack ogres.
var friends = hero.findByType("griffin-rider", hero.findFriends());
for (var i = 0; i <= friends.length; i += 1){
var friend = friends[i];
var enemy = hero.findNearestEnemy();
if (friend) {
if (enemy && enemy.type != "fangrider") {
hero.command(friend, "attack", enemy);
}
}
}
}
function pickUpCoin() {
// Collect coins
var coin = hero.findNearestItem();
if (coin) {
hero.moveXY(coin.pos.x, coin.pos.y);
}
}
function heroAttack() {
// Your hero should attack fang riders that cross the minefield.
var enemy = hero.findNearestEnemy();
if (enemy && enemy.type == "fangrider") {
if (enemy.pos.x <= 36) {
hero.attack(enemy);
}
}
}
while(true) {
commandAttack();
var strategy = chooseStrategy();
// Call a function, depending on what the current strategy is.
if (strategy == "griffin-rider") {
hero.summon("griffin-rider");
commandAttack();
}
else if (strategy == "fight-back") {
heroAttack();
}
else if (strategy == "collect-coins") {
pickUpCoin();
}
}
I didn’t check your code in the level and I have forgotten how I had passed it.
Your most important first job is to collect coins, so put it first and then other conditions
if (strategy == "collect-coins") pickUpCoin(); // no need of { action;} if you have only one statement
and check the position of the fangrider
// If there is a fangrider on your side of the mines, return “fight-back”
var fangrider = hero.findByType("fangrider");
if (fangrider) return "fight-back"; // partly right
I’ve tried to pass the level tweaking your code but didn’t succeed. I think my last advice / pickUpCoins() first / was not so good. Start from scratch and find other members problems and solutions. Most of them are in python but it’s easy to translate them to java-script. You can check {SOLVED}Reaping Fire (python) as many other topics.