Need help with "Sarven Siege"

How do I build a soldier with 20 gold?

The comments tell you; did you not read them?

I don’t remember comments popping up…

You need a Boss Star I or better as a neckless. The command is (in JavaScript) :

this.summon("soldier");

With 20 gold in your pocket, a soldier will pop next to you.

Edit : @ChronistGilver is right. I’m wrong. My solution is general where his answer is precisely the answer to your question.

Or, step on the red X in front of an Arrow Tower once you have twenty gold.

Yeah, that’s pretty much what I’m doing.

In that case, what is your problem?

Solved. My problem is solved.

Hi all I got confused here, and here attached my code:
coins=self.findItems()
coinIndex=0
loop:
while coinIndex<=20:
coin=coins[coinIndex]
self.moveXY(coin.pos.x, coin.pos.y)
coinIndex+=1
self.moveXY(84, 51)

but the system kept on telling me that the coin.pos.x is null.
and I really have no idea whats going on here and what should i do next.
please help for some hints

Hello, Kelvin. Please format your code correctly according to the FAQ.

You don’t check if there is a coin. Add an if coin: before the moveXY statement.

thanks for your help,chronistGilver.
This is my new code:
coins=self.findItems()
coinIndex=0

loop:
while coinIndex<=20:
        coin=coins[coinIndex]
        if coin:
            self.moveXY(coin.pos.x, coin.pos.y)
        coinIndex+=1
    self.moveXY(84, 51)

The codes above still got porblem with that my hero didnot collect coin at all instead of goint to the location 84,51 directly…

and one more curious, is that possiable I stick the flag and only control the hero by flag to pass…

sorry for my so many fresher questions to give troubles. thanks a lot for your help

