Play Home Contribute GitHub Join Us Discourse Staff Members GitHub Contribution Guides Team

[SOLVED] Grim Determination - paladin problem


#1

Hello,

I can’t find any mistakes in my code. However I do get that error line (see pic). Why doesn’t the game recognize the variable lowHPal ? I defined it above. Plus the Paladins die and I can’t fix it.

// Your goal is to protect Reynaldo
// Find the paladin with the lowest health.
function lowestHealthPaladin() {
    var lowestHealth = 99999;
    var lowestFriend = null;
    var friends = hero.findFriends();
    for (var f = 0; f < friends.length; f++) {
        var friend = friends[f];
        if (friend.type != "paladin") {
            continue;
        }
        if (friend.health < lowestHealth && friend.health < friend.maxHealth) {
            lowestHealth = friend.health;
            lowestFriend = friend;
        }
    }
    return lowestFriend;
}
function commandPaladin(paladin) {
    var lowHPal = lowestHealthPaladin();
    if (lowHPal) {
        if (paladin.canCast("heal", lowHPal)) {
            hero.say(paladin + " will cast heal on " + lowHPal);
            hero.command(paladin, "cast", "heal", lowHPal);
            hero.command(lowHPal, "shield");
        }
    }
    var enemy = paladin.findNearestEnemy();
    if (enemy) {
        hero.command(paladin, "attack", enemy);
    }
}
function commandPeasant(peasant) {
    if (peasant) {
        var coin = peasant.findNearestItem();
        if (coin) {
            hero.command(peasant, "move", coin.pos);
        }
    }
}
function commandGriffin(griffin) {
    var enemy = griffin.findNearestEnemy();
    if (enemy) {
        hero.command(griffin, "attack", enemy);
    }
}
function commandFriends() {
    var friends = hero.findFriends();
    for (var i = 0; i < friends.length; i++) {
        var friend = friends[i];
        if (friend.type == "peasant") {
            commandPeasant(friend);
        } else if (friend.type == "griffin-rider") {
            commandGriffin(friend);
        } else if (friend.type == "paladin") {
            commandPaladin(friend);
        }
    }
}
while (true) {
    commandFriends();
    // Summon griffin riders!
    if (hero.gold >= hero.costOf("griffin-rider")) {
        hero.summon("griffin-rider");
    }
}

Thank you for help in advance!


#2

I’m still looking for help pls


#3

Can you show me line 25, I can’t see which line that is in your posted code because your code doesn’t have line numbers.
:lion: :lion: :lion:


#4

Of course. Sorry bout that.


#5

After trying to beat the level with better achievement ( I failed :slight_smile: , simply saved all my warriors as before) I noticed Kaija_Solod has passed the level. So for those still interested:



