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

.Kelvintaph Crusader Help!

On this level, I know how to make my hero survive, but the paladin won’t heal the rest of the soldiers after the witch has been killed. Here is my code:

// You can find friends through walls, but not enemies.
// Watch out for smooth, frictionless ice patches!
function findWeakestFriend() {
 var friends = hero.findFriends();
 var weakestFriend = null;
 var lowestHealth = Infinity;
 for(var i = 0; i < friends.length; i++) {
  var friend = friends[i];
  var health = friend.health;
  if(health < lowestHealth) {
   lowestHealth = health;
   weakestFriend = friend;
  }
 }
 return weakestFriend;
}
//COMMAND THE PALADINS!!!!!!
function commandPaladin() {
 var paladin = hero.findNearest(hero.findByType("paladin"));
 var witch = hero.findNearest(hero.findByType("witch"));
 if(paladin) {
  if(witch) {
   hero.command(paladin, "attack", witch);   
  } else {
   paladinHeals();   
  }
 }
}
function commandArchers() {
    var friends = hero.findByType("archer");
    var enemies = hero.findByType("skeleton");
    var archer1 = friends[0];
    var archer2 = friends[1];
    if(enemies) {
    if(archer1) {
     var witch = hero.findNearest(hero.findByType("witch"));
     if(witch) {
      hero.command(archer1, "attack", witch);   
     } else {
         hero.command(archer1, "attack", archer1.findNearest(enemies));
     }
    }
    if(archer2) {
     var f = findWeakestFriend();
     if(f) {
      hero.command(archer2, "defend", f.pos);   
     }
    }
    }
}

function paladinHeals() {
 var p = hero.findByType("paladin")[0];
 if(p) {
  var f = findWeakestFriend();
  if(f && p.canCast("heal", f)) {
   hero.command(p, "cast", "heal", f);   
  }
 }
}
function commandSoldiers() {
 var soldiers = hero.findByType("soldier");
 var enemies = hero.findByType("ogre");
 var s = soldiers[0];
 var s1 = soldiers[1];
}


var points = [{x: 37, y: 15}, {x: 20, y: 14}, {x: 68, y: 14}, {x: 45, y: 14}, {x: 78, y: 14}];
for(var i = 0; i < points.length; i++) {
 var p = points[i];
 var x = p.x;
 while(hero.pos.x != x) {
     hero.move(p);
     commandPaladin();
    commandArchers();
    commandSoldiers();
    paladinHeals();
 }
 
}
while(true) {
    commandPaladin();
    commandArchers();
    commandSoldiers();
    paladinHeals();
}

My code works like this:

My hero would use the catapults’ fire balls to kill the brawlers and then escape. The paladin should heal the soldiers and archers. The soldiers protect the archers from dying, while the archers kill the strongest enemies, the skeletons.

Please help me!!

1 Like

Didn’t test it but:
in the two while loops you have a succession of two functions commanding paladins.This is error prone. Make only one function commanding the paladin.

 while(hero.pos.x != x) {
    hero.move(p);
    commandPaladin(); // 1
    commandArchers();
    commandSoldiers();
    paladinHeals(); // 2
 }
 
}
while(true) {
    commandPaladin(); // 1
    commandArchers();
    commandSoldiers(); // 2
    paladinHeals();
}

Congratulations for trying to solve the problem yourself, not copying from the net!

1 Like

So should I delete the while(true) loop or delete the paladinHeals()?

Keep the two while loops - it’s an interesting solution :slight_smile: Merge commandPaladin () and paladinHeals () into one single function. Your paladin can shield or move when the witch is defeated and it’s more difficult to keep track of all actions (move, heal, shield, attack, move) if they are dispersed between multiple functions

I fixed that, and now my paladin will heal as normal! All the enemies were killed except the skeletons, and my archers would take care of the skeletons while the soldiers protect them, but the skeletons won’t get killed in time. Is there anything I can do to save time killing the skeletons?

Post your new code or better post your sessionID in separate post you’re going to delete afterwards.If you don’t know how see Bash 'em all. Please Help - #10 by xython

Here you go:

// You can find friends through walls, but not enemies.
// Watch out for smooth, frictionless ice patches!


