My hero can't move in prime pathing


#1

def Primes(trap):
for div in range (2,Math.ceil(Math.sqrt(trap.value))):
if trap.value % div == 0:
return False
self.say(trap.value)
return True

traps = self.findByType(“fire-trap”)
P = []
for trap in traps:
if Primes(trap):
P.append(trap)

nearest = self.findNearest§
if nearest:
hero.move(nearest.pos)
friends = hero.findFriends()
for friend in friends:
hero.command(friend, ‘move’, nearest.pos)


Help with Diamond Dozen
#2


#3

Well, I would say that you are not in a loop. Whne you use: hero.move(x, y) the program simply moves one ‘step’ in that direction. If there was a overarching while True function then your program would calculate everything for every step but would actually move more regularly. In order to use the hero.move(x,y) you need a looping function similar to this:

while hero.pos.x =! nearest.pos.x:
    hero.move(nearest.pos)

You will probobly want to have a better move function setup, but in conlucion you need two loops. One for moving, and one for keeping your program ‘alive’.
-Cheers


#4

Ok, finally my hero reach the end now. But I’m still confused about how to command my troop. They just stay together when they are moving. Then they will step on the bomb. Could any one help me?
def Primes(trap):
for div in range (2,Math.ceil(Math.sqrt(trap.value))):
if trap.value % div == 0:
return False
self.say(trap.value)
return True

traps = self.findByType(“fire-trap”)
P = []
for trap in traps:
if Primes(trap):
P.append(trap)
loop:
nearest = self.findNearest§
if nearest:
hero.moveXY(hero.pos.x, nearest.pos.y)
while hero.pos.x != nearest.pos.x:
hero.move(nearest.pos)
hero.moveXY(hero.pos.x-15,hero.pos.y)
if hero.now()>16:
break
hero.moveXY(4, 15)
loop:
nearest = self.findNearest§
if nearest:
hero.moveXY(hero.pos.x, nearest.pos.y)
while hero.pos.x != nearest.pos.x:
hero.move(nearest.pos)
hero.moveXY(hero.pos.x+15,hero.pos.y)
indent preformatted text by 4 spaces


#5
I try to command my friends, but they didn't works well<img src="/uploads/codecombat/original/2X/9/91db8e23266c43a5e5b2e10a0d486f19aeea5a24.png" width="368" height="175">
def Primes(trap):
    for div in range (2,Math.ceil(Math.sqrt(trap.value))):
        if trap.value % div == 0:
            return False
    self.say(trap.value)
    return True
   
traps = self.findByType("fire-trap")
P = []
for trap in traps:
    if Primes(trap):
        P.append(trap)
traps2 = self.findByType("bear-trap")
G = []
for trap2 in traps2:
    if Primes(trap2):
        G.append(trap2)
loop:
    nearest = self.findNearest(P)
    if nearest:
        hero.moveXY(hero.pos.x, nearest.pos.y)
        friends = hero.findFriends()
        friendIndex=0
        for friend in friends:
            friendIndex+=1
            hero.command(friend, 'move', {"x":hero.pos.x+2*friendIndex,"y":hero.pos.y})
            hero.wait(1)
        while hero.pos.x != nearest.pos.x:
            hero.move(nearest.pos)
            friendIndex1=0
            for friend in friends:
                hero.command(friend, 'move', {"x":hero.pos.x+2*friendIndex1,"y":hero.pos.y})
                hero.wait(1)
        hero.moveXY(hero.pos.x-15,hero.pos.y)
        friendIndex2=0
        for friend in friends:
                hero.command(friend, 'move', {"x":hero.pos.x+friendIndex2*2,"y":hero.pos.y})
                hero.wait(1)
        if hero.now()>50:
            break
hero.moveXY(4, 15)
loop:
    nearest = self.findNearest(P)
    if nearest:
        hero.moveXY(hero.pos.x, nearest.pos.y)
        while hero.pos.x != nearest.pos.x:
            hero.move(nearest.pos)
        hero.moveXY(hero.pos.x+15,hero.pos.y)   
        if hero.now()>26:
            break`indent preformatted text by 4 spaces`

#6

Here is the image


#7

My soldiers keep pushing me forward and I can’t move to next trap


#8

