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.

Hereā€™s my code:
while True:
paladins =
enemies = hero.findEnemies()
if hero.canCast(ā€œinvisibilityā€, hero):
hero.cast(ā€œinvisibilityā€, hero)
friends = hero.findFriends()
for friend in friends:
if friend.type == ā€œpaladinā€:
paladins.append(paladin)
for enemy in enemies:
if enemy.type == ā€œcatapultā€:
while enemy.health > 0:
hero.attack(enemy)
friends = hero.findFriends()
for friend in friends:
enemy = hero.findNearestEnemy()
if enemy:
hero.command(friend, ā€œattackā€, enemy)
fire = hero.findNearestEnemy()
if fire:
while fire.health > 0:
hero.attack(enemy)
if fire.type == ā€˜towerā€™:
pass
if hero.pos.x > 160:
hero.moveXY(175, 34)
hero.moveXY(175, 8)
hero.moveXY(245, 14)
hero.moveXY(245, 34)
while hero.health < hero.maxhealth:
hero.moveXY(245, 34)
if hero.gold >= 20:
hero.summon(ā€œsoldierā€)
paladins = hero.findByType(ā€œpaladinā€)
for paladin in paladins:
if paladin:
if paladin.canCast(ā€œhealā€, paladin):
hero.command(paladin, ā€œcastā€, ā€œhealā€, hero)
if hero.maxHealth <= hero.health:
if fire:
hero.moveXY(277, 34)
hero.moveXY(277, 5)
DJ = hero.findNearestEnemy()
if DJ:
while DJ.health > 0:
hero.attack(DJ)
hero.moveXY(277, 48)
lie = hero.findNearestEnemy()
if lie:
while lie.health > 0:
hero.attack(lie)
while True:
paladins = hero.findByType(ā€œpaladinā€)
for paladin in paladins:
if paladin:
if paladin.canCast(ā€œhealā€, paladin):
hero.command(paladin, ā€œcastā€, ā€œhealā€, hero)
fire = hero.findNearestEnemy()
if fire:
hero.attack(fire)
Please help!

Would you please format your code using this button:

2 Likes
while True:
    paladins = []
    enemies = hero.findEnemies()
    if hero.canCast("invisibility", hero):
        hero.cast("invisibility", hero)
    friends = hero.findFriends()
    for friend in friends:
        if friend.type == "paladin":
            paladins.append(paladin)
    for enemy in enemies:
        if enemy.type == "catapult":
            while enemy.health > 0:
                hero.attack(enemy)
        friends = hero.findFriends()
        for friend in friends:
            enemy = hero.findNearestEnemy()
            if enemy:
                hero.command(friend, "attack", enemy)
    fire = hero.findNearestEnemy()
    if fire:
        while fire.health > 0:
            hero.attack(enemy)
        if fire.type == 'tower':
            pass
        if hero.pos.x > 160:
            hero.moveXY(175, 34)
            hero.moveXY(175, 8)
            hero.moveXY(245, 14)
            hero.moveXY(245, 34)
            while hero.health < hero.maxhealth:
                hero.moveXY(245, 34)
                if hero.gold >= 20:
                    hero.summon("soldier")
                paladins = hero.findByType("paladin")
                for paladin in paladins:
                    if paladin:
                        if paladin.canCast("heal", paladin):
                            hero.command(paladin, "cast", "heal", hero)
            if hero.maxHealth <= hero.health:
                if fire:
                    hero.moveXY(277, 34)
                    hero.moveXY(277, 5)
                    DJ = hero.findNearestEnemy()
                    if DJ:
                        while DJ.health > 0:
                            hero.attack(DJ)
                    hero.moveXY(277, 48)
                    lie = hero.findNearestEnemy()
                    if lie:
                        while lie.health > 0:
                            hero.attack(lie)
                    while True:
                        paladins = hero.findByType("paladin")
                        for paladin in paladins:
                            if paladin:
                                if paladin.canCast("heal", paladin):
                                    hero.command(paladin, "cast", "heal", hero)
                        fire = hero.findNearestEnemy()
                        if fire:
                            hero.attack(fire)

1 Like

What errors are you getting?