Decoy Drill: help

I have been trying to beat decoy drill. Code so far:

// 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
// Keep a count of decoys you built as you go along.
// Break out of the loop when you have built 4.
var decoysBuilt = 0;
var gold = 0;
loop {
    var item = this.findNearestItem();
    this.moveXY(item.pos.x, item.pos.y);
    gold = gold + item.value;
    if (gold >= 25) {
        this.buildXY("decoy", this.pos.x, this.pos.y);
        decoysBuilt = decoysBuilt + 1;
    }
    else if (decoysBuilt >= 4) {
        break;
    }
    
    
}

this.say("Done building decoys!");
// Go to Naria and say how many decoys you built.
this.moveXY(14, 36);
this.say("4 decoys");

The problem is that once I get 25 gold, my hero builds a decoy, but then he stops doing anything. I have also tried using an if statement to collect coins, but the same thing happened with that. I assume this has something to do with my code, but I guess it could also be a bug. Please help.

Curious, does item.value still work on this level?

I used:

gold = this.gold;

Let me comment out my code and try what you have…

Your code works after switching to:

gold = this.gold;

Also you’ll want to move to the “X” before speaking that you are done building decoys. Remember that you will want to also let her know HOW MANY decoys you built.

Give that a shot! =D

EDIT: So this can be a bug as it accepted the item.value (I know this was used in a previous level…but it doesn’t seem to work here.)

If you track gold manually, you 1) aren’t decrementing your gold variable by 25 when you spend it and 2) can’t keep track of everything you pick up, since you only know what coins you are going for, not any other coins you pick up in the process. That’s why the quartz sense stone, with access to this.gold, comes in handy.

2 Likes

But how to get the decoy value Oo

Decoy value? ???

If you build a decoy you should increment a counter for decoys built by 1. You manually keep track of the decoys. A decoy is a decoy…it’s a 1:1 value.

@tjchan
Yeah i’ve already noticed that , btw ty

A solution I used:

As the level doesn’t specify you have to build 1 decoy at every 25 interval (collect all gold first)
Then build 4 decoys (probably should of looped this :stuck_out_tongue:)
Go to marker and say 4.

On an unrelated matter game doesn’t point out if your hammer cannot produce decoys.

var item = this.findNearestItem();
while (this.gold < 100)
{
    item = this.findNearestItem();
    this.moveXY(item.pos.x, item.pos.y);    
}
    
this.buildXY("decoy", 20, 40);
this.buildXY("decoy", 25, 40);
this.buildXY("decoy", 30, 40);
this.buildXY("decoy", 35, 40);


this.say("Done building decoys!");
// Go to Naria and say how many decoys you built.
this.moveXY(14, 36);
this.say("4");

what do you mean by a “1:1” value ?

We know that we could do this , but that would be a very noob code , as you can see your code has more than 15 lines and only works for 4 decoys , but our codes can be working for unlimited decoys building , you’ll have to change your way of thinking , the lvl wanted to teach you how to use “break” and how to add a value to another variables wich got another value
ex : decoy = 0
after adding 1 each time you build a decoy it would be 4
now this.say (decoy)
sorry if i’m being rude ,not trying to be looking like a pro or something just wanted you to c the difference between codes and coders
tip : use variables instead of numbers

If you build a decoy it counts as a single decoy unlike coins which depending on the type like Gold, Silver, and Copper, could be 3, 2, and 1 respectively (can’t remember true value of coins).

So collecting 3 gold and 2 copper would mean a total value of (3*3+2)=11

i can’t seem to get this level right, can someone help me? this is my cde…

decoysBuilt = 0
totalGold = 0
loop:
    coin = self.findNearestItem()
    if coin:
        position = coin.pos
        x = position.x
        y = position.y
        self.moveXY(x, y)
        totalGold = self.gold + coin.value            
    if totalGold = 25:
        self.buildXY("decoy", 36, 30)
        decoysBuilt + 1
    else:
        if decoysBuilt = 4:
            break
        
self.moveXY(14, 36)    
self.say("Done building decoys!")
self.say("4 decoys built!")

the part that I was stuck on may also be what’s hanging HAMINPANTS up - the check for number of decoys built requires a double equals sign (my non-noob big brother had to show me that - might be a useful thing to include somewhere in this lesson!)

