Help with mad maxer strikes back

a little help?

while True:
    weakest = None
    leastHealth = 99999
    enemyIndex = 0
    enemies = hero.findEnemies()
    # Loop through all enemies.
    while enemyIndex < enemies:
        # If an enemy's health is less than leastHealth,
        if enemy.health < leastHealth:
            # make it the weakest and set leastHealth to its health.
            enemy = weakest
            enemy.health = leastHealth
            enemyIndex += 1
    if weakest:
        # Attack the weakest ogre.
        hero.attack(weakest)
        pass

you need to define enemy. You never define enemy before the if statement. Also, in the if statement you define enemy as weakest. You need to define weakest as enemy, not the other way around.

Help please, my hero just stands there when I run it.

while True:
    weakest = None
    leastHealth = 99999
    enemyIndex = 0
    enemies = hero.findEnemies()
    enemy = enemies[enemyIndex]
    # Loop through all enemies.
    while enemyIndex < enemies:
        # If an enemy's health is less than leastHealth,
        if enemy.health < leastHealth:
            # make it the weakest and set leastHealth to its health.
            weakest = enemy
            enemy.health = leastHealth
            enemyIndex += 1
    if weakest:
        # Attack the weakest ogre.
        hero.attack(weakest)
        pass

There are three problems with your code.

  1. The while conditional statement does not address the array (length) of enemies.
  2. leastHealth should not be assigned to enemy.health (it should be the other way around).
  3. The last is a structure issue; enemyIndex += 1 is not tabbed correctly.

`

Thank you, it worked!

I did the same thing as CodeCombatMaster did and fixed my issues but now when I run my code my person dies.

This is my code:

// The smaller ogres here do more damage!
// Attack the ogres with the least health first.
while(true) {
    var weakest = null;
    var leastHealth = 99999;
    var enemyIndex = 0;
    var enemies = hero.findEnemies();

    // Loop through all enemies.
    while (enemyIndex < enemies.length) {
         var enemy = hero.findNearestEnemy();
        if (enemy.health < leastHealth) { 
            weakest = enemy;
            leastHealth  = enemy.health;
        }
        enemyIndex +=1; 
    }
    if (weakest) {
        // Attack the weakest ogre.
        hero.attack(weakest);
        continue;
    }
}

It works for me.
Please either post a screenshot of your equipment, or just list it.
And could you also post a screenshot of the level if you can.
Thanks,
:lion: :lion: :lion:

The main problem with the code:

    while (enemyIndex < enemies.length) {
         var enemy = hero.findNearestEnemy(); // is here
         // code...
   }

You’re finding the nearest enemy, but you need to iterate through the enemies by the enemyIndex.
Next - your hero is too weak. Change it with Tharin and post your findings. Do not buy hastily new armor or sword.

1 Like

hi, my hero keeps looping on enemies in a “nearest enemy” way instead of searching for the one with lowest health, whats wrong with my code? ^^

javascript

while (true) {
  deleted  solution part
    // Loop through all enemies.
    while (enemyIndex < enemies.length) {
        // If an enemys health is less than leastHealth,
        var enemy = enemies[enemyIndex];
        if (enemy.health < leastHealth) {
            // make it the weakest and set leastHealth to its health.
deleted solution part

Maybe try better armour. The code looks all right.

1 Like

I tried your code and it works fine, but I did use 1000 health points. So like @AnSeDra says, try more armour.

Or if you have more than 1000 health, try resummiting, maybe it was just an unlucky seed.

1 Like

i ran it again and youre right it did work (even though i did try it few times before asking!), i passed the level BUT hero is supposed to attack little ogres first as the description says, but he attacks in a circular way clockwise/counterclock and doesnt break cycle, is that normal? i dont think im supposed to pass level just cause of good armor ^^"

1 Like

Hmmm, I hadn’t looked at the hp of each of the ogres before - I’d just assumed that my hero was attacking the weakest first. But you’re right - in the third round, she attacks the ogres with health of 5, 6, 8, 7, 7 (in that order), which makes no sense. Someone who knows more will have to answer that!

In terms of passing because of good armour: in this case I don’t think you have (I think your code is OK). There are other levels that you can either pass by buying more armour, or by writing good code; or in some cases by writing code that is completely different to the thing that you’re supposed to be practising and bypassing the problem altogether. I guess it’s up to you how you want to approach coding (and indeed how you approach life), and what your personal rules are. If you want to become good at coding, then there’s no point in avoiding problems or copying other people’s solutions (but equally, you don’t want to get fed up by spending weeks on a level that could be solved by buying a helmet).

It sounds as though you’re switched on to the difference by the fact that you’re asking the question :smiley: .

4 Likes

ty and yes i do have good armor exactly because i dont want to get stuck on same level, i usually do the main road and when i get stuck i go back and do the side missions etc because ive probably missed something new there when i dont recognize instruction on main missions (none the less i complete all missions eventually, i dont pass on any), but i definetly want to understand the logic best i can and in this level the instruction to begin with are saying that the smaller one are much stronger :woman_shrugging:

3 Likes

how do I make my hero attack?

while True:
if enemy:
enemy = hero.findNearestEnemy()
hero.attack(enemy)
else:
self.shield()

define the enemy before the if enemy loop
so it should be

while True:
    enemy = hero.findNearestEnemy()
    if enemy:
        hero.attack(enemy)
    else:
        hero.shield()

why do you switch between self and hero?

(Thanks for indenting @Deadpool198 :grin: )

2 Likes