# Backwoods Treasure - Question about my code [Python]

I wrote the code and added comments to make it clearer. I needed to add the condition that we only walk to our desired coin if there was a clear path, because otherwise I pick up all the coins in my area and then walk into the wall. So if there is not a clear path, I make my character walk into the next grove. But I get an error for if self.isPathClear(self.pos, pos) as “isPathClear is not defined”. I can’t seem to fix it.

``````loop:
enemies = self.findEnemies()
enemyIndex = 0
minDistance = 10
coins = self.findItems()
coinIndex = 0

# While there are less than 5 enemies, we cycle through each coin and determine the closest one. We rename it "closestCoin"
while coinIndex < len(coins) and len(enemies) < 6:
coin = coins[coinIndex]
distance = self.distanceTo(coin)
coinIndex += 1

if coin.value >= 2 and distance < minDistance:
minDistance = distance
closestCoin = coin
pos = closestCoin.pos
x = pos.x
y = pos.y

# If there is a clear path to "closestCoin" and not enough enemies, we walk there.
if self.isPathClear(self.pos, pos) and len(enemies) < 6:
self.moveXY(x, y)

# Else if there are not enough enemies, we walk to the new grove.
elif len(enemies) < 6:
self.moveXY(28, 28)
pass

#Otherwise, we kill the closest enemy.
else:
enemy = self.findNearest(enemies)
while enemy.health > 0:
self.powerUp()
else:
self.attack(enemy)

``````
1 Like

That is because the function doesn’t exist…

Where did you get the idea that it did?
Is it on one of your pieces of equipment?

It’s a new skill on the higher-level glasses, which we introduced for Adventurers for the Boulder Forest level yesterday, and which will be also explained in a mountain level next week. Jacob, do you have the higher-level glasses equipped?

Hmm, I tried it again today and now it is happy.

Sorry, about that.I can only suspect that I left off the “self.” since I didn’t change my glasses (enchanted lenses).

I am wearing enchanted lenses which grant me:

distanceTo:…

findByType:…

findEnemies:…

findEnemyMissiles:…

findFriendlyMissiles:…

findFriends:…

findHazards:…

findItems:…

findNearest:…

isPathClear:…

Hmm, I don’t understand this. I had to state the variables “coin”, “pos”, “x”, and “y” at the beginning of the loop, because if they were only stated in my ‘while-loop’ then they would come up as null sometimes and break my code. The isPathClear suddenly started to work now, but now my code runs through and works but keeps giving me an error saying “pos” on line 7 is undefined. Also, when he picks up all of the items in the first grove, he just stands there and waits, he doesn’t move to (28, 28) like he should.

``````loop:
enemies = self.findEnemies()
enemyIndex = 0
minDistance = 10
coins = self.findItems()
coin = self.findNearest(coins)
pos = coin.pos
x = pos.x
y = pos.y
coinIndex = 0

# While there are less than 5 enemies, we cycle through each coin and determine the closest one. We rename it "closestCoin"
while coinIndex < len(coins) and len(enemies) < 7:
coin = coins[coinIndex]
distance = self.distanceTo(coin)
coinIndex += 1

if coin.value >= 2 and distance < minDistance:
minDistance = distance
closestCoin = coin
pos = closestCoin.pos
x = pos.x
y = pos.y

# If there is a clear path to "closestCoin" and not enough enemies, we walk there.
if self.isPathClear(self.pos, pos) and len(enemies) < 7:
self.moveXY(x, y)

# Else if there are not enough enemies, we walk to the new grove.
elif len(enemies) < 7:
self.moveXY(28, 28)
pass

#Otherwise, we kill the closest enemy.
else:
enemy = self.findNearest(enemies)
while enemy.health > 0:
self.powerUp()
else:
self.attack(enemy)
``````

Ok, after lots of tinkering and rewriting, I managed to unbreak my code! Thanks a lot for the input everyone.

1 Like

how do you fix this bug? as far as i can see my syntax is ok.

``````loop {
var enemy = this.findNearest(this.findEnemies());
var kind = enemy.type;
var yak_nearby = this.distanceTo(enemy);
goX = this.pos.x + 10;
goY = this.pos.y;
var path_clear = this.isPathClear({x: this.pos.x + 20, y: this.pos.y});
if (yak_nearby < 20 && kind == 'sand-yak' && path_clear) {
this.say(kind);
this.moveXY(goX, goY);
}
}``````

What is the bug problem? (besides having a say that takes time while the yaks are moving)

“isPathClear is not defined”.

i have no idea whats wrong. cant see anyone who posted a solution here

Sounds like you aren’t wearing the right equipment to be using isPathClear

im wearing the hardened steel glasses so thats not it. who knows ae.

OK, so you have glasses with isPathClear.

Having figured that out I realized that you aren’t using the function correctly. So, what it is doing is telling you that there is no, “isPathClear(pos)” function . . . the function is:

isPathClear(start,end)

iow, it doesn’t assume you always mean from my position…

1 Like

ok i get you the tooltip is saying find the clearance between two positions each with their own xy. the next step i guess would be to figure out how to find the XY of a path with no handle. the tooltip shows how to find the handle of an item but not “10 metres in front”. probably im misusing this method and making this beginner level harder than it needs to be.

thanks for the interest!

I’m not quite sure what you mean…???

but you have the pieces above for what I’m guessing you mean…

You know where you are, this.pos, and you know where you want to go, 20 meters in front of you, so, just add in yourself as the start…???..

var path_clear = this.isPathClear(this.pos, {x: this.pos.x + 20, y: this.pos.y});

That is how you would get to some arbitrary point (of course you have to figure out the proper angle if it wasn’t some straight X or Y direction, but there are Math functions for that (and Vector stuff in javascript)

By the way, there is a level specific workaround if you want isPathClear:

Note that level consists of three chambers. Inside these chambers path is always clear, between chambers it is usually not.
Let point in the center (junction between chambers) have coordinates (CENTER_X, CENTER_Y) Use something like:

``````def get_region(pos):
if(pos.x < CENTER_X):
return "A"
if(pos.y < CENTER_Y):
return "B"
return "C"
``````

If start and end belong to same region, i.e. get_region(self) == get_region(item) - path is clear

I moved 2 posts to a new topic: isPathClear shows error

Well after tinkering with the code, I got through this.
loop:
enemy = self.findNearestEnemy()
item = self.findNearestItem()
flag = self.findFlag(“green”)
if item:
pos = item.pos
x = pos.x
y = pos.y
self.moveXY(x, y)
if flag:
self.pickUpFlag(flag)
self.cleave(enemy)
elif enemy:
self.attack(enemy)
Right, but it says “Line 20: tmp41 [tmp42] is not a function” what does that mean?

You are trying to call a member function that does not exists. Example:

``````selv.attack(enemy)
self.attak(enemy)
``````

check which is the line 20 in your code.

loop:
enemy = self.findNearestEnemy()
item = self.findNearestItem()
if enemy:
self.attack(enemy)
else:
pos = item.pos
x = pos.x
y = pos.y
self.moveXY(x, y)