The code runs okay, but the minefield is not surviving for 30 seconds… any help would be appreciated! As you can see, I’ve tried some things and then commented them out when I discovered they didn’t help.
# The goal is to survive for 30 seconds, and keep the mines intact for at least 30 seconds.
def chooseStrategy():
enemies = hero.findEnemies()
# If you can summon a griffin-rider, return "griffin-rider"
if hero.gold >= hero.costOf("griffin-rider"):
return "griffin-rider"
# If there is a fangrider on your side of the mines, return "fight-back"
fangrider = None
fangrider = hero.findNearest(hero.findByType("fangrider", hero.findEnemies()))
if fangrider and fangrider.pos.x <= 36:
return "fight-back"
# Otherwise, return "collect-coins"
else:
return "collect-coins"
def commandAttack():
# Command your griffin riders to attack ogres.
riders = None
riders = hero.findByType("griffin-rider", hero.findFriends())
enemies = hero.findEnemies()
if enemies:
dist = 9999
nearest = None
for enemy in enemies:
# Attack if it is not a fangrider
if (not (enemy.type == "fangrider")) and hero.distanceTo(enemy) < dist:
nearest = enemy
dist = hero.distanceTo(enemy)
if nearest and riders:
for rider in riders:
hero.command(rider, "attack", nearest)
def pickUpCoin():
# Collect coins
"""
# Find the "best" coin based on value and distance from hero.
value = 0
bestCoin = None
coins = hero.findItems()
if coins:
for coin in coins:
if coin.value / hero.distanceTo(coin) > value:
value = coin.value
bestCoin = coin
if bestCoin:
hero.move(bestCoin.pos)
"""
coin = None
coin = hero.findNearest(hero.findItems())
if coin:
hero.move(coin.pos)
def heroAttack():
# Your hero should attack fang riders that cross the minefield.
nearest = hero.findNearest(hero.findByType("fangrider", hero.findEnemies()))
while nearest.health > 0:
if hero.isReady("chain-lightning"):
hero.cast("chain-lightning", nearest)
else:
hero.attack(nearest)
"""
def commandPeasants():
peasants = None
peasants = hero.findByType("peasant", hero.findFriends())
if not peasants:
if hero.gold >= hero.costOf("peasant"):
hero.summon("peasant")
if peasants:
for peasant in peasants:
pc = None
pc = peasantCoin(peasant.pos.x, peasant.pos.y)
if pc:
hero.command(peasant, "move", pc.pos)
def peasantCoin(x1, y1):
coins = hero.findItems()
if coins:
dist = 9999
bestCoin = None
for coin in coins:
between = distanceBetween(coin.pos.x, coin.pos.y, x1, y1)
if between < dist:
dist = between
bestCoin = coin
if bestCoin:
return bestCoin
else:
return False
def distanceBetween(x1, y1, x2, y2):
deltaX = abs(x1-x2)
deltaY = abs(y1-y2)
return((deltaX**2)+(deltaY**2)**0.5)
"""
while True:
commandAttack()
# commandPeasants()
strategy = chooseStrategy()
# Call a function, depending on what the current strategy is.
if strategy == "fight-back":
heroAttack()
elif strategy == "griffin-rider":
hero.summon("griffin-rider")
elif strategy == "collect-coins":
pickUpCoin()
Oh! Sure! Looking at it play out, it looks like the enemy fangriders are attacking my griffin riders on the way in and totally decimating them. Maybe I need to make them dodge? Did anyone else have to do that?
You could also let your riders fight the fangriders to give them a fighting chance. Instead of your code below, I’d look to see if the enemy.pos.x > 55 to append the targets. This allows your riders to fight the fangriders, but only on the right side of the minefield.
for enemy in enemies:
if not enemy.type == "fangrider":
targets.append(enemy)
The biggest problem with this code is while being faulty it works!
riders = hero.findByType("griffin-rider", hero.findFriends())
# riders = hero.findByType("griffin-rider") # all griffins are friends
# not an error but needless computations
enemies = hero.findEnemies()
if enemies: # will be always true even when there are no enemies
# code
if nearest and riders: # if nearest exists check will be always true
# if riders true even when there are no riders
# code
coins = hero.findItems()
if coins: # always true even when there are no coins
# code
peasants = hero.findByType("peasant", hero.findFriends())
# not an error: simply hero.findByType("peasant")
if peasants: # always true even when there are no peasants
# code
return((deltaX**2)+(deltaY**2)**0.5)
# last round bracket is misplaced
Sorry, it just seems that this level is rather inconsistent with what it accepts as a solution. I think the issue I found with my code was that I ended up setting the distance to attack the fang riders a little too high. Also, what is necroposting?