So i just dived in and came up with this to translate the numbers to binary, it’s all good until i try to fill in the 0’s. Now you’ll notice the various self.says in there, those are checking the code is functioning fine. The second to last one gives back the binary number, only reversed and with None instead of 0’s. My problem is, no matter how i format it, it won’t change them. I’ve tried counters with loops or while and fors in all shapes and sizes, but i can only transform ONE of the None’s into a 0, and only if i haven’t reversed the array yet. It’s driving me nuts, what the hell is going on?
Code:
bin =
def base2(numero):
store = numero
counter = 0
difference = store - Math.pow(2,counter)
counter = -1
clas = 1
while store > 0:
counter = -1
clas = 1
while store/clas >= 1:
clas = 2*clas
counter +=1
self.say(counter)
bin[counter] = 1
store = store - Math.pow(2,counter)
n = 0
self.say(bin)
for n in bin:
if bin[n] == None:
bin[n] = 0
bin2 = bin.reverse()
self.say(bin)
elnum =base2(25)
edit: obviously the code has it’s proper spacing on the actual game.
edit2: self-fixed with
n = len(bin)
m=0
while m < n:
if bin[m] != 1:
bin[m] = 0
m +=1
bin.reverse()
self.say(bin)
i’d still really like to know why what i was doing before wasn’t working
Hello, polsi, and welcome. In order to get the proper spacing, please format your code according to the FAQ.
Please refrain from swearing within this forum. We have some sensitive players here.
I believe your problem was this:
You have while-loop while store > 0:. Once this while-loop is over, another while-loop occurs, while store/clas >= 1. However, since store is now 0 (after the first while-loop) and clas is 1, this while-loop never takes place.
Here we go, sorry about that, here is the formatted code.
The while is working,matter of fact the code works, i got it running without changing the top part, i am curious as to why the for “n in bin” bit didn’t work.
bin = []
def base2(numero):
store = numero
counter = 0
difference = store - Math.pow(2,counter)
counter = -1
clas = 1
while store > 0:
counter = -1
clas = 1
while store/clas >= 1:
clas = 2*clas
counter +=1
self.say(counter)
bin[counter] = 1
store = store - Math.pow(2,counter)
n = 0
self.say(bin)
for n in bin:
if bin[n] == None:
bin[n] = 0
bin2 = bin.reverse()
self.say(bin)
elnum =base2(25)
Ah, thanks. Your problem with the for n in bin: is that for n in bin: loops through all the elements in a list, not the indices. Inside the for-loop, bin[n] should throw an error, because n is an element of the list, not an index.
Thanks! i had been mindlessly using “for” since the forest :P. One more question though, i also tried for n in len(bin) and for n in enumerate(bin), is there any way i can use a for to achieve what i want there or while and a counter is the only way to go?
Okay i need help again! i’ve got the binary and trinary algortihms down returning arrays with the conversion. My problem is moving such a mass of troops is proving difficult, the spawning part works, but as soon as i get to the move part the code implodes and crashes the game. It works fine without looping it, but since i have a huge clutter of troops not all of them are able to execute their commands. Anyway i can move them in position in a more friendly way?
friends = self.findFriends()
mynum = []
for friend in friends:
if friend.type == "paladin":
mynum = base2(friend.deployment)
for i in range(len(mynum)):
if mynum[i] == 0:
self.summon("soldier")
if mynum[i] ==1:
self.summon("archer")
if friend.type == "warlock":
mynum = base3(friend.deployment)
for i in range(len(mynum)):
if mynum[i] == 0:
self.summon("soldier")
if mynum[i] ==1:
self.summon("archer")
if mynum[i] ==2:
self.summon("griffin-rider")
soldiers = self.findByType("soldier")
archers =self.findByType("archer")
griffins = self.findByType("griffin-rider")
loop:
self.wait(1)
for friend in friends:
s = 0
v = 0
h = 0
if friend.type == "paladin":
mynum = base2(friend.deployment)
for i in range(len(mynum)):
if mynum[i] == 0:
self.command(soldiers[s],"move",Vector(friend.pos.x +10+5*i,friend.pos.y))
s +=1
if mynum[i] ==1:
self.command(archers[v],"move",Vector(friend.pos.x +10+5*i,friend.pos.y))
v +=1
if friend.type == "warlock":
mynum = base3(friend.deployment)
for i in range(len(mynum)):
if mynum[i] == 0:
self.command(soldiers[s],"move",Vector(friend.pos.x +10+5*i,friend.pos.y))
s +=1
if mynum[i] ==1:
self.command(archers[v],"move",Vector(friend.pos.x +10+5*i,friend.pos.y))
v +=1
if mynum[i] ==2:
self.command(griffins[h],"move",Vector(friend.pos.x +10+5*i,friend.pos.y))
h +=1
You dirty cheater! Can’t you convert your numbers yourself (now I have to rewrite my attempt to use the built-in functions…)?
Where exactly does it explode? Does it complete the first part fine (Put a self.say behind it).
Or even better, does it show an error?
The only thing bothering me is actually that the second part is looped, which maybe could cause a HEL. But your self.wait(1) should catch this. You don’t have to command your units constantly, they will continue to execute their last command until they can’t (either because the target is invalid or they are dead).
You wrote about that… Then I have no real idea, maybe try to handle each paladin in order and not all at the same time?
Thanks for the replies everyone. First my algorithm was way too resource intensive, a friend mentioned how % can be used to transform decimal to different bases and I used that to refine it (went from 20+ lines of code to 9). Second problem was i wasn’t aware findfriends() would always return the same index for any given friend when looped, which is quite handy to command units that have been very recently created. Now knowing this I’ve managed to solve the problems and complete the level :).
this is my code and it works just fine: Mod edit: solution removed. Solutions aren’t allowed as they do not help others learn, they just give people a way to dodge difficult levels (well done for completing it by the way! )