Reaping fire help (javascript)


#1

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();
>     }
> }

#2

Do you get an error? From what i can see you have it right. Maybe it could me an equipment issue. Take a screenshot of your equipment @freesat-106


#3

very quick look:

> 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());`

and you must fight all foes , not only ogres:

 if (enemy && enemy.type == "ogre") 

#4

I agree but you can change it just slightly. Put enemy.team instead of enemy.type


#5

sorry, that was a typo.


#6

ok, I changed it to enemy.team, and I fixed the typo, but it didn’t work. My griffin riders just stand there.


#7

Maybe just change it to if enemy


#8

Thanks, that part worked @Enderlord832, but a munchkin always detonates the mine field before my griffin riders can kill it.


#9

You don’t have to protect the minefield forever. Just for 30 seconds


#10

I know, but it blows up before the 30 seconds is up.


#11

Also, because I changed enemy.type to enemy, now my griffin riders chase after the fangriders, leaving the mine field undefended.:scream:


#12

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();
    }
}

#13

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


#14

I put pickUpCoins(); first, but my hero still isn’t collecting coins. What should I do?


#15

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.


#16

You can do if enemy.type != "fandrider" LOL i just put “fandrider” :smile: @freesat-106