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

Help with mad maxer strikes back


#1

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

#2

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.


#3

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


#4

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.

`


#5

Thank you, it worked!


#7

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


#8

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:


#9


#10

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.