Play Home Contribute Github Join Us! Discourse Staff Members Github Contribution Guides Team

Hoarding Gold (No coin.value code?)


#21

I’m having trouble getting my avatar to actually go around and collect the gold. She just runs straight to Naria and says “Done collecting gold!”. Any suggestions on why the program is ignoring my code?

var totalGold = 0;
loop {
    var coin = this.findNearestItem();
    // Pick up the coin and add its value to the total.
    // Get its value with:  coin.value
    if (coin) {
        var pos = coin.pos;
        var x = this.pos.x;
        var y = this.pos.y;
        this.moveXY(x,y);
        totalGold = totalGold += coin.value;
    }
    
    if (totalGold >= 25) {
        // >= means totalGold is greater than or equal to 25.
        // This breaks out of the loop to run code at the bottom.
        break;
        
    }
}

this.moveXY(64, 34);
this.say("Done collecting gold!");

#22

I don’t know if this is the problem, but it is wrong…

it should either be “totalGold += coin.value;” or “totalGold = TotalGold + coin.value” not both.


#25

Strangely enough we where trying “totalGold + coin.value = totalGold” and it wouldn’t work. It has to be " totalGold = totalGold + coin.value "


#26

An assignment always looks the same:

variable = computation

It is not forbidden to use variable in computation if it has already been defined. In fact, the following expression is absolutely valid:

variable = variable

Look at the following example:

totalGold = totalGold + coin.value

What actually happens is this:

  1. Get the value stored in totalGold
  2. Get the value stored in coin.value
  3. Add the two stored values together
  4. Store the result in a variable named totalGold, unimportant whether it existed before or not.

#27

[…deleted poorly formatted, unhelpful code…]


#29

Draz, please do not give out correct code, as it gives any reader the answer without providing the learning. Besides, the person you are replying to gave the issue over five months ago. And you still haven’t learned to post your code correctly.


#30

Could you help me, I need to figure out how to pick up the “coin” I do not remember how to pick up items in this game without it being an “item”


#31

You pick the coin by moving to its position.

self.move(coin.pos)
#or
self.moveXY(coin.pos.x,coin.pos,y)

#32

the reason is that you have to say totalGold in the message


#33

Please do not revive dead threads! This problem has already been solved and left alone for one year.


#34

just trying to be helpful


#35

Thanks for trying to help the CodeCombat community, but this problem is already solved.