

'loop' breaks multiplayer code

Hi everyone,
I’ve recently found that a lot of my Multiplayer Treasure Grove matches end in ties. At first I thought it was my code, but closer inspection revealed that in every case it was just their hero with an error, and my hero not doing anything:

Another strange thing is that I only get the ties when fighting against red players, and the enemy hero always seems to be Tharin or Anya.

Here is my code in case there is a bug in it, in which case apologies for the disturbance:

def findBestItem(items):
    bestItem = None
    bestValue = 0
    itemsIndex = 0
    while itemsIndex < len(items):
        item = items[itemsIndex]
        if item.value / hero.distanceTo(item) > bestValue:
            bestItem = item
            bestValue = item.value / hero.distanceTo(item)
        itemsIndex += 1
    return bestItem
def summonArmy(target):
    if hero.canCast("summon-burl", target):
    if hero.canCast("summon-undead", target):
def findBestCorpse(corpses):
    bestCorpse = None
    bestValue = 0
    corpsesIndex = 0
    while corpsesIndex < len(corpses):
        corpse = corpses[corpsesIndex]
        corpseValue = corpse.maxHealth / hero.distanceTo(corpse)
        if corpseValue > bestValue:
            bestCorpse = corpse
            bestValue = corpseValue
        corpsesIndex += 1
    return bestCorpse
def findLunch():
    enemies = hero.findEnemies()
    enemyIndex = 0
    lunch = None
    closestLunch = 9999
    while enemyIndex < len(enemies):
        potentialLunch = enemies[enemyIndex]
        if potentialLunch:
            distance = hero.distanceTo(potentialLunch)
        if <= 200 and distance < closestLunch:
            lunch = potentialLunch
            closestLunch = distance
        enemyIndex += 1
    return lunch

def findEnemyHero():
    enemies = hero.findEnemies()
    enemyIndex = 0
    enemyHero = None 
    maxHealth = 0
    while enemyIndex < len(enemies):
        enemy = enemies[enemyIndex]
        if enemy.type == "Sorceror":
            enemyHero = enemy
        elif enemy.maxHealth > maxHealth:
            enemyHero = enemy
            maxHealth = enemy.maxHealth
        enemyIndex += 1
    return enemyHero

def spellBook(spell, target, distance):
    if target:
        e_distance = hero.distanceTo(target)
        if e_distance <= distance:
            if hero.canCast(spell, target):
                hero.cast(spell, target)
def castSpells(target):
    spellBook("fear", target, 25)
    spellBook("poison-cloud", target, 30)
def raiseDead(c):
    if c and hero.canCast("raise-dead"):
        hero.moveXY(c.pos.x, c.pos.y)
        if c:
def eatLunch(lunch):
    if lunch and hero.isReady("devour"):
    elif lunch:
        hero.cast("drain-life", lunch)

#hero.moveXY(hero.pos.x + 5, hero.pos.y)
while True:
    lunch = findLunch()
    enemies = hero.findEnemies()
    friends = hero.findFriends()
    enemyHero = findEnemyHero()
    enemy = hero.findNearestEnemy()
    items = hero.findItems()
    item = findBestItem(items)
    corpses = hero.findCorpses()
    c = findBestCorpse(corpses)
    if item:
        itemB = None
        items = hero.findItems()
        itemB = findBestItem(items)
        if itemB:
    enemy = hero.findNearestEnemy()

Note: I had to remove the Blue fox change shape function, as that word was not allowed by the codecombat word filter thing.
Thanks in advance,
Brenin Llwyd.

# you have
def summonArmy(target):
    if hero.canCast("summon-burl", target):
    if hero.canCast("summon-undead", target):
# and you are calling the function with

But what happens if the enemy Hero is invisible or their hero is not “operational”?
==> if hero.canCast(“summon-burl”, target) will fail I think
so a simple

if target:
    # your code here
# or
     if hero.canCast("summon-burl"):
     # or if hero.isReady("summon-burl"): will give same result as above 

side note: I gave up fighting in Multiplayer levels, because I’m using only the free heroes despite being a subscriber. It seems to me that the advantage given to the richer users as payed heroes is too big and this is depressing for children of poorer parents.

I used my non sub account (ArcaneBlade) and am number 9 in cavern survival, so even if you dont have payed heroes you still can win

Yes, I know it’s possible to be between 10th and 20th place in some multiplayer levels and I have been there for a while. But it’s true that ordinary heroes have no power even with good code to give much resistance to wizards.
About the BreninLlwyd’s problem: I didn’t test my supposition…

Hi xython,
thanks for the reply. I tried your suggestion, but unfortunately it had no effect. I’m fairly sure that my code doesn’t have any mechanical errors in it, and this seems to be backed up by the fact that the codecombat error checker doesn’t flag any issues with it either.
I think that the problem lies within the multiplayer system itself, as my brother has exactly the same problem.
On a different note, I do agree with you that the payed heroes, especially Ritic, have a big advantage in their basic stats and abilities over the free heroes. However, these barriers can be overcome with imaginative tactics and clever code, It just depends on your mindset. And finally, Codecombat would not be able to provide this incredible, free, jumping off point into the world of computer programming without subscribers like yourself, so while subscribers may seem to have an unfair advantage, they are also providing non-subscribers with a great, free resource.

Playing against Ania, Tharin, Ida, Alejandro with someone from the same ‘free’ pool results in a mutual error, and the heroes are not doing anything.
So it’s a real BUG!

Someone should post this on CoCo github.

Hello. Just to say I’ve run into this since recently restarting with CodeCombat. I see exactly the same symptoms (in Cavern Survival in case it matters), meaning my hero just stops doing anything after the first move if the enemy hero crashes. Extremely irritating! (This with a program that worked fine submitted 2 years ago and not modified etc.)

I’ve noticed the following error in the browser dev console (might be unrelated):

(My hero is in JS so I assume this is indicative of the error in the enemy hero.)

(Edit: Actually that may well be unrelated, I’ve now had the same behaviour against a JS opponent. The console in this case was clean but the problem still present.)

(Edit: The issue is on github I see:


This problem still exists. I thought it’d be somewhat enlightening to show exactly what I’m getting (and how frustrating it is.) Here’s 2 different battles, one is lost because the hero does nothing, the other is won despite the enemy hero being more powerful. (Both is fought in case this is not obvious, by the same program.)

Working as it should:

Broken example:

Should everyone who has an old code(loop ;) be not simulated? I posted duplicate codes on red and blue and am #6 on red and #1722 on blue :thinking::thinking:.

I think a script replacing loop with while (true) or while True: must be run. I’m sure there were better players in the past deserving their top places.
Edit: Every script must be tested scrupulously on a sample data before use. I don’t know why but the images in all topics before May 2015 are lost.


Might take a while to do, but it’ll help solve it.

@Chaboi_3000 Who can change the Simple, Easy, Hard code selections when starting out in the Multiplayer?

When playing Blue in Harrowland, the Simple CPU code locks your code up and you can’t complete the level. Even when playing red, the enemy code doesn’t work, but you don’t get locked up. Below is a post with a video showing the code being locked up when playing as Blue. I switched the opponent to the Easy enemy and it worked.