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!");
It asks us to sort them from strongest to weakest… but that’s wrong, it wants weakest to strongest.
getFriends(); doesn’t update, so you have to manage the damn array in memory.
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!");
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.
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;
}
}
Hey SpedsT, I appreciate your assistance, but you can see I eventually posted code that works
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)
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.
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