As of now I am not in the Ice world, so my help is severely limited as I have never played the level. But my suggestion would be to add a delay or slow your troops down. Have your hero move, then move forward +10 or something. That way the hero is not standing on the trap area when the soldiers come though. Maybe vice versa. I’m not sure I can really do anything at this point. Try to get everyone moving outside of the group. Maybe individually?
-Cheers


#9

I am currently in the process of solving this level myself as well, I have solved all the primes, but i haven’t gotten to the movement part yet.

Obviously if you command all units to move at the same time you risk accidentally walking over a mine due to unit concision, so think i would just calculate a route, store the important locations in variables and then make a ‘train’ of units.


#10
while hero.pos.x != nearest.pos.x:
            hero.move(nearest.pos)
            friendIndex1=0
            for friend in friends:
                hero.command(friend, 'move', {"x":hero.pos.x+2*friendIndex1,"y":hero.pos.y})
                hero.wait(1)

seems like this is the part where your program gets stuck, i think.

Because as far as i can tell, your hero will try to move to a position and also order others to get there, the others will push him away from the position, and he will try to get back to there, and also order his friends to join him, pushing him back, and it will go on forever.

At least, that’s what i think.


#11

May we just order our hero go to the end first and order our soldier to go one by one?


#12

It looks like from what I can see there is enough room in each ‘cell’ to move the Hero beyond the traps, and then pull all the soldiers through one by one, or two by two etc.


#13

The most common way for people to do this was to make a list of points and then have the hero and minions move through the points. When I beat it, I moved to the location then I had minion 1 move to my position, minion 2 move to minion 1 position and so on.

I blew up a lot on the corners, but eventinally beat it.


#14

The code I have written now does this as well, but I somehow broke the level so it won’t load at all.

I hope I get to try the level soon so I can see if my code worked


#15

Ok, finally my soldiers move. But I don’t have enough time to run it
def Primes(trap):
for div in range (2,Math.ceil(Math.sqrt(trap.value))):
if trap.value % div == 0:
return False
self.say(trap.value)
return True

    traps = self.findByType("fire-trap")
    P = []
    for trap in traps:
        if Primes(trap):
            P.append(trap)
    traps2 = self.findByType("bear-trap")
    G = []
    for trap2 in traps2:
        if Primes(trap2):
            G.append(trap2)
    loop:
        nearest = self.findNearest(P)
        if nearest:
            hero.moveXY(hero.pos.x, nearest.pos.y)
            friends = hero.findFriends()
            friendIndex=0
            for friend in friends:
                friendIndex+=1
                hero.command(friend, 'move', {"x":hero.pos.x+2,"y":hero.pos.y})
                hero.wait(1)
            while hero.pos.x != nearest.pos.x:
                hero.move(nearest.pos)
            friendIndex1=0
            for friend in friends:
                hero.command(friend, 'move', {"x":hero.pos.x-2,"y":hero.pos.y})
                hero.wait(1)
            hero.moveXY(hero.pos.x-10,hero.pos.y)
            friendIndex2=0
            for friend in friends:
                friendIndex2+=1
                hero.command(friend, 'move', {"x":hero.pos.x+2,"y":hero.pos.y})
                hero.wait(1)
            if hero.pos.x<11:
                break
    hero.moveXY(2, 14)
    loop:
        nearest = self.findNearest(P)
        if nearest:
            hero.moveXY(hero.pos.x, nearest.pos.y)
            friends = hero.findFriends()
            friendIndex=0
            for friend in friends:
                friendIndex+=1
                hero.command(friend, 'move', {"x":hero.pos.x+4,"y":hero.pos.y})
                hero.wait(1)
            while hero.pos.x != nearest.pos.x:
                hero.move(nearest.pos)
            friendIndex1=0
            for friend in friends:
                hero.command(friend, 'move', {"x":hero.pos.x-4,"y":hero.pos.y})
                hero.wait(1)
            hero.moveXY(hero.pos.x+10,hero.pos.y)
            friendIndex2=0
            for friend in friends:
                friendIndex2+=1
                hero.command(friend, 'move', {"x":hero.pos.x-4,"y":hero.pos.y})
                hero.wait(1)
    `indent preformatted text by 4 spaces`