# .Kelvintaph Crusader Help!

**URL:** <https://discourse.codecombat.com/t/kelvintaph-crusader-help/28235>\
**Category:** Level Help\
**Created:** [April 1, 2021, 10:22pm UTC](https://discourse.codecombat.com/t/kelvintaph-crusader-help/28235 "2021-04-01T22:22:15Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ozakesus87](https://avatars.discourse-cdn.com/v4/letter/o/278dde/32.png) [@Ozakesus87](https://discourse.codecombat.com/u/Ozakesus87)\
**Post date:** [April 1, 2021, 10:22pm UTC](https://discourse.codecombat.com/t/kelvintaph-crusader-help/28235/1 "2021-04-01T22:22:15Z")

</div>

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:

```python
// 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!!

---

<div class="post-metadata">

**Author:** ![xython](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/xython/32/7269_2.png) [@xython](https://discourse.codecombat.com/u/xython)\
**Post date:** [April 1, 2021, 10:38pm UTC](https://discourse.codecombat.com/t/kelvintaph-crusader-help/28235/2 "2021-04-01T22:38:46Z")

</div>

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.

```python
 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!

---

<div class="post-metadata">

**Author:** ![Ozakesus87](https://avatars.discourse-cdn.com/v4/letter/o/278dde/32.png) [@Ozakesus87](https://discourse.codecombat.com/u/Ozakesus87)\
**Post date:** [April 7, 2021, 1:05am UTC](https://discourse.codecombat.com/t/kelvintaph-crusader-help/28235/3 "2021-04-07T01:05:46Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![xython](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/xython/32/7269_2.png) [@xython](https://discourse.codecombat.com/u/xython)\
**Post date:** [April 7, 2021, 12:32pm UTC](https://discourse.codecombat.com/t/kelvintaph-crusader-help/28235/4 "2021-04-07T12:32:30Z")

</div>

Keep the two while loops - it’s an interesting solution 🙂 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

```python

```

---

<div class="post-metadata">

**Author:** ![Ozakesus87](https://avatars.discourse-cdn.com/v4/letter/o/278dde/32.png) [@Ozakesus87](https://discourse.codecombat.com/u/Ozakesus87)\
**Post date:** [April 7, 2021, 9:53pm UTC](https://discourse.codecombat.com/t/kelvintaph-crusader-help/28235/5 "2021-04-07T21:53:01Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![xython](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/xython/32/7269_2.png) [@xython](https://discourse.codecombat.com/u/xython)\
**Post date:** [April 7, 2021, 10:05pm UTC](https://discourse.codecombat.com/t/kelvintaph-crusader-help/28235/6 "2021-04-07T22:05:27Z")

</div>

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](https://discourse.codecombat.com/t/bash-em-all-please-help/14857/10)

---

<div class="post-metadata">

**Author:** ![Ozakesus87](https://avatars.discourse-cdn.com/v4/letter/o/278dde/32.png) [@Ozakesus87](https://discourse.codecombat.com/u/Ozakesus87)\
**Post date:** [April 7, 2021, 11:07pm UTC](https://discourse.codecombat.com/t/kelvintaph-crusader-help/28235/7 "2021-04-07T23:07:33Z")

</div>

Here you go:

```python
// 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});
      }
     }
 }
}

```

---

<div class="post-metadata">

**Author:** ![xython](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/xython/32/7269_2.png) [@xython](https://discourse.codecombat.com/u/xython)\
**Post date:** [April 8, 2021, 3:39pm UTC](https://discourse.codecombat.com/t/kelvintaph-crusader-help/28235/8 "2021-04-08T15:39:46Z")

</div>

solved you problems but used chain lightning

```python
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?](https://discourse.codecombat.com/t/solved-chained-statements-like-movexy-and-behavior-override-each-other/22336)

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

```python
// 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:

 ![crusaderX1X2half](https://us1.discourse-cdn.com/flex016/uploads/codecombat/original/3X/2/c/2c3ce12b36eafc81ad762f0775755a579a0f06a1.gif)

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

[![](https://us1.discourse-cdn.com/flex016/uploads/codecombat/original/2X/e/ea2b5bed3f852aa551234e809ac68aa8aa54e714.gif) ](https://us1.discourse-cdn.com/flex016/uploads/codecombat/original/2X/e/ea2b5bed3f852aa551234e809ac68aa8aa54e714.gif)

---

<div class="post-metadata">

**Author:** ![Ozakesus87](https://avatars.discourse-cdn.com/v4/letter/o/278dde/32.png) [@Ozakesus87](https://discourse.codecombat.com/u/Ozakesus87)\
**Post date:** [April 9, 2021, 4:04am UTC](https://discourse.codecombat.com/t/kelvintaph-crusader-help/28235/9 "2021-04-09T04:04:17Z")

</div>

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