so my code - which works - looks like:

#Break out of the loop when you have built 4.
decoysBuilt = 0
loop:
    item = self.findNearestItem()
    
    if self.gold < 25:
        self.moveXY(item.pos.x, item.pos.y)
    
    if self.gold >= 25:
        self.buildXY("decoy", self.pos.x+1, self.pos.y)
        decoysBuilt = decoysBuilt+1
        self.say("thats")
        self.say(decoysBuilt)
        
    if decoysBuilt == 4:
        break
    
self.say("Done building decoys!")

etc

Refinig,

I couldn’t agree more my code is noob :), However I was trying to illustrate that it separation of gold retrieval and creation of decoys. As I believe highlighting this to OP would give him a good opportunity to tackle the problem again.
I don’t claim to be a pro coder either, but find if I’m unable to get through a hurdle, I may be able to work around.
The level specifies the requirements quite clearly unlike most RL scenarios and whilst I would normally use a variable for decoys as we already know how many we need we can just say the number hehe. Looping through a variable would add be adding additional unneeded complexity. I’m probably taking the KISS principle too seriously. Once you have a working example you can always refactor. I have noticed later levels seem to be missing the “Maximum Number of Code Statements challenges”. Hope they add them back in.

I am completely new to this. But I can’t get it to count the coin value right. At least I’m pretty sure that is where it’s hanging up at. Mainly because it doesn’t reach the amount intended and just stops since it doesn’t have enough to build a decoy. This is what I have so far. Any advice would be appreciated. Especially on how to pull the value of the coin.

# Declaring variables.
decoysBuilt = 0

# Setting location for decoy x and y
dx = 23
dy = 23


loop:
    
    
    # Connecting variable to object.
    item = self.findNearestItem()
    
    
    # Collecting coins
    while self.gold < 25:
        itemPos = item.pos  # Dot seperator
        gx = itemPos.x      # Assign gx to x value of coin
        gy = itemPos.y      # Assign gy to y value of coin
        
        self.moveXY(gx, gy)
        break
            
    # Build a decoy
    self.buildXY("decoy", dx, dy)  # This is where it hangs up at!!!
    dx = dx + 5
    dy = dy + 5
    decoysBuilt = decoysBuilt + 1

    
    # Test if there are enough decoys
    if decoysBuilt == 4:
        self.say("Done building decoys!")
        break
            

# Go to Naria and say how many decoys you built.
self.moveXY(14, 37)
self.say("Naria! I have collected " + decoysBuilt)
self.say(" decoys.")

That looks really good, just one little problem tripping it all up. Get rid of the break inside your first while self.gold < 25: loop. That basically stops the loop after the first iteration, and then you still don’t have enough gold. That while loop will automatically end when self.gold hits 25 or more, so you don’t need to explicitly break out of it.

You could also make the outermost loop a while loop, too: while decoysBuilt < 4:.

Thanks for the quick response. I had realized that I needed to change the 25 to a 26 after I had posted it. I got it to work now. Turns out that it also wouldn’t acknowledge the variable “item” unless I included the line
"item = self.findNearestItem()" in the while loop. I wasn’t sure if I could use a while loop to build the decoys, because I thought I would have to change the 26 after the comparative operator from the first while loop to 104 in order for that to work. Great job on the work you guys have done. I’m a CS student who is using this to help me become a stronger programmer before I get to take some real classes. (About to just get started on my second semester). When I finish, maybe I can help somehow.

The reason that you need that in the loop is because you picked up the “item” you found outside the loop. So in order to continue, your hero had to find another item using that call.

All you need is this:

var decoysBuilt = 0;
loop {
    var item = this.findNearestItem();
    if(this.gold < 25 && item) {
        this.moveXY(item.pos.x,item.pos.y);
    }
    else if(this.gold() >= 15) {
        var tx = this.pos.x;
        var ty = this.pos.y;
        this.buildXY("fire-trap",tx,ty); decoysBuilt++;
    }
    if(decoysBuilt == 4) {
        break;
    }
}
this.say("Done building decoys!");
this.moveXY(14,36);
this.say("4");

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)