Play Home Contribute Github Join Us! Discourse Staff Members Github Contribution Guides Team

Summit's Gate python, please help!

def commandPaladins():
    for paladin in hero.findByType("paladin", hero.findFriends()):
        if paladin.canCast("heal", hero):
            self.command(paladin, "cast", "heal", self)
        else:
            weakestFriend2 = weakestFriend()
            if weakestFriend2:
                hero.command(paladin, "cast", "heal", weakestFriend2)
            else:
                hero.command(paladin, "move", {'x': hero.pos.x - 4, 'y': hero.pos.y})
# Fight your way into the Inner Sanctum of the ogre chieftain, and defeat her.
def commandArchers():
    for archer in hero.findByType("archer", hero.findFriends()):
        enemy = archer.findNearestEnemy()
        if enemy:
            hero.command(archer, "attack", enemy)

def commandSoldiers():
    for soldier in hero.findByType("soldier", hero.findFriends()):
        enemy = soldier.findNearestEnemy()
        if enemy:
            hero.command(soldier, "attack", enemy)         

def commandGriffins():
    for griffin in hero.findByType("griffin-rider", hero.findFriends()):
        enemy = griffin.findNearestEnemy()
        if enemy:
            hero.command(griffin, "attack", enemy)
        else:
            hero.command(griffin, "defend", hero)

def commandPaladins():
    for paladin in hero.findByType("paladin", hero.findFriends()):
        if paladin.canCast("heal", hero):
            self.command(paladin, "cast", "heal", self)

def summonGriffin():
    if hero.gold >= hero.costOf("griffin-rider"):
        hero.summon("griffin-rider")

def moveForward():
    targetPos = {'x': 290, 'y': 34}
    hero.move(targetPos)

def pickTarget():
    enemies = hero.findEnemies()
    target = None
    minDistance = 300
    for enemy in enemies:
        if enemy.type != "door":
            if enemy.type is "catapult":
                target = enemy
                break
            elif enemy.type is "warlock":
                target = enemy
                break
            elif enemy.type is "witch":
                target = enemy
                break
            elif enemy.type is "chieftain":
                target = enemy
                break
            else:
                if hero.distanceTo(enemy) < minDistance:
                    minDistance = hero.distanceTo(enemy)
                    target = enemy
    if target:
        return target

def commandHero():
    target = pickTarget()
    friend = hero.findFriends()
    if target:
        while target.health > 0:
            if hero.isReady("bash") and target.health > 116:
                hero.bash(target)
            else:
                hero.attack(target)
                moveForward()
    else:
        moveForward()

while True:
    summonGriffin()
    commandArchers()
    commandSoldiers()
    commandGriffins()
    commandPaladins()
    commandHero()
    pass

Yes, I did get this from someone else, but I’ve altered it and tried out different things during the last month. However, nothing worked and I have no idea what I’m doing wrong. If anyone knows why this code isn’t working, please let me know.

Is your hero dies or not?

I’ve tried your code. I won it. But I think you don’t have enough health to beat it. You need to buy better armor to win it.

My hero dies, but I don’t have enough gems to buy any armor. Other times, my hero gets stuck on a wall and it just stays there trying to walk somewhere but going nowhere, then the time just runs out.

Have you tried writing your own code from scratch? I’m not massively against using someone else’s code, if you alter it; but writing code specifically for your hero might be a good idea if this code isn’t working.
Danny

I’ve tried, but I just don’t know where to start. The one’s that I’ve tried don’t work at all.

Try writing a few basic functions, then elaborating on them to succeed on the level:

# summonFriends
# commandFriends - commanding all your different types of troops
# commandHero - moving, maybe with flags

I also recommend maybe using a stage based system. It can be very useful for more advanced levels:

if hero.time >= 0 and hero.time < 20:
    stage = 0
if hero.time >= 20 and hero.time < 40:
    stage = 1
# etc.. etc..

Then you can do different things in each stage, and check for stages inside your commandFriends function.
Danny

1 Like

Can you elaborate more on the stage based system? I don’t really understand what “hero.time” is. Also, when you command friends, how do you command the different friends?

Hero.time is the time that you are in the level. e.g. when 10 seconds have passed from starting the level, hero.time == 10. Stages can be used to do different parts of a big level. Like fighting the troops at the start, then moving inside the building, then the bit with the paladins, then the end. You can find out when it would happen and make a stage using hero.time. Then you command your friends to do a specific thing at that time.
You just need to define the stages inside your while true loop and you can use them in all your functions.
For the friends:

for friend in friends:
    if friend.type == "archer":
        #command
2 Likes

In the “for friend in friends”, for the “friends”, do you have to also write: “friends = hero.findFriends” or “hero.findNearestFriend”? I don’t have codecombat open right now, so I don’t know exactly what the code looks like. I have to go now, so I’ll try making my own code tomorrow, thank you so much for your help!

2 Likes
def summonFriends():
    if hero.gold > hero.costOf("griffin-rider"):
        hero.summon("griffin-rider")
    elif hero.gold > hero.costOf("archer"):
        hero.summon("archer")
    elif hero.gold > hero.costOf("soldier"):
        hero.summon("soldier")

def commandPaladin():
    for paladin in hero.findByType("paladin", hero.findFriends()):
        if (paladin.canCast ("heal", hero)) and hero.health < hero.maxHealth:
            hero.command(paladin, "cast", "heal", hero)
        elif (paladin.health < 500) and paladin.canCast("heal"):
            hero.command(paladin, "cast", "heal", paladin)
        else:
            hero.command(paladin, "move", {hero.pos.x - 4, hero.pos.y})

def commandFriends():
    friends = hero.findFriends()
    enemy = hero.findNearestEnemy()
    if enemy:
        hero.command(friends, "attack", enemy)
    if hero.health < 300:
        hero.command(friends, "defend", hero)
    else:
        hero.command(friends, "move", {hero.pos.x-10, hero.pos.y})

def commandHero():
    enemy = hero.findNearestEnemy()
    if enemy:
        if hero.isReady("bash") and enemy.health>=116:
            hero.bash(enemy)
            hero.moveXY(293, 34)
        else:
            hero.attack(enemy)
            hero.moveXY(293, 34)
    else:
        hero.moveXY(293, 34)

while True:
    summonFriends()
    commandPaladin()
    commandFriends()
    commandHero()
pass

Here’s my new code, I know it’s really simple, but I tried. :grimacing: It says “Don’t know how to transform: Set.” I don’t know what that means.

Try to look again at this section. You are commanding an array, not an unit. You should command each friend that is in friends in order for them to execute the commands.
And on what line do you get the error?

1 Like

It doesn’t give me a specific line.

Ok. Do you get the error after you fix what I told you to fix?

I don’t have code combat up yet. It’s 11 pm where I live, so I have to go to sleep. I’ll try fixing it tomorrow and then let you know.

1 Like

Ok then. But when you will be back, @Deadpool198 or @dedreous or myself will try to help you with this level.