Please help IDK how to make the soldiers work and fight stuff I can’t believe other players have gotten to level 10 and stuff so can someone help me make the soldiers actually fight here is my code:
# Stay alive for one minute.
# If you win, the next time you play will be more difficult and more rewarding!
# If you lose, you must wait a day before submitting again.
soldierindex = 0 # Move `soldierindex` definition outside
soldierhealthindex = 0
while True:
item = hero.findNearestItem()
if item:
hero.moveXY(item.pos.x, item.pos.y)
if hero.gold > hero.costOf("soldier"):
hero.summon("soldier")
enemy = hero.findNearestEnemy()
if enemy:
soldiers = hero.findFriends()
if soldiers and soldierindex < len(soldiers):
soldier = soldiers[soldierindex]
hero.command(soldier, "attack", enemy)
soldierindex += 1 # Increment index here
if hero.health < hero.maxhealth / 2:
while True:
hero.attack(enemy)
if enemy:
if hero.isReady("throw") and hero.distanceTo(enemy) <= hero.throwRange:
hero.throw(enemy)
if hero.isReady("cleave"):
hero.cleave(enemy)
i also have all the heros except Nalfar, Illia and Usara
If you go to the level Mountain Mercinaries, it teaches you how to command your troops. Here is the link to it.
1 Like
although it didnt work i still die
Your code is inefficient and messy. Your problem is on the soldierIndex
which is outside your main loop. This will cause the index not to reset, and therefore only commanding the soldier once (maybe for like half a second) and then stop.
The while loop with the index increment can be completely replaced by an enhanced for loop, as they behave completely the same. In your code, I did not find the need of another index other than addressing the element of the list.
I recommend you to write a method for commanding your soldiers with a enhanced for loop instead.
def command():
for s in hero.findFriends():
enemy = s.findNearestEnemy()
hero.command(s, "attack", enemy) # You checked for an enemy already
# outside of the loop
# rest of your code logic
I also noticed another possible issue.
if hero.health < hero.maxhealth / 2:
while True:
hero.attack(enemy)
Once you get below half health, you will just start an infinite loop that you will never get out of.
Instead, I recommend placing the health check at the very top and to add a continue statement, which do the same thing by skipping all other code and let you constantly attack enemies.
Demo Code (adjust based on your gameplay)
while True:
if hero.health < hero.maxhealth / 2:
hero.attack(enemy)
continue
item = hero.findNearestItem()
if item:
hero.moveXY(item.pos.x, item.pos.y)
if hero.gold > hero.costOf("soldier"):
hero.summon("soldier")
enemy = hero.findNearestEnemy()
if enemy:
command()
if hero.isReady("throw") and hero.distanceTo(enemy) <= hero.throwRange:
hero.throw(enemy)
if hero.isReady("cleave"):
hero.cleave(enemy)
Oh yeah btw just add an if statement checking for the enemy is the best in every case
It worked after removing the health thing and making everything in a single else: thingy(did i mention im trash at coding?) For those people who hate putting together the pieces of the code here is the code(tho i need codecombat to add better weapons and armor to the archers) I used ritic the cold with the rivited dragonscale set also thornprick and ring of speed(tho i might change to wooden strand) this is da code:
while True:
enemy = hero.findNearestEnemy()
if enemy:
hero.attack(enemy)
else:
item = hero.findNearestItem()
if item:
hero.moveXY(item.pos.x, item.pos.y)
if hero.gold > hero.costOf("soldier"):
hero.summon("soldier")
if enemy:
friend = hero.findNearest(hero.findFriends()) # Find a nearest friend command.
if friend:
hero.command(friend, "attack", enemy) # Command the nearest friend to attack.
if enemy and hero.isReady("throw") and hero.distanceTo(enemy) <= hero.throwRange:
hero.throw(enemy)
if hero.isReady("cleave"):
hero.cleave(enemy)
now i need help with cloudrip treasure also how do i change the chat name to cloudrip brawl and treasure pls help? here is the cloudrip treasurecode
# Your goal is to collect coins / gems.
# This level is repeatable. If you win, the difficulty and rewards will increase.
# If you fail, you have to wait a day to resubmit.
# This level is an optional challenge level. You don't need to beat it to continue the campaign!
while True:
gem = pet.findNearestByType("gem")
if gem:
hero.moveXY(gem.pos.x, gem.pos.y)
else:
gold = pet.findNearestByType("gold-coin")
if gold:
hero.moveXY(gold.pos.x, gold.pos.y)
friends = hero.findFriends()
coin = hero.findNearest(hero.findItems())
if coin: # Ensure coin exists before using it
for friend in friends:
hero.command(friend, "move", coin.pos)
do you still need help on this or have you found out the solution
i found solution for cloudrip brawl but i need help for cloudrip treasure