Maybe try tweaking the health ifs. the numbers as in for example:
if soldier.health > 40:
part, maybe change 40 to i dunno, 50?
and I think the archer target part should stay where it is
Maybe try tweaking the health ifs. the numbers as in for example:
if soldier.health > 40:
part, maybe change 40 to i dunno, 50?
and I think the archer target part should stay where it is
@EnderMarkoth I’ve fixed your problem
Only 2 fixes
at the part where you have archers = hero.findByType(“archers”), remove the ‘s’ in archers; There is no such type as “archers”
and the if hero.gold> hero.costof soldier, change if to while and the > to >=
The right code for findStrongestTarget() exist in this same topic several times. Nesting while and for loops leads to confusion and it’s at least no needed here.
almost the finished functions with while and for tested with dummy data
@xython
Can I have the site link for the test dummy thing you used? That could come in handy for me a lot!
And I’ve rewritten my code a bit.
Can you help me look over it for any issues? My eyes are tired.
Wait. My hero can build a fence. Could I build a fence around the soldiers and humans and be able to pass that off? Should I try that?
No. YOu can’t move off that cliff. I’ve tried. and Look at my post 23 hrs ago. I’ve solved your problem
I did those simple changes and the soldiers still kept dying off. Im really mad with this level and I can’t move on till I beat it.
Was those 2 changes from the original code you posted?
Yes. Here’s my code right now.
def commandSoldier(soldier, soldierIndex, numSoldiers):
angle = Math.PI * 2 * soldierIndex/numSoldiers
defendPos = {"x": 41, "y": 40}
DefendPos = {"x": 42, "y": 40}
defendPos.x += 10 * Math.cos(angle)
defendPos.y += 10 * Math.sin(angle)
if soldier.health > 40:
hero.command(soldier, "defend", defendPos);
else:
hero.command(soldier, "defend", DefendPos)
def findStrongestTarget():
mostHealth = 0
bestTarget = None
enemies = hero.findEnemies()
enemyIndex = 0
while len(enemies) > enemyIndex:
enemy = enemies[enemyIndex]
if enemy:
enemyIndex += 1
for enemy in enemies:
if enemy.health > mostHealth:
mostHealth = enemy.health
bestTarget = enemy
if bestTarget and bestTarget.health > 15:
return bestTarget
else:
return None
archerTarget = None
def commandArcher(archer):
nearest = archer.findNearestEnemy()
if archerTarget:
hero.command(archer, "attack", archerTarget)
elif nearest:
hero.command(archer, "attack", nearest)
while True:
if hero.gold > hero.costOf("soldier"):
hero.summon("soldier")
if not archerTarget or archerTarget.health <= 0:
archerTarget = findStrongestTarget()
friends = hero.findFriends()
soldiers = hero.findByType("soldier")
archer = hero.findByType("archers")
for a in range(len(archers)):
archer = archers[a]
commandArcher(archer)
for a, soldier in enumerate(soldiers):
commandSoldier(soldier, a, len(soldiers));
commandArcher()
You still haven’t fixed htis yet. archer, not archers
should be:
archers = hero.findByType("archer")
You still haven’t done this yet either. if should be while
Its says, " if you want to call hero.gold > hero.costOf(“soldier”) as a function, you need ()"
wait. nevermind. fixed it.
It keeps saying my code is either really slow or an infinite loop.
Try running it again. It took me 2 runs to success it. Use the code you posted earliest with the 2 changes I made. THat will work
fingers crossed. lets see if it does it for me this time.
Aaaand it said either a loop or too slow of code.
Run it again.
Solution removed.
Also, use the infinity range glasses
I am using those glasses. and I cant see the post before your last one very well.
wait. should I remove that first While one that’s engulfing the entire rest of the code? would that help?
for me, it’s fine with the first while loop