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
# Find your path.
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.
‘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.
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
# Find your path.
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)
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?
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.
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())
# Find your path.
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)
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].
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?
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.
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.