[HELP] Resource Valleys - Cloudrip


#1

Well… I was playing with Phyton but I can’t beat this level.
I don’t know what’s wrong, so I hope you can help me <3

Here’s my code:

def commandPeasant(peasant, coins):
# Command the peasant to the nearest of their array
distance = 0
for i in coins:
coin = coins[i]
if distance == 0:
distance = peasant.distanceTo(coin)
item = coin
elif distance < peasant.distanceTo(coin):
distance = peasant.distanceTo(coin)
item = coin

hero.command(peasant, "move", item.pos)

friends = hero.findFriends()
peasants = {
“Aurum”: friends[0],
“Argentum”: friends[1],
“Cuprum”: friends[2]
}

while True:
items = hero.findItems()
goldCoins = []
silverCoins = []
bronzeCoins = []
for item in items:
if item.value == 3:
goldCoins.append(item)
# Put bronze and silver coins in their approriate array:
if item.value == 2:
silverCoins.append(item)
if item.value == 1:
bronzeCoins.append(item)
commandPeasant(friends[0], goldCoins)
commandPeasant(friends[1], silverCoins)
commandPeasant(friends[2], bronzeCoins)


#2

I’m struggling with this level too, but can you tell me if what (if any) errors pop up.


#3

this was one year ago.

copy and past you’re code properly please


#4

You can revive an old post as long as it’s not off topic


#5

he might have found the answer by one year.


#6

It’s not about the original person’s code. It’s about @Reflective_Key’s code.


#7

Could someone please help me? I can’t get the peasant to move to the coins.
Here is part my code:

function commandPeasant(peasant, coins) {
// Command the peasant to the nearest of their array
hero.command(peasant, “move”, coins.pos);
}


#8

if item.value == 3:
goldCoins.append(item)
if item.value == 2:
silverCoins.append(item)
if item.value == 1:
The above construction may work by pure luck, but generally is wrong. Do it the right way

if item.value == 3:
    goldCoins.append(item)
elif item.value == 2:
    silverCoins.append(item)
elif item.value == 1:
    bronzeCoins.append(item)

this I can see from the broken format


#9

but i’m using JavaScript.


#10

same code in javascript:

        if(item.value == 3) goldCoins.push(item);
        else if(item.value == 2) silverCoins.push(item);
        else if(item.value == 1) bronzeCoins.push(item);

in your code read carefully the hint:
// Command the peasant to the nearest of their array

function commandPeasant(peasant, coins) {
// You must get here a single coin from coins and  tell the peasant to move there
// coins.pos  is an array of points , not a single coin.pos 
hero.command(peasant, “move”, coins.pos); // wrong
}

#11

So how would I command the peasant to the nearest of their array? Do I do something like this?

function commandPeasant(peasant, coins) {
// Command the peasant to the nearest of their array
var coin = coins[0];
hero.command(peasant, “move”, coin.pos);
}


#12



the first picture was a hint
the second is from Thang editor
see all API of thangs: https://codecombat.com/editor/thang/


#13

Sorry to bother you, but is this how I should be doing this?

hero.command(peasant, “move”, peasant.findNearest(peasant.findItems));


#14

peasant has no peasant.findItems method. / and if he has it will be peasant.findItems()/
Your function is commandPeasant(peasant, coins), where you have the array coins as parameter.
So it will be:

function commandPeasant(peasant, coins) {
    var coin = peasant.findNearest(coins);
    hero.command(peasant, “move”, coin.pos); 
}

#15

I did what you said, but now it says: "TypeError: Cannot read property ‘pos’ of null "


#16

somewhere you must have

var coins = hero.findItems()

to pass coins to commandPeasant(peasant, coins). A check

if (coin && peasant )  hero.command(peasant, “move”, coin.pos);

will get rid of the error "Cannot read property ‘pos’ of null ", but I think you have a logical error.


#17

It worked! Thank you very much! :grinning: