# Golden Choice Python

Hi I need help in golden choice for python. my problem is in finding the right coin to go to I get the error that gold is not an object but an array. plz help

# You must collect the required amount of gold.
# The gate keeper will tell you how much you need.
# Always move in the direction of the exit.
# For each row you can take only one coin.
# Choose only one from the nearest coins in the next row.

# Distance between rows and coins.
distanceX = 4
distanceY = 6
zeroPoint = {"x": 14, "y": 14}
coinLines = 10

def makeGoldMap(coins):
template = [[0 for j in range(2 * coinLines - 1)] for i in range(coinLines)]
for coin in coins:
row = int((coin.pos.y - zeroPoint.y) / distanceY)
col = int((coin.pos.x - zeroPoint.x) / distanceX)
template[row][col] = coin.value
return template

# Prepare the gold map. It looks like:
# [[1, 0, 9, 0, 4],
#  [0, 1, 0, 9, 0],
#  [8, 0, 2, 0, 9]]
goldMap = makeGoldMap(hero.findItems())
hero.moveXY(14, 8)
hero.moveXY(14, 14)
pass

while True:
for gold in goldMap:
if hero.distanceTo(gold) > 6:

hero.move(gold.pos)

also what does the make template thing do I canāt understand it.

Hi tv337,

āmakeGoldMapā makes an array of arrays - this is something youāve probably met before, for example on Snowdrops and on Danger Valley. Have another look at these levels if you need a refresher.

So āgold in goldMapā is still an array, which is why youāre getting the error message.

J.

1 Like

actually I am just playing the free levels so I havenāt played those levels but I made a new code and would like help debugging it.

# You must collect the required amount of gold.
# The gate keeper will tell you how much you need.
# Always move in the direction of the exit.
# For each row you can take only one coin.
# Choose only one from the nearest coins in the next row.

# Distance between rows and coins.
distanceX = 4
distanceY = 6
zeroPoint = {"x": 14, "y": 14}
coinLines = 10

def makeGoldMap(coins):
template = [[0 for j in range(2 * coinLines - 1)] for i in range(coinLines)]
for coin in coins:
row = int((coin.pos.y - zeroPoint.y) / distanceY)
col = int((coin.pos.x - zeroPoint.x) / distanceX)
template[row][col] = coin.value
return template

# Prepare the gold map. It looks like:
# [[1, 0, 9, 0, 4],
#  [0, 1, 0, 9, 0],
#  [8, 0, 2, 0, 9]]
goldMap = makeGoldMap(hero.findItems())
hero.moveXY(14, 8)
hero.moveXY(14, 14)
pass
minCoin = None
minVal = 0
def compare(items):
p = []
for item in items:
if item.pos.y == (hero.pos.y + 6):
if item.pos.x == (hero.pos.x + 4):
if item.pos.x == (hero.pos.x - 4):
p.append(item)
return p
continue
for item in items:
if item.pos.y == (hero.pos.y + 6):
if item.pos.x == (hero.pos.x + 4):
if item.pos.x == (hero.pos.x - 4):
p.append(item)
return p
continue
for randon in p:
if random.value > minVal:
minVal = random.value
minCoin = random
return minVal
return minCoin
pass
move = minCoin
hero.move(move.pos)
while True:
z = hero.findItems()
m = compare(z)

ok I also did a for g in gold but I need help getting the number to turn into an item to move to.

Hmmm, Iāll have a look at the code, but Iām learning Javascript and Iām only a little bit ahead of you, so I might struggle to help much.

One spelling thing - you have a random/randon inconsistency:

for randon in p:
if random.value > minVal:

ok thank you but that did not help too much Iwill see if @dedreous will help

What are you trying to achieve with these lines of code? I think youāre never going to get a true statement, as āitem.pos.xā canāt be equal to both āhero.pos.x + 4ā and āhero.pos.x - 4ā. Also, whatās your thinking for repeating the lines?

I want to find the 2 closest coins to me that are ahead of me also I changed that to this now:

if item.pos.y == (hero.pos.y + 6):
if item.pos.x <= (hero.pos.x + 5):
if item.pos.x >= (hero.pos.x - 5):
p.append(item)
return p

You might want to take a step back and think about the strategy before you get into writing the code. Itās a tricky level, and it took me a while to get my head round it. There are ~5000 different routes through the coins, and youāre looking for the best one, so it doesnāt work just to look at the next two coins each time - you need to plan out the whole path.

