Play Home Contribute Github Join Us! Discourse Staff Members Github Contribution Guides Team

Mountain reaping-fire javascript help [SOLVED]

#1

Cant defend the mines for 30 seconds, but I think my code is working properly.
What should I do?

// 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 (fangriders) {
        for (var i = 0;i < fangriders.length;i++) {
            var fangrider = fangriders[i];
            if (fangrider) {
                if (fangrider.pos.x < 38) {
                    return "thisisbadwordapperantlyandicantpostithere";
                } else {
                    return "thisisbadwordapperantlyandicantpostithere";    
                }
            }
        }
    } else {   
    // Otherwise, return "collect-coins"
        return "collect-coins";    
    }
    
    // If there is a fangrider on your side of the mines, return "thisisbadwordapperantlyandicantpostithere"
    var fangriders = hero.findByType("fangrider");

}
var defense = {"x":107,"y":40};
function commandAttack() {
    // Command your griffin riders to attack ogres.
    var riders = hero.findByType("griffin-rider");
    var scouts = hero.findByType("scout");
    if (riders) {
        for (var i = 0; i < riders.length; i++) {
            var rider = riders[i];
            var scout = scouts[i];
            if (scout) {
                var percentagehealth = rider.health*0.2;
                if (rider) {
                    hero.command(rider, "attack", scout); 
                } 
            }
        }
    }
}
function findBestItem(items) {
    var bestItem = null;
    var bestValue = 0;
    
    // Loop over the items array.
    // Find the item with the highest valueOverDistance()
    for (var itemsIndex = 0;itemsIndex < items.length; itemsIndex++) {
        var item = items[itemsIndex];
        if (item.value > bestValue) {
            bestValue = item.value;
            bestItem = item;
        }
    }
    return bestItem;
}
function pickUpCoin() {
    // Collect coins
           var coins = hero.findItems();
        var coin = null;
        coin = findBestItem(coins);
        if(coin) {
            hero.move(coin.pos);
        }
}

function heroAttack() {
    // Your hero should attack fang riders that cross the minefield.
    var thingstoattack = hero.findByType("fangrider");
    for (var i = 0; i < thingstoattack.length ; i++) {
        var thing = thingstoattack[i];
        if (thing) {
             while(thing.health > 0) {
                hero.attack(thing);   
            }
        }
    }
}
hero.summon("griffin-rider");
while(true) {
//    var arrow = hero.findEnemyMissiles()[0];
//    if (arrow) {
//        pet.catch(arrow);   
//    }
    commandAttack();
    var strategy = chooseStrategy();

    // Call a function, depending on what the current strategy is.
    if (strategy == "griffin-rider") {
        hero.summon("griffin-rider");
        var strategy = chooseStrategy();
    }
    if (strategy == "thisisbadwordapperantlyandicantpostithere") {
        heroAttack();
    }
    if (strategy == "collect-coins") {
        pickUpCoin();
    }
}

2 Likes
#3

I fixed the problem by removing else
and making griffins move to middle of field
before attacking

1 Like