The second image is taken from another thread about ‘Grim Determination’. See the similarities? Change Taric with Antary but:
The second part of the message is messed. I think this is a BUG .
The reason for the second message is Taric has switched to enemy camp. The solution not to conmand him is to make a check if ( unit.team == “humans" )
This will not solve the problems she had in her original function commandPaladin(paladin) even if she has passed the level with it. I think all the code is better to be put in a single

if ( condition1) // code
else if ( condition2) // code
// more else ifs if needed
else
// command paladin to do something if his health is OK ( are they all male?)

#6

Hi Xython, I unfortunately DID NOT pass the level. (could you please edit your message so that others don’t think I did.) I put Python code there instead just to move forward. I’m still working on this level. I changed my code a little, and the “error” dissapeared. However! My Paladins still don’t heal??? I would like to know why. Here is my new code:

// Your goal is to protect Reynaldo
// Find the paladin with the lowest health.
function lowestHealthPaladin() {
    var lowestHealth = 99999;
    var lowestFriend = null;
    var friends = hero.findFriends();
    for (var f = 0; f < friends.length; f++) {
        var friend = friends[f];
        if (friend.type != "paladin") {
            continue;
        }
        if (friend.health < lowestHealth && friend.health < friend.maxHealth) {
            lowestHealth = friend.health;
            lowestFriend = friend;
        }
    }
    return lowestFriend;
}
function commandPaladin(paladin) {
    var lowHPal = lowestHealthPaladin();
    if (lowHPal) {
        hero.say(lowHPal);
        if (paladin.canCast("heal", lowHPal)) {
            hero.say(paladin + " will cast heal on " + lowHPal);
            hero.command(paladin, "cast", "heal", lowHPal);
            hero.command(lowHPal, "shield");
        }
    }
    var enemy = paladin.findNearestEnemy();
    if (enemy && enemy.type == "warlock") {
        hero.command(paladin, "attack", enemy);
    } else if (enemy && enemy.type != "warlock") {
        hero.command(paladin, "attack", enemy);
    }
}
function commandPeasant(peasant) {
    var peasants = hero.findByType("peasant");
    var hector = peasants[1];
    var rose = peasants[0];
    if (hector) {
        var coin1 = hector.findNearest(hero.findItems());
        if (coin1 && coin1.pos.y > 40) {
            hero.command(hector, "move", coin1.pos);
        }
    }
    if (rose) {
        var coin2 = hero.findNearest(hero.findItems());
        if (coin2 && coin2.pos.y < 40) {
            hero.command(rose, "move", coin2.pos);
        }
    }
}
function commandGriffin(griffin) {
    var enemy = griffin.findNearestEnemy();
    if (enemy) {
        hero.command(griffin, "attack", enemy);
    }
}
function commandFriends() {
    var friends = hero.findFriends();
    for (var i = 0; i < friends.length; i++) {
        var friend = friends[i];
        if (friend.type == "peasant") {
            commandPeasant(friend);
        } else if (friend.type == "griffin-rider") {
            commandGriffin(friend);
        } else if (friend.type == "paladin") {
            commandPaladin(friend);
        }
    }
}
while (true) {
    commandFriends();
    // Summon griffin riders!
    if (hero.gold >= hero.costOf("griffin-rider")) {
        hero.summon("griffin-rider");
    }
}


#7


You can check others solutions to find where are your faults, to search topics with exactly the same problems.


#8

Thank you, I already did that though - and I’m not even asking for the solution. The only thing I ask for is to determine why my paladins won’t heal. Any ideas at all, anyone? Would be very thankful.


#9
            hero.say(paladin + " will cast heal on " + lowHPal); // use console.log
            hero.command(paladin, "cast", "heal", lowHPal);
            hero.command(lowHPal, "shield");

From another topic:
htrnblr
You also have stacking of ifs.

    if (lowHPal) {
       // code 
    }
    // code
    if (enemy && enemy.type == "warlock")

This may sometimes work, but not always… You already know if/ else if /…/ else


#10

Thanks there, but I already removed every “say” statement and even “shield” bc somehow(!) it was what obstructed paladins from healing. I also tried your suggestion to not stack up, and instead use if/else. I tried it before, and it didn’t work. However! I realized I had if(LowHPal) instead of if (lowHPal && paladin.canCast("heal", lowHPal)), and that was my main problem, bc there simply always was someone hurt. I now get to literally the last second of the game but every paladin still dies plus my minefield is blown! You drive me crazy CodeCombat :cold_sweat:


#11

"shield ” bc somehow(!) obstructed paladins from healing." - not somehow - htrnbl has explained why
/ he’s the most prominent level champion/…

Organize every action in if( smth0)/ else if (smth1)/…/else (smthX)
Imagine the situation as in real battle:
Every warrior must fight when not mortally wounded:

friend.health < friend.maxHealth/some_value

The priority is to keep him alive - so:
if( smth0) {heal him }
else if (smth1) {tell him to shield}
else( everything is OK) {fight}
My structure isn’t exactly the same, but the idea is clear.


#12

why can’t I have heal and shield in one statement?


#13

Not sure exactly what happens… But I passed the level when hit “Submit”. Although I didn’t get a bonus. Then, I used heal and shield in one statement- and with “run” I did not pass anything. However with submit I passed the whole thing! Thank you all for your ideas.
Is there a thing with choosing to hit “run” or “submit”?


#14

Clicking Run uses the same seed no matter what, so all runs will use the same seed. Submit uses a random seed every time, so some submissions may be unlucky, some may not.


#15

Got it, thank you!!!