As a first step, get the hero to say the goldMap out loud. Seeing the first 15 characters helped me to understand what I was working with.

1 Like

ok I gave up on that code in frustration and am trying to instead find the 2 closest coins to me but it never enters the bit with the hero.say(āhiā)

# You must collect the required amount of gold.
# The gate keeper will tell you how much you need.
# Always move in the direction of the exit.
# For each row you can take only one coin.
# Choose only one from the nearest coins in the next row.

# Distance between rows and coins.
distanceX = 4
distanceY = 6
zeroPoint = {"x": 14, "y": 14}
coinLines = 10

def makeGoldMap(coins):
template = [[0 for j in range(2 * coinLines - 1)] for i in range(coinLines)]
for coin in coins:
row = int((coin.pos.y - zeroPoint.y) / distanceY)
col = int((coin.pos.x - zeroPoint.x) / distanceX)
template[row][col] = coin.value
return template

# Prepare the gold map. It looks like:
# [[1, 0, 9, 0, 4],
#  [0, 1, 0, 9, 0],
#  [8, 0, 2, 0, 9]]
goldMap = makeGoldMap(hero.findItems())

hero.moveXY(50, 8)
nearest = None
twoNearest = None
distNearest = 999
distTwoNearest = 999

def theNearest(bugs):
for bug in bugs:

n = hero.distanceTo(bug)

if n <= distNearest:
hero.say("hi")
twoNearest = nearest
nearest = bug
distTwoNearest = distNearest
distNearest = n

elif n <= distTwoNearest:
twoNearest = bug
distTwoNearest = n
return twoNearest
return nearest
return distTwoNearest
return distNearest
hero.say(nearest)
pass
while True:
items = hero.findItems()
goToCoin = theNearest(items)

im also interested in how the gold map thing works are the 1st 15 numbers the values of the coins from left to right?

1 Like

The first 19 numbers in the Gold Map represents the first row of the grid. Alternate numbers are 0, as these are the gaps. The other numbers are coins (obviously the value of that coin).

So you donāt need to get the hero to find the nearest 2 coins - if the current coin is [x ][y] in the goldMap array, the two above it will be [x - 1][y + 1] and [x + 1][y + 1].

3 Likes

ok I can see that how does it know the position to go to I cant figure that out I think I have to do a sequence for every row and check each one. also how do you look at the next row?

1 Like

I am now offically annoyed with this level I just cant understand it. I know I cant juust tell it to take a certain path cause of the random seed thing but I cant figure out how anything is supposed to work. The code all confuses me so I dont understand what is going on. If there was a way to transform the numbers given in the goldMap array to their original coins that would be great but I cant see that happening. So I have 2 questions:
is it possible to transform the numbers back to coins or even better into the coins positions?
what is the makeGoldMap thing doing line by line?
also thanks for all your help with this level @jka2706 it is not that I dont appreciate your help or anything I am just frustrated.

Hi tv337,

I get your frustration! This is the level I found I had to do the most thinking to understand the problem (let alone working out the solution). I left the level for a bit and came back to it in order to get my head around it.

Iām not sure I can answer your questions directly, but I donāt think you need to convert back to the coin positions. Knowing the reference of a coin in the array (for example goldMap[4][5]) means you can immediately know the next two coins (theyāll be goldMap[5][4] and goldMap[5][6]). And no, I didnāt find a way to see the whole goldMap written out - you just need to take on trust that itās a 10 x 19 array.

In terms of how to solve it, I started thinking of what the second line of coin values would look like if I replaced each number with the highest possible total to get to that point. (So for point goldMap[2][4] I would compare āgoldMap[1][3] + goldMap[2][4]ā with āgoldMap[1][5] + goldMap[2][4]ā, and chose the higher of these two numbers). I could repeat this by asking the same question for the third line, and the fourth lineā¦all the way to the tenth line.

Good luck, keep thinking about it .

Jenny

Is it really possible to tell the code to pull certain parts with goldMap[5][4]?

Sorry, I donāt understand your question. Can you rephrase it?

can you pull out the goldMap fifth array fourth element in that array?

Try it.

hero.say(goldMap[5][5])

I get a value of 1 on my current seed for that coin.
(I realised that hero.say(goldMap[5][4]) will always give you a value of 0).

Jenny

1 Like