Bubble Sort Level Broken?


#1

This code should be a perfect Javascript bubble sort, so what’s the deal?

this.say("Let's plan ahead!");
//We must sort the soldiers in order ,from strongest to weakest
//Use the this.getFriends() to get all the soldiers
//and compare them with the .scaleFactor property
//ex : if(friend[1].scaleFactor < friend[5].scaleFactor) doSmth();
//also to swap them on the battlefield use smth like
//this.say("<random>",[friend[2].id,friend[4].id]);
//after they are sorted just say "ATTACK!"

friends = this.getFriends();
var swapped;
do {

    swapped = false;
    for (var i = 0; i < friends.length - 1; i++) {
        if (friends[i].scaleFactor < friends[i + 1].scaleFactor) {
            this.say("Switch'em Up!", [
                friends[i].id,
                friends[i + 1].id
            ]);
            swapped = true;
        }
    }
} while (swapped);
// FINALLY!
this.say("ATTACK!");

#2

the sliding around has been fixed!


#3

There are a few problems I have with this level.

  1. It asks us to sort them from strongest to weakest… but that’s wrong, it wants weakest to strongest.
  2. getFriends(); doesn’t update, so you have to manage the damn array in memory.
  3. I can’t implement the javascript array.sort to make a nice clean solution, it demands manual bubble sort… ok this one is just a little gripe.

Solution code for anyone looking to handle this:

this.say("Let's plan ahead!");
//We must sort the soldiers in order ,from strongest to weakest
//Use the this.getFriends() to get all the soldiers
//and compare them with the .scaleFactor property
//ex : if(friend[1].scaleFactor < friend[5].scaleFactor) doSmth();
//also to swap them on the battlefield use smth like
//this.say("<random>",[friend[2].id,friend[4].id]);
//after they are sorted just say "ATTACK!"

friends = this.getFriends(); // this never updates, so we have to handle this manually
var swapped;
do {

    swapped = false;
    for (var i = 0; i < friends.length - 1; i++) 
    {
        if (friends[i].scaleFactor > friends[i + 1].scaleFactor) 
        {
            this.say("Switch'em Up! "  + friends[i] + " AND " + friends[i+1] + " with i == " + i + " And friends == " + friends, [
                friends[i].id,
                friends[i + 1].id
            ]);
            // update the order of friends
            var temp = friends[i];
            friends[i] = friends[i+1];
            friends[i+1] = temp;

            swapped = true;
        }
    }
} while (swapped);
// FINALLY!
this.say("ATTACK!");

#4

Well, the first problem in your Javascript bubble sort is that you don’t actually swap the soldiers. You just print the message “Switch’em Up!”.
Another problem is that you have to sort them ascending.
Here i have bolded out your problems.

if (friends[i].scaleFactor > friends[i + 1].scaleFactor) {
this.say(“Switch’em Up!” + friends[i] + " with " + friends[i+1],[ friends[i].id,friends[i+1].id]);
**var temp = friends[i];
friends[i] = friends[i+1];
friends[i+1] = temp; **
swapped = true;
}

Also, in the begining Tharin says : “In order to sort this soldiers you will have to use 2 for-loops”. So I would think that this solve is more aproppiate:

friends = this.getFriends();
for (var i = 0; i < friends.length - 1; i++) 
    for (var j = i+1; j < friends.length ; j++) 
    {
        if (friends[i].scaleFactor > friends[j].scaleFactor) 
        {
            this.say("Switch'em Up!"  + friends[i] + " with " + friends[j],[ friends[i].id,friends[j].id]);
            var temp = friends[i];
            friends[i] = friends[j];
            friends[j] = temp;
        }
    }

At least that’s my oppinion.


#5

Hey SpedsT, I appreciate your assistance, but you can see I eventually posted code that works :smile:

There was a bug on sliding, that was fixed, and after that it was a matter of identifying that getFriends(); was always the same order (wish they would have said that upfront)


#6

Gant_Laborde thanks for the feedback.
The sliding has been fixed , regarding the getFriends() function I will specify from the start how it works, unfortunately it will always return the same order due to the way the function works in the background.

PS. I am the creator of the level :smiley:


#7

Hey Darredevil! I love the idea of the level!

I was using this.say as my console.log, so I could find why my code wasn’t working :smile:

Specifying that getFriends() doesn’t update order will be a huge help! Thanks for making an awesome level, I really love what you’re doing!


#8

I had a really hard time with this level because, honestly, i had NO IDEA what it meant when it was telling me that this.getFriends didnt update.
I found it immensely frustrating though because I noticed that on the second iteration it would begin swapping soldiers improperly…
I knew SOMETHING was wrong with the array but I had no idea how to fix it.
Thanks for posting that solution-I was at wits end.

The code acts all kinds of funky in this level…
for some reason I cant get this to work…
Can someone explain why?

var friend = this.getFriends();

do{
    var sorted = true;
    
    for(var i=0;i<friend.length-1;i++)
    {
        var n= i+1;
        this.say(friend[i] + " SWAP WITH " +friend[n]);
        if(friend[i].scaleFactor > friend[n].scaleFactor)
        {
            this.say("<random>",[friend[i].id,friend[n].id]);
            
            var temp = friend[i];
            friend[i] = friend[n];
            friend[n] = temp;
            
            sorted = false;
        }
    }
    
}while(sorted===false);

The code LOOKS right to me but for some reason it won’t swap one of the soldiers.
BEFORE i added the array update it would do something REALLY weird-
IF I were to change the comparative operator > to < … it would either swap ONLY the top two and miss the middle guy or ignore either end of the group

I’d really suggest you flatout tell people that it is important that they manaully update the array X_X


#9
var friends=this.getFriends();
for(var j=0;j<friends.length;j++)
for(var i=0;i<friends.length-1;i++){
    if(friends[i].health>friends[i+1].health){
        this.say("swap",[friends[i].id,friends[i+1].id]);
        var temp=friends[i];
        friends[i]=friends[i+1];
        friends[i+1]=temp;
    }
}
this.say("attack");

//this is usable


#10

Hello,
i’m actually playing this level, using this code to sort my soldiers

    var friends = this.getFriends();

function echange(list, var1, var2) {
var sauvegarde = list[var1];
list[var1] = list[var2];
list[var2] = sauvegarde;
}

for (var i = 0; i < friends.length; i++) {
    for (var j = i; j < friends.length; j++) {
        if (friends[i].scaleFactor > friends[j].scaleFactor) {
            this.say("bougez vous le cul !", [
                friends[i].id,
                friends[j].id
            ]);
            echange(friends, i, j);
        }
    }
}
this.say("attack !");

Unfortunately when I launch the attack, all my soldiers get killed in despite of the order that seems good to me, is it normal ?


#11

Oops, i broke their distance checks in a recent update to use more accurate geometry. Fixed, now–give it another shot. Thanks for the heads up!


#12

Safe to say code combat has no tests :stuck_out_tongue:


#13

I find the error :

Soldiers don’t understand french ; D


#14

It works, thank you.

the soldier seems need some french injuries for the motivation :wink: