# Binary Deployment-Help (Java Script)

**URL:** <https://discourse.codecombat.com/t/binary-deployment-help-java-script/5262>\
**Category:** Uncategorized\
**Created:** [October 15, 2015, 11:42pm UTC](https://discourse.codecombat.com/t/binary-deployment-help-java-script/5262 "2015-10-15T23:42:20Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Paul\_Lum](https://avatars.discourse-cdn.com/v4/letter/p/ea5d25/32.png) [@Paul\_Lum](https://discourse.codecombat.com/u/Paul_Lum)\
**Post date:** [October 15, 2015, 11:42pm UTC](https://discourse.codecombat.com/t/binary-deployment-help-java-script/5262/1 "2015-10-15T23:42:20Z")

</div>

I am playing binary deployment, but I am having a few problems. I made a function that deploys the soldiers/archers in the correct order, but they don’t do it fast enough. I run out of time. if I run it too fast, they don’t receive my commands, but if I run it too slow, they don’t get done in time. My other problem is that I can’t find the paladin’s deployment attribute for the number.  
When I hit submit, the paladin’s number changes, and I have to change the number that is plugged into my function. How do I save the number stated as a variable that can be used for my function? Here’s my code:

this.deployLine = function(number,paladinX,paladinY){  
var binary = [“soldier”,“archer”];  
if (number\>=128){  
this.summon(binary[1]);  
var friend = this.findNearest(this.findFriends());  
this.command(friend, “move”, {x:paladinX+5,y:paladinY});  
number = number-128;  
}else{  
this.summon(binary[0]);  
friend = this.findNearest(this.findFriends());  
this.command(friend, “move”, {x:paladinX+5,y:paladinY});  
}  
if (number\>=64){  
this.summon(binary[1]);  
friend = this.findNearest(this.findFriends());  
this.command(friend, “move”, {x:paladinX+10,y:paladinY});  
number = number-64;  
}else{  
this.summon(binary[0]);  
friend = this.findNearest(this.findFriends());  
this.command(friend, “move”, {x:paladinX+10,y:paladinY});  
}  
if (number\>=32){  
this.summon(binary[1]);  
friend = this.findNearest(this.findFriends());  
this.command(friend, “move”, {x:paladinX+15,y:paladinY});  
number = number-32;  
}else{  
this.summon(binary[0]);  
friend = this.findNearest(this.findFriends());  
this.command(friend, “move”, {x:paladinX+15,y:paladinY});  
}  
if (number\>=16){  
this.summon(binary[1]);  
friend = this.findNearest(this.findFriends());  
this.command(friend, “move”, {x:paladinX+20,y:paladinY});  
number=number-16;  
}else{  
this.summon(binary[0]);  
friend = this.findNearest(this.findFriends());  
this.command(friend, “move”, {x:paladinX+20,y:paladinY});  
}  
if (number\>=8){  
this.summon(binary[1]);  
friend = this.findNearest(this.findFriends());  
this.command(friend, “move”, {x:paladinX+25,y:paladinY});  
}else{  
this.summon(binary[0]);  
friend = this.findNearest(this.findFriends());  
this.command(friend, “move”, {x:paladinX+25,y:paladinY});  
}  
if (number\>=4){  
this.summon(binary[1]);  
friend = this.findNearest(this.findFriends());  
this.command(friend, “move”, {x:paladinX+30,y:paladinY});  
number=number-4;  
}else{  
this.summon(binary[0]);  
friend = this.findNearest(this.findFriends());  
}  
if (number\>=2){  
this.summon(binary[1]);  
friend = this.findNearest(this.findFriends());  
this.command(friend, “move”, {x:paladinX+35,y:paladinY});  
number=number-2;  
}else{  
this.summon(binary[0]);  
friend = this.findNearest(this.findFriends());  
this.command(friend, “move”, {x:paladinX+35,y:paladinY});  
}  
if (number\>=1){  
this.summon(binary[1]);  
friend = this.findNearest(this.findFriends());  
this.command(friend, “move”, {x:paladinX+40,y:paladinY});  
number=number-1;  
}else{  
this.summon(binary[0]);  
friend = this.findNearest(this.findFriends());  
this.command(friend, “move”, {x:paladinX+40,y:paladinY});  
}  
};  
this.deployLine(???);

---

<div class="post-metadata">

**Author:** ![trotod](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/trotod/32/3263_2.png) [@trotod](https://discourse.codecombat.com/u/trotod)\
**Post date:** [October 16, 2015, 12:38am UTC](https://discourse.codecombat.com/t/binary-deployment-help-java-script/5262/2 "2015-10-16T00:38:50Z")

</div>

Paladins have a `deployment` property defined on them to determine the units needed. So something like:

```python
this.deployLine(paladin.deployment, paladin.pos.x, paladin.pos.y)

```

Instead of using `this.findNearest(this.findFriends())`, you can get the last summoned unit with `this.built[this.built.length - 1]`. You might be trying to command the wrong minion otherwise…

---

<div class="post-metadata">

**Author:** ![Paul\_Lum](https://avatars.discourse-cdn.com/v4/letter/p/ea5d25/32.png) [@Paul\_Lum](https://discourse.codecombat.com/u/Paul_Lum)\
**Post date:** [October 16, 2015, 9:45pm UTC](https://discourse.codecombat.com/t/binary-deployment-help-java-script/5262/3 "2015-10-16T21:45:22Z")

</div>

Thank you. That worked perfectly. Now all I need to do is figure out how to do it for the trinary one! 😛

---

<div class="post-metadata">

**Author:** ![naive](https://avatars.discourse-cdn.com/v4/letter/n/e495f1/32.png) [@naive](https://discourse.codecombat.com/u/naive)\
**Post date:** [November 8, 2015, 10:34am UTC](https://discourse.codecombat.com/t/binary-deployment-help-java-script/5262/4 "2015-11-08T10:34:23Z")

</div>

Hey guys, didn’t want to create a new topic, I also have problem with this level, I sort of found a way to summon the right troops, but now I just can’t figure out how to arrange them in neat columns, any suggestions? How could i target each troop, and make him move to the position of the last commanded troop?

this is my code so far

```
var toBinary = function(decNum){
return parseInt(decNum,10).toString(2);
};

loop{
var paladins = this.findByType("paladin");
for (i=0; i < paladins.length; i++){
    paladin = paladins[i];
    //all binaries looped through
    var binary = toBinary(paladin.deployment);
    
    var soldiersInLine = binary.length;
    var startY = paladin.pos.y;
    var columnY = startY;
    
    
    this.say(binary.length);
    for (j=0; j < binary.length; j++){
        singleBinary = binary[j]; //each binary number by number
       
        if (singleBinary === "0"){
            this.summon("soldier");    
        } else {
            this.summon("archer");    
        }
        friend = this.built[this.built.length-1];
        lastFriend = this.built[0];
  
        // this.say(singleBinary);
    }   
        
}

```

}

---

<div class="post-metadata">

**Author:** ![ChronistGilver](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/chronistgilver/32/3778_2.png) [@ChronistGilver](https://discourse.codecombat.com/u/ChronistGilver)\
**Post date:** [November 8, 2015, 4:35pm UTC](https://discourse.codecombat.com/t/binary-deployment-help-java-script/5262/5 "2015-11-08T16:35:19Z")

</div>

Tell the last thing you built to move the paladin’s `y` position, then make the `x` coordinate a number added to another number multiple by `j`. It might look something like this:

```python
// Assuming my paladin stands at (10,10)
friend = this.built(this.built.length-1);
this.command(friend,"move",{"x":x+(x*i),"y":paladin.pos.y})

```

where `x` is a number.

---

<div class="post-metadata">

**Author:** ![Imperial\_Soldier](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/imperial_soldier/32/5376_2.png) [@Imperial\_Soldier](https://discourse.codecombat.com/u/Imperial_Soldier)\
**Post date:** [February 17, 2017, 3:35am UTC](https://discourse.codecombat.com/t/binary-deployment-help-java-script/5262/6 "2017-02-17T03:35:24Z")

</div>

For some reason, I think it’s not showing the first number of the binary number: why? How can I fix it?  
Here’s my code:

```python
var Binary = function(Num){
    return parseInt(Num, 10).toString(2);
};
var Trinary = function(Num){
    return parseInt(Num, 10).toString(3);
};
var paladins = hero.findByType("paladin");
var warlocks = hero.findByType("warlock");
for (var i in paladins) {
    var paladin = paladins[i];
    var binary = Binary(paladin.deployment);
    
    for (var j = 0; j < binary.length; j++){
        var abinary = binary[j];
        if (abinary === "0"){
            hero.summon("soldier");
        }
        else if (abinary === "1"){
            hero.summon("archer");
        }
        var friend = hero.built[hero.built.length-1];
        hero.command(friend,"move",{"x":12+(4*j),"y":paladin.pos.y});
    }
}
for (var a in warlocks) {
    var warlock = warlocks[a];
    var trinary = Trinary(warlock.deployment);
    for (var b = 0; b < trinary.length; b++) {
        var atrinary = trinary[b];
        if (atrinary === "0") {
            hero.summon("soldier");
        }
        else if (atrinary === "1") {
            hero.summon("archer");
        }
        else if (atrinary === "2") {
            hero.summon("griffin-rider");
        }
        var friend2 = hero.built[hero.built.length-1];
        hero.command(friend2,"move",{"x":12+(4*b),"y":warlock.pos.y});
    }
}

```

(I’ll remove the code after this is resolved, if requested)

---

<div class="post-metadata">

**Author:** ![FausT\_MadMan](https://avatars.discourse-cdn.com/v4/letter/f/b782af/32.png) [@FausT\_MadMan](https://discourse.codecombat.com/u/FausT_MadMan)\
**Post date:** [May 30, 2017, 4:02am UTC](https://discourse.codecombat.com/t/binary-deployment-help-java-script/5262/7 "2017-05-30T04:02:34Z")

</div>

Hi.  
I fixed it this way.

```python
function DecToAnyBase(Number, Base, Length) {
    var baseLine = parseInt(Number, 10).toString(Base).split("");
    if (baseLine.length < Length) {
        baseLine.reverse();
        while(baseLine.length < Length) {
            baseLine.push("0");
        }
        baseLine.reverse();
    }
    return baseLine;
}

function TacticalFormationOfTroops (commanders) {
    var formationLine = [];
    for (var i in commanders) {
        var commander = commanders[i];
        if (commander.type == "paladin") {
            formationLine = DecToAnyBase(commander.deployment, 2, 8);
        } else if (commander.type == "warlock") {
            formationLine = DecToAnyBase(commander.deployment, 3, 8);
        }
        for (var j = 0; j <= formationLine.length - 1; j++){
            if (formationLine[j] === "0"){
                hero.summon("soldier");
            } else if (formationLine[j] === "1"){
                hero.summon("archer");
            } else if (formationLine[j] === "2") {
                hero.summon("griffin-rider");
            }
            var troop = hero.built[hero.built.length-1];
            hero.command(troop,"move",{x:20+(4*j),y:commander.pos.y});
        }
    }
}

TacticalFormationOfTroops(hero.findFriends());

```
