I’m in this level, CodeCombat - Coding games to learn Python and JavaScript
And my code:
summonOrder = ["trapper","paladin","paladin","paladin","paladin","necromancer","paladin","paladin","paladin","librarian","paladin","paladin","paladin","knight"]
summonIndex = 0
enemyBoss = hero.findByType("wizard-ninja", hero.findEnemies())[0]
if hero.team == "humans":
targetPos = {"x": 72, "y": 32}
safePos = {"x": 30, "y": 13}
knightPos= {"x": 22,"y": 42}
else:
targetPos = {"x": 10, "y": 32}
safePos = {"x": 68, "y": 10}
knightPos= {"x": 58,"y": 13}
def goliathLogic(goliath):
enemy = goliath.findNearestEnemy()
if enemy:
if goliath.isReady("hurl"):
hero.command(goliath, "hurl", enemy)
# The hero has access to "stomp" and "warcry" as well:
if goliath.isReady("stomp"):
hero.command(goliath, "stomp", enemy)
if goliath.isReady("warcry"):
hero.command(goliath, "warcry", self)
else:
hero.command(goliath, "attack", enemy)
def trapperLogic(trapper):
targets = hero.findByType("archer")
nearest = hero.findNearest(targets, hero.findEnemies())
assassin = hero.findNearest(hero.findByType("assassin"), hero.findEnemies())
griffin = hero.findNearest(hero.findByType("griffin"), hero.findEnemies())
trapper = hero.findNearest(hero.findByType("trapper"), hero.findEnemies())
enemy = trapper.findNearestEnemy()
if enemy:
if trapper.distanceTo(enemy)<35:
if trapper.isReady("scattershot"):
hero.command(trapper, "scattershot", enemy)
elif trapper.isReady("throw"):
if nearest:
hero.command(trapper, "throw", nearest)
elif griffin:
hero.command(trapper, "throw", griffin)
elif assassin:
hero.command(trapper, "throw", assassin)
else:
hero.command(trapper, "throw", enemy)
else:
hero.command(trapper, "attack", enemy)
else:
hero.command(trapper, "defend", safePos)
def knightLogic(knight):
enemy = knight.findNearestEnemy()
ready = knight.isReady("cleave")
ready1 = knight.isReady("bash")
if enemy and knight.distanceTo(enemy)<20:
if ready and knight.distanceTo(enemy)<10:
hero.command(knight, "cleave")
elif ready1 and knight.distanceTo(enemy)<10:
hero.command(knight, "bash", enemy)
else:
hero.command(knight, "shield")
else:
hero.command(knight, "move", knightPos)
def assassinLogic(assassin):
enemy = assassin.findNearestEnemy()
if enemy:
if assassin.isReady("phase-shift"):
hero.command(assassin, "phaseShift", enemyBoss)
# The hero has access to "stomp" and "warcry" as well:
if assassin.isReady("wall-of-darkness"):
hero.command(assassin, "wallOfDarkness", [assassin.pos, enemyBoss.pos])
if assassin.isReady("shadow-vortex"):
targets = hero.findByType("knight")
nearest = hero.findNearest(targets, assassin.findFriends)
enemy = hero.findNearestEnemy()
hero.command(assassin, "shadowVortex", enemy.pos, enemyBoss.pos)
else:
hero.command(assassin, "defend", safePos)
def necromancerLogic(necromancer):
enemy = necromancer.findNearestEnemy()
if necromancer.canCast("summon-undead"):
hero.command(necromancer, "cast", "summon-undead")
if enemy:
# The hero has access to "stomp" and "warcry" as well:
if necromancer.canCast("sacrifice"):
hero.command(necromancer, "cast", "sacrifice", enemy)
if necromancer.isReady("soul-link"):
hero.command(necromancer, "cast", "soul-link", enemy)
if necromancer.distanceTo(enemy)<20:
hero.command(necromancer, "attack", enemy)
else:
hero.command(necromancer, "defend", safePos)
def paladinLogic(paladin):
enemy = paladin.findNearestEnemy()
leastHealth = 9999
friendIndex = 0
friends = hero.findFriends()
if paladin.canCast("heal"):
if hero.health<1734:
hero.command(paladin, "cast", "heal", hero)
else:
while (friendIndex < friends.length):
friend = friends[friendIndex]
if (friend.health < leastHealth and friend.health != 30):
leastHealth = friend.health
weakestFriend = friend
friendIndex += 1
hero.command(paladin, "cast", "heal", weakestFriend)
if enemy:
hero.command(paladin, "attack", enemy)
def librarianLogic(librarian):
hero.command(librarian, "move", safePos)
enemy = librarian.findNearestEnemy()
targets = hero.findByType("archer",hero.findFriends())
nearest = hero.findNearest(targets)
targets1 = hero.findByType("goliath",hero.findFriends())
nearest1 = hero.findNearest(targets1)
highestHealth = 1
friendIndex = 0
friends = hero.findFriends()
while (friendIndex < friends.length):
friend = friends[friendIndex]
if (friend.health > highestHealth and friend.health != 30):
highestHealth = friend.health
strongestFriend = friend
friendIndex += 1
if enemy:
if librarian.canCast("haste") and nearest:
hero.command(librarian, "cast", "haste", nearest)
# The hero has access to "stomp" and "warcry" as well:
if librarian.canCast("grow") and strongestFriend:
hero.command(librarian, "cast", "grow", strongestFriend)
if librarian.isReady("force-bolt") and enemy and librarian.distanceTo(enemy)<10:
hero.command(librarian, "cast", "force-bolt", enemy)
else:
hero.command(librarian, "defend", safePos)
hero.summon("assassin")
hero.summon("knight")
targets = hero.findByType("knight")
knight = hero.findNearest(targets, hero.findFriends())
hero.command(knight, "move", knightPos)
while True:
nextUnit = summonOrder[summonIndex % summonOrder.length]
if hero.costOf(nextUnit) <= hero.gold:
hero.summon(nextUnit)
summonIndex += 1
friends = hero.findFriends()
for friend in friends:
if friend.type == "goliath":
goliathLogic(friend)
elif friend.type == "assassin":
assassinLogic(friend)
elif friend.type=='necromancer':
necromancerLogic(friend)
elif friend.type=='knight':
knightLogic(friend)
elif friend.type=='paladin':
paladinLogic(friend)
elif friend.type=='librarian':
librarianLogic(friend)
elif friend.type=='trapper':
trapperLogic(friend)
else:
if friend.findNearestEnemy() and friend.type!='skeleton':
hero.command(friend, "attack", friend.findNearestEnemy())