If you try to use to escape from the ogers using a teleporter, as suggested in the description, you get bashed:
Stepping on a transporter puts your hero to the opposite transporter, but then the hero immediately starts to run back to the start transporter, passing the ogers and getting damage.
This is probably because the transporters do not handle the moveXY correctly: just putting the hero to another location does not finish the method.
To demonstrate I used the following example code:
waitPositions = [[12, 45], [65, 24]]
porters = [[5, 49], [76, 19]]
counter = 0
while True:
item = hero.findNearestItem()
enemy = hero.findNearestEnemy()
hero.say(āwaitingā) #move to the wait position
pos = waitPositions[counter]
hero.moveXY(pos[0], pos[1])
porter = porters[counter] #wait for enemy getting near
while enemy and hero.distanceTo(enemy) > 20:
enemy = hero.findNearestEnemy()
if enemy:
# move to the porter
hero.say(hero.distanceTo(enemy))
counter = (counter + 1) % 2
hero.moveXY(porter[0], porter[1])
Eric_Tang: you are right: I just proceeded and learned that āmoveā behaves differently .
So, using āmoveā rather than āmoveXYā works perfectly:
So, I am very sorry to have reported this as a bug.
But I guess, players at the level this challenge is presented are not supposed to know about the different behaviour of āmoveā.
Maybe at least a hint in the description of the challenge could be an improvementā¦?
Teleporting works but I now get a serios problem with iterating lists.
The following code once passed and yielded me another level in āSarven Treasureā, but when running locally I mostly get āyour code is really slow or has and endless loopā.
And yes: it is incredibly slow! If more than 5 enemies are present an it iterates through hero.findEnemies() it halts noticeably, after some time ending with the message above.
I also had this issue when iterating through the coins list.
I am sure that there is no endless loop in my code as I use only āforā loops and the main loop should always perform a āmoveā.
Btw.: is there any possibility to debug my code? I found some hints to a ādebug()ā feature but that does not work for me.
File output using the common Python āopen()ā does not work either: this method seems not to be known or is suppressed at purpose.
Here my code, once having passed the āsentā run:
def DistBetweenObjects(obj1, obj2):
dx = obj2.pos.x - obj1.pos.x
dy = obj2.pos.y - obj1.pos.y
return Math.sqrt(dx**2 + dy**2)
def DistOfObjectToPos(obj, pos):
dx = obj.pos.x - pos.x
dy = obj.pos.y - pos.y
return Math.sqrt(dx**2 + dy**2)
def FindNearestPorter(target, porters):
minDist = 9999
nearestPorter = None
for porter in porters:
dist = DistOfObjectToPos(target, porter)
if dist < minDist:
minDist = dist
nearestPorter = porter
return nearestPorter
# check if distance of any enemy to coin is smaller than my distance to coin
def IsSafeDistToEnemies(coin, myDistToCoin):
enemies = hero.findEnemies()
if len(enemies) == 0:
return True
for enemy in enemies:
if DistBetweenObjects(coin, enemy) < myDistToCoin + 5:
return False
return True
# check if distance of coin to the porter next to it
# is bigger than any enemies distance to that porter
def IsSafeDistToPorter(coin, porters):
enemies = hero.findEnemies()
if len(enemies) == 0:
return True
#porter next to the coin:
nextPorter = FindNearestPorter(coin, porters)
coinDistToNextPorter = DistOfObjectToPos(coin, nextPorter)
for enemy in hero.findEnemies():
enemyDistToNextPorter = DistOfObjectToPos(enemy, nextPorter)
if enemyDistToNextPorter < coinDistToNextPorter + 5:
return False
return True
def FindBestCoin(porters):
maxWeight = 0
bestCoin = None
coins = hero.findByType("coin")
for coin in coins:
myDistToCoin = hero.distanceTo(coin)
weight = coin.value / myDistToCoin
if weight < maxWeight:
continue
# check if distance of any enemy to coin is smaller than my distance to coin
if not IsSafeDistToEnemies(coin, myDistToCoin):
continue
# check if distance of coin to the porter next to it
# is bigger than any enemies distance to that porter
if not IsSafeDistToPorter(coin, porters):
continue
bestCoin = coin
maxWeight = weight
return bestCoin
porters = [{'x': 5, 'y': 49}, {'x': 5, 'y': 19}, {'x': 76, 'y': 19}, {'x': 76, 'y': 51}]
counter = 0
jumpTime = -1
# measured around 7 to 7.3, not official
jumpCooldown = 8
while True:
enemy = hero.findNearestEnemy()
missiles = hero.findEnemyMissiles()
missile = hero.findNearest(missiles)
coin = None
if not enemy or hero.distanceTo(enemy.pos) > 10:
coin = FindBestCoin(porters)
if coin:
hero.move(coin.pos)
else:
nextPorterToMe = hero.findNearest(porters)
hero.move(nextPorterToMe)
hero.move(nextPorterToMe)
Oh, youāre here, nice
I am using all functions and I also tried to simplify as much as possible. However there are indeed some unused variables in the main loop relating to ājumpā and āmissilesā but that shouldnāt matter.
I really tried hard to get behind the problem and am at the end of my possibilities (note that Iām not a student but a professional software developer, mainly using C# and C up to now. But Iām new to Python and try this also for my son. Apart from the lagging problem here, āCodeCombatā is actually great fun and educationally well made!)
Moreover, I think the challenge here (and others on this level) require complex code
And it will get more complex if missiles get involved which is the case at the next level: but thatās the point where it comes to an end for me because iterating through the missiles list (and doing some calculations) seems to be finally too muchā¦
This can sometimes be caused by the sheer amount of enemy units in a level rather than just your code. What stage are you on? If youāre on a high one like 6, 7+ then the problem could be that the game just canāt load all the troops.
I often find reloading the page works, even if there was an infinite loop with the code you ran during testing the level.
It could still be a problem with your code, so if that doesnāt work, Iāll copy it into my level and see if I can get it to workā¦
Danny
Yes, infinite loop is often because of many ogres. I have quite big scores everywhere and I canāt submit them (because of infinite loop), even I have success. Sometimes, it can be solved by this:
When you see the infinite loop error, and you are sure that you will have success after clicking āsubmitā, click submit.
Then, you leave the tab open, go to drink tea and eat cookies). And then, after a couple of hours, or hopefully earlier the level loads and you get your money and XP.
Thank you very much for your responses
It is reassuring that I am not the only one experiencing this.
On the other hand it should basically not be a big deal for a Python script to iterate through say 30 coins and 10 ogres (not really doing complicated calculations in there) once per letās say 100ms but of course I donāt know how the underlying machine translates itā¦
I will think about some workaround and let you know if I find any approach.
I think in this case there are some flaws in the logic of the program. It gives me also āyour code is really slow or has and endless loopā.
So I only simplified it:
Can you explain what exactly you want to achieve with FindBestCoin(porters) / porters is a global variable so no need to put it as function parameter. /
P.S. I have never been a software developer, only helping my son
@xython:
The task of the challenge is to collect a certain value of coins in a given time without dying.
When you step on a āporterā position, you will be immediately ported to the opposite āporterā and thatās the way to escape from the enemies, heading for you in a very densely packed group.
Attacking them will probably of no use, so I donāt even try and use a hero with low health but relativly high speed (āAmaraā).
It is essential that collecting coins is done in a very safe way, i.e.
do not try for a coin if any(!) enemy is closer to it than you are
stay closer to the porter next to you as any(!) enemy - this has to be applied also for the coin youāre heading for
Thatās the purpose of āIsSafeDistToEnemies()ā and āIsSafeDistToPorter()ā which are both are essential components of FindBestCoin().
And they both iterate through a list of all enemies. Maybe there are ways to optimize this by doing a pre-selection of the enemies, but this will make the code really complicated and, above all, I would expect that those iterations should be possible in an adequate time (of course supposed there are no real bugs in the code).
I started with a simple code, similar to the one you proposed, but you wonāt reach the goal that way, at least for higher levels.
Btw. you are right about āportersā being global - this has just not been in my focusā¦
Update:
itās running now, more or less fluently (with Chrome more, with Firefox less, but ā¦ yeah).
I did not change anything to the code posted here apart from minor renaming, which should really not matter.
So - itās magicā¦
Currently I have another annoying problem with the other āSarvanā challenge because, at a certain point, it apparently comes to a race condition where the hero shoots an enemy that is shot by a turret in parallelā¦
But maybe itās only a broken server? Or even a new security feature??
(one of the debug messages is
DevTools failed to load SourceMap: Could not parse content for https://codecombat.com/play/level/treema.css.map: Unexpected token < in JSON at position 0
followed by
Access to fetch at 'https://codecombat.dexecure.net/play/level/treema.css.map' from origin 'https://codecombat.com' has been blocked by CORS policy: Request header field accept is not allowed by Access-Control-Allow-Headers in preflight response.
So yeah, itās magicā¦ but anyway:
If you really want to help your son, tell him to always save a text copy of his code before running it, at least if it is a more sophisticated challenge.
Because, what is REALLY ANNOYING is, that, if you transmit your code and it comes to a hang-up, your code may be completely deleted if you finally manage to rerun the challenge by either clicking āreset levelā or ācomment out my codeā even if you try hours later - it seems to make no difference.
And thereās no way to restore it then.
That is really, really bad - tears and smashed keyboards guaranteed.
Ok, I have to stop, thatās enough for nowā¦
Anyway, apart from all these technical issues, I still like CodeCombat - it has the perfect mix of simple introductions to a topic and, in the right measure, increased and ever more advanced challenges. <3
Ehm, āreset levelā really delets the whole code, but ācommenting outā only puts a couple of lines at the top of the code editor, and you can save it.
But yes, copying backup of the code somewhere else is a good practice and I do it from time to time.
is there any possibility to debug my code? do it in the browser Web Console
for quick debug message use
pet.debug("debug message")
never hero.say(ātalking takes timeā)
The distance between two objects can be found using Vector library:
enemyDistToNextPorter < coinDistToNextPorter # same as
enemy.pos.distance(porter.pos) < coin.pos.distance(porter.pos)
Officially you need Programmatication V but you donāt really have to own any book to use the language possibilities. How to see what is available?
Go to some Leader Board and find a person with that book:
Thank you very very much - this is extremely helpful
Have a happy new year !
For interested future readers who made it up to here: logging via Web Console works perfectly for me using Chrome: albeit I cannot use ādebug()ā, āconsole.log()ā works.
It did not work for me using Firefox but I tried this only once. I did not try other browsers.
Thank you for your reply.
Maybe I did something wrong and clicked āresetā rather than ācomment outā, however I am pretty sure I didnāt - I will try again next time (hopefully not so soon)
And sorry for my irritated last post - it was just one of those hours where nothing workedā¦