//First we must find the weakest friend:
function findWeakestFriend() {
 var friends = hero.findFriends();
 var weakestFriend = null;
 var lowestHealth = Infinity;
 for(var i = 0; i < friends.length; i++) {
  var friend = friends[i];
  var health = friend.health;
  if(health < lowestHealth) {
   lowestHealth = health;
   weakestFriend = friend;
  }
 }
 return weakestFriend;
}
//COMMAND THE PALADINS!!!!!!
function commandPaladin() {
 var paladin = hero.findNearest(hero.findByType("paladin"));
 var witch = hero.findNearest(hero.findByType("witch"));
 var skeletons = hero.findByType("skeleton");
 if(paladin) {
     if(witch) {
         //Attack the witch, if so:
      hero.command(paladin, "attack", witch);   
     } else {
         //If not, then the paladin heals the weakest:
      var f = findWeakestFriend();
      if(f && f.type != "archer" && paladin.canCast("heal", f)) {
       hero.command(paladin, "cast", "heal", f);   
      }
     }
 }
}
   
//Next, the archers:
function commandArchers() {
    var archers = hero.findByType("archer");
    var witches = hero.findByType("witch");
    var skeletons = hero.findByType("ogre");
    var ogres = hero.findByType("skeleton");
    for(var i = 0; i < archers.length; i++) {
     var a = archers[i];
     var witch = a.findNearest(witches);
     if(witch) {
      hero.command(a, "attack", witch);   
     } else {
         var ogre = a.findNearest(skeletons);
         if(ogre) {
          hero.command(a, "attack", ogre);   
         } else {
          var skeleton = a.findNearest(ogres);  
          if(skeleton) {
           hero.command(a, "attack", skeleton);   
          }
         }
     }
    }
}
//Next, the soldiers:
function commandSoldiers() {
 var soldiers = hero.findByType("soldier");
 var enemies = hero.findByType("skeleton");
 for(var i = 0; i < soldiers.length; i++) {
  var s = soldiers[i];
  var archer = s.findNearest(hero.findByType("archer"));
  if(archer) {
   hero.command(s, "defend", archer.pos);   
  }
 }
}


var points = [{x: 37, y: 15}, {x: 20, y: 14}, {x: 68, y: 14}, {x: 45, y: 14}, {x: 78, y: 14}];
for(var i = 0; i < points.length; i++) {
 var p = points[i];
 var x = p.x;
 while(hero.pos.x != x) {
     hero.move(p);
     commandPaladin();
    commandArchers();
    commandSoldiers();
 }
 
}

while(true) {
 commandPaladin();
 commandArchers();
 commandSoldiers();
 var enemies = hero.findEnemies();
 if(!enemies) {
     var friends = hero.findFriends();
     for(var i = 0; i < friends.length; i++) {
      var f = friends[i];
      if(f.pos.x != 50) {
       hero.command(f, "move", {x: 48, y: 58});
       hero.command(f, "move", {x: 78, y: 40});
      }
     }
 }
}

solved you problems but used chain lightning

var enemies = hero.findEnemies();
if(!enemies){ // will be always false - find why
    // code
}

      if(f.pos.x != 50) { // this will never work, find in the forum why
       hero.command(f, "move", {x: 48, y: 58});
       hero.command(f, "move", {x: 78, y: 40});
      }

see how you can move with move() and command minions [Solved] Chained statements like .moveXY() and behavior override each other?

The level is working in a random javascript session taken from the leaderboard:
Corrections made to the original code:

// code
function commandPaladin() {
     // original  code
     else {
       var f = findWeakestFriend();
       if(f && f.type != "archer" && paladin.canCast("heal", f)) 
          hero.command(paladin, "cast", "heal", f);
       else
          hero.command(paladin, "shield"); // shield if paladin cannot heal
      }

// original  code
var points = [{x: 35, y: 15}, {x: 20, y: 14}, {x: 68, y: 14}, {x: 45, y: 14}, {x: 78, y: 14}];
hero.cast("chain-lightning", hero.findNearestEnemy());

for(var i = 0; i < points.length; i++) {
   // original  code
}

while(true) {
 // original  code   
 var enemies = hero.findEnemies(); 
 if( !enemies.length ) break; // enemies.length will be 0 if enemies is empty
   // you can insert the code for moving minions here or
   // call previously created function moveMinions()
}

// Instead of putting the moveMinions code in the above loop
// you can put the code in a new and last while true loop
// i call this Jenny's method

while(true) {
     var friends = hero.findFriends();
     for(var i = 0; i < friends.length; i++) {
       var f = friends[i];
       // i don't think this is the best way to move minions
       // but it's closer to your original approach
       if(f.pos. x < 60) 
          hero.command(f, "move", {x: 60, y: 39});
       else if(f.pos. x > 50) 
          hero.command(f, "move", {x: 78, y: 39});
     }
}

the result:

You can improve the hero’s code. He/she barely survives. It can be done with no armor :

Thanks for the help!! I finally got to solve it by using the chain lightning.