Distance to code...The Agrippa Defense A (JS) [solved]

I am working on The Agrippa Defense A

Ok, perfect…now the code?

And totally agree there @Alexbrand…quite a bit too early :slight_smile:

while(true) {
    var enemy = hero.findNearestEnemy();
    if(enemy) {
      var distance = hero.distanceTo(enemy);
if (distance < 5) {
  var ready = hero.isReady("cleave");
     hero.cleave(enemy);       
    }
           }
          
            else {
                hero.attack(enemy);
               
            }
    }


Im not sure what’s wrong and what’s right at this point.

that’s what we’re here for…alex, please pipe in as you see fit too!

I don’t have much skills in java csript, so I’ll try something else… )

Ok, first problem is your indentation. By your code, you appear to be attempting to nest multiple ‘if’ statements under your while loop. Think of them as rings:

while (true) { // this is your outer ring, sometimes also called the 'parent' ring
    var enemy = hero.findNearestEnemy();
    if (enemy) { // this is your first inner ring (or child ring)...it is under the control of the parent
      var distance = hero.distanceTo(enemy);
if (distance < 5) { // this is supposed to be another child ring (step-child), as it is under the control of the first child ring
  var ready = hero.isReady("cleave"); // we'll talk about this when the others are fixed
     hero.cleave(enemy);       
    }
           }
          
            else {
                hero.attack(enemy);
               
            }
    }

so should I try having less ‘if’ statements…?

The proper structure would look something like this:

while (true) {
   if (condition1 is true) {
      performCode;
      if (condition2 is true) {
         performMoreCode;
      }
   }
}

See how everything lines up…

  • while is the outer/parent
    • if #1 is the inner/child
      • if #2 is the next inner/step-child

oh ok I see let me go try that.

No, it is totally ok to have as many if’s as needed…you just need to ensure they are properly structured/aligned.

So, I’m gonna show some magic))

Methods of your equipment are listed on the left hand from code editor and you can click it and see some examples and explanations about the way it should work.
Also most of the levels are provided with hints and they may clarify a lot)

For this level there are 7 hints and the 6th is written in Java Script as I see. Not help much, but I think you can reach the solution with this things. Good luck)

1 Like

Good point Alex!..missed that tip :smiley:

Thank you that helped a lot.

I tried using the hints just to try and get through the level and it still isn’t working for me but it could have something to do with my last few lines of code

Again, we are back to the ‘we’ll get to this’ part :wink:

You defined the variable ‘ready’ perfectly, but then don’t use it. Instead, try using the definition code as part of your if statement.

Otherwise, you do need to make use of your ‘ready’ variable.

enemyExists cleaveReady distanceIsOK     action           case
         no          no           no     collectCoins()   1
        yes          no           no     attack()         2  
         no         yes           no     collectCoins()   3
        yes         yes           no     attack()         4
         no          no          yes     collectCoins()   5 
        yes          no          yes     attack()         6 
         no         yes          yes     collectCoins()   7
        yes         yes          yes     cleave()         8

if enemy
  if not cleaveReady or not distanceIsOK   // case 2,4,6
     attack()                
  else                                     // case 8 
     cleave()             
else                                       // case 1,3,5,7
  collectCoins()                           // collectCoins or do nothing
1 Like

Ok…I’m adding the term ‘xythonism’ to the CC dictionary! :stuck_out_tongue_winking_eye:

2 Likes

But we don’t need to collect coins on this level)

I think that was just an added example to show the ‘case’ differences…a comparative analysis, if you will. Still, it would be a tad advanced for this topic. Maybe tomorrow…