# Grim Determination Stuck

#1

Hi, guys!

I have troubles with Grim Determination level.

What are the main ideas for building right strategy?

My current code
``````
# Your goal is to protect Reynaldo

# Find the paladin with the lowest health.
lowestHealth = 99999
lowestFriend = None
friends = self.findFriends()
for friend in friends:
continue
if friend.health < lowestHealth and friend.health < friend.maxHealth:
lowestHealth = friend.health
lowestFriend = friend

return lowestFriend

paladinPoint = {'x': 88, 'y': 43}

warlocks = self.findByType('warlock')
#    if nearestWarlock:
#        dx = nearestWarlock.pos.x - paladin.pos.x
#        dy = nearestWarlock.pos.y - paladin.pos.y
#        dist = dx**2 + dy**2

if lowestFriend and paladin.canCast('heal') and lowestFriend.health < healingThreshold:

elif nearestWarlock:

elif nearestEnemy:

else:

def commandGriffin(griffin):
warlocks = self.findByType('warlock')
nearestWarlock = griffin.findNearest(warlocks)
nearestEnemy = griffin.findNearest(self.findEnemies())

if nearestWarlock:
self.command(griffin, 'attack', nearestWarlock)

elif nearestEnemy:
self.command(griffin, 'attack', nearestEnemy)

else:

def commandPeasant(peasant):
peasants = self.findByType('peasant')

for i, pes in enumerate(peasants):
if pes is peasant:
break

coins = self.findItems()
nearestCoin = peasant.findNearest(coins)

if i == 0 and nearestCoin.pos.y <= 43:
self.command(peasant, 'move', nearestCoin.pos)
elif i == 1 and nearestCoin.pos.y > 43:
self.command(peasant, 'move', nearestCoin.pos)

def commandFriends():
friends = self.findFriends()
for friend in friends:
if friend.type == "peasant":
commandPeasant(friend)

elif friend.type == "griffin-rider":
commandGriffin(friend)

def summonGriffin():
if self.gold >= self.costOf('griffin-rider'):
self.summon('griffin-rider')

loop:
commandFriends()
# Summon griffin riders!
summonGriffin()

``````
#2

You are chasing the warlocks too hard, even if they are miles away or blocked by hordes of enemies.

I passed with your code by deleting the lines 39 and 40:
`[...]elif nearestWarlock:[...]`

Even with bonus, cause itâs good code besides that.

3 Likes
#3

It seems that once I kill a warlock another one or two spawn in a couple of seconds.
So, without prioritizing them, I summon only 2 instead of 15 during the combat.

Thatâs amazing! Thank you.

1 Like
#4

# Find the paladin with the lowest health.

lowestHealth = 99999
lowestFriend = None
friends = self.findFriends()
for friend in friends:
continue
if friend.health < lowestHealth and friend.health < friend.maxHealth:
lowestHealth = friend.health
lowestFriend = friend
return lowestFriend

paladinPoint = {âxâ: 88, âyâ: 43}

``````if lowestFriend and paladin.canCast("heal") and lowestFriend.health < healingThreshold:
elif nearestEnemy:
else:
# And don't forget, they can attack, too!
``````

def commandGriffin(griffin):
warlocks = self.findByType(âwarlockâ)
nearestWarlock = griffin.findNearest(warlocks)
nearestEnemy = griffin.findNearest(self.findEnemies())

``````elif nearestEnemy:
self.command(griffin, "attack", nearestEnemy)
``````

def commandPeasant(peasant):
peasants = self.findByType(âpeasantâ)

``````for i, pes in enumerate(peasants):
if pes is peasant:
break

coins = self.findItems()
nearestCoin = peasant.findNearest(coins)

if i == 0 and nearestCoin.pos.y <= 43:
self.command(peasant, "move", nearestCoin.pos)
elif i == 1 and nearestCoin.pos.y > 43:
self.command(peasant, "move", nearestCoin.pos)
``````

def commandFriends():
friends = self.findFriends()
for friend in friends:
if friend.type == âpeasantâ:
commandPeasant(friend)
if friend.type == âgriffin-riderâ:
commandGriffin(friend)

def summonGriffin():
if self.gold >= self.costOf(âgriffin-riderâ):
self.summon(âgriffin-riderâ)

loop:
commandFriends()
summonGriffin()

con some one help me
sorry about the FAQ it wouldnât work

#5

To format your code correctly, please highlight it and click the </> button. This lets others check the indentation and makes the code much easier to read.

#6

simple problem but huge.my griffins attack and the peasants collect coins but the griffins donât block them enough. my paladins die and break through is there something wrong or what

#7

the enemy break through easily

#8

Are your paladins healing? To call a function you must add the argument list ( ) after its name:

``````lowestFriend = lowestHealthPaladin()
``````
#9

what do mean and where do i add it. i tried myself with the info. i kow and it said âmaximum stack exceededâ

#10

my paladins either heal or attack, but if they heal they attack for a few minutes then stop attacking and just heal. Same if I choose to shield.

``````def commandPaladin(paladin):
friends = self.findFriends()
for friend in friends:
enemy = friend.findNearest(friend.findEnemies())
if target:
if friend.canCast("heal", target):
self.command(friend, "cast","heal", target)
elif enemy:
self.command(friend, "attack", enemy)
#else:
#self.command(friend, "shield")

``````
#11

What happens if thereâs a `target` but the paladin canât cast heal again yet?

#12

If you use combined if structures you must count all the possible paths your code can take. Each if instruction adds 1 extra branch on top of the one branch a normal code has. So 4 ifâs will generate 5 branches (elif counts as an if also)

``````if friend.type == "paladin":
if target:
if friend.canCast("heal", target):
else:
else:
if enemy:
# 3rd branch: paladin, none to help, enemy found
else:
# 4th branch: paladin, none to help, no enemy
else:
``````

Ugggh! So many branches! And in the #2 and #3 branch you may have to right similar code. No one wants to write code twice. The solution is to add an extra variable which catches all the untreated branches:

``````if friend.type == "paladin":
busy = False
#busy must be set to False every time before the if structure starts
if target:
if friend.canCast("heal", target):
#We do something here
busy = True
#else:
# we do not write this branch at all, it will be caught
# No we start a second if structure instead of putting ifs inside each other
if not busy:
if enemy:
# 2rd branch: paladin, not busy = has not cast heal this turn, enemy found
#we do something  here
busy = True
#else:
# 3rd branch: paladin, not busy, not enemy
# maybe move or shield
busy = True
#else
# well, he is already busy doing something, leave him alone, no need to write
else: