Hi guys. I am trying to make the fragile maze know who it’s master is but for some reason I am not able to complete the level. I have tried various different combinations on the basis that in some of my conditions, the operators are different to make the code work. Here is my code:
while True:
startRoom = {"x": 18, "y": 19}
visited = []
distanceBetweenRooms = 16
if hero.isPathClear(hero.pos, {"x": hero.pos.x, "y": hero.pos.y + distanceBetweenRooms}) and hero.pos != startRoom:
for visit in visited:
if visit == hero.pos:
break
hero.moveXY(hero.pos.x, hero.pos.y + distanceBetweenRooms)
visited.append(hero.pos)
elif hero.isPathClear(hero.pos, {"x": hero.pos.x, "y": hero.pos.y + distanceBetweenRooms}) and hero.pos != startRoom:
for visit in visited:
if visit == hero.pos:
break
hero.moveXY(hero.pos.x, hero.pos.y - distanceBetweenRooms)
visited.append(hero.pos)
elif hero.isPathClear(hero.pos, {"x": hero.pos.x - distanceBetweenRooms, "y": hero.pos.y}) and hero.pos != startRoom:
for visit in visited:
if visit == hero.pos:
break
hero.moveXY(hero.pos.x + distanceBetweenRooms, hero.pos.y)
visited.append(hero.pos)
elif hero.isPathClear(hero.pos, {"x": hero.pos.x + distanceBetweenRooms, "y": hero.pos.y}) and hero.pos != startRoom:
for visit in visited:
if visit == hero.pos:
break
hero.moveXY(hero.pos.x + distanceBetweenRooms, hero.pos.y)
visited.append(hero.pos)
Ok, I think there are some holes in the logic of this code.
Firstly:
It’s a good (and essential) addition to use visited array, so good job. But look carefully at what the code is actually doing. At the time you run the for loop you haven’t moved from your last location yet. That means your pos is in the visited array. Now, when you get to that visit in visited, the for loop will break. Then you’ll move to your location and append the pos to visited. So, what did you actually check with the for loop? You checked the wrong thing, remember you haven’t moved yet so you need to check the next pos you move to.
Also in both of the first two statments, the if and the elif, you’ve got "y": hero.pos.y + distanceBetweenRooms Maybe one should be -?
Finally
visited is being refreshed with every loop, and it’s being refreshed to , that is to say: empty. That’s no good. (maybe define it where it won’t be looped over and over again…)
I think this method (visited and object literals like {"x": 57, "y": 44}) can work, but if you have enough fiddling around with this one (like I did), then I’ll help you try and change it to another method: one much shorter and more powerful (to almost quote Star Wars). But try this one first and see if you can get it to work. It’s definitely possible.
Danny
Thanks @Deadpool198. Sorry I took long replying, if it was long to you.
This bit what I am trying to get my head around. Initially, I thought that by putting the for-loop after moving, it would append that position to the visited array; however, I am still getting the same results except this:
Before:
My hero bumps into a wall that won’t open
After:
My hero just stops. I think it is because of the operators in moving, but if I change any one of them, then I won’t be able to get as far in the maze.
Other than that, I have done everything else that you have told me to do.
And as a final note @Deadpool198, if I make all the operators in the if-statements for moving the same (when moving up, down, left, right), then I will just be moving in a square. That’s why they are different
Well, you could perfect the visited array check and then it would work… I’m not sure changing it like this will work very well. It’s quite inconsistent. Please could you post your new code onto the forum so I can Ctrl C, Ctrl V it into CoCo.
Danny
# Escape from the maze!
# Some doors are blocked, some open when you are near them.
startRoom = {"x": 18, "y": 19}
visited = []
while True:
distanceBetweenRooms = 16
if hero.isPathClear(hero.pos, {"x": hero.pos.x, "y": hero.pos.y + distanceBetweenRooms}) and hero.pos != startRoom:
hero.moveXY(hero.pos.x, hero.pos.y + distanceBetweenRooms)
visited.append(hero.pos)
for visit in visited:
if visit == hero.pos:
break
elif hero.isPathClear(hero.pos, {"x": hero.pos.x, "y": hero.pos.y + distanceBetweenRooms}) and hero.pos != startRoom:
hero.moveXY(hero.pos.x, hero.pos.y - distanceBetweenRooms)
visited.append(hero.pos)
for visit in visited:
if visit == hero.pos:
break
elif hero.isPathClear(hero.pos, {"x": hero.pos.x + distanceBetweenRooms, "y": hero.pos.y}) and hero.pos != startRoom:
hero.moveXY(hero.pos.x + distanceBetweenRooms, hero.pos.y)
visited.append(hero.pos)
for visit in visited:
if visit == hero.pos:
break
elif hero.isPathClear(hero.pos, {"x": hero.pos.x + distanceBetweenRooms, "y": hero.pos.y}) and hero.pos != startRoom:
hero.moveXY(hero.pos.x - distanceBetweenRooms, hero.pos.y)
visited.append(hero.pos)
for visit in visited:
if visit == hero.pos:
break
Ah @Deadpool198, I think I have what you want me to do. You want me to check the last position and not the hero’s position. The thing is, how do I do that? I am really stuck on how to do that:
elif hero.isPathClear(hero.pos, {"x": hero.pos.x, "y": hero.pos.y - distanceBetweenRooms}):
if hero.pos != startRoom:
hero.moveXY(hero.pos.x, hero.pos.y - distanceBetweenRooms)
for visit in visited:
if visit == visited[len(visited) - 1]:
visited.append(hero.pos)
continue
Okay, I’m afraid we have come to a bit of a turning point here. I thought it might be possible to win this level using a tactic like yours (that’s what mine was when I first tried this level, and I failed.), but I, personally, can’t find a way to do it.
So, instead, let’s try Vectors! Yay! such fun.
To start off with, I would recommend a quick refresh of the Vector levels (trust me, you need to refresh them all the time to actually remember how to use them…) Then we’ll get going.
Danny
Hello @Deadpool198, but I have an inkling that Vectors aren’t ‘such fun’. In actual fact, I haven’t done any levels that rely on Vectors except Summits Gate but you already know about that. I have a vague idea that when commanding troops to vector move means to make them go to the points around it in a circle. I am actually on the way to school now so I won’t be able to reply until after half-past six in the evening due to the usual. Bye for now, but I will keep in mind what vectors in python are.
I think they are . Once you get used to them they’re alright. If you haven’t done any Vector levels then I strongly recommend, in fact it’s essential that you do the Vector levels that come in the Glacier. If the levels haven’t shown up for you in the right order, then that’s the game’s fault, but you really do have to do those levels before completing this. Start with circle-walking (put that in the URL, and it will enter the level.) After that, then skating-away will unlock, then brewball. Once you’ve done them you’ll have a much better understanding of Vectors. I would also recommend trying to use them in levels (like summit’s gate) for fleeing from enemies. That’s good practice.
Danny
Hello @Deadpool198, sorry I haven’t been around for a while; there was a fire near our school and I had to do my work on Google Classroom which takes longer than if I am actually at school.
Anyway, I am a bit stuck on Skating Away:
# The point you want to get to.
goalPoint = Vector(78, 34)
while True:
# This creates a vector that will move you 10 meters toward the goalPoint
# First, create a vector from your hero to the goal point.
goal = Vector.subtract(goalPoint, hero.pos)
# Then, normalize it into a 1m distance vector
goal = Vector.normalize(goal)
# Finally, multiply the 1m vector by 10, to get a 10m long vector.
goal = Vector.multiply(goal, 10)
# To avoid the yaks, if you get within 10 meters of a yak, you should vector away from it.
yak = hero.findNearest(hero.findEnemies())
distance = hero.distanceTo(yak)
if distance < 10:
# First, make a Vector from the yak to you
yakVector = Vector.subtract(hero.pos, yak.pos)
# Now use Vector.normalize and Vector.multiply to make it 10m long
yakVector = Vector.normalize(yakVector)
yakVector = Vector.multiply(yakVector, 10)
# Once you have the 10m vector away from the yak, use Vector.add to add it to your goal vector!
yakVector = Vector.add(yakVector, goal)
pass
# Finally, determine where to move by adding your goal vector to your current position.
moveToPos = Vector.add(hero.pos, goal)
hero.move(moveToPos)
The good thing is that I am moving but the bad thing is that I am moving into the yaks. What is wrong? I think there may be just some little fix.
Oof, I hope everyone’s alright.
About your code, it’s almost right, but there are two small problems. They’re both about the order in which you do stuff, which in Vectors is very important.
It’s here:
And here:
For the first one, just follow the order they say it in, yak to you. You’ve got hero.pos → yak.pos. Whenever you’re making a vector towards something, but starting at your location, you need to use their pos first in the subtraction, then yours.
For the second one, if you want to change a variable, you have to put it behind the = sign. At the moment you’re only changing yakVector. The goal vector which you should be trying to change, does not change by just being involved in the addition. The actual Vector() bit is right, but you just need to put goal in yakVectors place. Remember the goal variable is the one you use to add to your pos at the end.
Danny
Thanks @Deadpool198. I did that but I was still getting the same result: getting head butted all over place by some big angry yaks. I solved it by changing the yak variable yak = hero.findNearestEnemy(). That worked. I just need to do Brewball now.
Ok I have an issue with the fact that, although I think my code is correct, my hero doesn’t move, which obviously means it isn’t correct. Silly me. I have based it off of Skating Away and this topic, but I am not getting anywhere. Here is my code:
while True:
potion = hero.findFriendlyMissiles()[0]
firetraps = hero.findHazards()
# Remember that a Fire Trap will trigger if you move closer than 3 meters!
omarn = hero.findByType("potion-master")[0]
if potion:
dest = potion.targetPos
# Go get the potion.
potion = Vector.subtract(dest.pos, hero.pos)
potion = Vector.normalize(potion)
potion = Vector.multiply(potion, 3)
if hero.distanceTo(firetraps) < 3:
firetrap = Vector.subtract(firetrap.pos, hero.pos)
firetrap = Vector.normalize(firetrap)
firetrap = Vector.multiply(firetrap, 3)
potion = Vector.add(potion, firetrap)
moveToPos = Vector.add(hero.pos, potion)
hero.move(moveToPos)
pass
else:
if omarn and hero.distanceTo(omarn) > 10:
# Move back to Omarn.
home = omarn.targetPos
# Go get the potion.
omarn = Vector.subtract(home.pos, hero.pos)
omarn = Vector.normalize(omarn)
omarn = Vector.multiply(omarn, 3)
if hero.distanceTo(firetraps) < 3:
firetrap = Vector.subtract(hero.pos, firetrap.pos)
firetrap = Vector.normalize(firetrap)
firetrap = Vector.multiply(firetrap, 3)
omarn = Vector.add(omarn, firetrap)
moveToPos = Vector.add(hero.pos, omarn)
hero.move(moveToPos)
# Warning: isPathClear doesn't work with Hazards!
hero.say("Hup, hup!")
pass
I’ve found one issue which I want to tell you about first, before I look at the rest of your code. It happens in both the outwards, and return journeys.
dest is already a pos (like Vector(44, 35)), you don’t need to .pos it twice, dest can go straight into the vector function by itself.
Danny
Thanks but now my hero is moving again but she blows the mines up. Here is my code:
while True:
potion = hero.findFriendlyMissiles()[0]
firetraps = hero.findHazards()
# Remember that a Fire Trap will trigger if you move closer than 3 meters!
omarn = hero.findByType("potion-master")[0]
if potion:
dest = potion.targetPos
# Go get the potion.
potion = Vector.subtract(dest, hero.pos)
potion = Vector.normalize(potion)
potion = Vector.multiply(potion, 3)
firetrap = hero.findNearest(firetraps)
if hero.distanceTo(firetrap) < 3:
firetrap = Vector.subtract(firetrap.pos, hero.pos)
firetrap = Vector.normalize(firetrap)
firetrap = Vector.multiply(firetrap, 3)
potion = Vector.add(potion, firetrap)
moveToPos = Vector.add(hero.pos, potion)
hero.move(moveToPos)
pass
else:
omarn = hero.findByType("potion-master")[0]
if not potion:
# Move back to Omarn.
home = omarn.pos
# Go get the potion.
omarn = Vector.subtract(home, hero.pos)
omarn = Vector.normalize(omarn)
omarn = Vector.multiply(omarn, 3)
firetrap = hero.findNearest(firetraps)
if hero.distanceTo(firetrap) < 3:
firetrap = Vector.subtract(hero.pos, firetrap.pos)
firetrap = Vector.normalize(firetrap)
firetrap = Vector.multiply(firetrap, 3)
omarn = Vector.add(omarn, firetrap)
moveToPos = Vector.add(hero.pos, omarn)
hero.move(moveToPos)
# Warning: isPathClear doesn't work with Hazards!
hero.say("Hup, hup!")
pass
I have only got ten minutes and then I have to go for the day. I might come back after 18:30 though. As always, thanks in advance because the advance in life is what is important.
Elijah