# \[Solved\] hero.IsPathClear() bug?

**URL:** <https://discourse.codecombat.com/t/solved-hero-ispathclear-bug/22972>\
**Category:** Bugs\
**Created:** [July 12, 2020, 5:22pm UTC](https://discourse.codecombat.com/t/solved-hero-ispathclear-bug/22972 "2020-07-12T17:22:08Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![vladin](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/vladin/32/13162_2.png) [@vladin](https://discourse.codecombat.com/u/vladin)\
**Post date:** [July 12, 2020, 5:22pm UTC](https://discourse.codecombat.com/t/solved-hero-ispathclear-bug/22972/1 "2020-07-12T17:22:08Z")

</div>

In the [Boulder Woods](https://codecombat.com/play/level/boulder-woods?) hero moves ahead after IsPathClear() returns True but gets stuck up into boulders or woods quite a lot and says “I can’t get there”. Because of that it runs out of time and fails.  
I ave came up with checking a wider corridor around target that solved the problem but it seems to me quite complicated for a kid so I thought it was not intended and likely a bug. Is that so?  
That is, the question is whether hero supposed to stuck up and lose time if the path was clear?

```python
  if hero.isPathClear(hero.pos, target):
      hero.moveXY(target.x, target.y)

```

---

<div class="post-metadata">

**Author:** ![AnSeDra](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/ansedra/32/13123_2.png) [@AnSeDra](https://discourse.codecombat.com/u/AnSeDra)\
**Post date:** [July 12, 2020, 5:25pm UTC](https://discourse.codecombat.com/t/solved-hero-ispathclear-bug/22972/2 "2020-07-12T17:25:52Z")

</div>

Hi @vladin and welcome to the forum! 🥳

Can you show us your code formated as it is described [here](https://discourse.codecombat.com/t/essentials-how-to-post-format-your-code-correctly/15521) because maybe there is an issue in another part of your code that causes this one?

Andrei

---

<div class="post-metadata">

**Author:** ![Anna](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/anna/32/8378_2.png) [@Anna](https://discourse.codecombat.com/u/Anna)\
**Post date:** [July 12, 2020, 7:16pm UTC](https://discourse.codecombat.com/t/solved-hero-ispathclear-bug/22972/3 "2020-07-12T19:16:21Z")

</div>

Try using `(target.pos.x, target.pos.y)` instead of `(target.x, target.y)`.

---

<div class="post-metadata">

**Author:** ![xython](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/xython/32/7269_2.png) [@xython](https://discourse.codecombat.com/u/xython)\
**Post date:** [July 12, 2020, 8:03pm UTC](https://discourse.codecombat.com/t/solved-hero-ispathclear-bug/22972/4 "2020-07-12T20:03:07Z")

</div>

For me this is a “hit and miss” level. It’s very complicated to be finished with moveXY().  
because targetX and targetY are number, not coordinates

```python
        targetX = hero.pos.x + coef * Math.cos(angle) # default coef = 5
        targetY = hero.pos.y + coef * Math.sin(angle

```

finding the path will be:

```python
        if hero.isPathClear(hero.pos, {'x': targetX,'y': targetY})

```

then to succeed with moveXY(targetX, targetY) you need to play with coef  
it’s easier with:

```python
            hero.move({'x': targetX,'y': targetY})

```

---

<div class="post-metadata">

**Author:** ![vladin](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/vladin/32/13162_2.png) [@vladin](https://discourse.codecombat.com/u/vladin)\
**Post date:** [July 13, 2020, 12:07am UTC](https://discourse.codecombat.com/t/solved-hero-ispathclear-bug/22972/5 "2020-07-13T00:07:33Z")

</div>

Here is the code w/o workaround below. It fails due to timeout. If I uncomment “beamPath()” in the middle, which is nothing but checking path of wider corridor or “beam” to the target then it works because hero gets stuck much less so it can finish on time, typically using more than half duration allowed.  
May be something else is wrong, pls advise.

> Try using `(target.pos.x, target.pos.y)` instead of `(target.x, target.y)`

I believe target here is a dictionary that expected to have x,y and not another dict pos (pos.x,pos.y). This is clear also from first arg “hero.pos” and not “hero”.

```python
# Use isPathClear to move around the randomly positioned boulders.
# Automatic pathfinding doesn't work in Boulder Woods.
endTarget = {"x": 76, "y": 35}
angle = Math.PI / 2

while True:
    targetX = hero.pos.x + 6 * Math.cos(angle)
    targetY = hero.pos.y + 6 * Math.sin(angle)
    moveX = hero.pos.x + 2 * Math.cos(angle)
    moveY = hero.pos.y + 2 * Math.sin(angle)
    # Use isPathClear between your current `pos` and the target.
    target = {"x": targetX, "y": targetY}
    if hero.isPathClear(hero.pos, endTarget): 
        #first try to reach end Target 
        hero.moveXY(endTarget.x, endTarget.y)
    #elif beamPath(target,2):
    elif hero.isPathClear(hero.pos, target):
        # If the path is clear, move to the target.
        hero.moveXY(target.x, target.y)
        angle = -Math.PI / 8
    else:
        # Otherwise, sweep the `angle` clockwise a bit.
        angle += Math.PI / 16

```

---

<div class="post-metadata">

**Author:** ![xython](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/xython/32/7269_2.png) [@xython](https://discourse.codecombat.com/u/xython)\
**Post date:** [July 13, 2020, 9:27pm UTC](https://discourse.codecombat.com/t/solved-hero-ispathclear-bug/22972/6 "2020-07-13T21:27:33Z")

</div>

Your code works fine with this particular seed:

 ![boulderXY](https://us1.discourse-cdn.com/flex016/uploads/codecombat/original/2X/e/ee80727cd431523e49b02263a56c79c3d27229e4.gif)

If you replace

```python
        hero.moveXY(endTarget.x, endTarget.y) # with
        hero.move(endTarget) # and
        hero.moveXY(target.x, target.y) # with
        hero.move(target)        

```

 ![boulderMove](https://us1.discourse-cdn.com/flex016/uploads/codecombat/original/2X/4/47dcc914bfc3060500032c9b54a52f10983477f0.gif)  
Using the default code but with move() is even faster:

```python
            hero.move({'x':targetX,'y':targetY});

```

 ![boulderMoveDefault](https://us1.discourse-cdn.com/flex016/uploads/codecombat/original/2X/9/91fb96c607a1713e81bf822aafd8373df871b1af.gif)  
“Whether hero supposed to stuck up and lose time if the path was clear?” -Yes  
“it seems to me quite complicated for a kid” - too complicated, it can fail depending on the seed,  
the hero speed, the boots even writing the correct code

The default code fails if using hero.moveXY - “hit and miss” level…

---

<div class="post-metadata">

**Author:** ![vladin](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/vladin/32/13162_2.png) [@vladin](https://discourse.codecombat.com/u/vladin)\
**Post date:** [July 14, 2020, 3:03am UTC](https://discourse.codecombat.com/t/solved-hero-ispathclear-bug/22972/7 "2020-07-14T03:03:25Z")

</div>

> [@xython](#):
>
> code fails if using hero.moveXY

Thanks! This was the issue. Once changed to hero.move(target) it works like a charm 🙂

Note: I would expect move() and moveXY() to do the same thing (based on name) but read help and they are different. move is actually one step towards the target and moveXY is moving until target reached or an obstacle encountered in which case hero slows down and makes 2 steps aside that results in another clash 2 steps later. It is easy to see from the trajectories. In case of move() the trajectory is parallel to the wall and in case of moveXY() it is zigzagging. Bottom line: I would change the name: hero.move() =\> hero.step()
