Decoy Drill: help

You must increment the index decoysBuilt++;

I need a help you. I don’t know how I solve this code. I tried to search other example though. Even if I reached 25gold, I keep getting golds untill timeover.

loop {
    var coin = this.findNearest(this.findItems());
    var decoysBuilt = 0;
    var gold = 0;
     
    if(gold < 25){
     
     this.moveXY(coin.pos.x, coin.pos.y);      
    }
        
    if(gold == 25){
        var gx = pos.x;
        var gy = pos.y;
        this.buildXY("decoy", gx, gy);
        decoysBuilt = decoysBuilt + 1;
        
        
    }if(decoysBuilt >= 4){
        
        break;
    }
}
        this.moveXY(14, 36);
        this.say("Done building decoys!");
        this.say("decoys 4");

I appriciate it.

You never update gold anywhere. Your variable gold always stays 0, even though you have more gold.

You also set gold = 0 every time around the loop so even if you change gold, it would always be at or close to zero. And gold == 25, assumes all coins are worth one…

I don’t understand what i’m doing wrong… help!
decoysBuilt = 0
loop:
coin = self.findNearestItem()

if coin:
    # Loot the coin!
    pos = coin.pos 
    x = pos.x 
    y = pos.y 
    self.moveXY(x, y)
    pass
# Each decoy costs 25 gold.
# Know when you have more than 25 gold with self.gold
if self.gold >= 25:
    self.buildXY("decoy", x, y)
    # Keep a count of decoys you built as you go along.
    decoysBuilt + 1
if decoysBuilt >= 4:
    break
    pass
    self.say("Done building decoys!")
    self.moveXY(14, 36)
if coin:
    ...
    pass

will keep Tharin from building decoys as long as there are coins. Since coins keep spawning, it is very unlikely that this will happen.

if decoysBuilt >= 4:
    break

will keep the code after the break from running, thus preventing Tharin from telling Naria how many decoys he built.

Posting tip: To format all the code correctly, you’ll need to highlight it all before clicking the </> button.

Good luck!

I don’t get it… lol… but thanks anyways bud!

pass skips the rest of the loop. As long as if coin is true, the code will move Tharin to the coin, then start the loop over, not checking how much gold you have or how many decoys you have built.

Oh… Thanks for clarifying! I thought pass was a place holder!

help how do i get it to stop hitting the ground on the last decoy

decoysBuilt = 0
loop:
item = self.findNearestItem()
if item and self.gold <=25 and decoysBuilt < 4:
pos = item.pos
x = pos.x
y = pos.y
self.moveXY(x, y)

elif self.gold >= 25 and decoysBuilt < 4 :
    self.buildXY("decoy", self.pos.x-5, self.pos.y)
    decoysBuilt += decoysBuilt + 1
elif decoysBuilt == 4:
    break

self.moveXY(14, 36)
self.say(“Hey, i am done building “+decoysBuilt+” decoys.”)

You have to change what you’re saying when done and also, change your “elif” statements. Like so, It would’ve been easier to just say:
if self.gold >= 25:
pos = self.pos
x = self.pos.x - 5
y = self.pos.y
self.buildXY(“decoy”, x, y)
decoysBuilt += 1
if decoysBuilt == 4:
break

# Go to Naria and say how many decoys you built.
self.moveXY(14, 36)   
self.say("Done building decoys!")
self.say(decoysBuilt)

this is my code.

[redacted, please don’t post solutions]

For those who still stuck in this stage, read the instruction carefully, everything is well explained, also mind for the spacing it’s very important, thank you.

My code doesn’t work! My character keeps on building decoys, and won’t break out of the loop! Here’s my code.

We are field testing a new battle unit: the decoy.

Build 4 decoys, then report the total to Naria.

decoysBuilt == 0
while True:
coin = hero.findNearestItem()

if coin:
    # Collect the coin!
    hero.moveXY(coin.pos.x, coin.pos.y)
    pass
# Each decoy costs 25 gold.
# If hero.gold is greater than or equal to 25:
if hero.gold >= 25:
    # buildXY a "decoy"
    hero.buildXY("decoy", 41, 41)
    # Add 1 to the decoysBuilt count.
    decoysBuilt + 1
if decoysBuilt == 4:
    # Break out of the loop when you have built 4.
    break
    pass

hero.say(“Done building decoys!”)
hero.moveXY(14, 36)

Say how many decoys you built.

hero.say(decoysBuilt)

Instead of decoysBuilt + 1, make sure you write decoysBuilt += 1, so it adds one to “decoysBuilt”

1 Like

Mod edit: Please do not post for assistance in multiple locations for the same issue. You will not receive assistance faster with multiple posts.

why are you talking to a dead post :grimacing:

decoysBuilt = 0
while True:
coin = hero.findNearestItem()

if coin:
    # Collect the coin!
    hero.moveXY(coin.pos.x, coin.pos.y)
    pass
# Each decoy costs 25 gold.
# If hero.gold is greater than or equal to 25:
if hero.gold >= 25:
    # buildXY a "decoy"
    hero.buildXY("decoy", 36, 30)
    # Add 1 to the decoysBuilt count.
    decoysBuilt += 1 
if decoysBuilt == 4:
    # Break out of the loop when you have built 4.
    break 
    pass

hero.say(“Done building decoys!”)
hero.moveXY(14, 36)

Say how many decoys you built.

hero.say(decoysBuilt)

here is the code

decoysBuilt = 0
while True:
coin = hero.findNearestItem()

if coin:
    # Collect the coin!
    hero.moveXY(coin.pos.x, coin.pos.y)
    pass
# Each decoy costs 25 gold.
# If hero.gold is greater than or equal to 25:
if hero.gold >= 25:
    # buildXY a "decoy"
    hero.buildXY("decoy", 36, 30)
    # Add 1 to the decoysBuilt count.
    decoysBuilt += 1 
if decoysBuilt == 4:
    # Break out of the loop when you have built 4.
    break 
    pass

hero.say(“Done building decoys!”)
hero.moveXY(14, 36)

Say how many decoys you built.

hero.say(decoysBuilt)

@Asher_Tu can you format your code as it is described in the following topic?

Andrei

1 Like