Desert Combat - What am I missing?

When I have the code like this:

# while-loops repeat until the condition is false.
# Always take an action inside a while loop, or it'll go infinite!
ordersGiven = 0
while ordersGiven < 5:
    # Move and order each of your allies into battle.
    x = self.pos.x
    y = self.pos.y - 10
    self.moveXY(x, y)
    self.say("Attack!")
    ordersGiven = ordersGiven + 1
    
# When you're done giving orders, join the attack.
    enemy = self.findNearestEnemy()
    loop:
        if enemy:
            if self.isReady("cleave"):
                self.cleave(enemy)
            else:
                self.attack(enemy)
        else:
            pass

my hero decides to command my soldier and then go attack the ogre. When I move the enemy = self.findNearestEnemy() to before the while loop (from in the middle) my hero commands the one soldier and stays where he is (the following code).

# Always take an action inside a while loop, or it'll go infinite!
ordersGiven = 0
enemy = self.findNearestEnemy()
while ordersGiven < 5:
    # Move and order each of your allies into battle.
    x = self.pos.x
    y = self.pos.y - 10
    self.moveXY(x, y)
    self.say("Attack!")
    ordersGiven = ordersGiven + 1
    
# When you're done giving orders, join the attack.
    
    loop:
        if enemy:
            if self.isReady("cleave"):
                self.cleave(enemy)
            else:
                self.attack(enemy)
        else:
            pass

Aha… when you are finished with the while loop, move your indenting back to the same level as the while loop: example:

ordersGiven = 0
while ordersGiven < 5:
    # Move and order each of your allies into battle.
    x = self.pos.x
    y = self.pos.y - 10
    self.moveXY(x, y)
    self.say("Attack!")
    ordersGiven = ordersGiven + 1
self.say("I am ready to run more code!")

The code on the same indent level will run once the while loop is complete.

Well, that fixed one problem! Now my hero commands the soldiers, and then decides to stand there and die. How powerful must your hero be to finish the level?

He shouldn’t be just sitting there… repost new code for me?

It was because of the enemy = self.findNearestEnemy() was only after if enemy, so my hero would never attack.I still die because my hero isn’t strong enough, though.

Well… better equipment of course, or maybe you could add flags and retreat a bit so your allies do more of the fighting. Perhaps you could try using a new tactic (shield perhaps).

I ended up just letting my troops do most of the fighting when my health got too low … worked ok after getting better armor

1 Like

I have no idea what to do why my hero doesn’t attack.
Can someone please point out what’s wrong with my code?

loop:
    if ordersGiven >= 5:
        self.moveXY(52, 29)
    if enemy:
        self.attack(enemy)
    else:
        self.isReady("cleave")
        self.cleave(enemy)

Look at the previous post in this thread

note : you have used if instead if while , you haven’t told your friends to attack

1 Like

What you should do is put findNearestEnemy() at the start of your loop (but not in it!)
That way you can find the enemy.

Well, I decided to find another solution to the problem.

Create fire-traps.

1 Like

Hi Reznik,
there are a few points in your code which might not behave as you want.

First, you never actually give any orders. As a worthy commander, stand in front of each Soldier (that is on the cross) and self.say("Attack").
When you gave each soldier it’s order, self.findNearestEnemy() and self.attack(enemy) until all are dead.

The easiest way to solve this is to use while for the command-part and loop for the attack-part.


That might work as well, but was not the intention of the level.

1 Like

A general orders attacks from in front of his or her soldiers :wink:

General Dwight D. Eisenhower has been quoted as using a piece of string as a metaphor for leadership. He stated, "Leadership is like a piece of string. You will not succeed by pushing it, but only by pulling it along. … leading from in front :wink:

I have same problem.
To move on X I use Flags and then say to make soldiers to attack.
But If I am not quick enough to put next flag, my hero goes to attack like if while ordersGiven < 5 is true.

Here is the code.
Where I am wrong?
Thanks for help in advance!


ordersGiven = 0
while ordersGiven < 5:
    # Move and order each of your allies into battle.
    flag = self.findFlag("green")
    if flag:
        self.pickUpFlag(flag)
    self.say("Attack!")
    ordersGiven = ordersGiven + 1
# When you're done giving orders, join the attack.
loop:
    enemy = self.findNearestEnemy()
    self.attack(enemy)

Rather than using flags to move, tell him to move as part of your loop. You can simply move 10 units down each time through the loop.

this is my code:

# We are field testing a new battle unit: the decoy.
# Build 4 decoys, then report the total to Naria.
# Each decoy costs 25 gold. Use the Quartz Sense Stone
# to know when you have more than 25 gold with self.gold.
# Keep a count of decoys you built as you go along.
# Break out of the loop when you have built 4.
decoysBuilt = 0
loop:
    item = self.findNearestItem()
    self.moveXY(item.pos.x, item.pos.y)
    if self.gold >= 25:
        self.buildXY("decoy", 47, 43)
        decoysBuilt = decoysBuilt +1
    if decoysBuilt == 4:
        break  #中断语句
      
       
self.say("Done building decoys!")
# Go to Naria and say how many decoys you built.
self.moveXY(15, 36)
self.say(decoysBuilt)

as long you are set up the enemy = self.findNerestEnemy() is inside the loop not outside the loop it will not find the enemy or wont attack at all.

for example look good

loop {
enemy = self.findNerestEnemy()
// the code here
}

other example wont attack

enemy = self.findNerestIEnemy()
loop {
// the code here
}

use this

ordersGiven = 0

while ordersGiven < 5:
    # 移動與命令同伴加入戰鬥 (你必須要站在士兵面前,他們才會聽到你的鼓舞)
    self.moveXY(self.pos.x, self.pos.y-9)
    self.say("Attack!")
    ordersGiven = ordersGiven+1
self.moveXY(47, 32)
loop:
    enemy = self.findNearestEnemy()
    if enemy:
        self.attack(enemy)

this doesnt work (if it did it doesnt now

erm… you can check my solved topic and make a new code
link : Desert combat help : how to increment ordersGiven?
I hope you guys can get it!