Is the “coins=[…]” and the “coinIndex=0” part of your code? Because it is outside of the ```

In the code you posted the “while” is not indented. Hopefully, you copied and pasted your code and didn’t retype it (retyping introduces errors and sometimes corrects them.

If the while is not indented in your code then the loop is empty. And you should do everything once.
If it is indented, then I’d expect the code to work, and would guess that the problem is two things. One you set coins first thing and there may not be any coins on the map at that time, therefore you walk to 84,51 having looked for and collected all of the non-existent coins.

The “coins=…findItems()” and the “coinIndex=0” need to be inside the “loop:” if you want to collect coins more than once. (otherwise, coins will never get a new set of coins and coinIndex will never be reset to zero.)

You can control the hero only by flag but that is slow, tedious, and would stop you from learning how to control your hero via programming…

loop:  
    coins=self.findItems()
    coinIndex=0
    while coinIndex<=20:
        coin=coins[coinIndex]
        if coin:
            self.moveXY(coin.pos.x, coin.pos.y)
        coinIndex+=1
    self.moveXY(84, 51)

Hi Vlevo
you are right about my question. and my hero moved after the change.
but the new problem came out this time,my hero just kept on collection gold even the gold number already more than 20…I am sorry to so give you all so many trouble to help me…
and can you give me a hint how to ask my hero go to the nearest X location after the gold collected enough.

P.S thanks you guys gernerously answers which gives a lot help and confidence for the coding fresher.

In answer to your first question,

while coinIndex <= 20:

tells your hero to collect twenty coins, not twenty gold. Discard it

and use

while self.gold <= 20:

instead.

In answer to your second question, you can move to the X by saying something like

if self.gold >= 20:
    #Find the nearest Arrow Tower with findNearest() and findByType().
    #If there is a tower:
        #Move the the tower. Also, add about six the the X-coordinate, so you land on the X. 

if you have the findByType method.

loop:
    coins=self.findItems()
    coinIndex=0
    while coinIndex<len(coins):
        coin=coins[coinIndex]
        coinIndex+=1
        if self.gold<=20:
            self.moveXY(coin.pos.x, coin.pos.y)
        if self.gold>=20:
            arrowTower=self.findByType("ARROW-TOWER")
            nearestTower=self.findNearest(self.findByType("arrowTower"))
            if nearestTower:
                self.moveXY(nearestTower.pos.x, nearestTower.pos.y)

hi my friend

this is the new code I wrote for this level, but there is still some problem somewhere which I really could not find, my hero always stop moving after collect 26 coins

please help

You should be getting errors from your misspelling of “arrow-tower”…

I see multiple problems:

First, your while loop will pick the next coin in the list, not the nearest coin. That is because the self.findItems() method gives you the list of coins, but they are not sorted by distance (or any other ways). In fact, you don’t even need a while loop – you just need to find the nearest coin. To do that, use the self.findNearest() method:

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

…or if you don’t need the full list of coins, you can immediately pick the nearest coin by combining the two methods:

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

Second, your if statements are conflicting. Your code says:

  • if I have 20 or less gold, do this,
  • if I have 20 or more gold, do that.

So when you have 20 gold, it will do both. It’s better to use else in this case. Also, you can already buy a soldier for 20 gold, so you need to collect coins only as long as you have less than 20 gold:

  • if I have less than 20 gold, do this,
  • otherwise (when I have 20 or more gold), do that.
if self.gold < 20:
    # pick up coin
else:
    # go to the arrow tower

Third, you misspelled arrow-tower and mixed up a bit the code. You can find the nearest tower in a similar way as you find the nearest coin:

towers = self.findByType("arrow-tower")
nearestTower = self.findNearest(towers)

…or again, combine the two methods:

nearestTower = self.findNearest( self.findByType("arrow-tower") )

Last, you try to move to the middle of the arrow tower, but you will not be able to get there, because it’s too solid :wink: As somebody suggested earlier, try to add 6 to the x-position of the tower to get to the red X:

self.moveXY(nearestTower.pos.x + 6, nearestTower.pos.y)

Try fixing these and your code will be OK!

sample code - check only if you're *really* desperate!

Check this only if you’re really-really stuck!

(you need to click or highlight the text)

loop:
    coins = self.findItems()
    coin = self.findNearest(coins)
    if self.gold < 20:
        self.moveXY(coin.pos.x, coin.pos.y)
    else:
        towers = self.findByType("arrow-tower")
        nearestTower = self.findNearest(towers)
        if nearestTower:
            self.moveXY(nearestTower.pos.x + 6, nearestTower.pos.y)

ps: it looks like that the spoiler tag doesn’t work on preformatted code… is this normal?

2 Likes
loop:
    coin = self.findNearest(self.findItems())
    if self.gold < 20:
             self.moveXY(coin.pos.x, coin.pos.y)
         else:  
               nearestTower = self.findNearest(self.findByType("arrow-tower"))
          if nearestTower:
                self.moveXY(nearestTower.pos.x +4, nearestTower.pos.y)

hi to all! i cant win with this code, so i want to try to move to the very damaged tower (health < 200 for example) and build soldiers there. but i’m really cant figure out how to start this action. maybe because of my bad english understanding, i’m doing a half of my code in kind of random way, but anyway, can someone give me a tip to win this round.

Hello, Alex, and welcome.

Firstly, your indentation is a little messed up, Your if and else-blocks should line up with each other. I’m assuming your code looks like this:

loop:
    coin = self.findNearest(self.findItems())
    if self.gold < 20:
        self.moveXY(coin.pos.x, coin.pos.y)
    else:
        nearestTower = self.findNearest(self.findByType("arrow-tower"))
        if nearestTower:
            self.moveXY(nearestTower.pos.x + 4, nearestTower.pos.y)

You can use a while-loop to iterate over all towers, like this:

towerIndex = 0
weakTower = None
towers = self.findByType("arrow-tower")
while towerIndex < len(towers):
    tower = towers(towerIndex)
    if tower.health < 200:
        weakTower = tower
    towerIndex += 1

Alternatively, you can use a for-loop, but you learn those in Cloudrip Mountain, and I don’t think you’ve reached there yet.

Thank you for answer! But looks like i dont know and can’t understand where to put this piece of code to my main code. I have troubles with spaces and understanding of flow of the program, I’ve tryed to put this in different places in my code, but hero just circulating between one nearest tower and coins, and thats all.