[SOLVED]Perimeter defence help!

Hello! This is my code:

# Нужно построить сторожевые башни вокруг деревни.
# Один крестьянин может построить одну башню.
# Укажи им место строительства.
# Эти башни будут автоматически атаковать ВСЕХ юнитов вне города.

# Сначала пройди вдоль северной границы (y=60) от x=40 до x=80 с шагом 20.
# `range` не включает вторую границу.
for x in range(40, 80, 20):
    # Двигайся на каждую точку и что-нибудь говори.
    hero.moveXY(x, 60)
    hero.say("Here")
# Далее пройди вдоль восточной границы (x=80) от y=40 до y=20 с отрицательным шагом -20.
for y in range(40, 20, -20):
    hero.moveXY(80, y)
    hero.say("Here")
# Продолжай для двух оставшихся границ.
# Далее южная граница (y=20) от x=60 до x=20 с отрицательным шагом -20.
for x in range(60, 20, -20):
    hero.moveXY(x, 20)
    hero.say("Here")
# Далее западная граница (x=20) от y=40 до y=60 с шагом 20.
for y in range(40, 60, 20):
    hero.moveXY(20, y)
    hero.say("Here")

# Не забудь укрыться в деревне.
hero.moveXY(50, 40)

Here result:


Here equipment:

My hero moves completely wrong direction, even though I followed all advices.Please help!

can I have a link to the level @Anonym

Hi @Anonym,
There’s only one mistake, but it’s repeated three times in your code. It’s to do with how for loops work.
Essentially for loops take thre inputs:
(start, end, step change)
But the weird thing is, the end number has to be one further (in the direction the for loop is going, up or down) than what you want it to run. This is because arrays start on zero. If you’re confused, I think reading my lengthy post here might help you a little bit more (it’s a very similar issue):

Also maybe read the rest of that thread.
Try this code and see what happens (just for testing don’t use this for the level):

# Let's say you want your hero to say the numbers 1-10:
for i in range(1, 10):
    hero.say(i)

#Output:
#1
#2
#3
#4
#5
#6
#7
#8
#9

Notice how your hero stops at 9? So if you want your hero to say 10 you’d do this:

for i in range(0, 11):
    hero.say(i)

So if you want to end at 80 on the first for loop you would go one further in the direction that the for loop is going in (this time it’s upwards) == 81.
Now see if you can do the other ones.
Danny

3 Likes

Thanks a lot @Deadpool198!

Here: https://codecombat.com/play/level/perimeter-defense?

Ok, @Deadpool198, I followed your advices, but it didn’t work.
Code:

# Нужно построить сторожевые башни вокруг деревни.
# Один крестьянин может построить одну башню.
# Укажи им место строительства.
# Эти башни будут автоматически атаковать ВСЕХ юнитов вне города.

# Сначала пройди вдоль северной границы (y=60) от x=40 до x=80 с шагом 20.
# `range` не включает вторую границу.
for x in range(40, 81, 20):
    # Двигайся на каждую точку и что-нибудь говори.
    hero.moveXY(x, 60)
    hero.say("Here")
# Далее пройди вдоль восточной границы (x=80) от y=40 до y=20 с отрицательным шагом -20.
for y in range(40, 21, -20):
    hero.moveXY(80, y)
    hero.say("Here")
# Продолжай для двух оставшихся границ.
# Далее южная граница (y=20) от x=60 до x=20 с отрицательным шагом -20.
for x in range(60, 21, -20):
    hero.moveXY(x, 20)
    hero.say("Here")
# Далее западная граница (x=20) от y=40 до y=60 с шагом 20.
for y in range(40, 61, 20):
    hero.moveXY(20, y)
    hero.say("Here")

# Не забудь укрыться в деревне.
hero.moveXY(50, 40)

Screenshot:

1 Like

You’re almost there. The first and last for loops are correct, but the middle two aren’t. Let’s look at how for loops go up or down. Normally a for loop would increase the i (or x or y) variable by 1 every time it loops, until it reaches one below the number you have set as the end variable. (you used 81 in your first for loop, that’s correct).
But, think about what happens if you put a step change number (the third number in the brackets of the for loop, you’ve got 20 and -20) which is negative? What will happen is the for loop will go down, instead of up. So every loop the x or y variable will decrease by 20. So, thinking about the fact that the end number should be 1 further in the direction (up or down) the for loop is travelling, what do you think the end number 21 should be changed to?
Danny

1 Like

I also noticed that he modified the second for-loop which if he hadn’t that one would have worked fine. I think the last for-loop is the problem @Deadpool198

Milton, please elaborate on your post. Have you tested your theory? If so, I think you’ll find I’m correct.
Danny

Well, you’ve made the same mistake as him, so I don’t really see how you can be offering help I’m afraid. Maybe just read my posts in this topic, and in the one I gave the link to and you might be able to solve it as well. I don’t think it’s very helpful to help someone with a level you can’t do yourself.
Danny

Thanks a lot to everyone!I solved it! :partying_face:

This topic was automatically closed 12 hours after the last reply. New replies are no longer allowed.