Vital powers help

it seems im needing alot of these
my hero never attacks any headhunter or brawler or ogre (im going to call these the big 3) even though it is before my soldiers are commanded to attack alsi ive known how to use def(): for a long time so why does the level say “this level helps you learn how to define you own functions”

def pickUpNearestCoin():
    items = hero.findItems()
    nearestCoin = hero.findNearest(items)
    if nearestCoin:
        hero.move(nearestCoin.pos)

def summonSoldier():
    if hero.gold >= hero.costOf("soldier"):
        hero.summon("soldier")
    pass

def ifBrawler():
    bad = hero.findNearest(hero.findByType("Brawler" or "Headhunter" or "Ogre"))
    if bad and hero.distanceTo(bad) < 8:
        hero.consecrate()
        hero.attack(bad)
    elif bad:
        hero.attack(bad)
def commandSoldiersAndMe():
    notOk = hero.findByType("brawler")
    alsoBad = hero.findByType("headhunter")
    notGood = hero.findByType("ogre")
    if notOk or notGood or alsoBad:
        if notOk:
            attack1 = hero.findNearest(notOk)
            if attack1 and hero.distanceTo(attack1) < 5:
                hero.consecrate()
                hero.attack(attack1)
            elif attack1:
                hero.attack(attack1)
        elif alsoBad:
            attack3 = hero.findNearest(alsoBad)
            if attack3 and hero.distanceTo(attack3) < 5:
                hero.consecrate()
                hero.attack(attack3)
            elif attack3:
                hero.attack(attack3)
        elif notGood:
            attack2 = hero.findNearest(notGood)
            if attack2 and hero.distanceTo(attack2) < 5:
                hero.consecrate()
                hero.attack(attack2)
            elif attack2:
                hero.attack(attack2)
    for soldier in hero.findFriends():
        enemy = soldier.findNearestEnemy()
        if enemy:
            hero.command(soldier, "attack", enemy)

while True:
    pickUpNearestCoin()
    summonSoldier()
    commandSoldiersAndMe()

oh wait someone esle taught me how to use def early lol

Congratulations, you’ve run into one of the old bugs of CodeCombat! @Chaboi_3000, this should be fixed.

In any proper Python interpreter, the truthiness of an empty list is false. The code
if []: print("err")
will print nothing.

CodeCombat evaluates an empty list to true and incorrectly executes the content of the if block.

This applies to if notOk: and similar statements, and thus your hero will attack only brawlers. A simple workaround is checking the truthiness of the length of the list, which CodeCombat evaluates correctly if len(notOk):


Suggestions:

ifBrawler bug

Responding to your other thread

you can put multiple things in findbytype right

False. From python’s help:

The expression “x or y” first evaluates x; if x is true, its value
is returned; otherwise, y is evaluated and the resulting value is
returned.

The expression "Brawler" or "Headhunter" evaluates to "Brawler" as a non-empty string is considered truthy. As there are no enemies with the type "Brawler" (it should be lowercase), no enemies are found.

(You never call this function, but it will be buggy if you decide to. I explain finding multiple types of enemies below)

Attacking multiple types

commandSoldiersAndMe is quite repetitive. You’re using the same logic regardless of which of the “big three” the enemy is, so might as well combine them.

# If you want to attack the nearest of ANY of the "big three"
# I use `+` to concatenate two arrays
# [1, 2] + [3, 4] == [1, 2, 3, 4]
def commandSoldiersAndMe():
    enemy = hero.findNearest(hero.findByType("brawler") + hero.findByType("headhunter") + hero.findByType("ogre"))
    if enemy: ...

# If you want to attack the nearest brawler, and only then the nearest headhunter, and only then the nearest ogre (which is equivalent to your current code)
# This SHOULD be the same as above but with `or` operators instead of concatenation, but for the same empty-list-truthiness bug, CodeCombat misevaluates that
# As such, this code is functional but not ideal
def commandSoldiersAndMe():
    enemy = hero.findNearest(hero.findByType("brawler")) or hero.findNearest(hero.findByType("headhunter")) or hero.findNearest(hero.findByType("ogre"))
    if enemy: ...

Yeah it’s been here for a while. Our wonderful @xython made a quiz specifically for this bug. It’s also still using Python 2.7. Matias mentioned before in the public slack channel that no updates are being made to the parser anymore.

I am getting so close to the full 60s now (ty @Hydrobolic) but how does consecrate() work, does it drain all the enemies around you or just your targeted enemy? and what is the limit of how much it can drain?

Matias mentioned before in the public slack channel that no updates are being made to the parser anymore.

Why? There are a number of such bugs.

how does consecrate() work

I won’t be of any help. I only briefly used the ring and found it was less powerful than other options.

what options i must know

also this is my def commandSoldiersAndMe():

def commandSoldiersAndMe():
    notOk = hero.findByType("brawler")
    alsoBad = hero.findByType("headhunter")
    notGood = hero.findByType("ogre")
    if len(notOk) > 0 or len(notGood) > 0 or len(alsoBad) > 0:
        if len(notOk) > 0:
            attack1 = hero.findNearest(notOk)
            if attack1 and hero.distanceTo(attack1) < 5:
                hero.consecrate()
                yep = hero.isReady("time-travel")
                if yep and attack1:
                    hero.cast("time-reavel", hero)
                hero.attack(attack1)
            elif attack1:
                yep = hero.isReady("time-travel")
                if yep and attack1 and hero.distanceTo(attack1) < 15:
                    hero.cast("time-reavel", hero)
                hero.attack(attack1)
        elif len(alsoBad) > 0:
            attack3 = hero.findNearest(alsoBad)
            if attack3 and hero.distanceTo(attack3) < 5:
                hero.consecrate()
                yep = hero.isReady("time-travel")
                if yep and attack3:
                    hero.cast("time-reavel", hero)
                hero.attack(attack3)
            elif attack3:
                yep = hero.isReady("time-travel")
                if yep and attack3 and hero.distanceTo(attack3) < 15:
                    hero.cast("time-reavel", hero)
                hero.attack(attack3)
        elif len(notGood) > 0:
            attack2 = hero.findNearest(notGood)
            if attack2 and hero.distanceTo(attack2) < 5:
                hero.consecrate()
                yep = hero.isReady("time-travel")
                if yep and attack2:
                    hero.cast("time-reavel", hero)
                hero.attack(attack2)
            elif attack2:
                yep = hero.isReady("time-travel")
                if yep and attack2 and hero.distanceTo(attack2) < 15:
                    hero.cast("time-reavel", hero)
                hero.attack(attack2)

CodeCombat uses Filbert for Python parser, which was made by Matt. I’m not too sure what caused its discontinuation, but it hasn’t been touched since 2019.

By “other options” I was referring to other rings.

The easiest way to succeed in the level would be to come back later with better gear. If you want to improve your strategy without doing so, perhaps try different unit combinations and commanding them more effectively.

Did you read the suggestion drop-downs above? If you understand the concepts they can help simplify commandSoldiersAndMe.

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